ofek / bit

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

Transfer BTC. InsufficientFunds #106

Closed deliugard closed 4 years ago

deliugard commented 4 years ago

When I try to create a transaction, it gives an error that there are not enough funds on the balance: bit.exceptions.InsufficientFunds: Balance 54022 is less than 60800 (including fee).

key.send([(address, 0.0001, 'btc')], fee=200) 0.0001 btc = 10000 satoshi. So total is 30000 satoshi (0.0003 btc).

Why im getting error? I noticed that when transferring the indicated amount, it is added to the whole balance and is trying to transfer it.

bjarnemagnussen commented 4 years ago

Couple of points:

0.0001 btc = 10000 satoshis

Setting the fee parameter without also setting the absolute_fee parameter to True will actually calculate a fee based on a fee rate defined with fee.

deliugard commented 4 years ago

You are right with the amount of Satoshi. absolute_fee parameter solved the problem, but why is it necessary if possible to check for the presence of the specified parameter fee?

deliugard commented 4 years ago

If use absolute_fee im getting next error: ConnectionError: Transaction broadcast failed, or Unspents were already used.

bjarnemagnussen commented 4 years ago

The fee argument normally denotes a rate (satoshis per byte). So for a simple transaction with e.g. one input and output the size will be around 250 bytes. Setting the fee rate to 200 would result in around 250bytes * 200 satoshis/byte = 50000 satoshis for the fee.

You don't have enough funds to cover for both the amount you send and the cost of fee with that fee rate.

Beware that an absolute fee value of 200 satoshis is currently very low and may not confirm in time, whereas a fee rate of 200 satoshis/byte would probably confirm with the next block.

deliugard commented 4 years ago

Is it possible to get the final fee for transaction?

bjarnemagnussen commented 4 years ago

If use absolute_fee im getting next error: ConnectionError: Transaction broadcast failed, or Unspents were already used.

Using an absolute fee of 200 Satoshis is below 1 satoshi per byte, which is typically the minimum fee rate that nodes will allow transactions to propagate with (node policy). The network API is therefore probably blocking the transaction and not broadcasting it.

Is it possible to get the final fee for transaction?

There is no easy to use function implemented. The estimation depends on numerous things (number of inputs and outputs, types of inputs spent (legacy, multisig, segwit, etc.). You can see how an helper function to estimate the fee is used here https://github.com/ofek/bit/blob/79726d893678e1f221a74cc39461f629333acc50/bit/transaction.py#L315

deliugard commented 4 years ago

https://www.blockchain.com/btc/tx/1b2306978e28c7e1e8090017ebe41d2e0b67e1e60b3cda62f50b6d80de45b775 key.send([(address, 0.0001, 'btc')], fee=5) I indicated 5 satoshi per byte, however used only 3.320 why?

bjarnemagnussen commented 4 years ago

Since Segwit transaction size is actually measured in vBytes. Blockchain.com however does not show vBytes (only bytes and weight units, all of which are convertible: https://en.bitcoin.it/wiki/Weight_units).

Checkout this block explorer that shows the correct fee rate in vBytes: https://blockstream.info/tx/1b2306978e28c7e1e8090017ebe41d2e0b67e1e60b3cda62f50b6d80de45b775

deliugard commented 4 years ago

Is there implemented function for calculating the number of bytes?

bjarnemagnussen commented 4 years ago

No, but you can see in the code how it is calculated https://github.com/ofek/bit/blob/79726d893678e1f221a74cc39461f629333acc50/bit/transaction.py#L184

deliugard commented 4 years ago

I solved the problem. It would be convenient if this function was developed. If necessary, I can provide a code. I would be grateful if you contribute to the project.

ofek commented 4 years ago

@deliugard We love contributions, please do!

deliugard commented 4 years ago

https://github.com/ofek/bit/pull/107

deliugard commented 4 years ago

If use absolute_fee im getting next error: ConnectionError: Transaction broadcast failed, or Unspents were already used.

Using an absolute fee of 200 Satoshis is below 1 satoshi per byte, which is typically the minimum fee rate that nodes will allow transactions to propagate with (node policy). The network API is therefore probably blocking the transaction and not broadcasting it.

Is it possible to get the final fee for transaction?

There is no easy to use function implemented. The estimation depends on numerous things (number of inputs and outputs, types of inputs spent (legacy, multisig, segwit, etc.). You can see how an helper function to estimate the fee is used here

https://github.com/ofek/bit/blob/79726d893678e1f221a74cc39461f629333acc50/bit/transaction.py#L315

@bjarnemagnussen In an attempt to resolve independently, I found a critical error in the library. The calculation of the number of bytes in the transaction is incorrect, because of which the node will reject the transaction, indicating a commission of 1 satoshi per byte (since there are actually more bytes).

ofak:bit

https://www.blockchain.com/btc/tx/5bc2a806c908532e8260b2a08e0e1de60c2c19761856b140aca81cd54292709b - transaction

ghost commented 4 years ago

Doesn't that output match what you wrote?

# printed: Estimated fee: 1640 satoshis for 164 bytes

Fee: 0.00001640 BTC

deliugard commented 4 years ago

The amount of commission matches, but the number of bytes does not match. key.send([('3Kw32jS3BGwMMTNWZSFaT5BytZu7oTfH6Y', 0.0001, 'btc')], fee=10) 1640 / 10 = 164 (bytes, but the correct value is 247). Now, suppose I specify fee of 1 satoshi per byte, the node will give an error, because there will be an attempt to complete the transaction in less than 1 satoshi per byte (247 / 164 = < 1).

ghost commented 4 years ago

Got it. I wonder if only P2SH has the issue.

deliugard commented 4 years ago

@teran-mckinney i tested p2pkh, the values ​​are not correct either

deliugard commented 4 years ago

If there are corrections, I will be very grateful if the function for calculating bytes will be implemented separately

bjarnemagnussen commented 4 years ago

The calculation is based on the virtual size (vBytes), which in fact is 166. See this block explorer that actually lists those values: https://blockstream.info/tx/5bc2a806c908532e8260b2a08e0e1de60c2c19761856b140aca81cd54292709b

Nevertheless the value is slightly off by two, which I suspect is because it underestimates e.g. signature size that can vary by one or two bytes. I will take a look at that.