| 808 | } |
| 809 | |
| 810 | static struct command_result *convert_currency(struct command *cmd, |
| 811 | struct invreq *ir) |
| 812 | { |
| 813 | struct out_req *req; |
| 814 | u64 raw_amount; |
| 815 | double double_amount; |
| 816 | struct command_result *err; |
| 817 | const struct iso4217_name_and_divisor *iso4217; |
| 818 | |
| 819 | assert(ir->invreq->offer_currency); |
| 820 | |
| 821 | /* Multiply by quantity *first*, for best precision */ |
| 822 | err = invreq_amount_by_quantity(cmd, ir, &raw_amount); |
| 823 | if (err) |
| 824 | return err; |
| 825 | |
| 826 | /* BOLT #12: |
| 827 | * - MUST calculate the *expected amount* using the `offer_amount`: |
| 828 | * - if `offer_currency` is not the `invreq_chain` currency, convert to the |
| 829 | * `invreq_chain` currency. |
| 830 | */ |
| 831 | iso4217 = find_iso4217(ir->invreq->offer_currency, |
| 832 | tal_bytelen(ir->invreq->offer_currency)); |
| 833 | /* We should not create offer with unknown currency! */ |
| 834 | if (!iso4217) |
| 835 | return fail_internalerr(cmd, ir, |
| 836 | "Unknown offer currency %.*s", |
| 837 | (int)tal_bytelen(ir->invreq->offer_currency), |
| 838 | ir->invreq->offer_currency); |
| 839 | double_amount = (double)raw_amount; |
| 840 | for (size_t i = 0; i < iso4217->minor_unit; i++) |
| 841 | double_amount /= 10; |
| 842 | |
| 843 | req = jsonrpc_request_start(cmd, "currencyconvert", |
| 844 | currency_done, error, ir); |
| 845 | json_add_stringn(req->js, "currency", |
| 846 | (const char *)ir->invreq->offer_currency, |
| 847 | tal_bytelen(ir->invreq->offer_currency)); |
| 848 | json_add_primitive_fmt(req->js, "amount", "%f", double_amount); |
| 849 | return send_outreq(req); |
| 850 | } |
| 851 | |
| 852 | static struct command_result *listoffers_done(struct command *cmd, |
| 853 | const char *method, |
no test coverage detected