<p>If the inputs to the transaction are known to the Bitcoin Core wallet, then you can use the <code>fundrawtransaction</code> command to skip fee estimation and input selection. The process then becomes:</p>

<ol>
<li><code>createrawtransaction</code> with only the outputs you want (i.e. no change, only the recipients).</li>
<li><code>fundrawtransaction</code></li>
<li><code>signrawtransaction</code></li>
<li><code>sendrawtransaction</code></li>
</ol>

<hr>

<p>If you are not using Bitcoin Core's wallet, you can use effective values.</p>

<p>Presumably you know the amounts in the inputs before you actual create the transaction. So you can then calculate how large the inputs will be, calculate how much in fees that will cost based on the output of <code>estimatesmartfee</code>, and subtract that fee amount from the actual value of the input. This is your effective value.</p>

<p>When you create the outputs, you calculate the total size of the non-input data and calculate the fees for those data. Now you have how much total value your inputs must cover. You can then select coins that way.</p>

<p>So the process is:</p>

<ol>
<li><code>estimatesmartfee</code></li>
<li>For each potential input, calculate the fee for that input and compute the effective value.</li>
<li>For each output (including change if you assume that there will be change) and for the extra fixed size transaction overhead, calculate the non-input fees.</li>
<li>Add the non-input fees to the amount you want to send, this is the amount that your inputs will need to cover the total cost.</li>
<li>Choose the inputs you want to spend based upon their effective values.</li>
<li><code>createrawtransaction</code>, <code>signrawtransaction</code>, <code>sendrawtransaction</code>.</li>
</ol>

<hr>

<p>The size of inputs in vbytes are as follows:</p>

<p>P2PKH: 148 vbytes</p>

<p>P2WPKH: 37 vbytes</p>

<p>P2SH-P2WPKH: 60 vbytes</p>

<p>For P2SH and P2WSH inputs, it depends on the script that you are using. Most people though will be using P2PKH, P2WPKH, or P2SH-P2WPKH.</p>

CC BY-SA 3.0
2018-04-11T21:31:04.370Z
2018-04-11T21:31:04.370Z
Ava Chow
<p>I have my code working correctly now to create and send a transaction with the correct network fee. Here is my pseudo-code using the json rpc:</p>

<ol>
<li><p>Estimate network fee using <code>estimatesmartfee</code> "ECONOMICAL" based on a typical transaction size</p></li>
<li><p>Adjust payout amount based on fee estimate</p></li>
<li><p><code>createrawtransaction</code> using estimated payout amount</p></li>
<li><p><code>signrawtransaction</code></p></li>
<li><p>read <code>size</code> of decoded raw transaction from step 2</p></li>
<li><p>Recalculate network fee using <code>estimatesmartfee</code> "ECONOMICAL" based on a actual transaction size from last step</p></li>
<li><p>Adjust payout amount based on actual network fee </p></li>
<li><p><code>createrawtransaction</code> using actual payout amount</p></li>
<li><p><code>signrawtransaction</code></p></li>
<li><p><code>sendrawtransaction</code></p></li>
</ol>

<p>Steps 6-9 repeats Steps 1-4.</p>

<p>Is there a way to do this without having to call everything twice?</p>

CC BY-SA 3.0
2018-04-11T21:20:50.097Z
Are you using inputs that are available to the Bitcoin Core wallet?
Ava Chow
CC BY-SA 3.0
2018-04-11T21:41:46.003Z
@AndrewChow, before the pseudo-code begins, I already know the txid and the vout for my input. This tx will not be one from my wallet; it will be a tx created by someone else.
toddmo
<json-rpc><raw-transaction><fee-estimation>
Less circuitous way to get network fee in raw transaction
toddmo
# Less circuitous way to get network fee in raw transaction

(Asked by: username_0 on 2018-04-11)

I have my code working correctly now to create and send a transaction with the correct network fee. Here is my pseudo-code using the json rpc:

1. Estimate network fee using `estimatesmartfee` "ECONOMICAL" based on a typical transaction size
2. Adjust payout amount based on fee estimate
3. `createrawtransaction` using estimated payout amount
4. `signrawtransaction`
5. read `size` of decoded raw transaction from step 2
6. Recalculate network fee using `estimatesmartfee` "ECONOMICAL" based on a actual transaction size from last step
7. Adjust payout amount based on actual network fee
8. `createrawtransaction` using actual payout amount
9. `signrawtransaction`
10. `sendrawtransaction`

Steps 6-9 repeats Steps 1-4.

Is there a way to do this without having to call everything twice?

**Tags:** json-rpc, raw-transaction, fee-estimation

> **Comments:**
> **username_1**: Are you using inputs that are available to the Bitcoin Core wallet?  
> **username_0**: @AndrewChow, before the pseudo-code begins, I already know the txid and the vout for my input. This tx will not be one from my wallet; it will be a tx created by someone else.  

---

## 1 answers

### Answer by username_1 on 2018-04-11. Score: 0 | Accepted answer

If the inputs to the transaction are known to the Bitcoin Core wallet, then you can use the `fundrawtransaction` command to skip fee estimation and input selection. The process then becomes:

1. `createrawtransaction` with only the outputs you want (i.e. no change, only the recipients).
2. `fundrawtransaction`
3. `signrawtransaction`
4. `sendrawtransaction`

---

If you are not using Bitcoin Core's wallet, you can use effective values.

Presumably you know the amounts in the inputs before you actual create the transaction. So you can then calculate how large the inputs will be, calculate how much in fees that will cost based on the output of `estimatesmartfee`, and subtract that fee amount from the actual value of the input. This is your effective value.

When you create the outputs, you calculate the total size of the non-input data and calculate the fees for those data. Now you have how much total value your inputs must cover. You can then select coins that way.

So the process is:

1. `estimatesmartfee`
2. For each potential input, calculate the fee for that input and compute the effective value.
3. For each output (including change if you assume that there will be change) and for the extra fixed size transaction overhead, calculate the non-input fees.
4. Add the non-input fees to the amount you want to send, this is the amount that your inputs will need to cover the total cost.
5. Choose the inputs you want to spend based upon their effective values.
6. `createrawtransaction`, `signrawtransaction`, `sendrawtransaction`.

---

The size of inputs in vbytes are as follows:

P2PKH: 148 vbytes

P2WPKH: 37 vbytes

P2SH-P2WPKH: 60 vbytes

For P2SH and P2WSH inputs, it depends on the script that you are using. Most people though will be using P2PKH, P2WPKH, or P2SH-P2WPKH.


---

