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

Function handle_mfc_change

plugins/spender/multifundchannel.c:1225–1268  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1223}
1224
1225static struct command_result *
1226handle_mfc_change(struct multifundchannel_command *mfc)
1227{
1228 size_t change_weight;
1229 struct amount_sat change_fee, fee_paid, total_fee;
1230 struct amount_sat change_min_limit;
1231
1232 /* Determine if adding a change output is worth it.
1233 * Get the weight of a change output and how much it
1234 * costs.
1235 */
1236 change_weight = bitcoin_tx_output_weight(
1237 BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN);
1238
1239 /* To avoid 'off-by-one' errors due to rounding down
1240 * (which we do in `amount_tx_fee`), we find the total calculated
1241 * fees (estimated_weight + change weight @ feerate) and subtract
1242 * the originally calculated fees (estimated_weight @ feerate) */
1243 fee_paid = amount_tx_fee(mfc->feerate_per_kw,
1244 mfc->estimated_final_weight);
1245 total_fee = amount_tx_fee(mfc->feerate_per_kw,
1246 mfc->estimated_final_weight + change_weight);
1247 if (!amount_sat_sub(&change_fee, total_fee, fee_paid))
1248 abort();
1249
1250 /* The limit is equal to the change_fee plus the dust limit. */
1251 if (!amount_sat_add(&change_min_limit,
1252 change_fee, chainparams->dust_limit))
1253 plugin_err(mfc->cmd->plugin,
1254 "Overflow dust limit and change fee.");
1255
1256 /* Is the excess over the limit? */
1257 if (amount_sat_greater(mfc->excess_sat, change_min_limit)) {
1258 bool ok = amount_sat_sub(&mfc->change_amount,
1259 mfc->excess_sat, change_fee);
1260 assert(ok);
1261 mfc->change_needed = true;
1262 if (!mfc->change_scriptpubkey)
1263 return acquire_change_address(mfc);
1264 } else
1265 mfc->change_needed = false;
1266
1267 return mfc_psbt_acquired(mfc);
1268}
1269
1270/* If one of the destinations specified "all", figure out how much that is. */
1271static struct command_result *

Callers 2

compute_mfc_allFunction · 0.85
after_fundpsbtFunction · 0.85

Calls 9

bitcoin_tx_output_weightFunction · 0.85
abortFunction · 0.85
amount_sat_greaterFunction · 0.85
acquire_change_addressFunction · 0.85
mfc_psbt_acquiredFunction · 0.85
amount_tx_feeFunction · 0.50
amount_sat_subFunction · 0.50
amount_sat_addFunction · 0.50
plugin_errFunction · 0.50

Tested by

no test coverage detected