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()
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'] = { 'href': QueryDataPostProcessor.API_ENDPOINT + '/address/' + address + '/mutations/' } # 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()
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()
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()
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()
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()
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()
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 = pp.process_raw(session.transaction_stats(since=since)).data return pp.process_raw({ 'amount': data['transactions'], 'totalvalue': data['transactedvalue'] }).json()
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()
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()
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()
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()
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()
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.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()
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()
def total_coins(): with db.new_session() as session: with QueryDataPostProcessor() as pp: return pp.process_raw(session.total_coins_info())['total'].json()
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()
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()
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()
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()
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()