From 9a1041ee979a9668853fba2f73a5e1bc09504732 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 4 May 2021 18:50:02 -0500 Subject: [PATCH] fundpsbt: dont add utxos that are a net-loss if the utxo can't pay for its own fees, dont put it in the tx Changelog-Changed: JSONRPC: fundpsbt will not include UTXOs that aren't economic (can't pay for their own fees), unless 'all' --- wallet/reservation.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/wallet/reservation.c b/wallet/reservation.c index 6a445741f..4fd2a2867 100644 --- a/wallet/reservation.c +++ b/wallet/reservation.c @@ -460,6 +460,8 @@ static struct command_result *json_fundpsbt(struct command *cmd, while (!inputs_sufficient(input, *amount, *feerate_per_kw, *weight, &diff)) { struct utxo *utxo; + struct amount_sat fee; + u32 utxo_weight; utxo = wallet_find_utxo(utxos, cmd->ld->wallet, cmd->ld->topology->tip->height, @@ -468,6 +470,14 @@ static struct command_result *json_fundpsbt(struct command *cmd, maxheight, cast_const2(const struct utxo **, utxos)); if (utxo) { + utxo_weight = utxo_spend_weight(utxo, + *min_witness_weight); + fee = amount_tx_fee(*feerate_per_kw, utxo_weight); + + /* Uneconomic to add this utxo, skip it */ + if (!all && amount_sat_greater_eq(fee, utxo->amount)) + continue; + tal_arr_expand(&utxos, utxo); /* It supplies more input. */ @@ -476,7 +486,7 @@ static struct command_result *json_fundpsbt(struct command *cmd, "impossible UTXO value"); /* But also adds weight */ - *weight += utxo_spend_weight(utxo, *min_witness_weight); + *weight += utxo_weight; continue; }