mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-22 08:34:20 +01:00
change fees: more accurate rounding for change amount
We were getting off-by-one for the total amount that the change is for, since it rounds the fee *down*, independent of the total weight of the entire tx. We fix this by using the diff btw the fee of the total weight (w/ and w/o the change output)
This commit is contained in:
@@ -277,6 +277,7 @@ static struct command_result *psbt_created(struct command *cmd,
|
||||
const jsmntok_t *psbttok;
|
||||
struct out_req *req;
|
||||
struct amount_sat excess;
|
||||
u32 weight;
|
||||
|
||||
psbttok = json_get_member(buf, result, "psbt");
|
||||
txp->psbt = json_tok_psbt(txp, buf, psbttok);
|
||||
@@ -300,6 +301,14 @@ static struct command_result *psbt_created(struct command *cmd,
|
||||
result->end - result->start,
|
||||
buf + result->start);
|
||||
|
||||
if (!json_to_number(buf, json_get_member(buf, result,
|
||||
"estimated_final_weight"),
|
||||
&weight))
|
||||
return command_fail(cmd, LIGHTNINGD,
|
||||
"Unparsable estimated_final_weight: '%.*s'",
|
||||
result->end - result->start,
|
||||
buf + result->start);
|
||||
|
||||
/* If we have an "all" output, now we can derive its value: excess
|
||||
* in this case will be total value after inputs paid for themselves. */
|
||||
if (txp->all_output_idx != -1) {
|
||||
@@ -313,7 +322,7 @@ static struct command_result *psbt_created(struct command *cmd,
|
||||
}
|
||||
|
||||
/* So, do we need change? */
|
||||
txp->change_amount = change_amount(excess, txp->feerate);
|
||||
txp->change_amount = change_amount(excess, txp->feerate, weight);
|
||||
if (amount_sat_eq(txp->change_amount, AMOUNT_SAT(0)))
|
||||
return finish_txprepare(cmd, txp);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user