| 458 | } |
| 459 | |
| 460 | static struct command_result *json_fundpsbt(struct command *cmd, |
| 461 | const char *buffer, |
| 462 | const jsmntok_t *obj UNNEEDED, |
| 463 | const jsmntok_t *params) |
| 464 | { |
| 465 | struct utxo **utxos; |
| 466 | u32 *feerate_per_kw; |
| 467 | u32 *minconf, *weight, *min_witness_weight; |
| 468 | struct amount_sat *amount, input, diff; |
| 469 | bool all, *excess_as_change; |
| 470 | u32 *locktime, *reserve, maxheight; |
| 471 | |
| 472 | if (!param(cmd, buffer, params, |
| 473 | p_req("satoshi", param_sat_or_all, &amount), |
| 474 | p_req("feerate", param_feerate, &feerate_per_kw), |
| 475 | p_req("startweight", param_number, &weight), |
| 476 | p_opt_def("minconf", param_number, &minconf, 1), |
| 477 | p_opt_def("reserve", param_reserve_num, &reserve, |
| 478 | RESERVATION_DEFAULT), |
| 479 | p_opt("locktime", param_number, &locktime), |
| 480 | p_opt_def("min_witness_weight", param_number, |
| 481 | &min_witness_weight, 0), |
| 482 | p_opt_def("excess_as_change", param_bool, |
| 483 | &excess_as_change, false), |
| 484 | NULL)) |
| 485 | return command_param_failed(); |
| 486 | |
| 487 | all = amount_sat_eq(*amount, AMOUNT_SAT(-1ULL)); |
| 488 | maxheight = minconf_to_maxheight(*minconf, cmd->ld); |
| 489 | |
| 490 | /* We keep adding until we meet their output requirements. */ |
| 491 | utxos = tal_arr(cmd, struct utxo *, 0); |
| 492 | |
| 493 | input = AMOUNT_SAT(0); |
| 494 | while (!inputs_sufficient(input, *amount, *feerate_per_kw, *weight, |
| 495 | &diff)) { |
| 496 | struct utxo *utxo; |
| 497 | struct amount_sat fee; |
| 498 | u32 utxo_weight; |
| 499 | |
| 500 | utxo = wallet_find_utxo(utxos, cmd->ld->wallet, |
| 501 | cmd->ld->topology->tip->height, |
| 502 | &diff, |
| 503 | *feerate_per_kw, |
| 504 | maxheight, |
| 505 | cast_const2(const struct utxo **, utxos)); |
| 506 | if (utxo) { |
| 507 | utxo_weight = utxo_spend_weight(utxo, |
| 508 | *min_witness_weight); |
| 509 | fee = amount_tx_fee(*feerate_per_kw, utxo_weight); |
| 510 | |
| 511 | /* Uneconomic to add this utxo, skip it */ |
| 512 | if (!all && amount_sat_greater_eq(fee, utxo->amount)) |
| 513 | continue; |
| 514 | |
| 515 | tal_arr_expand(&utxos, utxo); |
| 516 | |
| 517 | /* It supplies more input. */ |
nothing calls this directly
no test coverage detected