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. */
| 3479 | * an absolute restriction here. If we can't, fail and let pay handle |
| 3480 | * it. */ |
| 3481 | static 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 |
no test coverage detected