1) if I set "subtractfeefromamount" to false, fee deducted from where? and How is fee calculated?
2) for example , if i have 2 bitcoin in my wallet, and the user withdraws one bitcoin, the rest coin (one bitcoin - fee) how calculated (utxo) ?
It's always "
false" by default and the fee should be covered by the input,
if its value isn't enough, Bitcoin core will have to include your other inputs or select the one with the right value,
otherwise, it will return with an error: "
Insufficient funds"
3) consider the above example, I want the rest coin return to the new address (different from the source address (HD wallet)), Is there a way?
Use
sendmany (
0.21.0/rpc/wallet/sendmany) instead of
sendtoaddress and enable "
subtractfeefromamount" for the "
new address" to make sure that it wont generate a change address.
The tricky part is to set the correct amount up to the last satoshi, if it was set with lower amount, a change output will be added.
For example (
RegTest):
sendmany "" "{\"bcrt1qk3hqnujdex64v045qdh7df28vywg5rwtdzj008\":1,\"2Mu9fzGjgnuvCrAJ1cuAu1vZGfDVzHZNHKU\":18.10239312}" 1 "comment" "[\"2Mu9fzGjgnuvCrAJ1cuAu1vZGfDVzHZNHKU\"]"
- bcrt1qk3hqnujdex64v045qdh7df28vywg5rwtdzj008 - user's address that you'll pay.
- 2Mu9fzGjgnuvCrAJ1cuAu1vZGfDVzHZNHKU - your "new address" from other wallet that will receive the rest of the balance.
The example wallet's balance is 19.10239312 RegTestBTC, so the values of the outputs are
1 and
18.10239312 respectively.
But the final sent amount to 2Mu9fzGjgnuvCrAJ1cuAu1vZGfDVzHZNHKU is only
18.10238080 RegTestBTC because it's set with
subtractfeefromamount flag "
true".