Exemple #1
0
    def get_transaction(cls, txid):
        r = requests.get(cls.MAIN_TX_API.format(txid), timeout=DEFAULT_TIMEOUT)
        if r.status_code != 200:  # pragma: no cover
            raise ConnectionError
        response = r.json(parse_float=Decimal)

        tx = Transaction(
            response['txid'], response['blockheight'],
            (Decimal(response['valueIn']) * BSV_TO_SAT_MULTIPLIER).normalize(),
            (Decimal(response['valueOut']) *
             BSV_TO_SAT_MULTIPLIER).normalize(),
            (Decimal(response['fees']) * BSV_TO_SAT_MULTIPLIER).normalize())

        for txin in response['vin']:
            part = TxPart(txin['addr'], txin['valueSat'],
                          txin['scriptSig']['asm'])
            tx.add_input(part)

        for txout in response['vout']:
            addr = None
            if 'addresses' in txout['scriptPubKey'] and txout['scriptPubKey'][
                    'addresses'] is not None:
                addr = txout['scriptPubKey']['addresses'][0]

            part = TxPart(addr, (Decimal(txout['value']) *
                                 BSV_TO_SAT_MULTIPLIER).normalize(),
                          txout['scriptPubKey']['asm'])
            tx.add_output(part)

        return tx
Exemple #2
0
    def get_transaction(self, txid):
        rawtx = self.rpc.getrawtransaction(txid)
        txjson = self.rpc.decoderawtransaction(rawtx)
        inputs = []
        outputs = []
        amount_in = 0
        amount_out = 0
        for vin in txjson['vin']:
            src = self.rpc.getrawtransaction(vin['txid'], True)
            src = self.rpc.decoderawtransaction(src['hex'])
            src = src['vout'][vin['vout']]
            addr = None
            if 'addresses' in src['scriptPubKey']:
                addr = src['scriptPubKey']['addresses'][0]
            amount = int((src['value'] * BSV_TO_SAT_MULTIPLIER).normalize())
            amount_in += amount
            part = TxInput(addr, amount)
            inputs += [part]

        for vout in txjson['vout']:
            addr = None
            if 'addresses' in vout['scriptPubKey']:
                addr = vout['scriptPubKey']['addresses'][0]
            amount = int((vout['value'] * BSV_TO_SAT_MULTIPLIER).normalize())
            amount_out += amount
            part = TxOutput(addr, amount, asm=vout['scriptPubKey']['asm'])
            outputs += [part]

        tx = Transaction(txjson['txid'], amount_in, amount_out)
        for part in inputs:
            tx.add_input(part)
        for part in outputs:
            tx.add_output(part)
        return tx
Exemple #3
0
    def get_transaction(self, transaction_id):
        """
        Gets a single transaction

        :param transaction_id: The transaction ID
        """
        r = requests.get(
            'https://api.bitindex.network/api/v3/{}/tx/{}'.format(
                self.network, transaction_id),
            headers=self.headers,
        )
        r.raise_for_status()
        response = r.json()
        response_vin = response['vin']
        response_vout = response['vout']

        # get txid
        txid = response['txid']

        # get amount_in
        running_total_vin = 0
        for i in response_vin:
            running_total_vin += int(i['valueSat'])
        amount_in = running_total_vin

        # get amount_out
        running_total_vout = 0
        for i in response_vout:
            running_total_vout += int(i['valueSat'])
        amount_out = running_total_vout

        tx = Transaction(txid, amount_in, amount_out)

        # add TxInputs
        for txin in response_vin:
            part = TxInput(txin['addr'], txin['valueSat'])
            tx.add_input(part)

        # add TxOutputs
        for txout in response_vout:
            addr = None
            if 'addresses' in txout['scriptPubKey'] and \
                    txout['scriptPubKey']['addresses'] is not None:
                addr = txout['scriptPubKey']['addresses'][0]

            part = TxOutput(addr, txout['valueSat'],
                            txout['scriptPubKey']['asm'])
            tx.add_output(part)

        return tx