@ledgerhq/cryptoassets 13.30.0 → 13.31.0-nightly.1

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.
Files changed (92) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +32 -0
  3. package/README.md +0 -257
  4. package/lib/abandonseed.d.ts.map +1 -1
  5. package/lib/abandonseed.js +4 -1
  6. package/lib/abandonseed.js.map +1 -1
  7. package/lib/api-token-converter.js +15 -15
  8. package/lib/api-token-converter.js.map +1 -1
  9. package/lib/crypto-assets-importer/importers/sui/index.d.ts.map +1 -1
  10. package/lib/crypto-assets-importer/importers/sui/index.js +4 -1
  11. package/lib/crypto-assets-importer/importers/sui/index.js.map +1 -1
  12. package/lib/crypto-assets-importer/importers/sui/sui.test.js +14 -3
  13. package/lib/crypto-assets-importer/importers/sui/sui.test.js.map +1 -1
  14. package/lib/currencies.d.ts.map +1 -1
  15. package/lib/currencies.js +92 -10
  16. package/lib/currencies.js.map +1 -1
  17. package/lib/data/sui.d.ts +2 -1
  18. package/lib/data/sui.d.ts.map +1 -1
  19. package/lib/data/sui.js.map +1 -1
  20. package/lib/legacy/legacy-data.d.ts +25 -0
  21. package/lib/legacy/legacy-data.d.ts.map +1 -0
  22. package/lib/legacy/legacy-data.js +102 -0
  23. package/lib/legacy/legacy-data.js.map +1 -0
  24. package/lib/legacy/legacy-state.d.ts +9 -0
  25. package/lib/legacy/legacy-state.d.ts.map +1 -0
  26. package/lib/legacy/legacy-state.js +12 -0
  27. package/lib/legacy/legacy-state.js.map +1 -0
  28. package/lib/legacy/legacy-utils.d.ts +89 -0
  29. package/lib/legacy/legacy-utils.d.ts.map +1 -0
  30. package/lib/legacy/legacy-utils.js +448 -0
  31. package/lib/legacy/legacy-utils.js.map +1 -0
  32. package/lib/legacy/legacy.test.d.ts +2 -0
  33. package/lib/legacy/legacy.test.d.ts.map +1 -0
  34. package/lib/legacy/legacy.test.js +466 -0
  35. package/lib/legacy/legacy.test.js.map +1 -0
  36. package/lib/tokens.d.ts +20 -50
  37. package/lib/tokens.d.ts.map +1 -1
  38. package/lib/tokens.js +49 -503
  39. package/lib/tokens.js.map +1 -1
  40. package/lib/tokens.test.js +4 -3
  41. package/lib/tokens.test.js.map +1 -1
  42. package/lib-es/abandonseed.d.ts.map +1 -1
  43. package/lib-es/abandonseed.js +4 -1
  44. package/lib-es/abandonseed.js.map +1 -1
  45. package/lib-es/api-token-converter.js +2 -2
  46. package/lib-es/api-token-converter.js.map +1 -1
  47. package/lib-es/crypto-assets-importer/importers/sui/index.d.ts.map +1 -1
  48. package/lib-es/crypto-assets-importer/importers/sui/index.js +4 -1
  49. package/lib-es/crypto-assets-importer/importers/sui/index.js.map +1 -1
  50. package/lib-es/crypto-assets-importer/importers/sui/sui.test.js +14 -3
  51. package/lib-es/crypto-assets-importer/importers/sui/sui.test.js.map +1 -1
  52. package/lib-es/currencies.d.ts.map +1 -1
  53. package/lib-es/currencies.js +92 -10
  54. package/lib-es/currencies.js.map +1 -1
  55. package/lib-es/data/sui.d.ts +2 -1
  56. package/lib-es/data/sui.d.ts.map +1 -1
  57. package/lib-es/data/sui.js.map +1 -1
  58. package/lib-es/legacy/legacy-data.d.ts +25 -0
  59. package/lib-es/legacy/legacy-data.d.ts.map +1 -0
  60. package/lib-es/legacy/legacy-data.js +76 -0
  61. package/lib-es/legacy/legacy-data.js.map +1 -0
  62. package/lib-es/legacy/legacy-state.d.ts +9 -0
  63. package/lib-es/legacy/legacy-state.d.ts.map +1 -0
  64. package/lib-es/legacy/legacy-state.js +9 -0
  65. package/lib-es/legacy/legacy-state.js.map +1 -0
  66. package/lib-es/legacy/legacy-utils.d.ts +89 -0
  67. package/lib-es/legacy/legacy-utils.d.ts.map +1 -0
  68. package/lib-es/legacy/legacy-utils.js +427 -0
  69. package/lib-es/legacy/legacy-utils.js.map +1 -0
  70. package/lib-es/legacy/legacy.test.d.ts +2 -0
  71. package/lib-es/legacy/legacy.test.d.ts.map +1 -0
  72. package/lib-es/legacy/legacy.test.js +464 -0
  73. package/lib-es/legacy/legacy.test.js.map +1 -0
  74. package/lib-es/tokens.d.ts +20 -50
  75. package/lib-es/tokens.d.ts.map +1 -1
  76. package/lib-es/tokens.js +27 -475
  77. package/lib-es/tokens.js.map +1 -1
  78. package/lib-es/tokens.test.js +2 -1
  79. package/lib-es/tokens.test.js.map +1 -1
  80. package/package.json +3 -5
  81. package/src/abandonseed.ts +4 -1
  82. package/src/api-token-converter.ts +2 -2
  83. package/src/crypto-assets-importer/importers/sui/index.ts +6 -2
  84. package/src/crypto-assets-importer/importers/sui/sui.test.ts +15 -3
  85. package/src/currencies.ts +92 -10
  86. package/src/data/sui.ts +2 -1
  87. package/src/legacy/legacy-data.ts +119 -0
  88. package/src/legacy/legacy-state.ts +10 -0
  89. package/src/legacy/legacy-utils.ts +571 -0
  90. package/src/legacy/legacy.test.ts +580 -0
  91. package/src/tokens.test.ts +1 -1
  92. package/src/tokens.ts +67 -588
