Skip to content

Commit 06a7ac1

Browse files
authored
Merge pull request #14922 from LedgerHQ/bugfix/market-currency-id-resilience
fix(common): improve resilience when market API returns no data for currency
2 parents 01c148d + b43dd92 commit 06a7ac1

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ledgerhq/live-common": minor
3+
---
4+
5+
Improve resilience when market API returns no data for a currency id (e.g. unknown or unsupported coin). Prevents "Cannot read property 'id' of undefined" crash in getCurrencyData; query now returns undefined instead of throwing.

libs/ledger-live-common/src/market/hooks/__tests__/useCurrencyData.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { renderHook, waitFor } from "@testing-library/react";
7+
import { http, HttpResponse } from "msw";
78
import { useCurrencyData } from "../useMarketDataProvider";
89
import { countervaluesApi } from "../../state-manager/countervaluesApi";
910
import { server } from "@tests/server";
@@ -96,4 +97,19 @@ describe("useCurrencyData", () => {
9697

9798
unmount();
9899
});
100+
101+
it("should return undefined data when API returns empty array (id not resolving to a currency)", async () => {
102+
server.use(http.get("*/v3/markets", () => HttpResponse.json([])));
103+
const wrapper = createWrapper(store);
104+
const { result, unmount } = renderHook(
105+
() => useCurrencyData({ id: "unknown-currency", counterCurrency: "usd" }),
106+
{ wrapper },
107+
);
108+
109+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
110+
expect(result.current.data).toBeUndefined();
111+
expect(result.current.isError).toBe(false);
112+
113+
unmount();
114+
});
99115
});

libs/ledger-live-common/src/market/state-manager/countervaluesApi.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const countervaluesApi = createApi({
3737
transformResponse: (response: MarketItemResponse[]) => response.map(formatPerformer),
3838
keepUnusedDataFor: REFETCH_TIME_ONE_MINUTE / 1000,
3939
}),
40-
getCurrencyData: build.query<MarketCurrencyData, MarketCurrencyRequestParams>({
40+
getCurrencyData: build.query<MarketCurrencyData | undefined, MarketCurrencyRequestParams>({
4141
query: ({ id, counterCurrency }) => ({
4242
url: "/v3/markets",
4343
params: {
@@ -48,7 +48,8 @@ export const countervaluesApi = createApi({
4848
},
4949
}),
5050
providesTags: [MarketDataTags.CurrencyData],
51-
transformResponse: (response: MarketItemResponse[]) => format(response[0]),
51+
transformResponse: (response: MarketItemResponse[]) =>
52+
response?.[0] ? format(response[0]) : undefined,
5253
keepUnusedDataFor: (REFETCH_TIME_ONE_MINUTE * BASIC_REFETCH) / 1000,
5354
}),
5455
}),

0 commit comments

Comments
 (0)