def test_init(self): transaction = Transaction(txid='txid', amount_in=10000, amount_out=9000) assert transaction.amount_in == 10000 assert transaction.amount_out == 9000 assert transaction.fee == 1000 assert transaction.txid == 'txid' # More output than input with pytest.raises(ArithmeticError): Transaction(txid='txid', amount_in=10000, amount_out=10001)
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
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
def test_init(self): inputs = [TxInput(txid='txid', index=1)] outputs = [TxOutput(scriptpubkey='spk', amount=100000000)] transaction = Transaction(txid='txid', inputs=inputs, outputs=outputs) assert transaction.txid == 'txid' assert transaction.inputs == inputs assert transaction.outputs == outputs assert repr( transaction) == "Transaction(txid='txid', inputs=1, outputs=1)" assert repr(transaction.inputs) == "[Input(txid=txid, index=1)]" assert repr(transaction.outputs ) == "[Output(scriptpubkey=spk, amount=100000000)]"
def woc_tx_to_transaction(response): tx_inputs = [] for vin in response['vin']: tx_input = TxInput(txid=vin['txid'], index=vin['vout']) tx_inputs.append(tx_input) tx_outputs = [] for vout in response['vout']: tx_output = TxOutput(scriptpubkey=vout['scriptPubKey']['hex'], amount=vout['value']) tx_outputs.append(tx_output) tx = Transaction(response['txid'], tx_inputs, tx_outputs) return tx
def get_transaction(cls, txid): r = requests.get(cls.MAIN_TX_API.format(txid), timeout=DEFAULT_TIMEOUT) r.raise_for_status() # pragma: no cover response = r.json(parse_float=Decimal) tx_inputs = [] for vin in response['vin']: tx_input = TxInput(vin['txid'], vin['vout']) tx_inputs.append(tx_input) tx_outputs = [] for vout in response['vout']: tx_output = TxOutput(scriptpubkey=vout['hex'], amount=currency_to_satoshi(vout['value'], 'bsv')) tx_outputs.append(tx_output) tx = Transaction(response['txid'], tx_inputs, tx_outputs) return tx
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
def get_transaction(self, transaction_id): """ Gets a single transaction :param transaction_id: The transaction ID """ r = requests.get( 'https://api.mattercloud.net/api/v3/{}/tx/{}'.format(self.network, transaction_id), headers=self.headers, ) r.raise_for_status() response = r.json() tx_inputs = [] for vin in response['vin']: tx_input = TxInput(vin['txid'], vin['vout']) tx_inputs.append(tx_input) tx_outputs = [] for vout in response['vout']: tx_output = TxOutput(scriptpubkey=vout['scriptPubKey']['hex'], amount=vout['valueSat']) tx_outputs.append(tx_output) tx = Transaction(response['txid'], tx_inputs, tx_outputs) return tx