# Calculating Bitcoin Tx Fee

I am trying to transfer Bitcoin from the canister to another p2pkh_address. The problem is when I try to transfer the whole amount, it gives an error on “building transaction…”, when I transfer the amount - fee_per_byte it works -but after the transfer, some sats are still left in the canister address.
So is there any API to pre-calculate the fee on the network so I can replace it with fee_per_byte?
Thanks!

Specifically, it would help to see the source code that you use to build the transaction.

I am using this example all I change is using different derivation_paths to get distinct public keys.
The problem is, let’s say I have 1BTC in one of the canister addresses, and I enter `1 BTC(10^8 sats)` in the ` request.amount_in_satoshi` to send to another address it gives an `Error building transaction.` When I enter `10^8 - fee_per_byte( or >2000 sats)` it works

I thought if I entered the whole amount to send then this method take care of the network fee and automatically deducts from the total amount.

Is there any way to calculate the network fee? I am not sure whether the length of utxos of a spender does matter in calculating the total fee for a transaction (or in the length of a signed transaction). If yes, then in my use case, I considered all derived canister-public keys (addresses) should contain only one utxo to spend, so can we precalculate the fee on the basis of one utxo?

Yes, if you try to send the whole amount, it will fail because there is nothing left for the fee.

Note that sending `10^8 - fee_per_byte` is not correct because the fee actually depends on the size of the transaction. The amount you want to send is the total amount minus the fee, which is the fee per byte (or, more precisely, fee per virtual byte) times the size of the transaction.

The function `build_transaction_with_fee` just tries to build a transaction with the specified fee. If it succeeds, the change is returned to the canister as you can see here. That’s why you see some left-over satoshi in the canister.

So, the function does not exactly do what you want. If you want to send all bitcoins held by a certain address, consider adding a separate function that simply increases the output value by the `remaining_amount` (rather than creating a new output).

Is there any way to calculate the network fee?

Note that you can set the fee to whatever you want. You can use the `bitcoin_get_current_fee_percentiles` endpoint to get recent fees (in millisatoshi per virtual byte). You can then build a transaction to get the size and multiply the size with the chosen fee-per-vbyte percentile.

so can we precalculate the fee on the basis of one utxo?

If you know how many inputs and outputs you have in your transactions, you can determine the size using a transaction size calculator. You can then compute the fee by multiplying the size with the chosen fee per vbyte and you get your fee.

I hope this helps!

2 Likes

What’s going on with the new guy spamming left and right?

Thanks for the explanation