@ledgerhq/cryptoassets 13.31.0-nightly.7 → 13.31.0-nightly.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +15 -0
- package/lib/api-asset-converter.d.ts.map +1 -1
- package/lib/api-asset-converter.js +31 -1
- package/lib/api-asset-converter.js.map +1 -1
- package/lib/hooks.d.ts +7 -1
- package/lib/hooks.d.ts.map +1 -1
- package/lib/hooks.js +33 -0
- package/lib/hooks.js.map +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib-es/api-asset-converter.d.ts.map +1 -1
- package/lib-es/api-asset-converter.js +31 -1
- package/lib-es/api-asset-converter.js.map +1 -1
- package/lib-es/hooks.d.ts +7 -1
- package/lib-es/hooks.d.ts.map +1 -1
- package/lib-es/hooks.js +33 -0
- package/lib-es/hooks.js.map +1 -1
- package/lib-es/index.d.ts +0 -1
- package/lib-es/index.d.ts.map +1 -1
- package/lib-es/index.js +0 -1
- package/lib-es/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api-asset-converter.test.ts +22 -5
- package/src/api-asset-converter.ts +32 -1
- package/src/hooks.test.ts +134 -1
- package/src/hooks.ts +44 -1
- package/src/index.ts +0 -1
- package/lib/data/exchange/bep20-hash.json +0 -1
- package/lib/data/exchange/bep20.d.ts +0 -5
- package/lib/data/exchange/bep20.d.ts.map +0 -1
- package/lib/data/exchange/bep20.js +0 -11
- package/lib/data/exchange/bep20.js.map +0 -1
- package/lib/data/exchange/bep20.json +0 -1
- package/lib/data/exchange/coin-hash.json +0 -1
- package/lib/data/exchange/coins.d.ts +0 -5
- package/lib/data/exchange/coins.d.ts.map +0 -1
- package/lib/data/exchange/coins.js +0 -11
- package/lib/data/exchange/coins.js.map +0 -1
- package/lib/data/exchange/coins.json +0 -1
- package/lib/data/exchange/erc20-hash.json +0 -1
- package/lib/data/exchange/erc20.d.ts +0 -5
- package/lib/data/exchange/erc20.d.ts.map +0 -1
- package/lib/data/exchange/erc20.js +0 -11
- package/lib/data/exchange/erc20.js.map +0 -1
- package/lib/data/exchange/erc20.json +0 -1
- package/lib/data/exchange/polygon-erc20-hash.json +0 -1
- package/lib/data/exchange/polygon-erc20.d.ts +0 -5
- package/lib/data/exchange/polygon-erc20.d.ts.map +0 -1
- package/lib/data/exchange/polygon-erc20.js +0 -11
- package/lib/data/exchange/polygon-erc20.js.map +0 -1
- package/lib/data/exchange/polygon-erc20.json +0 -1
- package/lib/data/exchange/trc20-hash.json +0 -1
- package/lib/data/exchange/trc20.d.ts +0 -5
- package/lib/data/exchange/trc20.d.ts.map +0 -1
- package/lib/data/exchange/trc20.js +0 -11
- package/lib/data/exchange/trc20.js.map +0 -1
- package/lib/data/exchange/trc20.json +0 -1
- package/lib/exchange.d.ts +0 -8
- package/lib/exchange.d.ts.map +0 -1
- package/lib/exchange.js +0 -25
- package/lib/exchange.js.map +0 -1
- package/lib-es/data/exchange/bep20-hash.json +0 -1
- package/lib-es/data/exchange/bep20.d.ts +0 -5
- package/lib-es/data/exchange/bep20.d.ts.map +0 -1
- package/lib-es/data/exchange/bep20.js +0 -4
- package/lib-es/data/exchange/bep20.js.map +0 -1
- package/lib-es/data/exchange/bep20.json +0 -1
- package/lib-es/data/exchange/coin-hash.json +0 -1
- package/lib-es/data/exchange/coins.d.ts +0 -5
- package/lib-es/data/exchange/coins.d.ts.map +0 -1
- package/lib-es/data/exchange/coins.js +0 -4
- package/lib-es/data/exchange/coins.js.map +0 -1
- package/lib-es/data/exchange/coins.json +0 -1
- package/lib-es/data/exchange/erc20-hash.json +0 -1
- package/lib-es/data/exchange/erc20.d.ts +0 -5
- package/lib-es/data/exchange/erc20.d.ts.map +0 -1
- package/lib-es/data/exchange/erc20.js +0 -4
- package/lib-es/data/exchange/erc20.js.map +0 -1
- package/lib-es/data/exchange/erc20.json +0 -1
- package/lib-es/data/exchange/polygon-erc20-hash.json +0 -1
- package/lib-es/data/exchange/polygon-erc20.d.ts +0 -5
- package/lib-es/data/exchange/polygon-erc20.d.ts.map +0 -1
- package/lib-es/data/exchange/polygon-erc20.js +0 -4
- package/lib-es/data/exchange/polygon-erc20.js.map +0 -1
- package/lib-es/data/exchange/polygon-erc20.json +0 -1
- package/lib-es/data/exchange/trc20-hash.json +0 -1
- package/lib-es/data/exchange/trc20.d.ts +0 -5
- package/lib-es/data/exchange/trc20.d.ts.map +0 -1
- package/lib-es/data/exchange/trc20.js +0 -4
- package/lib-es/data/exchange/trc20.js.map +0 -1
- package/lib-es/data/exchange/trc20.json +0 -1
- package/lib-es/exchange.d.ts +0 -8
- package/lib-es/exchange.d.ts.map +0 -1
- package/lib-es/exchange.js +0 -18
- package/lib-es/exchange.js.map +0 -1
- package/src/data/exchange/bep20-hash.json +0 -1
- package/src/data/exchange/bep20.json +0 -1
- package/src/data/exchange/bep20.ts +0 -7
- package/src/data/exchange/coin-hash.json +0 -1
- package/src/data/exchange/coins.json +0 -1
- package/src/data/exchange/coins.ts +0 -7
- package/src/data/exchange/erc20-hash.json +0 -1
- package/src/data/exchange/erc20.json +0 -1
- package/src/data/exchange/erc20.ts +0 -7
- package/src/data/exchange/polygon-erc20-hash.json +0 -1
- package/src/data/exchange/polygon-erc20.json +0 -1
- package/src/data/exchange/polygon-erc20.ts +0 -7
- package/src/data/exchange/trc20-hash.json +0 -1
- package/src/data/exchange/trc20.json +0 -1
- package/src/data/exchange/trc20.ts +0 -7
- package/src/exchange.ts +0 -29
package/src/hooks.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { renderHook, waitFor } from "@testing-library/react";
|
|
2
2
|
import { createCryptoAssetsHooks } from "./hooks";
|
|
3
|
-
import type { TokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
3
|
+
import type { TokenCurrency, CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
4
4
|
|
|
5
5
|
// Mock the legacy store
|
|
6
6
|
jest.mock("./legacy/legacy-store", () => ({
|
|
@@ -10,7 +10,34 @@ jest.mock("./legacy/legacy-store", () => ({
|
|
|
10
10
|
},
|
|
11
11
|
}));
|
|
12
12
|
|
|
13
|
+
// Mock the currencies module
|
|
14
|
+
jest.mock("./currencies", () => ({
|
|
15
|
+
findCryptoCurrencyById: jest.fn(),
|
|
16
|
+
}));
|
|
17
|
+
|
|
13
18
|
import { legacyCryptoAssetsStore } from "./legacy/legacy-store";
|
|
19
|
+
import { findCryptoCurrencyById } from "./currencies";
|
|
20
|
+
|
|
21
|
+
// Mock crypto currency data
|
|
22
|
+
const mockCryptoCurrency: CryptoCurrency = {
|
|
23
|
+
type: "CryptoCurrency",
|
|
24
|
+
id: "bitcoin",
|
|
25
|
+
name: "Bitcoin",
|
|
26
|
+
ticker: "BTC",
|
|
27
|
+
units: [
|
|
28
|
+
{
|
|
29
|
+
code: "BTC",
|
|
30
|
+
name: "Bitcoin",
|
|
31
|
+
magnitude: 8,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
family: "bitcoin",
|
|
35
|
+
managerAppName: "Bitcoin",
|
|
36
|
+
coinType: 0,
|
|
37
|
+
scheme: "bitcoin",
|
|
38
|
+
color: "#FF9900",
|
|
39
|
+
explorerViews: [],
|
|
40
|
+
};
|
|
14
41
|
|
|
15
42
|
// Mock token data
|
|
16
43
|
const mockToken: TokenCurrency = {
|
|
@@ -51,6 +78,9 @@ const mockFindTokenByAddressInCurrency =
|
|
|
51
78
|
legacyCryptoAssetsStore.findTokenByAddressInCurrency as jest.MockedFunction<
|
|
52
79
|
typeof legacyCryptoAssetsStore.findTokenByAddressInCurrency
|
|
53
80
|
>;
|
|
81
|
+
const mockFindCryptoCurrencyById = findCryptoCurrencyById as jest.MockedFunction<
|
|
82
|
+
typeof findCryptoCurrencyById
|
|
83
|
+
>;
|
|
54
84
|
|
|
55
85
|
beforeEach(() => {
|
|
56
86
|
jest.clearAllMocks();
|
|
@@ -69,6 +99,13 @@ beforeEach(() => {
|
|
|
69
99
|
}
|
|
70
100
|
return undefined;
|
|
71
101
|
});
|
|
102
|
+
|
|
103
|
+
mockFindCryptoCurrencyById.mockImplementation((id: string) => {
|
|
104
|
+
if (id === "bitcoin") {
|
|
105
|
+
return mockCryptoCurrency;
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
});
|
|
72
109
|
});
|
|
73
110
|
|
|
74
111
|
describe("Hooks Factory", () => {
|
|
@@ -77,8 +114,10 @@ describe("Hooks Factory", () => {
|
|
|
77
114
|
|
|
78
115
|
expect(hooks.useTokenById).toBeDefined();
|
|
79
116
|
expect(hooks.useTokenByAddressInCurrency).toBeDefined();
|
|
117
|
+
expect(hooks.useCurrencyById).toBeDefined();
|
|
80
118
|
expect(hooks.useTokenById).toBeInstanceOf(Function);
|
|
81
119
|
expect(hooks.useTokenByAddressInCurrency).toBeInstanceOf(Function);
|
|
120
|
+
expect(hooks.useCurrencyById).toBeInstanceOf(Function);
|
|
82
121
|
});
|
|
83
122
|
|
|
84
123
|
it("should throw an error when useCALBackend is true", () => {
|
|
@@ -275,4 +314,98 @@ describe("Legacy hooks", () => {
|
|
|
275
314
|
expect(result.current.token).toBeUndefined();
|
|
276
315
|
});
|
|
277
316
|
});
|
|
317
|
+
|
|
318
|
+
describe("useCurrencyById", () => {
|
|
319
|
+
it("should return crypto currency when found", async () => {
|
|
320
|
+
const hooks = createCryptoAssetsHooks({ useCALBackend: false });
|
|
321
|
+
const { result } = renderHook(() => hooks.useCurrencyById("bitcoin"));
|
|
322
|
+
|
|
323
|
+
// Initial loading state
|
|
324
|
+
expect(result.current.loading).toBe(true);
|
|
325
|
+
expect(result.current.currency).toBeUndefined();
|
|
326
|
+
expect(result.current.error).toBeUndefined();
|
|
327
|
+
|
|
328
|
+
// Wait for the async operation to complete
|
|
329
|
+
await waitFor(() => {
|
|
330
|
+
expect(result.current.loading).toBe(false);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
// Settled state with crypto currency
|
|
334
|
+
expect(result.current.currency).toBe(mockCryptoCurrency);
|
|
335
|
+
expect(result.current.error).toBeUndefined();
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it("should fallback to token when crypto currency not found", async () => {
|
|
339
|
+
const hooks = createCryptoAssetsHooks({ useCALBackend: false });
|
|
340
|
+
const { result } = renderHook(() => hooks.useCurrencyById("ethereum/erc20/usd_coin"));
|
|
341
|
+
|
|
342
|
+
// Initial loading state
|
|
343
|
+
expect(result.current.loading).toBe(true);
|
|
344
|
+
|
|
345
|
+
// Wait for the async operation to complete
|
|
346
|
+
await waitFor(() => {
|
|
347
|
+
expect(result.current.loading).toBe(false);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
// Settled state with token
|
|
351
|
+
expect(result.current.currency).toBe(mockToken);
|
|
352
|
+
expect(result.current.error).toBeUndefined();
|
|
353
|
+
expect(mockFindCryptoCurrencyById).toHaveBeenCalledWith("ethereum/erc20/usd_coin");
|
|
354
|
+
expect(mockFindTokenById).toHaveBeenCalledWith("ethereum/erc20/usd_coin");
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("should handle empty id parameter", async () => {
|
|
358
|
+
const hooks = createCryptoAssetsHooks({ useCALBackend: false });
|
|
359
|
+
const { result } = renderHook(() => hooks.useCurrencyById(""));
|
|
360
|
+
|
|
361
|
+
// Should immediately settle with no currency
|
|
362
|
+
expect(result.current.loading).toBe(false);
|
|
363
|
+
expect(result.current.currency).toBeUndefined();
|
|
364
|
+
expect(result.current.error).toBeUndefined();
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("should handle non-existent currency id", async () => {
|
|
368
|
+
const hooks = createCryptoAssetsHooks({ useCALBackend: false });
|
|
369
|
+
const { result } = renderHook(() => hooks.useCurrencyById("non-existent"));
|
|
370
|
+
|
|
371
|
+
// Initial loading state
|
|
372
|
+
expect(result.current.loading).toBe(true);
|
|
373
|
+
|
|
374
|
+
// Wait for the async operation to complete
|
|
375
|
+
await waitFor(() => {
|
|
376
|
+
expect(result.current.loading).toBe(false);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
// Settled state with no currency
|
|
380
|
+
expect(result.current.currency).toBeUndefined();
|
|
381
|
+
expect(result.current.error).toBeUndefined();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("should re-run when id changes", async () => {
|
|
385
|
+
const hooks = createCryptoAssetsHooks({ useCALBackend: false });
|
|
386
|
+
const { result, rerender } = renderHook(({ id }) => hooks.useCurrencyById(id), {
|
|
387
|
+
initialProps: { id: "bitcoin" },
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
// Wait for first currency to load
|
|
391
|
+
await waitFor(() => {
|
|
392
|
+
expect(result.current.loading).toBe(false);
|
|
393
|
+
expect(result.current.currency).toBe(mockCryptoCurrency);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
// Change the id to a token
|
|
397
|
+
rerender({ id: "ethereum/erc20/usd_coin" });
|
|
398
|
+
|
|
399
|
+
// Should start loading again
|
|
400
|
+
expect(result.current.loading).toBe(true);
|
|
401
|
+
|
|
402
|
+
// Wait for new search to complete
|
|
403
|
+
await waitFor(() => {
|
|
404
|
+
expect(result.current.loading).toBe(false);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// Should have token for the new id
|
|
408
|
+
expect(result.current.currency).toBe(mockToken);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
278
411
|
});
|
package/src/hooks.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useState, useEffect } from "react";
|
|
2
2
|
import { legacyCryptoAssetsStore } from "./legacy/legacy-store";
|
|
3
|
-
import type { TokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
3
|
+
import type { TokenCurrency, CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
4
|
+
import { findCryptoCurrencyById } from "./currencies";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* CryptoAssetsHooks is the hooks interface version of the CryptoAssetsStore
|
|
@@ -19,6 +20,7 @@ export function createCryptoAssetsHooks(
|
|
|
19
20
|
return {
|
|
20
21
|
useTokenById: useLegacyTokenById,
|
|
21
22
|
useTokenByAddressInCurrency: useLegacyTokenByAddressInCurrency,
|
|
23
|
+
useCurrencyById: useLegacyCurrencyById,
|
|
22
24
|
};
|
|
23
25
|
}
|
|
24
26
|
}
|
|
@@ -29,9 +31,16 @@ type TokenResult = {
|
|
|
29
31
|
error: unknown;
|
|
30
32
|
};
|
|
31
33
|
|
|
34
|
+
type CurrencyResult = {
|
|
35
|
+
currency: CryptoCurrency | TokenCurrency | undefined;
|
|
36
|
+
loading: boolean;
|
|
37
|
+
error: unknown;
|
|
38
|
+
};
|
|
39
|
+
|
|
32
40
|
type Hooks = {
|
|
33
41
|
useTokenById: (id: string) => TokenResult;
|
|
34
42
|
useTokenByAddressInCurrency: (address: string, currencyId: string) => TokenResult;
|
|
43
|
+
useCurrencyById: (id: string) => CurrencyResult;
|
|
35
44
|
};
|
|
36
45
|
|
|
37
46
|
/////////// Legacy hooks using legacyCryptoAssetsStore ///////////
|
|
@@ -102,3 +111,37 @@ function useLegacyTokenByAddressInCurrency(address: string, currencyId: string)
|
|
|
102
111
|
|
|
103
112
|
return { token, loading, error };
|
|
104
113
|
}
|
|
114
|
+
|
|
115
|
+
function useLegacyCurrencyById(id: string) {
|
|
116
|
+
const [currency, setCurrency] = useState<CryptoCurrency | TokenCurrency | undefined>(undefined);
|
|
117
|
+
const [loading, setLoading] = useState<boolean>(true);
|
|
118
|
+
const [error, setError] = useState<unknown>(undefined);
|
|
119
|
+
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (!id) {
|
|
122
|
+
setCurrency(undefined);
|
|
123
|
+
setLoading(false);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const fetchCurrency = async () => {
|
|
128
|
+
try {
|
|
129
|
+
setLoading(true);
|
|
130
|
+
setError(undefined);
|
|
131
|
+
// First try to find as a crypto currency
|
|
132
|
+
const cryptoCurrency = await Promise.resolve(findCryptoCurrencyById(id));
|
|
133
|
+
const result = cryptoCurrency || (await legacyCryptoAssetsStore.findTokenById(id));
|
|
134
|
+
setCurrency(result);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
setError(err);
|
|
137
|
+
setCurrency(undefined);
|
|
138
|
+
} finally {
|
|
139
|
+
setLoading(false);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
fetchCurrency();
|
|
144
|
+
}, [id]);
|
|
145
|
+
|
|
146
|
+
return { currency, loading, error };
|
|
147
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"\"0ebfdae5c996ad89d0f009e300e3a652\""
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bep20.d.ts","sourceRoot":"","sources":["../../../src/data/exchange/bep20.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAErD,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,mBAAmB,CAAC;;AAIpD,wBAAyC"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.hash = void 0;
|
|
7
|
-
var bep20_hash_json_1 = require("./bep20-hash.json");
|
|
8
|
-
Object.defineProperty(exports, "hash", { enumerable: true, get: function () { return __importDefault(bep20_hash_json_1).default; } });
|
|
9
|
-
const bep20_json_1 = __importDefault(require("./bep20.json"));
|
|
10
|
-
exports.default = bep20_json_1.default;
|
|
11
|
-
//# sourceMappingURL=bep20.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bep20.js","sourceRoot":"","sources":["../../../src/data/exchange/bep20.ts"],"names":[],"mappings":";;;;;;AAEA,qDAAoD;AAA3C,wHAAA,OAAO,OAAQ;AAExB,8DAAkC;AAElC,kBAAe,oBAAyB,CAAC"}
|