Skip to content

Commit 4aba72d

Browse files
authored
Merge pull request #18943 from LedgerHQ/feat/arc-swap-native-usdc-exchange-config-release
[LWDM] chore(llc): use Arc ERC20 token CAL descriptor for the exchange config
2 parents 9c454e4 + d60474c commit 4aba72d

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@ledgerhq/live-common": patch
3+
"live-mobile": minor
4+
"ledger-live-desktop": minor
5+
---
6+
7+
fix(llc): use Arc native USDC's ERC20 token CAL descriptor for the exchange config, so the device shows the correct (6-decimal) swap amount
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
});

libs/ledger-live-common/src/exchange/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,20 @@ export const isExchangeSupportedByApp = (appName: string, appVersion: string): b
3636
return !!(valid(minVersion) && valid(appVersion) && gte(appVersion, minVersion));
3737
};
3838

39+
const ARC_NATIVE_USDC_TOKEN_ID_BY_CURRENCY_ID: Record<string, string> = {
40+
arc: "arc/erc20/usdc_0x0000000000000000000000000000000000000000",
41+
arc_testnet: "arc_testnet/erc20/usdc_0x3600000000000000000000000000000000000000",
42+
};
43+
3944
export const getCurrencyExchangeConfig = async (
4045
currency: CryptoCurrency | TokenCurrency,
4146
): Promise<ExchangeCurrencyNameAndSignature> => {
4247
const env = getEnv("MOCK_EXCHANGE_TEST_CONFIG") ? "test" : "prod";
43-
const res = await calService.findCurrencyData(currency.id, { env });
48+
const lookupId = ARC_NATIVE_USDC_TOKEN_ID_BY_CURRENCY_ID[currency.id] ?? currency.id;
49+
const res = await calService.findCurrencyData(lookupId, { env });
4450

4551
if (!res) {
46-
throw new Error(`Exchange, missing configuration for ${currency.id}`);
52+
throw new Error(`Exchange, missing configuration for ${lookupId}`);
4753
}
4854

4955
return {

0 commit comments

Comments
 (0)