MCPcopy Create free account
hub / github.com/ElementsProject/lightning / change_for_emergency

Function change_for_emergency

wallet/reservation.c:443–486  ·  view source on GitHub ↗

Returns false if it needed to create change, but couldn't afford. */

Source from the content-addressed store, hash-verified

441
442/* Returns false if it needed to create change, but couldn't afford. */
443static 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
488static struct command_result *json_fundpsbt(struct command *cmd,
489 const char *buffer,

Callers 2

json_fundpsbtFunction · 0.85
json_utxopsbtFunction · 0.85

Calls 9

wallet_has_fundsFunction · 0.85
change_amountFunction · 0.85
change_feeFunction · 0.85
abortFunction · 0.85
get_block_heightFunction · 0.50
amount_sat_greater_eqFunction · 0.50
amount_sat_subFunction · 0.50
amount_sat_addFunction · 0.50
amount_sat_eqFunction · 0.50

Tested by

no test coverage detected