<p>If you want to implement your coin selection, the only alternative to calculating the fee manually I can think of is the following:</p>

<ul>
<li>You select which inputs you want to be used in your transaction based on your coin selection algorithm.</li>
<li>You then proceed to lock every input that is not the one you selected. You can get the list of all inputs through <code>listunspent</code> and you can lock an input with <code>lockunspent false [{"txid":"hex", "vout":n}, ...]</code>.</li>
<li>Now you can use your <code>walletcreatefundedpsbt</code> without being afraid it will select any input that you don't want and you can pass the option <code>{"feerate":your_feerate}</code>.</li>
<li>After getting your raw transaction, you can unlock all previously locked inputs with <code>lockunspent true</code></li>
</ul>

CC BY-SA 4.0
2019-12-20T17:25:21.303Z
I tried to implement this with a call to `walletcreatefundedpsbt()`, it fails with `Signing transaction failed (-4)`, I updated my OP with more details, any idea what I'm doing wrong?
eric
CC BY-SA 4.0
2019-12-21T15:49:03.620Z
Not sure what that error message means... maybe try to remove the last set of arguments and work your way adding more and more arguments/options. Also, launching bitcoind with the `debug=rpc` option might give some valuable information in the `debug.log`
Pedro
CC BY-SA 4.0
2020-01-08T20:52:53.680Z
I figured out why I was getting `Signing transaction failed (-4)`.  It's because the input UTXO is not "solvable" as explained here: https://bitcoin.stackexchange.com/questions/91425/errors-with-walletcreatefundedpsbt-non-solvable-utxos

My original  problem remains unresolved.
eric
CC BY-SA 4.0
2019-12-20T15:41:55.977Z
2019-12-20T15:41:55.977Z
Pedro
<p>When my user invokes my app, he passes as an input parameter the desired value of the TX fee in satoshis per byte.  I create a PSBT by calling <code>createpsbt()</code>.  I exert complete control over the determination of inputs and outputs.  For example, I don't call <code>walletcreatefundedpsbt()</code>, because I don't want bitcoind to select inputs on my behalf.  I have a chicken and egg problem in that I need to call <code>createpsbt()</code> in order to determine the size of the TX in bytes, but I need to specify the fee amount before the call to <code>createpsbt()</code>.  The best solution that I can think of is to execute a dummy call to <code>createpsbt()</code> using a dummy fee value, then measure the size of the TX, then do a real call to <code>createpsbt()</code> after correctly calculating the fee amount.  Is there a better way?</p>

<p><strong>Edit</strong>: I want to spend all UTXOs.  I want two outputs, one to receive a fixed amount, the other to receive change less the fee.  I have tried many incantations, I can't get it to work.  Here's my latest attempt:</p>

<pre><code>raw_psbt = rpc.walletcreatefundedpsbt(
    # all UTXOs, as returned by listunspent():
    inputs,
    # one recipient: [ { addr : amount } ]
    outputs,
    0,  # locktime
    {
        "replaceable" : True,
        # I would like for the change less the fee to go here:
        "changeAddress" : xxx,
        "includeWatching" : True,
        "feeRate" : 0.005,
    }
)
</code></pre>

<p>This fails with <code>Signing transaction failed (-4)</code>.  Any idea what I'm doing wrong?</p>

<p><strong>Edit #2</strong>: I can't use <code>walletcreatefundedpsbt()</code> as suggested below to create the PSBT, because that function requires that its inputs be "solvable", which is not the case in my environment.  More info at this link:</p>

<p><a href="https://bitcoin.stackexchange.com/questions/91425/errors-with-walletcreatefundedpsbt-non-solvable-utxos">Errors with <code>walletcreatefundedpsbt</code> &amp; non-solvable UTXOs</a></p>

<p>So my original question remains unresolved at this point.</p>

<p><strong>Edit #3</strong>: So I asked on Slack.  The problem is that our UTXOs are 1) not "solvable" and 2) cannot be expressed in the descriptor language, so Bitcoin Core has no way to calculate the TX size before the TX is signed.  Apparently I would have to calculated it manually as explained in the attached screenshot.<a href="https://i.sstatic.net/p3i1U.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/p3i1U.png" alt="slack chat"></a></p>

