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