Before calling, ensure `onchain_fee` is accounted for in `needed_funds`. * * If we need to fund from the onchain wallet this requires another pass so * `onchain_fee` will be subtracted out of `needed_funds` and an * `onchain_wallet_fund` command is returned. * * If we are finished funding or we can take the needed funds out of a wallet * output, we return NULL for success and leave `needed_
| 1302 | * output, we return NULL for success and leave `needed_funds` unmolested. |
| 1303 | */ |
| 1304 | static struct command_result *handle_wallet_fund(struct command *cmd, |
| 1305 | struct splice_cmd *splice_cmd, |
| 1306 | struct amount_sat onchain_fee) |
| 1307 | { |
| 1308 | size_t index; |
| 1309 | struct splice_script_result *input, *output; |
| 1310 | struct amount_sat already_funded, wallet_funding, missing_funds; |
| 1311 | |
| 1312 | wallet_funding = wallet_funding_amnt(splice_cmd); |
| 1313 | |
| 1314 | if (!amount_sat_sub(&missing_funds, splice_cmd->needed_funds, |
| 1315 | wallet_funding)) |
| 1316 | return do_fail(cmd, splice_cmd, |
| 1317 | JSONRPC2_INVALID_PARAMS, |
| 1318 | "Failed to calculate missing funds"); |
| 1319 | |
| 1320 | plugin_log(cmd->plugin, LOG_INFORM, "handle_wallet_fund needed_funds" |
| 1321 | " %s, current_funds %s, missing_funds %s", |
| 1322 | fmt_amount_sat(tmpctx, splice_cmd->needed_funds), |
| 1323 | fmt_amount_sat(tmpctx, wallet_funding), |
| 1324 | fmt_amount_sat(tmpctx, missing_funds)); |
| 1325 | |
| 1326 | input = input_wallet(splice_cmd, &index); |
| 1327 | output = output_wallet(splice_cmd); |
| 1328 | |
| 1329 | if (!input) |
| 1330 | return do_fail(cmd, splice_cmd, |
| 1331 | JSONRPC2_INVALID_PARAMS, |
| 1332 | "Can't fund wallet with no input wallet"); |
| 1333 | |
| 1334 | /* Can we fund the input by just subtracting from the output? */ |
| 1335 | if (output && amount_sat_greater(output->in_sat, missing_funds)) { |
| 1336 | |
| 1337 | plugin_log(cmd->plugin, LOG_INFORM, "Taking %s" |
| 1338 | " from output wallet %s to cover fee", |
| 1339 | fmt_amount_sat(tmpctx, missing_funds), |
| 1340 | fmt_amount_sat(tmpctx, output->in_sat)); |
| 1341 | |
| 1342 | if (!amount_sat_sub(&output->in_sat, output->in_sat, |
| 1343 | missing_funds)) |
| 1344 | return do_fail(cmd, splice_cmd, |
| 1345 | JSONRPC2_INVALID_PARAMS, |
| 1346 | tal_fmt(tmpctx, |
| 1347 | "Failed to subtract fee amount %s" |
| 1348 | " from output %s", |
| 1349 | fmt_amount_sat(tmpctx, missing_funds), |
| 1350 | fmt_amount_sat(tmpctx, output->in_sat))); |
| 1351 | |
| 1352 | return NULL; |
| 1353 | } |
| 1354 | |
| 1355 | already_funded = input->out_sat; |
| 1356 | |
| 1357 | if (!amount_sat_add(&input->out_sat, input->out_sat, missing_funds)) |
| 1358 | return do_fail(cmd, splice_cmd, |
| 1359 | JSONRPC2_INVALID_PARAMS, |
| 1360 | "Unable to add missing funds to wallet input"); |
| 1361 |
no test coverage detected