示例#1
0
    def get_transaction(self, txid):
        url = '{api_url}/tx/{txid}?verbose=3'.format(api_url=self.url,
                                                     txid=txid)
        try:
            LOG.info('GET %s' % url)
            r = requests.get(url)
            data = r.json()
        except Exception as ex:
            LOG.error('Unable to get transaction %s from BTC.com: %s' %
                      (txid, ex))
            return {
                'error': 'Unable to get transaction %s from BTC.com' % txid
            }

        data = data['data'] if data['data'] is not None else {}

        # todo check key names , test by setting testnet wrong on explorers
        tx = TX()
        tx.txid = txid
        tx.wtxid = data['witness_hash']
        tx.lock_time = data['lock_time']
        tx.block_height = data[
            'block_height'] if 'block_height' in data and data[
                'block_height'] != -1 else None
        tx.confirmations = data[
            'confirmations'] if 'confirmations' in data else None

        for item in data['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(data['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)

        return {'transaction': tx.json_encodable()}
示例#2
0
    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}