@ledgerhq/coin-tron 2.1.0-nightly.2 → 3.0.0-nightly.3

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 (68) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +12 -0
  3. package/jest.config.js +1 -0
  4. package/lib/api/index.d.ts.map +1 -1
  5. package/lib/api/index.js +5 -1
  6. package/lib/api/index.js.map +1 -1
  7. package/lib/api/index.test.js +2 -2
  8. package/lib/api/index.test.js.map +1 -1
  9. package/lib/bridge/synchronization.d.ts.map +1 -1
  10. package/lib/bridge/synchronization.js +1 -1
  11. package/lib/bridge/synchronization.js.map +1 -1
  12. package/lib/logic/listOperations.d.ts +1 -1
  13. package/lib/logic/listOperations.d.ts.map +1 -1
  14. package/lib/logic/listOperations.integ.test.js +21 -7
  15. package/lib/logic/listOperations.integ.test.js.map +1 -1
  16. package/lib/logic/listOperations.js +14 -5
  17. package/lib/logic/listOperations.js.map +1 -1
  18. package/lib/logic/listOperations.unit.test.js +22 -9
  19. package/lib/logic/listOperations.unit.test.js.map +1 -1
  20. package/lib/network/index.d.ts +10 -7
  21. package/lib/network/index.d.ts.map +1 -1
  22. package/lib/network/index.integ.test.js +1 -1
  23. package/lib/network/index.integ.test.js.map +1 -1
  24. package/lib/network/index.js +32 -8
  25. package/lib/network/index.js.map +1 -1
  26. package/lib/network/index.test.js +1 -1
  27. package/lib/network/index.test.js.map +1 -1
  28. package/lib/network/types.d.ts +6 -1
  29. package/lib/network/types.d.ts.map +1 -1
  30. package/lib/network/types.js.map +1 -1
  31. package/lib-es/api/index.d.ts.map +1 -1
  32. package/lib-es/api/index.js +5 -1
  33. package/lib-es/api/index.js.map +1 -1
  34. package/lib-es/api/index.test.js +2 -2
  35. package/lib-es/api/index.test.js.map +1 -1
  36. package/lib-es/bridge/synchronization.d.ts.map +1 -1
  37. package/lib-es/bridge/synchronization.js +2 -2
  38. package/lib-es/bridge/synchronization.js.map +1 -1
  39. package/lib-es/logic/listOperations.d.ts +1 -1
  40. package/lib-es/logic/listOperations.d.ts.map +1 -1
  41. package/lib-es/logic/listOperations.integ.test.js +21 -7
  42. package/lib-es/logic/listOperations.integ.test.js.map +1 -1
  43. package/lib-es/logic/listOperations.js +15 -6
  44. package/lib-es/logic/listOperations.js.map +1 -1
  45. package/lib-es/logic/listOperations.unit.test.js +23 -10
  46. package/lib-es/logic/listOperations.unit.test.js.map +1 -1
  47. package/lib-es/network/index.d.ts +10 -7
  48. package/lib-es/network/index.d.ts.map +1 -1
  49. package/lib-es/network/index.integ.test.js +2 -2
  50. package/lib-es/network/index.integ.test.js.map +1 -1
  51. package/lib-es/network/index.js +30 -7
  52. package/lib-es/network/index.js.map +1 -1
  53. package/lib-es/network/index.test.js +2 -2
  54. package/lib-es/network/index.test.js.map +1 -1
  55. package/lib-es/network/types.d.ts +6 -1
  56. package/lib-es/network/types.d.ts.map +1 -1
  57. package/lib-es/network/types.js.map +1 -1
  58. package/package.json +5 -4
  59. package/src/api/index.test.ts +2 -2
  60. package/src/api/index.ts +16 -2
  61. package/src/bridge/synchronization.ts +2 -1
  62. package/src/logic/listOperations.integ.test.ts +27 -7
  63. package/src/logic/listOperations.ts +24 -6
  64. package/src/logic/listOperations.unit.test.ts +35 -10
  65. package/src/network/index.integ.test.ts +13 -2
  66. package/src/network/index.test.ts +7 -2
  67. package/src/network/index.ts +48 -17
  68. package/src/network/types.ts +7 -1
@@ -37,6 +37,7 @@ import {
37
37
  } from "./format";
38
38
  import {
39
39
  AccountTronAPI,
40
+ Block,
40
41
  isMalformedTransactionTronAPI,
41
42
  isTransactionTronAPI,
42
43
  MalformedTransactionTronAPI,
@@ -365,17 +366,31 @@ export async function fetchTronAccount(addr: string): Promise<AccountTronAPI[]>
365
366
  }
366
367
  }
367
368
 
