I see this question time and time again, but I only find answers for calculating the fees in creating raw transactions.

I am using sendtoaddress and would like to be able to calculate the tx fee needed prior to calling the api. I would like to avoid creating a raw transaction if reasonably possible.

how might i do this using the json-rpc api? (i'm using bitcoind)


You have to use raw transactions in order to gain full control over the tx fees. Then you know what inputs and outputs you are using and it's very easy to calculate the tx fees.

Sample implementation: https://github.com/GeorgeKimionis/BitcoinLib/blob/master/CoinWrapper/Services/RpcServices/RpcExtenderService/RpcExtenderService.cs#L95

  • 2
    thx for the personal link @GeorgeKimionis! i kinda thought that i would have to create the raw tx, but wanted to make sure i wasn't missing something!
    – Jon B
    Jun 7 '14 at 18:54
  • How exactly does the graphical wallet calculate fees then? In bitcoin 0.9 at least, it'll tell you just before sending a transaction the amount of fees that is required to send it.
    – Earlz
    Jun 7 '14 at 19:12
  • @Earlz the reference client uses its own coin selection algorithms and calculated the tx fees based on the inputs and outputs selected by this algorithm, this functionality however is only exposed through the RPC API commands: sendfrom, sendmany, and sendtoaddress that return the tx id if successful which you have to look up after the tx is sent and find out what the tx fee was, in other case you can send a raw tx and calculate all figures before the tx is sent. Jun 8 '14 at 9:12

If you call

bitcoin-cli settxfee <fee>

just before call

bitcoin-cli sendtoaddress "1LipeR1AjHL6gwE7WQECW4a2H4tuqm768N" <value>

Then the fee will be <fee>.

Important note: make sure in the graphic mode (when you start with bitcoin-qt command) the "custom" is selected, otherwise your command settxfee will be ignored (see image bellow).

Also, I'm not sure if this option is selected by default, and I am not sure as well if it you can set "custom" via API. If someone knows that, please edit this answer and fix it.

Note 2: I'm not sure if this is strictly necessary, maybe not. I didn't make many tests. The test I made was starting bitcoin with bitcoin-qt -txindex -server & and then letting the interface open. If "recommended" is selected the command settxfee will be ignored. I didn't tested e.g. what if let the "recommended" option selected, close the interface and then start with bitcoind -txindex for example.

enter image description here


A bit late but you might take a look at https://github.com/bitcoin/bitcoin/pull/5503 It's not merged (yet) but it would give you the opportunity to get the fee before sending the transaction (with 4 steps: createrawtransaction, fundrawtransaction [get fee], signrawtransaction, sendrawtransaction)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.