-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
500 lines (396 loc) · 18.3 KB
/
api.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
from gevent import monkey; monkey.patch_all()
from binascii import hexlify
from datetime import datetime
from flask import Flask, jsonify, redirect, request, Response
from flask_cors import cross_origin
from urllib import urlencode
from werkzeug.datastructures import Headers
from coinsupport.addresscodecs import decode_any_address, encode_base58_address, encode_bech32_address
from config import Configuration
from database import DatabaseIO
from models import Block, _make_transaction_ref, ADDRESS_TYPES
from postprocessor import QueryDataPostProcessor
from eventgen import IndexerEventStream
webapp = Flask('indexer-api')
db = DatabaseIO(Configuration.DATABASE_URL, debug=Configuration.DEBUG_SQL)
stream = IndexerEventStream(db, poll_interval=(2 if not Configuration.DEBUG_SQL else 30))
with db.new_session() as session:
ADDRESS_TRANSLATIONS = session.detect_address_translations()
BECH32_ADDRESS_PREFIX = session.detect_bech32_address_prefix()
def param_true(param_name, default=None):
param = request.args.get(param_name)
if param is None or param == '':
return default
return param.lower() == 'true' or param == '1'
def encode_address(address_type, address_version, pubkeyhash):
if address_type == ADDRESS_TYPES.BASE58:
return encode_base58_address(address_version, pubkeyhash)
if address_type == ADDRESS_TYPES.BECH32 and BECH32_ADDRESS_PREFIX is not None:
return encode_bech32_address(BECH32_ADDRESS_PREFIX, pubkeyhash)
raise ValueError('Cannot re-encode to %s address' % address_type)
def make404():
return jsonify(None), 404
@webapp.errorhandler(404)
def page_not_found(e):
return jsonify(error=404, text=str(e)), 404
@webapp.route('/events/subscribe')
@cross_origin()
def subscribe():
headers = Headers()
headers.add('X-Accel-Buffering', 'no')
headers.add('Cache-Control', 'no-cache')
return Response(stream.subscriber(
channels=(request.args.get('channels').split(',') if request.args.get('channels') is not None else [])),
mimetype='text/event-stream',
headers=headers
)
@webapp.route('/address/<address>/')
@cross_origin()
def address_info(address):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
info = session.address_info(address)
if info == None:
return make404()
info['mutations'] = pp.get_reflink_object('/address/' + address + '/mutations/')
info['utxos'] = pp.get_reflink_object_or_data('/address/' + address + '/utxos/', 'utxos', lambda: session.address_utxos(address, limit=pp.MAX_OBJECTS_PER_PAGE))
# FIXME: Move this some place else?
if ADDRESS_TRANSLATIONS is not None and 'address' in info and info['address'] is not None:
aliases = []
try:
address_type, address_version, pubkeyhash = decode_any_address(info['address'])
for translation in ADDRESS_TRANSLATIONS.items():
if translation[1] == (address_type, address_version):
try:
aliases.append(encode_address(translation[0][0], translation[0][1], pubkeyhash))
except ValueError:
pass
info['aliases'] = aliases
except ValueError:
pass
return pp.process_raw(info).json()
@webapp.route('/address/<address>/balance/')
@cross_origin()
def address_balance(address):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
balance = session.address_balance(address)
if balance == None:
return make404()
return pp.process_raw(balance).json()
@webapp.route('/address/<address>/pending/')
@cross_origin()
def address_pending(address):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pending_balance = session.address_pending_balance(address)
if pending_balance == None:
return make404()
return pp.process_raw(pending_balance).json()
@webapp.route('/address/<address>/mutations/')
@cross_origin()
def address_mutations(address):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.pagination()
mutations = session.address_mutations(address, confirmed=param_true('confirmed'), start=pp.start, limit=pp.limit)
for mutation in mutations:
mutation['transaction'] = _make_transaction_ref(mutation['txid'])
del mutation['txid']
return pp.process_raw(mutations).json()
@webapp.route('/address/<address>/utxos/')
@cross_origin()
def address_utxos(address):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.pagination(default_limit=pp.MAX_OBJECTS_PER_PAGE)
utxos = session.address_utxos(address, confirmed=param_true('confirmed'), start=pp.start, limit=pp.limit)
return pp.process_raw(utxos).json()
@webapp.route('/blocks/')
@cross_origin()
def blocks():
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.pagination(backwards_indexes=True, tipresolver=(lambda: session.chaintip().height + 1), allow_interval=True)
pp.baseurl('/blocks/<Block.hash>/')
pp.reflinks('miner', 'transactions')
pp.reflink('mutations', '/transactions/<Transaction.txid>/mutations')
pp.reflink('inputs', '/transactions/<Transaction.txid>/inputs')
pp.reflink('outputs', '/transactions/<Transaction.txid>/outputs')
pp.reflink('coinbase', '/transactions/<Transaction.txid>/coinbase')
pp.autoexpand()
pp.reflink('block', '/blocks/<query:transaction.block.hash>/', ['hash', 'height'])
return pp.process(session.blocks(pp.start, pp.limit, pp.interval)).json()
@webapp.route('/blocks/<blockid>/')
@cross_origin()
def block(blockid):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.baseurl('/blocks/<Block.hash>/')
pp.reflinks('miner', 'transactions')
pp.reflink('mutations', '/transactions/<Transaction.txid>/mutations')
pp.reflink('inputs', '/transactions/<Transaction.txid>/inputs')
pp.reflink('outputs', '/transactions/<Transaction.txid>/outputs')
pp.reflink('coinbase', '/transactions/<Transaction.txid>/coinbase')
pp.autoexpand()
pp.reflink('block', '/blocks/<query:transaction.block.hash>/', ['hash', 'height'])
block = session.block(blockid)
if block == None:
return make404()
return pp.process(block).json()
@webapp.route('/blocks/<blockid>/miner/')
@cross_origin()
def blockminer(blockid):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.resolve_keys(Block.miner)
pp.reflink('block', '/blocks/<query:transaction.block.hash>/', ['hash', 'height'])
block = session.block(blockid)
if block == None:
return make404()
return pp.process(block)['miner'].json()
@webapp.route('/blocks/<blockid>/transactions/')
@cross_origin()
def blocktransactions(blockid):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.reflink('mutations', '/transactions/<Transaction.txid>/mutations')
pp.reflink('inputs', '/transactions/<Transaction.txid>/inputs')
pp.reflink('outputs', '/transactions/<Transaction.txid>/outputs')
pp.reflink('coinbase', '/transactions/<Transaction.txid>/coinbase')
pp.autoexpand()
pp.resolve_keys('Block.transactions', 'Transaction.block', 'Transaction.mutations', 'Transaction.inputs', 'Transaction.outputs', 'Transaction.coinbase')
pp.reflink('block', '/blocks/<query:transaction.block.hash>/', ['hash', 'height'])
block = session.block(blockid)
if block == None:
return make404()
return pp.process(block)['transactions'].json()
@webapp.route('/transactions/')
@cross_origin()
def transactions():
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.pagination()
pp.baseurl('/transactions/<Transaction.txid>/')
pp.reflinks('mutations', 'inputs', 'outputs')
pp.reflink('block', '/blocks/<query:transaction.block.hash>/', ['hash', 'height'])
pp.reflink('miner', '/blocks/<query:transaction.block.hash>/miner')
pp.autoexpand()
pp.reflink('transactions', '/blocks/<query:transaction.block.hash>/transactions/')
query_confirmed = request.args.get('confirmed')
if query_confirmed is None or query_confirmed == '':
data = session.latest_transactions(limit=pp.limit)
elif query_confirmed == 'true':
data = session.latest_transactions(limit=pp.limit, confirmed_only=True)
elif query_confirmed == 'false':
data = session.mempool()
else:
data = []
return pp.process(data).json()
@webapp.route('/transactions/<txid>/')
@cross_origin()
def transaction(txid):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.baseurl('/transactions/<Transaction.txid>/')
pp.reflinks('mutations', 'inputs', 'outputs', 'coinbase')
pp.reflink('block', '/blocks/<query:transaction.block.hash>/', ['hash', 'height'])
pp.reflink('miner', '/blocks/<query:transaction.block.hash>/miner')
pp.autoexpand()
pp.reflink('transactions', '/blocks/<query:transaction.block.hash>/transactions/')
transaction = session.transaction(txid, include_confirmation_info=True)
if transaction == None:
return make404()
return pp.process(transaction).json()
@webapp.route('/transactions/<txid>/coinbase/')
@cross_origin()
def transaction_coinbase(txid):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
transaction = session.transaction(txid, include_confirmation_info=False)
if transaction == None:
return make404()
return pp.process(transaction.coinbase).json()
@webapp.route('/transactions/<txid>/mutations/')
@cross_origin()
def transaction_mutations(txid):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
transaction = session.transaction(txid, include_confirmation_info=False)
if transaction == None:
return make404()
return pp.process_raw(transaction.mutations).json()
@webapp.route('/transactions/<txid>/inputs/')
@cross_origin()
def transaction_inputs(txid):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
transaction = session.transaction(txid, include_confirmation_info=False)
if transaction == None:
return make404()
return pp.process_raw(transaction.inputs).json()
@webapp.route('/transactions/<txid>/inputs/<index>/')
@cross_origin()
def transaction_input(txid, index):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
transaction = session.transaction(txid, include_confirmation_info=False)
if transaction == None:
return make404()
return pp.process_raw(transaction.inputs[int(index)]).json()
@webapp.route('/transactions/<txid>/outputs/')
@cross_origin()
def transaction_outputs(txid):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
transaction = session.transaction(txid, include_confirmation_info=False)
if transaction == None:
return make404()
return pp.process_raw(transaction.outputs).json()
@webapp.route('/transactions/<txid>/outputs/<index>/')
@cross_origin()
def transaction_output(txid, index):
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
transaction = session.transaction(txid, include_confirmation_info=False)
if transaction == None:
return make404()
return pp.process_raw(transaction.outputs[int(index)]).json()
@webapp.route('/networkstats/')
@cross_origin()
def network_stats():
since = datetime.fromtimestamp(int(request.args.get('since') or 0))
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
data = pp.process_raw(session.network_stats(since=since)).data
return pp.process_raw({
'blocks': {
'amount': data['blocks'],
'totalfees': data['totalfees']
},
'transactions': {
'amount': data['transactions'],
'totalvalue': data['transactedvalue'],
'coindaysdestroyed':data['destroyed']
},
'coins': {
'released': data['coinsreleased']
}
}).json()
@webapp.route('/networkstats/coins/')
@cross_origin()
def network_coin_stats():
since = datetime.fromtimestamp(int(request.args.get('since') or 0))
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
data = pp.process_raw(session.network_stats(since=since, ignore=['blocks', 'totalfees', 'transactions', 'transactedvalue', 'coindaysdestroyed'])).data
return pp.process_raw({
'released': data['coinsreleased']
}).json()
@webapp.route('/networkstats/transactions/')
@cross_origin()
def network_transaction_stats():
since = datetime.fromtimestamp(int(request.args.get('since') or 0))
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
data = session.transaction_stats(since=since)
data.update(session.coindays_stats(since=since))
data = pp.process_raw(data).data
return pp.process_raw({
'amount': data['transactions'],
'totalvalue': data['transactedvalue'],
'coindaysdestroyed':data['destroyed']
}).json()
@webapp.route('/networkstats/transactions/amount/')
@cross_origin()
def total_transactions():
since = datetime.fromtimestamp(int(request.args.get('since') or 0))
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
return pp.process_raw(session.total_transactions_since(since=since)).json()
@webapp.route('/networkstats/transactions/coindaysdestroyed/')
@cross_origin()
def transactions_coindays_stats():
since = datetime.fromtimestamp(int(request.args.get('since') or 0))
interval = int(request.args.get('interval') or 0)
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
return pp.process_raw(session.coindays_destroyed(since=since, interval=interval if interval > 0 else None)).json()
@webapp.route('/networkstats/blocks/')
@cross_origin()
def network_block_stats():
since = datetime.fromtimestamp(int(request.args.get('since') or 0))
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
data = pp.process_raw(session.network_stats(since=since, ignore=['transactions', 'transactedvalue', 'coinsreleased', 'coindaysdestroyed'])).data
return pp.process_raw({
'amount': data['blocks'],
'totalfees': data['totalfees']
}).json()
@webapp.route('/poolstats/')
@cross_origin()
def poolstats():
since = datetime.fromtimestamp(int(request.args.get('since')))
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
return pp.process_raw(session.pool_stats(since=since)).json()
@webapp.route('/richlist/')
@cross_origin()
def richlist():
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
pp.pagination()
return pp.process_raw(session.richlist(start=pp.start, limit=pp.limit)).json()
@webapp.route('/coins/')
@cross_origin()
def coins_info():
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
return pp.process_raw(session.total_coins_info()).json()
@webapp.route('/coins/total/')
@cross_origin()
def total_coins():
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
return pp.process_raw(session.total_coins_info())['total'].json()
@webapp.route('/coins/total/released/')
@cross_origin()
def total_coins_released():
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
return pp.process_raw(float(session.total_coins_released())).json()
@webapp.route('/coins/total/current/')
@cross_origin()
def total_coins_in_addresses():
with db.new_session() as session:
with QueryDataPostProcessor() as pp:
return pp.process_raw(float(session.total_coins_in_addresses())).json()
@webapp.route('/search/<id>')
@cross_origin()
def search(id):
id = str(id.encode('utf8'))
pubkeyhash = None
id_int = None
try:
id_int = int(id)
except ValueError:
pass
try:
address_type, address_version, pubkeyhash = decode_any_address(id, bech32_prefix=BECH32_ADDRESS_PREFIX)
if (address_type, address_version) in ADDRESS_TRANSLATIONS.keys():
address_type, address_version = ADDRESS_TRANSLATIONS[(address_type, address_version)]
id = encode_address(address_type, address_version, pubkeyhash)
except ValueError:
pass
with db.new_session() as session:
if pubkeyhash is not None:
if session.address_info(id) != None:
return redirect('/address/%s/?%s' % (id, urlencode(request.args)))
if len(id) == 32*2:
if session.transaction(id) != None:
return redirect('/transactions/%s/?%s' % (id, urlencode(request.args)))
if len(id) == 32*2 or id_int is not None:
block = session.block(id)
if block != None:
return redirect('/blocks/%s/?%s' % (hexlify(block.hash), urlencode(request.args)))
return make404()