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

Function calc_maxfee

plugins/xpay/xpay.c:3481–3559  ·  view source on GitHub ↗

xpay doesn't have maxfeepercent or exemptfee, so we convert them to * an absolute restriction here. If we can't, fail and let pay handle * it. */

Source from the content-addressed store, hash-verified

3479 * an absolute restriction here. If we can't, fail and let pay handle
3480 * it. */
3481static bool calc_maxfee(struct command *cmd,
3482 const char **maxfeestr,
3483 const char *buf,
3484 const jsmntok_t *invstringtok,
3485 const jsmntok_t *amount_msattok,
3486 const jsmntok_t *exemptfeetok,
3487 const jsmntok_t *maxfeepercenttok)
3488{
3489 u64 maxfeepercent_ppm;
3490 struct amount_msat amount, maxfee, exemptfee;
3491
3492 if (!exemptfeetok && !maxfeepercenttok)
3493 return true;
3494
3495 /* Can't have both */
3496 if (*maxfeestr)
3497 return false;
3498
3499 /* If they specify amount easy, otherwise take from invoice */
3500 if (amount_msattok) {
3501 if (!parse_amount_msat(&amount, buf + amount_msattok->start,
3502 amount_msattok->end - amount_msattok->start))
3503 return false;
3504 } else {
3505 const struct bolt11 *b11;
3506 const char *invstr, *fail;
3507
3508 /* We need to know total amount to calc fee */
3509 if (!invstringtok)
3510 return false;
3511
3512 invstr = json_strdup(tmpctx, buf, invstringtok);
3513 b11 = bolt11_decode(tmpctx, invstr, NULL, NULL, NULL, &fail);
3514 if (b11 != NULL) {
3515 if (b11->msat == NULL)
3516 return false;
3517 amount = *b11->msat;
3518 } else {
3519 const struct tlv_invoice *b12;
3520 b12 = invoice_decode(tmpctx, invstr, strlen(invstr),
3521 NULL, NULL, &fail);
3522 if (b12 == NULL || b12->invoice_amount == NULL)
3523 return false;
3524 amount = amount_msat(*b12->invoice_amount);
3525 }
3526 }
3527
3528 if (maxfeepercenttok) {
3529 if (!json_to_millionths(buf,
3530 maxfeepercenttok,
3531 &maxfeepercent_ppm))
3532 return false;
3533 } else
3534 maxfeepercent_ppm = 500000;
3535
3536 if (!amount_msat_fee(&maxfee, amount, 0, maxfeepercent_ppm / 100))
3537 return false;
3538

Callers 1

handle_rpc_commandFunction · 0.85

Calls 12

bolt11_decodeFunction · 0.85
invoice_decodeFunction · 0.85
amount_msat_feeFunction · 0.85
amount_msatClass · 0.70
parse_amount_msatFunction · 0.50
json_strdupFunction · 0.50
json_to_millionthsFunction · 0.50
amount_msat_lessFunction · 0.50
fmt_amount_msatFunction · 0.50
plugin_logFunction · 0.50
json_tok_full_lenFunction · 0.50
json_tok_fullFunction · 0.50

Tested by

no test coverage detected