@@ -0,0 +1,571 @@
1
+ import type { TokenCurrency, CryptoCurrency } from "@ledgerhq/types-cryptoassets";
2
+ import { findCryptoCurrencyById, getCryptoCurrencyById } from "../currencies";
3
+ import {
4
+ tokensArray,
5
+ tokensArrayWithDelisted,
6
+ tokensByCryptoCurrency,
7
+ tokensByCryptoCurrencyWithDelisted,
8
+ tokensById,
9
+ tokensByCurrencyAddress,
10
+ tokenListHashes,
11
+ } from "./legacy-state";
12
+ import {
13
+ ERC20Token,
14
+ AlgorandASAToken,
15
+ MultiversXESDTToken,
16
+ TRC10Token,
17
+ TRC20Token,
18
+ } from "../types";
19
+ import type { CardanoNativeToken } from "../data/cardanoNative";
20
+ import type { TonJettonToken } from "../data/ton-jetton";
21
+ import type { StellarToken } from "../data/stellar";
22
+ import type { HederaToken } from "../data/hedera";
23
+ import type { Vip180Token } from "../data/vip180";
24
+ import type { SPLToken } from "../data/spl";
25
+ import type { AptosToken as AptosCoinToken } from "../data/apt_coin";
26
+ import type { AptosToken as AptosFAToken } from "../data/apt_fungible_asset";
27
+ import type { SuiToken } from "../data/sui";
28
+
29
+ // Export types for compatibility
30
+ export interface TokensListOptions {
31
+ withDelisted: boolean;
32
+ }
33
+
34
+ // Convert functions moved from legacy.ts
35
+
36
+ /**
37
+ * @deprecated
38
+ */
39
+ export function convertERC20([
40
+ parentCurrencyId,
41
+ token,
42
+ ticker,
43
+ magnitude,
44
+ name,
45
+ ledgerSignature,
46
+ contractAddress,
47
+ disableCountervalue,
48
+ delisted,
49
+ ]: ERC20Token): TokenCurrency | undefined {
50
+ const parentCurrency = findCryptoCurrencyById(parentCurrencyId);
51
+
52
+ if (!parentCurrency) {
53
+ return;
54
+ }
55
+
56
+ const tokenType = parentCurrencyId === "bsc" ? "bep20" : "erc20";
57
+
58
+ return {
59
+ type: "TokenCurrency",
60
+ id: `${parentCurrencyId}/${tokenType}/${token}`,
61
+ ledgerSignature,
62
+ contractAddress,
63
+ parentCurrency,
64
+ tokenType,
65
+ name,
66
+ ticker,
67
+ delisted,
68
+ disableCountervalue: !!parentCurrency.isTestnetFor || !!disableCountervalue,
69
+ units: [
70
+ {
71
+ name,
72
+ code: ticker,
73
+ magnitude,
74
+ },
75
+ ],
76
+ };
77
+ }
78
+
79
+ /**
80
+ * @deprecated
81
+ */
82
+ export function convertAlgorandASATokens([
83
+ id,
84
+ abbr,
85
+ name,
86
+ contractAddress,
87
+ precision,
88
+ ]: AlgorandASAToken): TokenCurrency {
89
+ const parentCurrency = getCryptoCurrencyById("algorand");
90
+
91
+ return {
92
+ type: "TokenCurrency",
93
+ id: `algorand/asa/${id}`,
94
+ contractAddress,
95
+ parentCurrency,
96
+ tokenType: "asa",
97
+ name,
98
+ ticker: abbr,
99
+ disableCountervalue: false,
100
+ units: [
101
+ {
102
+ name,
103
+ code: abbr,
104
+ magnitude: precision,
105
+ },
106
+ ],
107
+ };
108
+ }
109
+
110
+ /**
111
+ * @deprecated
112
+ */
113
+ export function convertVechainToken([
114
+ tokenIdenfitier,
115
+ ticker,
116
+ name,
117
+ contractAddress,
118
+ precision,
119
+ ]: Vip180Token): TokenCurrency {
120
+ return {
121
+ type: "TokenCurrency",
122
+ id: `vechain/vip180/${tokenIdenfitier}`,
123
+ contractAddress: contractAddress,
124
+ parentCurrency: getCryptoCurrencyById("vechain"),
125
+ tokenType: "vip180",
126
+ name,
127
+ ticker,
128
+ disableCountervalue: false,
129
+ units: [
130
+ {
131
+ name,
132
+ code: ticker,
133
+ magnitude: precision,
134
+ },
135
+ ],
136
+ };
137
+ }
138
+
139
+ /**
140
+ * @deprecated
141
+ */
142
+ export function convertTRONTokens(type: "trc10" | "trc20") {
143
+ return ([id, abbr, name, contractAddress, precision, delisted, ledgerSignature]:
144
+ | TRC10Token
145
+ | TRC20Token): TokenCurrency => {
146
+ const parentCurrency = getCryptoCurrencyById("tron");
147
+
148
+ return {
149
+ type: "TokenCurrency",
150
+ id: `tron/${type}/${id}`,
151
+ contractAddress,
152
+ parentCurrency,
153
+ tokenType: type,
154
+ name,
155
+ ticker: abbr,
156
+ delisted,
157
+ disableCountervalue: false,
158
+ ledgerSignature,
159
+ units: [
160
+ {
161
+ name,
162
+ code: abbr,
163
+ magnitude: precision,
164
+ },
165
+ ],
166
+ };
167
+ };
168
+ }
169
+
170
+ /**
171
+ * @deprecated
172
+ */
173
+ export function convertMultiversXESDTTokens([
174
+ ticker,
175
+ identifier,
176
+ decimals,
177
+ signature,
178
+ name,
179
+ ]: MultiversXESDTToken): TokenCurrency {
180
+ const MULTIVERSX_ESDT_CONTRACT = "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u";
181
+ const parentCurrency = getCryptoCurrencyById("elrond");
182
+
183
+ return {
184
+ type: "TokenCurrency",
185
+ id: `multiversx/esdt/${identifier}`,
186
+ contractAddress: MULTIVERSX_ESDT_CONTRACT,
187
+ ledgerSignature: signature,
188
+ parentCurrency,
189
+ tokenType: "esdt",
190
+ disableCountervalue: false,
191
+ name,
192
+ ticker,
193
+ units: [
194
+ {
195
+ name,
196
+ code: name,
197
+ magnitude: decimals,
198
+ },
199
+ ],
200
+ };
201
+ }
202
+
203
+ /**
204
+ * @deprecated
205
+ */
206
+ export function convertSplTokens([
207
+ id,
208
+ network,
209
+ name,
210
+ symbol,
211
+ address,
212
+ decimals,
213
+ ]: SPLToken): TokenCurrency {
214
+ return {
215
+ type: "TokenCurrency",
216
+ id,
217
+ contractAddress: address,
218
+ parentCurrency: getCryptoCurrencyById(network),
219
+ name,
220
+ tokenType: "spl",
221
+ ticker: symbol,
222
+ disableCountervalue: false,
223
+ units: [
224
+ {
225
+ name,
226
+ code: symbol,
227
+ magnitude: decimals,
228
+ },
229
+ ],
230
+ };
231
+ }
232
+
233
+ /**
234
+ * @deprecated
235
+ */
236
+ function convertAptosTokens(
237
+ tokenType: "coin" | "fungible_asset",
238
+ [id, ticker, name, address, decimals, delisted]: AptosCoinToken | AptosFAToken,
239
+ ): TokenCurrency {
240
+ return {
241
+ type: "TokenCurrency",
242
+ id,
243
+ contractAddress: address,
244
+ parentCurrency: getCryptoCurrencyById("aptos"),
245
+ name,
246
+ tokenType,
247
+ ticker,
248
+ disableCountervalue: false,
249
+ delisted,
250
+ units: [
251
+ {
252
+ name,
253
+ code: ticker,
254
+ magnitude: decimals,
255
+ },
256
+ ],
257
+ };
258
+ }
259
+
260
+ /**
261
+ * @deprecated
262
+ */
263
+ export function convertAptCoinTokens(token: AptosCoinToken): TokenCurrency {
264
+ return convertAptosTokens("coin", token);
265
+ }
266
+
267
+ /**
268
+ * @deprecated
269
+ */
270
+ export function convertAptFaTokens(token: AptosFAToken): TokenCurrency {
271
+ return convertAptosTokens("fungible_asset", token);
272
+ }
273
+
274
+ /**
275
+ * @deprecated
276
+ */
277
+ export function convertSuiTokens([
278
+ id,
279
+ name,
280
+ ticker,
281
+ address,
282
+ decimals,
283
+ ledgerSignature,
284
+ ]: SuiToken): TokenCurrency {
285
+ return {
286
+ type: "TokenCurrency",
287
+ id,
288
+ contractAddress: address,
289
+ parentCurrency: getCryptoCurrencyById("sui"),
290
+ name,
291
+ ledgerSignature,
292
+ tokenType: "sui",
293
+ ticker,
294
+ disableCountervalue: false,
295
+ units: [
296
+ {
297
+ name,
298
+ code: ticker,
299
+ magnitude: decimals,
300
+ },
301
+ ],
302
+ };
303
+ }
304
+
305
+ /**
306
+ * @deprecated
307
+ */
308
+ export function convertHederaTokens([
309
+ id,
310
+ tokenId,
311
+ name,
312
+ ticker,
313
+ network,
314
+ decimals,
315
+ delisted,
316
+ ]: HederaToken): TokenCurrency {
317
+ return {
318
+ type: "TokenCurrency",
319
+ id,
320
+ contractAddress: tokenId,
321
+ parentCurrency: getCryptoCurrencyById(network),
322
+ tokenType: "hts",
323
+ name,
324
+ ticker,
325
+ delisted,
326
+ disableCountervalue: false,
327
+ units: [
328
+ {
329
+ name,
330
+ code: ticker,
331
+ magnitude: decimals,
332
+ },
333
+ ],
334
+ };
335
+ }
336
+
337
+ /**
338
+ * @deprecated
339
+ */
340
+ export function convertCardanoNativeTokens([
341
+ parentCurrencyId,
342
+ policyId,
343
+ assetName,
344
+ name,
345
+ ticker,
346
+ decimals,
347
+ delisted,
348
+ ]: CardanoNativeToken): TokenCurrency | undefined {
349
+ const assetId = policyId + assetName;
350
+
351
+ const parentCurrency = getCryptoCurrencyById(parentCurrencyId);
352
+
353
+ if (!parentCurrency) {
354
+ return;
355
+ }
356
+
357
+ return {
358
+ type: "TokenCurrency",
359
+ id: `${parentCurrencyId}/native/${assetId}`,
360
+ // Tracking and accounting of native tokens is natively supported by cardano ledger.
361
+ // As there's no contract for native tokens, using unique assetId in place of contractAddress
362
+ contractAddress: assetId,
363
+ parentCurrency,
364
+ tokenType: "native",
365
+ name,
366
+ ticker,
367
+ delisted,
368
+ disableCountervalue: false,
369
+ units: [
370
+ {
371
+ name,
372
+ code: ticker,
373
+ magnitude: decimals,
374
+ },
375
+ ],
376
+ };
377
+ }
378
+
379
+ /**
380
+ * @deprecated
381
+ */
382
+ export function convertStellarTokens([
383
+ assetCode,
384
+ assetIssuer,
385
+ assetType,
386
+ name,
387
+ precision,
388
+ ]: StellarToken): TokenCurrency {
389
+ const parentCurrency = getCryptoCurrencyById("stellar");
390
+
391
+ return {
392
+ type: "TokenCurrency",
393
+ id: `stellar/asset/${assetCode.toUpperCase()}:${assetIssuer.toUpperCase()}`,
394
+ contractAddress: assetIssuer.toUpperCase(),
395
+ parentCurrency,
396
+ tokenType: assetType,
397
+ name,
398
+ ticker: assetCode,
399
+ disableCountervalue: false,
400
+ units: [
401
+ {
402
+ name,
403
+ code: assetCode,
404
+ magnitude: precision,
405
+ },
406
+ ],
407
+ };
408
+ }
409
+
410
+ /**
411
+ * @deprecated
412
+ */
413
+ export function convertJettonToken([address, name, ticker, magnitude, delisted]: TonJettonToken):
414
+ | TokenCurrency
415
+ | undefined {
416
+ const parentCurrency = findCryptoCurrencyById("ton");
417
+
418
+ if (!parentCurrency) {
419
+ return;
420
+ }
421
+
422
+ return {
423
+ type: "TokenCurrency",
424
+ id: "ton/jetton/" + address.toLocaleLowerCase(),
425
+ contractAddress: address,
426
+ parentCurrency,
427
+ tokenType: "jetton",
428
+ name,
429
+ ticker,
430
+ delisted,
431
+ disableCountervalue: false,
432
+ units: [
433
+ {
434
+ name,
435
+ code: ticker,
436
+ magnitude,
437
+ },
438
+ ],
439
+ };
440
+ }
441
+
442
+ /**
443
+ * @deprecated
444
+ */
445
+ export function createTokenHash(token: TokenCurrency): string {
446
+ return token
447
+ ? `${token.id}${token.contractAddress}${token.delisted}${token.ticker}${token.ledgerSignature}`
448
+ : "";
449
+ }
450
+
451
+ /**
452
+ * Only for jest purpose, clean object to be empty
453
+ * @param obj
454
+ */
455
+ const __clearObject = (obj: Record<string, TokenCurrency | TokenCurrency[]>): void => {
456
+ for (const key in obj) {
457
+ delete obj[key];
458
+ }
459
+ };
460
+
461
+ /**
462
+ * Only for jest purpose, clear all the init list
463
+ */
464
+ export function __clearAllLists(): void {
465
+ tokensArray.length = 0;
466
+ tokensArrayWithDelisted.length = 0;
467
+ __clearObject(tokensByCryptoCurrency);
468
+ __clearObject(tokensByCryptoCurrencyWithDelisted);
469
+ __clearObject(tokensById);
470
+ __clearObject(tokensByCurrencyAddress);
471
+ tokenListHashes.clear();
472
+ }
473
+
474
+ function removeTokenFromArray(array: TokenCurrency[], tokenId: string) {
475
+ if (array && array.length > 0) {
476
+ const index = array.findIndex(currentToken => currentToken && currentToken.id === tokenId);
477
+ if (index === -1) return array;
478
+ return array.splice(index, 1);
479
+ }
480
+ }
481
+
482
+ function removeTokenFromRecord(record: Record<string, TokenCurrency>, key: string) {
483
+ tokenListHashes.delete(record[key]);
484
+ delete record[key];
485
+ }
486
+
487
+ /**
488
+ * Delete previous token entry to all array
489
+ * @param token
490
+ */
491
+ function removeTokenFromAllLists(token: TokenCurrency) {
492
+ const { id, contractAddress, parentCurrency } = token;
493
+ const lowCaseContract = contractAddress.toLowerCase();
494
+
495
+ removeTokenFromRecord(tokensById, id);
496
+ removeTokenFromRecord(tokensByCurrencyAddress, parentCurrency.id + ":" + lowCaseContract);
497
+ removeTokenFromArray(tokensArray, id);
498
+ removeTokenFromArray(tokensArrayWithDelisted, id);
499
+ removeTokenFromArray(tokensByCryptoCurrency[parentCurrency.id], id);
500
+ removeTokenFromArray(tokensByCryptoCurrencyWithDelisted[parentCurrency.id], id);
501
+ }
502
+
503
+ /**
504
+ * @deprecated This function is deprecated since tokens will soon be loaded dynamically
505
+ */
506
+ export function addTokens(list: (TokenCurrency | undefined)[]): void {
507
+ list.forEach(token => {
508
+ if (!token) return;
509
+ const tokenHash = createTokenHash(token);
510
+ if (tokenListHashes.has(tokenHash)) return;
511
+
512
+ /**
513
+ * We clean all the reference of an existing token, if an hash doesn't match.
514
+ * Like this we can update any change from a already added token coming from Dynamic CAL
515
+ * and maintain it up to date without having to release a new version of LLD or LLM
516
+ */
517
+ const { id, contractAddress, parentCurrency, delisted } = token;
518
+ if (tokensById[id]) removeTokenFromAllLists(token);
519
+ const lowCaseContract = contractAddress.toLowerCase();
520
+
521
+ if (!delisted) tokensArray.push(token);
522
+ tokensArrayWithDelisted.push(token);
523
+ tokensById[id] = token;
524
+
525
+ tokensByCurrencyAddress[parentCurrency.id + ":" + lowCaseContract] = token;
526
+
527
+ if (!(parentCurrency.id in tokensByCryptoCurrency)) {
528
+ tokensByCryptoCurrency[parentCurrency.id] = [];
529
+ }
530
+
531
+ if (!(parentCurrency.id in tokensByCryptoCurrencyWithDelisted)) {
532
+ tokensByCryptoCurrencyWithDelisted[parentCurrency.id] = [];
533
+ }
534
+
535
+ if (!delisted) tokensByCryptoCurrency[parentCurrency.id].push(token);
536
+ tokensByCryptoCurrencyWithDelisted[parentCurrency.id].push(token);
537
+
538
+ tokenListHashes.add(tokenHash);
539
+ });
540
+ }
541
+
542
+ // Legacy list functions - deprecated since moving to DaDa API
543
+
544
+ const defaultTokenListOptions: TokensListOptions = {
545
+ withDelisted: false,
546
+ };
547
+
548
+ /**
549
+ * @deprecated This function is deprecated since tokens will no longer be listable as we moved to DaDa API everywhere
550
+ * List all available tokens
551
+ */
552
+ export function listTokensLegacy(options?: Partial<TokensListOptions>): TokenCurrency[] {
553
+ const { withDelisted } = { ...defaultTokenListOptions, ...options };
554
+ return withDelisted ? tokensArrayWithDelisted : tokensArray;
555
+ }
556
+
557
+ /**
558
+ * @deprecated This function is deprecated since tokens will no longer be listable as we moved to DaDa API everywhere
559
+ * List tokens for a specific cryptocurrency
560
+ */
561
+ export function listTokensForCryptoCurrencyLegacy(
562
+ currency: CryptoCurrency,
563
+ options?: Partial<TokensListOptions>,
564
+ ): TokenCurrency[] {
565
+ const { withDelisted } = { ...defaultTokenListOptions, ...options };
566
+ if (withDelisted) {
567
+ return tokensByCryptoCurrencyWithDelisted[currency.id] || [];
568
+ }
569
+
570
+ return tokensByCryptoCurrency[currency.id] || [];
571
+ }