1200wd / bitcoinlib

Bitcoin and other Cryptocurrencies Library for Python. Includes a fully functional wallet, Mnemonic key generation and management and connection with various service providers to receive and send blockchain and transaction information.
http://bitcoinlib.readthedocs.io/
GNU General Public License v3.0
596 stars 199 forks source link

I am encountering an issue with retrieving UTXO and balance values. Please help. #382

Closed kernaldpark closed 5 months ago

kernaldpark commented 5 months ago

The result of executing the following source code is a bit strange. The balance value is incorrect.

from bitcoinlib.transactions import Transaction from bitcoinlib.services.services import Service

addr_from = 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r' addr_to = 'tb1qrsjj80zgz2mat9h3mupce7tqcgqgycna6ywz6m'

utxos = Service(network='testnet').getutxos(addr_from) t = Transaction(network='testnet', fee=fee, witness_type='segwit') for utxo in utxos: t.add_input(utxo['txid'], utxo['output_n'], value=utxo['value'], witness_type='segwit') t.add_input(utxo['txid'], utxo['output_n'], value=utxo['value'], witness_type='legacy') value += utxo['value'] print(utxos)

output: (I tidied it up a bit to make it look nice.)

txid 18f70e05e503da28508235dbc029362f1f9b244141d97ace5cb142c3c441b678 44556 2581247 segwit p2wpkh txid 8d596216b708e85a254adf791ad49d0a06f289a35547b7b8c5a07a8b6a3de3a0 42640 2582013 segwit p2wpkh txid 83c131a40b1b50cd61f8526a72f02716eaa0558e907e5aa0c6bbd802d6e8cdea 180336 2579643 segwit p2wpkh txid e297d98a0d3f016776bd35be79375f1383839dc5d561dd1e43313582cb2f4403 2112 2582022 legacy p2wpkh txid a3754b3beed6917cbab3739db1ae19b686a80ff175bfe54fea22f03da9e1ba6a 155399 2580192 segwit p2wpkh txid bb8842710c0c1a8887fb9374dcfac493b24098011918baa1e73da9c233a761af 4700 2580743 legacy p2wpkh txid 972f3ba5681db39b2072bba264c13c3419ab53c33750290b6197bc058b0c5682 1000 2580758 segwit p2wpkh txid 232210ec0fddd2179632588020e5547bf1d0fc9505d1421ad3e052f36adf971f 1700 2580453 legacy p2wpkh Unspent txid 95cdd6ecc8af662ec35a213f1edf63fa02e0fa5314b96e882b293477f718222a 1112 2580741 legacy p2wpkh txid ac8dc69be88c542b790b865bb5d4d17a8169a718f05f45ac085cd1178bdf1b21 73119 2579885 segwit p2wpkh txid 75f5fbad8ed2e829af607a6bdfd9fd044ea11e47892adaedd59b84a21fe01c98 59357 2580430 segwit p2wpkh Unspent txid 232210ec0fddd2179632588020e5547bf1d0fc9505d1421ad3e052f36adf971f 1700 2580741 legacy p2wpkh Unspent txid 921d98a2e8110629e1b2180e8956a9b439476d4ed194189d19bfdfe02a5355bf 500 2580344 legacy p2wpkh Spent txid b485484e34ea4ace32e73fa97984b42eafaca1e167b9a500c7863b403ca06390 1200 2580453 legacy p2wpkh Unspent

total balance: 569431

The precise total balance value is 566031. https://blockstream.info/testnet/address/tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r

The following source outputs the accurate balance value and UTXO.

from cryptos import *

timeout_seconds = 120 c = Bitcoin(testnet=True, timeout=timeout_seconds) addr_from = 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r' addr_to = 'tb1qrsjj80zgz2mat9h3mupce7tqcgqgycna6ywz6m' inputs = c.unspent(addr_from) print(inputs) balance = sum(list(map(lambda o: o['value'], inputs))) print('balance : %d' % balance)

output: balance : 566031

{'height': 2581247, 'tx_hash': '18f70e05e503da28508235dbc029362f1f9b244141d97ace5cb142c3c441b678', 'tx_pos': 0, 'value': 44556, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2582013, 'tx_hash': '8d596216b708e85a254adf791ad49d0a06f289a35547b7b8c5a07a8b6a3de3a0', 'tx_pos': 0, 'value': 42640, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2579643, 'tx_hash': '83c131a40b1b50cd61f8526a72f02716eaa0558e907e5aa0c6bbd802d6e8cdea', 'tx_pos': 0, 'value': 180336, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2582022, 'tx_hash': 'e297d98a0d3f016776bd35be79375f1383839dc5d561dd1e43313582cb2f4403', 'tx_pos': 0, 'value': 2112, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2580192, 'tx_hash': 'a3754b3beed6917cbab3739db1ae19b686a80ff175bfe54fea22f03da9e1ba6a', 'tx_pos': 0, 'value': 155399, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2580743, 'tx_hash': 'bb8842710c0c1a8887fb9374dcfac493b24098011918baa1e73da9c233a761af', 'tx_pos': 0, 'value': 4700, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2580758, 'tx_hash': '972f3ba5681db39b2072bba264c13c3419ab53c33750290b6197bc058b0c5682', 'tx_pos': 65, 'value': 1000, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2580453, 'tx_hash': '232210ec0fddd2179632588020e5547bf1d0fc9505d1421ad3e052f36adf971f', 'tx_pos': 0, 'value': 1700, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2580741, 'tx_hash': '95cdd6ecc8af662ec35a213f1edf63fa02e0fa5314b96e882b293477f718222a', 'tx_pos': 0, 'value': 1112, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2579885, 'tx_hash': 'ac8dc69be88c542b790b865bb5d4d17a8169a718f05f45ac085cd1178bdf1b21', 'tx_pos': 0, 'value': 73119, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'}, {'height': 2580430, 'tx_hash': '75f5fbad8ed2e829af607a6bdfd9fd044ea11e47892adaedd59b84a21fe01c98', 'tx_pos': 1, 'value': 59357, 'address': 'tb1q6v2tn4k7j9w5xxvwepkhdjrdpuwlwjcw3par5r'} ]

kernaldpark commented 5 months ago

The log data has been changed...