1

From the description of Bitcoin Core, subtractfeefrom is a parameter to define which recipient address would opt-in to receive less and contribute to pay the fee

Here is a usecase:

Alex has one UTXO which contains 10 BTC and he sends Mary and Bob each 1 BTC. The transaction fee is 0.5 BTC, so the change to Alex is 10 - 2 - 0.5 = 7.5 BTC.

How does subtractfeefrom apply to above case in DETAIL?

My high level assumption is

  • Get the required fee for the txn
  • Deduct this amount evenly from every optin output destination, except the change output, e.g 0.5 BTC / 2 destinations
  • First optin output destination also deduct by the non divisible amount - e.g 0.5BTC % 2 destinations
  • check if the deducted output is dust, else continue
  • adding the total deducted amount to change address

Additionally, what if there is no change output, do we need to create one in this case?

2
  • May I ask which RPC you are asking about? I assume it is sendmany, since it has the subtractfeefrom parameter, but there is also subtract_fee_from_outputs on send, and subtractfeefromamount on sendtoaddress.
    – Murch
    Commented Jun 5 at 13:23
  • yes it is sendmany Commented Jun 6 at 2:24

1 Answer 1

1

As you write, the sendmany has the optional parameter subtractfeefrom. It takes an array of addresses which must be a subset of the addresses appearing in the amounts object. Instead of the default behavior of the sender paying for the transaction fees, when any addresses are provided in subtractfeefrom, the transaction fees are deducted in equal shares from the listed recipients.

Under the hood, Bitcoin Core performs coin selection as if the transaction did not cost fees using the actual values of UTXOs instead of their effective values. After assigning the leftover amount to a change output, the fees for the entire transaction are calculated and deducted from the recipients listed in subtractfeefrom in equal shares. If the amount does not divide cleanly, any remainder is deducted from the first listed address, i.e. if the transaction fee amounted to 800 sats and there were three recipients to deduct the fees from, the first would pay 268 sats and the latter two would pay 266 sats.

As the combination of SFFO and attempting to find changeless transactions has caused several bugs in the last year, Bitcoin Core will always create a transaction with change when any variant of subtractfeefromoutput (SFFO) is used. Incidentally, if you want to empty a wallet completely, perhaps consider using the sendall RPC instead.

Overall, SFFO is a huge pain in the neck as it turns a number of the general assumptions about coin selection and transaction building on its head. If you are looking to implement something like it, I would suggest that you do not attempt to copy Bitcoin Core’s approach.

5
  • Oh, i was trying to copy their approach, but some part is not quite easy to understand, would you mind to explain a bit more on how does the effective rate come from? in addtional, when i take a look on the CPP code, it seem it doesnt create change if the SelectCoins process doesnt require, and afterward, it only subtract the fee from recipient if fee_needed - current_fee, i have no idea what is diff by fee_needed vs current_fee too Commented Jun 6 at 2:23
  • so in that case, if the change doesnt create, and fee_needed - current_fee > 0 , means recipients receive less, and fee increase, this is not making sense anyway Commented Jun 6 at 2:26
  • 1. Here is a topic that explores effective value of UTXOs as a concept. The gist is that you know how much fees you will be paying to spend a UTXO when you know the feerate and the output type. You can then deduct the fee from the value of the UTXO in advance of coin selection and makes it much easier to figure out whether you have selected enough. ••• 2. The selection_target in Bitcoin Core is composed of the money needed to create the recipient outputs, plus the fees for the fixed size parts of the transactions, and a budget for change.
    – Murch
    Commented Jun 6 at 13:51
  • 3. The coin selection methods that create changeless transactions are disabled when the user specifies SFFO. Bitcoin Core will always create a transaction with change if the fee is paid by recipients. ••• 4. Usually, coin selection will consider UTXOs at their effective value, i.e. the fee for every UTXO that is selected as an input is already accounted for. When SFFO is active, the UTXOs are counted with their full value and the fee is calculated in the end, before it is deducted from the recipients. That’s why fee_needed - current_fee will not be 0 at that point.
    – Murch
    Commented Jun 6 at 13:53
  • Thank you for the information, i will take a deep dive! Commented Jun 7 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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