368
- export async function getLastBlock(): Promise<{
369
- height: number;
370
- hash: string;
371
- time: Date;
372
- }> {
369
+ export async function getLastBlock(): Promise<Block> {
373
370
  const data = await fetch(`/wallet/getnowblock`);
374
- return {
371
+ return toBlock(data);
372
+ }
373
+
374
+ export async function getBlock(blockNumber: number): Promise<Block> {
375
+ const data = await fetch(`/wallet/getblock?id_or_num=${encodeURIComponent(blockNumber)}`);
376
+ const ret = toBlock(data);
377
+ if (!ret.height) {
378
+ ret.height = blockNumber;
379
+ }
380
+ return ret;
381
+ }
382
+
383
+ function toBlock(data: any): Block {
384
+ // some old blocks doesn't have a timestamp
385
+ const timestamp = data.block_header.raw_data.timestamp;
386
+ const ret: Block = {
375
387
  height: data.block_header.raw_data.number,
376
388
  hash: data.blockID,
377
- time: new Date(data.block_header.raw_data.timestamp),
378
389
  };
390
+ if (timestamp) {
391
+ ret.time = new Date(timestamp);
392
+ }
393
+ return ret;
379
394
  }
380
395
 
381
396
  // For the moment, fetching transaction info is the only way to get fees from a transaction
@@ -463,23 +478,37 @@ const getTrc20 = async (
463
478
  };
464
479
  };
465
480
 
481
+ export type FetchTxsStopPredicate = (
482
+ txs: Array<TransactionTronAPI | Trc20API | MalformedTransactionTronAPI>,
483
+ ) => boolean;
484
+
485
+ export type FetchParams = {
486
+ limit: number;
487
+ minTimestamp: number;
488
+ };
489
+
490
+ export const defaultFetchParams: FetchParams = {
491
+ limit: 100,
492
+ minTimestamp: 0,
493
+ } as const;
494
+
466
495
  export async function fetchTronAccountTxs(
467
496
  addr: string,
468
- shouldFetchMoreTxs: (
469
- txs: Array<TransactionTronAPI | Trc20API | MalformedTransactionTronAPI>,
470
- ) => boolean,
497
+ shouldFetchMoreTxs: FetchTxsStopPredicate,
471
498
  cacheTransactionInfoById: Record<string, TronTransactionInfo>,
499
+ params: FetchParams,
472
500
  ): Promise<TrongridTxInfo[]> {
473
- const entireTxs = (
501
+ const queryParamsNativeTxs = `limit=${params.limit}&min_timestamp=${params.minTimestamp}`;
502
+ const nativeTxs = (
474
503
  await getAllTransactions<
475
504
  (TransactionTronAPI & { detail?: TronTransactionInfo }) | MalformedTransactionTronAPI
476
505
  >(
477
- `${getBaseApiUrl()}/v1/accounts/${addr}/transactions?limit=100`,
506
+ `${getBaseApiUrl()}/v1/accounts/${addr}/transactions?${queryParamsNativeTxs}`,
478
507
  shouldFetchMoreTxs,
479
508
  getTransactions(cacheTransactionInfoById),
480
509
  )
481
510
  )
482
- .filter((tx): tx is TransactionTronAPI => isTransactionTronAPI(tx))
511
+ .filter(isTransactionTronAPI)
483
512
  .filter(tx => {
484
513
  // custom smart contract tx has internal txs
485
514
  const hasInternalTxs =
@@ -496,17 +525,19 @@ export async function fetchTronAccountTxs(
496
525
  return !isDuplicated && !hasInternalTxs && type !== "TriggerSmartContract";
497
526
  })
498
527
  .map(tx => formatTrongridTxResponse(tx));
499
- // we need to fetch and filter trc20 transactions from another endpoint
500
528
 
501
- const entireTrc20Txs = (
529
+ // we need to fetch and filter trc20 transactions from another endpoint
530
+ // doc https://developers.tron.network/reference/get-trc20-transaction-info-by-account-address
531
+ const queryParamsTrc20Txs = `limit=${params.limit}&min_timestamp=${params.minTimestamp}`;
532
+ const trc20Txs = (
502
533
  await getAllTransactions<Trc20API>(
503
- `${getBaseApiUrl()}/v1/accounts/${addr}/transactions/trc20?get_detail=true`,
534
+ `${getBaseApiUrl()}/v1/accounts/${addr}/transactions/trc20?${queryParamsTrc20Txs}&get_detail=true`,
504
535
  shouldFetchMoreTxs,
505
536
  getTrc20,
506
537
  )
507
538
  ).map(tx => formatTrongridTrc20TxResponse(tx));
508
539
 
509
- const txInfos: TrongridTxInfo[] = compact(entireTxs.concat(entireTrc20Txs)).sort(
540
+ const txInfos: TrongridTxInfo[] = compact(nativeTxs.concat(trc20Txs)).sort(
510
541
  (a, b) => b.date.getTime() - a.date.getTime(),
511
542
  );
512
543
  return txInfos;
@@ -65,8 +65,8 @@ export type TransactionTronAPI<T = TransactionContract> = {
65
65
  raw_data_hex: string;
66
66
  net_fee: number;
67
67
  energy_usage: number;
68
- blockNumber?: number;
69
68
  block_timestamp: number;
69
+ blockNumber?: number;
70
70
  energy_fee: number;
71
71
  energy_usage_total: number;
72
72
  unfreeze_amount?: number;
@@ -180,6 +180,12 @@ export type MalformedTransactionTronAPI = {
180
180
  from_address: string;
181
181
  };
182
182
 
183
+ export type Block = {
184
+ height: number;
185
+ hash: string;
186
+ time?: Date;
187
+ };
188
+
183
189
  export function isMalformedTransactionTronAPI(
184
190
  tx: TransactionTronAPI | MalformedTransactionTronAPI,
185
191
  ): tx is MalformedTransactionTronAPI {