<bitcoin-core><transaction-fees><bip174-psbt>
How do I calculate transaction fee for call to createpsbt()?
eric
# How do I calculate transaction fee for call to createpsbt()?

(Asked by: username_0 on 2019-12-20)

When my user invokes my app, he passes as an input parameter the desired value of the TX fee in satoshis per byte. I create a PSBT by calling `createpsbt()`. I exert complete control over the determination of inputs and outputs. For example, I don't call `walletcreatefundedpsbt()`, because I don't want bitcoind to select inputs on my behalf. I have a chicken and egg problem in that I need to call `createpsbt()` in order to determine the size of the TX in bytes, but I need to specify the fee amount before the call to `createpsbt()`. The best solution that I can think of is to execute a dummy call to `createpsbt()` using a dummy fee value, then measure the size of the TX, then do a real call to `createpsbt()` after correctly calculating the fee amount. Is there a better way?

**Edit**: I want to spend all UTXOs. I want two outputs, one to receive a fixed amount, the other to receive change less the fee. I have tried many incantations, I can't get it to work. Here's my latest attempt:

```
raw_psbt = rpc.walletcreatefundedpsbt(
    # all UTXOs, as returned by listunspent():
    inputs,
    # one recipient: [ { addr : amount } ]
    outputs,
    0,  # locktime
    {
        "replaceable" : True,
        # I would like for the change less the fee to go here:
        "changeAddress" : xxx,
        "includeWatching" : True,
        "feeRate" : 0.005,
    }
)

```

This fails with `Signing transaction failed (-4)`. Any idea what I'm doing wrong?

**Edit #2**: I can't use `walletcreatefundedpsbt()` as suggested below to create the PSBT, because that function requires that its inputs be "solvable", which is not the case in my environment. More info at this link:

[Errors with `walletcreatefundedpsbt` & non-solvable UTXOs](https://bitcoin.stackexchange.com/questions/91425/errors-with-walletcreatefundedpsbt-non-solvable-utxos)

So my original question remains unresolved at this point.

**Edit #3**: So I asked on Slack. The problem is that our UTXOs are 1) not "solvable" and 2) cannot be expressed in the descriptor language, so Bitcoin Core has no way to calculate the TX size before the TX is signed. Apparently I would have to calculated it manually as explained in the attached screenshot.[![slack chat](https://i.sstatic.net/p3i1U.png)](https://i.sstatic.net/p3i1U.png)

**Tags:** bitcoin-core, transaction-fees, bip174-psbt


---

## 1 answers

### Answer by username_1 on 2019-12-20. Score: 0

If you want to implement your coin selection, the only alternative to calculating the fee manually I can think of is the following:

* You select which inputs you want to be used in your transaction based on your coin selection algorithm.
* You then proceed to lock every input that is not the one you selected. You can get the list of all inputs through `listunspent` and you can lock an input with `lockunspent false [{"txid":"hex", "vout":n}, ...]`.
* Now you can use your `walletcreatefundedpsbt` without being afraid it will select any input that you don't want and you can pass the option `{"feerate":your_feerate}`.
* After getting your raw transaction, you can unlock all previously locked inputs with `lockunspent true`

> **Comments:**
> **username_0**: I tried to implement this with a call to `walletcreatefundedpsbt()`, it fails with `Signing transaction failed (-4)`, I updated my OP with more details, any idea what I'm doing wrong?  
> **username_1**: Not sure what that error message means... maybe try to remove the last set of arguments and work your way adding more and more arguments/options. Also, launching bitcoind with the `debug=rpc` option might give some valuable information in the `debug.log`  
> **username_0**: I figured out why I was getting `Signing transaction failed (-4)`.  It's because the input UTXO is not "solvable" as explained here: https://bitcoin.stackexchange.com/questions/91425/errors-with-walletcreatefundedpsbt-non-solvable-utxos  My original  problem remains unresolved.  

---

