|
| 1 | +/* eslint-env jest */ |
| 2 | +import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets"; |
| 3 | +import calService from "@ledgerhq/ledger-cal-service"; |
| 4 | +import { getEnv } from "@ledgerhq/live-env"; |
| 5 | +import { getCurrencyExchangeConfig } from "."; |
| 6 | + |
| 7 | +jest.mock("@ledgerhq/ledger-cal-service", () => ({ |
| 8 | + __esModule: true, |
| 9 | + default: { findCurrencyData: jest.fn() }, |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock("@ledgerhq/live-env", () => ({ |
| 13 | + ...jest.requireActual("@ledgerhq/live-env"), |
| 14 | + getEnv: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +const findCurrencyData = jest.mocked(calService.findCurrencyData); |
| 18 | +const mockedGetEnv = jest.mocked(getEnv); |
| 19 | + |
| 20 | +describe("exchange/getCurrencyExchangeConfig", () => { |
| 21 | + beforeEach(() => { |
| 22 | + findCurrencyData.mockReset(); |
| 23 | + findCurrencyData.mockResolvedValue({ config: "abcd", signature: "ef01" } as Awaited< |
| 24 | + ReturnType<typeof calService.findCurrencyData> |
| 25 | + >); |
| 26 | + mockedGetEnv.mockReturnValue(false); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns the CAL config and signature as buffers", async () => { |
| 30 | + const res = await getCurrencyExchangeConfig({ id: "ethereum" } as CryptoCurrency); |
| 31 | + |
| 32 | + expect(res).toStrictEqual({ |
| 33 | + config: Buffer.from("abcd", "hex"), |
| 34 | + signature: Buffer.from("ef01", "hex"), |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it("looks up the CAL record by the currency id for non-arc currencies", async () => { |
| 39 | + await getCurrencyExchangeConfig({ id: "ethereum" } as CryptoCurrency); |
| 40 | + |
| 41 | + expect(findCurrencyData).toHaveBeenCalledWith("ethereum", { env: "prod" }); |
| 42 | + }); |
| 43 | + |
| 44 | + it.each([ |
| 45 | + ["arc_testnet", "arc_testnet/erc20/usdc_0x3600000000000000000000000000000000000000"], |
| 46 | + ["arc", "arc/erc20/usdc_0x0000000000000000000000000000000000000000"], |
| 47 | + ])("resolves %s native USDC to its token id", async (currencyId, tokenId) => { |
| 48 | + await getCurrencyExchangeConfig({ id: currencyId } as CryptoCurrency); |
| 49 | + |
| 50 | + expect(findCurrencyData).toHaveBeenCalledWith(tokenId, { env: "prod" }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("surfaces the resolved token id when the CAL record is missing", async () => { |
| 54 | + findCurrencyData.mockResolvedValue( |
| 55 | + undefined as unknown as Awaited<ReturnType<typeof calService.findCurrencyData>>, |
| 56 | + ); |
| 57 | + |
| 58 | + await expect( |
| 59 | + getCurrencyExchangeConfig({ id: "arc_testnet" } as CryptoCurrency), |
| 60 | + ).rejects.toThrow( |
| 61 | + "Exchange, missing configuration for arc_testnet/erc20/usdc_0x3600000000000000000000000000000000000000", |
| 62 | + ); |
| 63 | + }); |
| 64 | +}); |
0 commit comments