ofek / bit

Bitcoin made easy.
https://ofek.dev/bit/
MIT License
1.24k stars 212 forks source link

unconfirmed balance of an address? #80

Open djpnewton opened 5 years ago

djpnewton commented 5 years ago

is it possible to get the unconfirmed balance of an address?

bjarnemagnussen commented 5 years ago

There is no function for it, however this can be calculated easily:

import bit
k = bit.PrivateKeyTestnet()
unconfirmed_balance = sum(u.amount for u in k.get_unspents() if u.confirmations==0)
djpnewton commented 5 years ago

get_unspents does not seem to include unconfirmed outputs

ghost commented 5 years ago

Hmm. That might be API specific behavior giving intermittent results. Or the mempool propagation may be bad. Look at your TX and see if it even comes up in half of the explorers. I've seen tons of TXs that will only show up in a couple explorerers until confirmed.

bjarnemagnussen commented 5 years ago

This is definitely a problem with the API services -- and may only be for testnet. I can confirm that the problem is with BitpayAPI, which is the first one to be polled by NetworkAPI for testnet unspents. Bitpay does not return unconfirmed testnet unspents, whereas SmartbitAPI does.

In code:

import bit
k = bit.PrivateKeyTestnet()
k.get_unspents()  # Polls BitpayAPI behind the sceenes
# [] <-- empty list even though there is an unconfirmed unspent
# Hacky solution:
bit.network.services.SmartbitAPI.get_unspent_testnet(k.address)  # uses SmartbitAPI

I think this is exclusively a problem for testnet and we may in the future want to switch around between the two API's Bitpay and Smartbit inside NetworkAPI.GET_UNSPENT_TESTNET.

// Edit: Using the get_unspent_testnet function directly on SmartbitAPI is not recommended as it requires changing the Unspent objects for segwit unspents.