| 1223 | } |
| 1224 | |
| 1225 | static struct command_result * |
| 1226 | handle_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. */ |
| 1271 | static struct command_result * |
no test coverage detected