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

Function param_amount

plugins/offers_offer.c:33–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31}
32
33static struct command_result *param_amount(struct command *cmd,
34 const char *name,
35 const char *buffer,
36 const jsmntok_t *tok,
37 struct tlv_offer *offer)
38{
39 const struct iso4217_name_and_divisor *isocode;
40 jsmntok_t number, whole, frac;
41 u64 cents;
42
43 if (msat_or_any(buffer, tok, offer))
44 return NULL;
45
46 offer->offer_amount = tal(offer, u64);
47
48 /* BOLT #12:
49 *
50 * - MUST specify `offer_currency` `iso4217` as an ISO 4217 three-letter code.
51 * - MUST specify `offer_amount` in the currency unit adjusted by the ISO 4217
52 * exponent (e.g. USD cents).
53 */
54 if (tok->end - tok->start < ISO4217_NAMELEN)
55 return command_fail_badparam(cmd, name, buffer, tok,
56 "should be 'any', msatoshis or <amount>[.<amount>]<ISO-4217>");
57
58 isocode = find_iso4217(buffer + tok->end - ISO4217_NAMELEN, ISO4217_NAMELEN);
59 if (!isocode)
60 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
61 "Unknown currency suffix %.*s",
62 ISO4217_NAMELEN,
63 buffer + tok->end - ISO4217_NAMELEN);
64
65 offer->offer_currency
66 = tal_dup_arr(offer, utf8, isocode->name, ISO4217_NAMELEN, 0);
67
68 number = *tok;
69 number.end -= ISO4217_NAMELEN;
70 if (!split_tok(buffer, &number, '.', &whole, &frac)) {
71 whole = number;
72 cents = 0;
73 } else {
74 if (frac.end - frac.start != isocode->minor_unit)
75 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
76 "Currency %s requires %u minor units",
77 isocode->name, isocode->minor_unit);
78 if (!json_to_u64(buffer, &frac, &cents))
79 return command_fail_badparam(cmd, name, buffer,
80 &number,
81 "Bad minor units");
82 }
83
84 if (!json_to_u64(buffer, &whole, offer->offer_amount))
85 return command_fail_badparam(cmd, name, buffer, tok,
86 "should be 'any', msatoshis or <ISO-4217><amount>[.<amount>]");
87
88 for (size_t i = 0; i < isocode->minor_unit; i++) {
89 if (mul_overflows_u64(*offer->offer_amount, 10))
90 return command_fail_badparam(cmd, name, buffer,

Callers

nothing calls this directly

Calls 7

msat_or_anyFunction · 0.85
find_iso4217Function · 0.85
split_tokFunction · 0.85
mul_overflows_u64Function · 0.85
command_failFunction · 0.70
command_fail_badparamFunction · 0.50
json_to_u64Function · 0.50

Tested by

no test coverage detected