bitcoin core – Select a fee based on confirmation time. How to implement it?
- I’ve created a standalone endpoint to test estimatesmartfee method and the estimated fee is the same no matter how many blocks I have selected (2,4,6,…).
This is expected on testnet. It has very irregular and bursty transaction activity, as well as inconsistent fees due to a lack of market pressure. As fee estimation is based on observing the time it takes for transactions to confirm based on their feerate, there simply isn’t useful data on testnet to create reasonably-looking fee estimates.
If you’d do this on mainmet you would get useful numbers. A rule of thumb is that your node needs to be running about twice as long as the target confirmation window you want.
- I’m using createrawtransaction RPC method to build the transaction but I cannot find a point where I can specify to the transaction to be added in that block I have specified in the previous step.
You cannot control which block it goes into – you can offer a fee, and hope it incentivizes mimers to pick it up. Furter,
createrawtransaction is more low-level than what you want to do. Bitcoin transactions do not have an explicit fee; rather, the fee is the difference between the value of the UTXOs being spent in its inputs, and the value of the UTXOs being created in its outputs. With
createrawtransaction you have to manually specify the inputs and outputs, and the fee is just the resulting difference. In general, you’ll want to add an output back to an address you control yourself for “change”.
It is inadvisable to do this manually. You can use the
fundrawtransaction RPC to do this for you, if you are using the Bitcoin Core wallet functionality. You give it a raw transaction with just the output(s) you want, and it will select inputs from your own wallet to fund it, as well as add a change output back to itself when necessary.
Note: using the PSBT RPCs is generally a lot more convenient than using the raw transaction RPCs (see
I have tried with sendtoaddress method as well but I don’t know how to calculate fee_rate parameter to establish the transaction’s fee.