| 585 | } |
| 586 | |
| 587 | static struct command_result *convert_currency(struct command *cmd, |
| 588 | struct invreq *ir) |
| 589 | { |
| 590 | struct out_req *req; |
| 591 | u64 raw_amount; |
| 592 | double double_amount; |
| 593 | struct command_result *err; |
| 594 | const struct iso4217_name_and_divisor *iso4217; |
| 595 | |
| 596 | assert(ir->offer->currency); |
| 597 | |
| 598 | /* Multiply by quantity *first*, for best precision */ |
| 599 | err = invreq_amount_by_quantity(cmd, ir, &raw_amount); |
| 600 | if (err) |
| 601 | return err; |
| 602 | |
| 603 | /* BOLT-offers #12: |
| 604 | * - MUST calculate the *base invoice amount* using the offer |
| 605 | * `amount`: |
| 606 | * - if offer `currency` is not the invoice currency, convert |
| 607 | * to the invoice currency. |
| 608 | */ |
| 609 | iso4217 = find_iso4217(ir->offer->currency, |
| 610 | tal_bytelen(ir->offer->currency)); |
| 611 | /* We should not create offer with unknown currency! */ |
| 612 | if (!iso4217) |
| 613 | return fail_internalerr(cmd, ir, |
| 614 | "Unknown offer currency %.*s", |
| 615 | (int)tal_bytelen(ir->offer->currency), |
| 616 | ir->offer->currency); |
| 617 | double_amount = (double)raw_amount; |
| 618 | for (size_t i = 0; i < iso4217->minor_unit; i++) |
| 619 | double_amount /= 10; |
| 620 | |
| 621 | req = jsonrpc_request_start(cmd->plugin, cmd, "currencyconvert", |
| 622 | currency_done, error, ir); |
| 623 | json_add_stringn(req->js, "currency", |
| 624 | (const char *)ir->offer->currency, |
| 625 | tal_bytelen(ir->offer->currency)); |
| 626 | json_add_primitive_fmt(req->js, "amount", "%f", double_amount); |
| 627 | return send_outreq(cmd->plugin, req); |
| 628 | } |
| 629 | |
| 630 | static struct command_result *listoffers_done(struct command *cmd, |
| 631 | const char *buf, |
no test coverage detected