@@ -537,28 +537,45 @@ static struct command_result *json_fundpsbt(struct command *cmd,
537537 /* We keep adding until we meet their output requirements. */
538538 utxos = tal_arr (cmd , struct utxo * , 0 );
539539
540+ /* We seperate out UTXOs to exclude if they are uneconomical */
541+ struct utxo * * uneconomical_utxos = tal_arr (cmd , struct utxo * , 0 );
542+
540543 input = AMOUNT_SAT (0 );
541544 while (!inputs_sufficient (input , * amount , * feerate_per_kw , * weight ,
542545 & diff )) {
543546 struct utxo * utxo ;
544547 struct amount_sat fee ;
545548 u32 utxo_weight ;
546549
550+
551+ /* Merge the two lists for exclusion */
552+ struct utxo * * all_excluded = tal_arr (cmd , struct utxo * , 0 );
553+ for (size_t i = 0 ; i < tal_count (utxos ); i ++ ) {
554+ tal_arr_expand (& all_excluded , utxos [i ]);
555+ }
556+ for (size_t i = 0 ; i < tal_count (uneconomical_utxos ); i ++ ) {
557+ tal_arr_expand (& all_excluded , uneconomical_utxos [i ]);
558+ }
559+
547560 utxo = wallet_find_utxo (utxos , cmd -> ld -> wallet ,
548561 current_height ,
549562 & diff ,
550563 * feerate_per_kw ,
551564 maxheight ,
552565 * nonwrapped ,
553- cast_const2 (const struct utxo * * , utxos ));
566+ cast_const2 (const struct utxo * * , all_excluded ));
567+ tal_free (all_excluded );
568+
554569 if (utxo ) {
555570 utxo_weight = utxo_spend_weight (utxo ,
556571 * min_witness_weight );
557572 fee = amount_tx_fee (* feerate_per_kw , utxo_weight );
558573
559574 /* Uneconomic to add this utxo, skip it */
560- if (!all && amount_sat_greater_eq (fee , utxo -> amount ))
575+ if (!all && amount_sat_greater_eq (fee , utxo -> amount )){
576+ tal_arr_expand (& uneconomical_utxos , utxo );
561577 continue ;
578+ }
562579
563580 tal_arr_expand (& utxos , utxo );
564581
@@ -596,6 +613,8 @@ static struct command_result *json_fundpsbt(struct command *cmd,
596613 & diff ));
597614 }
598615
616+ tal_free (uneconomical_utxos );
617+
599618 if (all ) {
600619 /* We need to afford one non-dust output, at least. */
601620 if (!inputs_sufficient (input , AMOUNT_SAT (0 ),
0 commit comments