def get_transactions(self, address): pagesize = 50 # max 50 for BTC.com n_tx = None transactions = [] page = 1 while n_tx is None or len(transactions) < n_tx: url = '{api_url}/address/{address}/tx?page={page}&pagesize={pagesize}&verbose=3'.format( api_url=self.url, address=address, page=page, pagesize=pagesize) try: LOG.info('GET %s' % url) r = requests.get(url) data = r.json() except Exception as ex: LOG.error( 'Unable to get transactions of address %s from BTC.com: %s' % (address, ex)) return { 'error': 'Unable to get transactions of address %s from BTC.com' % address } data = data['data'] if data['data'] is not None else {} if all(key in data for key in ('total_count', 'list')): n_tx = data['total_count'] transactions += data['list'] page += 1 else: return {'error': 'Received invalid data: %s' % data} if len(transactions) < n_tx: sleep(1) txs = [] for transaction in transactions: tx = TX() tx.txid = transaction['hash'] tx.block_height = transaction['block_height'] tx.confirmations = transaction['confirmations'] tx.wtxid = transaction['witness_hash'] tx.lock_time = transaction['lock_time'] for item in transaction['inputs']: tx_input = TxInput() tx_input.address = item['prev_addresses'][0] if len( item['prev_addresses']) > 0 else None tx_input.value = item['prev_value'] tx_input.txid = item['prev_tx_hash'] tx_input.n = item['prev_position'] if item[ 'prev_position'] is not -1 else None tx_input.script = item['script_hex'] tx_input.sequence = item['sequence'] tx.inputs.append(tx_input) for i, item in enumerate(transaction['outputs']): tx_output = TxOutput() tx_output.address = item['addresses'][0] if len( item['addresses']) > 0 else None tx_output.value = item['value'] tx_output.n = i tx_output.spent = False if item['spent_by_tx'] is None else True tx_output.script = item['script_hex'] if item['script_hex'][:2] == '6a': tx_output.op_return = tx.decode_op_return( item['script_hex']) tx.outputs.append(tx_output) # Only append confirmed transactions if tx.block_height is not -1: txs.append(tx.to_dict(address)) else: # subtract 1 from total txs because it is unconfirmed n_tx -= 1 if n_tx != len(txs): return { 'error': 'BTC.com: Not all transactions are retrieved! expected {expected} but only got {received}' .format(expected=n_tx, received=len(txs)) } else: return {'transactions': txs}
def get_transactions(self, address): limit = 10 # number of tx given by insight is 10 n_tx = None transactions = [] i = 0 while n_tx is None or len(transactions) < n_tx: url = self.url + '/addrs/' + address + '/txs?from=' + str( limit * i) + '&to=' + str(limit * (i + 1)) try: LOG.info('GET %s' % url) r = requests.get(url) data = r.json() except Exception as ex: LOG.error( 'Unable to get transactions of address %s from %s: %s' % (address, url, ex)) return { 'error': 'Unable to get transactions of address %s from %s' % (address, url) } if all(key in data for key in ('totalItems', 'items')): n_tx = data['totalItems'] transactions += data['items'] i += 1 else: return {'error': 'Received Invalid data: %s' % data} txs = [] for transaction in transactions: tx = TX() tx.txid = transaction['txid'] tx.lock_time = transaction['locktime'] tx.confirmations = transaction['confirmations'] tx.block_height = transaction['blockheight'] for item in transaction['vin']: tx_input = TxInput() tx_input.address = item['addr'] if 'addr' in item else None tx_input.value = item['valueSat'] if 'value' in item else 0 tx_input.txid = item['txid'] if 'txid' in item else None tx_input.n = item['vout'] if 'coinbase' not in item else None tx_input.script = item['scriptSig'][ 'hex'] if 'scriptSig' in item else None if 'coinbase' in item: tx_input.script = item['coinbase'] tx_input.sequence = item['sequence'] tx.inputs.append(tx_input) for item in transaction['vout']: tx_output = TxOutput() tx_output.address = item['scriptPubKey']['addresses'][ 0] if 'addresses' in item['scriptPubKey'] else None tx_output.value = int( int(item['value'][:-9]) * 1e8 + int(item['value'][-8:])) tx_output.n = item['n'] tx_output.spent = True if 'spentTxId' in item and item[ 'spentTxId'] is not None else False tx_output.script = item['scriptPubKey']['hex'] if item['scriptPubKey']['hex'][:2] == '6a': tx_output.op_return = tx.decode_op_return( item['scriptPubKey']['hex']) tx.outputs.append(tx_output) # Only add confirmed txs if tx.block_height != -1: txs.insert(0, tx.to_dict(address)) else: # subtract 1 from total txs because it is unconfirmed n_tx -= 1 if n_tx != len(txs): return { 'error': 'Not all transactions are retrieved! expected {expected} but only got {received}' .format(expected=n_tx, received=len(txs)) } else: return {'transactions': txs}
def get_transactions(self, address): limit = 50 # max number of tx given by blockchain.info is 50 n_tx = None transactions = [] latest_block_height = self.get_latest_block_height() if latest_block_height is None: return {'error': 'Unable to get latest block height'} i = 0 while n_tx is None or len(transactions) < n_tx: url = '{api_url}/address/{address}?format=json&limit={limit}&offset={offset}'.format( api_url=self.url, address=address, limit=limit, offset=limit * i) try: LOG.info('GET %s' % url) r = requests.get(url) data = r.json() except Exception as ex: LOG.error( 'Unable to get transactions of address %s from %s: %s' % (address, url, ex)) return { 'error': 'Unable to get transactions of address %s from %s' % (address, url) } if all(key in data for key in ('n_tx', 'txs')): n_tx = data['n_tx'] transactions += data['txs'] i += 1 else: return {'error': 'Received Invalid data: %s' % data} if len(transactions) < n_tx: sleep(1) txs = [] for transaction in transactions: tx = TX() tx.txid = transaction['hash'] tx.lock_time = transaction['lock_time'] tx.block_height = transaction[ 'block_height'] if 'block_height' in transaction else None tx.confirmations = (latest_block_height - tx.block_height ) + 1 if 'block_height' in transaction else 0 for item in transaction['inputs']: tx_input = TxInput() tx_input.address = item['prev_out'][ 'addr'] if 'prev_out' in item else None tx_input.value = item['prev_out'][ 'value'] if 'prev_out' in item else 0 tx_input.txid = '' # Blockchain.info does not provide the txid of a tx input only their own tx_index, can be resolved for example via https://testnet.blockchain.info/tx-index/197277768?format=json but this would require too many http requests!!! tx_input.n = item['prev_out'][ 'n'] if 'prev_out' in item else None tx_input.script = item['script'] tx_input.sequence = item['sequence'] tx.inputs.append(tx_input) for item in transaction['out']: tx_output = TxOutput() tx_output.address = item['addr'] if 'addr' in item else None tx_output.value = item['value'] tx_output.n = item['n'] tx_output.spent = item['spent'] tx_output.script = item['script'] if item['script'][:2] == '6a': tx_output.op_return = tx.decode_op_return(item['script']) tx.outputs.append(tx_output) # Only append confirmed transactions if tx.block_height is not None: txs.insert(0, tx.to_dict(address)) else: # subtract 1 from total txs because it is unconfirmed n_tx -= 1 if n_tx != len(txs): return { 'error': 'Not all transactions are retrieved! expected {expected} but only got {received}' .format(expected=n_tx, received=len(txs)) } else: return {'transactions': txs}