| 81 | } |
| 82 | |
| 83 | static bool print_amount(const struct bitcoin_blkid *chains, |
| 84 | const char *iso4217, u64 amount) |
| 85 | { |
| 86 | const char *currency; |
| 87 | unsigned int minor_unit; |
| 88 | bool ok = true; |
| 89 | |
| 90 | /* BOLT-offers #12: |
| 91 | * - if the currency for `amount` is that of the first entry in `chains`: |
| 92 | * - MUST specify `amount` in multiples of the minimum |
| 93 | * lightning-payable unit (e.g. milli-satoshis for bitcoin). |
| 94 | * - otherwise: |
| 95 | * - MUST specify `iso4217` as an ISO 4712 three-letter code. |
| 96 | * - MUST specify `amount` in the currency unit adjusted by the |
| 97 | * ISO 4712 exponent (e.g. USD cents). |
| 98 | */ |
| 99 | if (!iso4217) { |
| 100 | if (tal_count(chains) == 0) |
| 101 | currency = "bc"; |
| 102 | else { |
| 103 | const struct chainparams *ch; |
| 104 | |
| 105 | ch = chainparams_by_chainhash(&chains[0]); |
| 106 | if (!ch) { |
| 107 | currency = tal_fmt(tmpctx, "UNKNOWN CHAINHASH %s", |
| 108 | type_to_string(tmpctx, |
| 109 | struct bitcoin_blkid, |
| 110 | &chains[0])); |
| 111 | ok = false; |
| 112 | } else |
| 113 | currency = ch->lightning_hrp; |
| 114 | } |
| 115 | minor_unit = 11; |
| 116 | } else { |
| 117 | const struct iso4217_name_and_divisor *iso; |
| 118 | iso = find_iso4217(iso4217, tal_bytelen(iso4217)); |
| 119 | if (iso) { |
| 120 | minor_unit = iso->minor_unit; |
| 121 | currency = iso->name; |
| 122 | } else { |
| 123 | minor_unit = 0; |
| 124 | currency = tal_fmt(tmpctx, "%.*s (UNKNOWN CURRENCY)", |
| 125 | (int)tal_bytelen(iso4217), iso4217); |
| 126 | ok = false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (!minor_unit) |
| 131 | printf("amount: %"PRIu64"%s\n", amount, currency); |
| 132 | else { |
| 133 | u64 minor_div = 1; |
| 134 | for (size_t i = 0; i < minor_unit; i++) |
| 135 | minor_div *= 10; |
| 136 | printf("amount: %"PRIu64".%.*"PRIu64"%s\n", |
| 137 | amount / minor_div, minor_unit, amount % minor_div, |
| 138 | currency); |
| 139 | } |
| 140 |
no test coverage detected