Returns false if it needed to create change, but couldn't afford. */
| 441 | |
| 442 | /* Returns false if it needed to create change, but couldn't afford. */ |
| 443 | static bool change_for_emergency(struct lightningd *ld, |
| 444 | bool have_anchor_channel, |
| 445 | struct utxo **utxos, |
| 446 | u32 feerate_per_kw, |
| 447 | u32 weight, |
| 448 | struct amount_sat *excess, |
| 449 | struct amount_sat *change) |
| 450 | { |
| 451 | struct amount_sat needed = ld->emergency_sat, fee; |
| 452 | |
| 453 | /* Only needed for anchor channels */ |
| 454 | if (!have_anchor_channel) |
| 455 | return true; |
| 456 | |
| 457 | /* Fine if rest of wallet has funds. Otherwise it may reduce |
| 458 | * needed amount. */ |
| 459 | if (wallet_has_funds(ld->wallet, |
| 460 | cast_const2(const struct utxo **, utxos), |
| 461 | get_block_height(ld->topology), |
| 462 | &needed)) |
| 463 | return true; |
| 464 | |
| 465 | /* If we can afford the rest with existing change output, great (or |
| 466 | * ld->emergency_sat is 0) */ |
| 467 | if (amount_sat_greater_eq(change_amount(*change, |
| 468 | feerate_per_kw, weight), |
| 469 | needed)) |
| 470 | return true; |
| 471 | |
| 472 | /* Try splitting excess to add to change. */ |
| 473 | fee = change_fee(feerate_per_kw, weight); |
| 474 | if (!amount_sat_sub(excess, *excess, fee) |
| 475 | || !amount_sat_sub(excess, *excess, needed)) |
| 476 | return false; |
| 477 | |
| 478 | if (!amount_sat_add(change, *change, fee) |
| 479 | || !amount_sat_add(change, *change, needed)) |
| 480 | abort(); |
| 481 | |
| 482 | /* We *will* get a change output now! */ |
| 483 | assert(amount_sat_eq(change_amount(*change, feerate_per_kw, weight), |
| 484 | needed)); |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | static struct command_result *json_fundpsbt(struct command *cmd, |
| 489 | const char *buffer, |
no test coverage detected