-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
204 lines (176 loc) · 7.11 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
from flask import (Flask, jsonify, render_template,
request)
import pymongo
app = Flask(__name__)
DB_NAME = 'zcashdb'
DB_PREFIX = 'zec'
client = pymongo.MongoClient()
db = client[DB_NAME]
AddressTransaction = db[DB_PREFIX + 'addresstransactions']
Blocks = db[DB_PREFIX + 'blocks']
Transactions = db[DB_PREFIX + 'transactions']
TxOutputs = db[DB_PREFIX + 'txoutputs']
@app.route('/', methods=['GET'])
def index():
blockchain = Blocks.find_one(sort=[('height', pymongo.DESCENDING)])
recent_blocks = []
for block in Blocks.find(sort=[('height', pymongo.DESCENDING)], limit=10):
num_transactions = Transactions.find(
{'blockhash': block['hash']}).count()
block['num_transactions'] = num_transactions
recent_blocks.append(block)
return render_template('index.html',
blockchain=blockchain,
recent_blocks=recent_blocks)
@app.route('/block/<hash>', methods=['GET'])
def block(hash):
if len(hash) == 64:
block = Blocks.find_one({'hash': hash})
else:
block = Blocks.find_one({'height': int(hash)})
prevblock = Blocks.find_one({'height': {"$lt": int(block['height'])}},
sort=[('height', pymongo.DESCENDING)])
nextblock = Blocks.find_one({'height': {"$gt": int(block['height'])}},
sort=[('height', pymongo.ASCENDING)])
transactions = Transactions.find({'blockhash': block['hash']}) \
.sort('blockindex', pymongo.ASCENDING)
txs = []
for tx in transactions:
txs.append(tx)
tx['out'] = TxOutputs.find({'txid': tx['txid']},
sort=[('pos', pymongo.ASCENDING)])
return render_template('block.html',
block=block,
transactions=txs,
prevblock=prevblock,
nextblock=nextblock)
@app.route('/block_list/', methods=['GET'])
def block_list():
start = int(request.args.get('start', 0))
limit = int(request.args.get('limit', 10))
block_list = []
for block in Blocks.find({'height': {'$gte': start}}).sort(
'height', pymongo.ASCENDING).limit(limit + 1):
num_transactions = Transactions.find(
{'blockhash': block['hash']}).count()
block['num_transactions'] = num_transactions
block_list.append(block)
nextblock = len(block_list) > limit
block_list = block_list[0:limit]
return render_template('block_list.html',
block_list=block_list,
start=start,
limit=limit,
nextblock=nextblock)
@app.route('/transaction/<txid>', methods=['GET'])
def transaction(txid):
transaction = Transactions.find_one({'txid': txid})
transaction['out'] = TxOutputs.find({'txid': transaction['txid']},
sort=[('pos', pymongo.ASCENDING)])
prevtransaction = Transactions.find_one({
'blockhash': transaction['blockhash'],
'blockindex': {"$lt": int(transaction['blockindex'])}},
sort=[('blockheight', pymongo.DESCENDING)])
nexttransaction = Transactions.find_one({
'blockhash': transaction['blockhash'],
'blockindex': {"$gt": int(transaction['blockindex'])}},
sort=[('blockheight', pymongo.ASCENDING)])
return render_template('transaction.html',
transaction=transaction,
prevtransaction=prevtransaction,
nexttransaction=nexttransaction)
@app.route('/address/<address>', methods=['GET'])
def address(address):
outputs = []
confirmed = 0
for output in TxOutputs.find({'address': address, 'spent': False}) \
.limit(10):
confirmed += int(output['satoshis'])
outputs.append(output)
balance = {
'confirmed': {'amount': confirmed},
'unconfirmed': {'amount': 0}
}
transactions = AddressTransaction.find({'address': address}) \
.sort('txtime', pymongo.DESCENDING).limit(10)
txs = []
for tx in transactions:
tx = Transactions.find_one({'txid': tx['txid']})
tx['out'] = TxOutputs.find({'txid': tx['txid']},
sort=[('pos', pymongo.ASCENDING)])
txs.append(tx)
return render_template('address.html',
address=address,
balance=balance,
transactions=txs,
outputs=outputs)
@app.route('/api/v1/blockchaininfo', methods=['GET'])
def get_blockchain_info():
block = Blocks.find_one(sort=[('height', pymongo.DESCENDING)])
block.pop('_id')
return jsonify(block)
@app.route('/api/v1/block/<hash>', methods=['GET'])
def get_block_info(hash):
hashes = hash.split(",")
response = {}
for hash in hashes:
if len(hash) == 64:
block = Blocks.find_one({'hash': hash})
else:
block = Blocks.find_one({'height': int(hash)})
if block:
block.pop('_id')
transactions = Transactions.find({'blockhash': block['hash']}) \
.sort('blockindex', pymongo.ASCENDING)
block['tx'] = [tx['txid'] for tx in transactions]
response[hash] = block
return jsonify(response)
@app.route('/api/v1/block', methods=['GET'])
def get_block_list():
limit = request.args.get('limit', 10)
blocks = Blocks.find().sort('height', pymongo.DESCENDING).limit(limit)
response = [block['hash'] for block in blocks]
return jsonify(response)
@app.route('/api/v1/tx/<txid>', methods=['GET'])
def get_transaction(txid):
txids = txid.split(",")
response = {}
for txid in txids:
tx = Transactions.find_one({'txid': txid})
if tx:
tx.pop('_id')
outputs = TxOutputs.find({'txid': txid}) \
.sort('pos', pymongo.ASCENDING)
tx['out'] = [output for output in outputs]
for output in tx['out']:
output.pop('_id')
response[txid] = tx
return jsonify(response)
@app.route('/api/v1/address/<address>/balance', methods=['GET'])
def get_address_balance(address):
addresses = address.split(",")
response = {}
for address in addresses:
unspentOutputs = TxOutputs.find({'address': address, 'spent': False})
response[address] = {
'confirmed': {
'amount': sum([int(unspent['satoshis'])
for unspent in unspentOutputs])
},
'unconfirmed': {
'amount': 0
}
}
return jsonify(response)
@app.route('/api/v1/address/<address>/transactions', methods=['GET'])
def get_address_transactions(address):
addresses = address.split(",")
response = {}
for address in addresses:
transactions = AddressTransaction.find({'address': address}) \
.sort('txtime', pymongo.DESCENDING)
response[address] = [tx['txid'] for tx in transactions]
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True)