@ledgerhq/coin-sui 0.12.0 → 0.13.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 (78) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +23 -0
  3. package/lib/api/index.d.ts +1 -1
  4. package/lib/api/index.d.ts.map +1 -1
  5. package/lib/api/index.integration.test.js +9 -2
  6. package/lib/api/index.integration.test.js.map +1 -1
  7. package/lib/api/index.js +1 -1
  8. package/lib/api/index.js.map +1 -1
  9. package/lib/api/index.test.js +3 -3
  10. package/lib/api/index.test.js.map +1 -1
  11. package/lib/bridge/estimateMaxSpendable.d.ts.map +1 -1
  12. package/lib/bridge/estimateMaxSpendable.js +9 -7
  13. package/lib/bridge/estimateMaxSpendable.js.map +1 -1
  14. package/lib/bridge/estimateMaxSpendable.test.js +2 -2
  15. package/lib/bridge/estimateMaxSpendable.test.js.map +1 -1
  16. package/lib/bridge/getTransactionStatus.d.ts.map +1 -1
  17. package/lib/bridge/getTransactionStatus.js +4 -1
  18. package/lib/bridge/getTransactionStatus.js.map +1 -1
  19. package/lib/bridge/utils.d.ts.map +1 -1
  20. package/lib/bridge/utils.js +2 -2
  21. package/lib/bridge/utils.js.map +1 -1
  22. package/lib/logic/listOperations.d.ts +1 -3
  23. package/lib/logic/listOperations.d.ts.map +1 -1
  24. package/lib/logic/listOperations.js +3 -3
  25. package/lib/logic/listOperations.js.map +1 -1
  26. package/lib/logic/listOperations.test.js +43 -39
  27. package/lib/logic/listOperations.test.js.map +1 -1
  28. package/lib/network/index.d.ts +1 -1
  29. package/lib/network/sdk.d.ts +9 -5
  30. package/lib/network/sdk.d.ts.map +1 -1
  31. package/lib/network/sdk.js +54 -27
  32. package/lib/network/sdk.js.map +1 -1
  33. package/lib/network/sdk.test.js +272 -170
  34. package/lib/network/sdk.test.js.map +1 -1
  35. package/lib-es/api/index.d.ts +1 -1
  36. package/lib-es/api/index.d.ts.map +1 -1
  37. package/lib-es/api/index.integration.test.js +9 -2
  38. package/lib-es/api/index.integration.test.js.map +1 -1
  39. package/lib-es/api/index.js +1 -1
  40. package/lib-es/api/index.js.map +1 -1
  41. package/lib-es/api/index.test.js +3 -3
  42. package/lib-es/api/index.test.js.map +1 -1
  43. package/lib-es/bridge/estimateMaxSpendable.d.ts.map +1 -1
  44. package/lib-es/bridge/estimateMaxSpendable.js +9 -7
  45. package/lib-es/bridge/estimateMaxSpendable.js.map +1 -1
  46. package/lib-es/bridge/estimateMaxSpendable.test.js +2 -2
  47. package/lib-es/bridge/estimateMaxSpendable.test.js.map +1 -1
  48. package/lib-es/bridge/getTransactionStatus.d.ts.map +1 -1
  49. package/lib-es/bridge/getTransactionStatus.js +4 -1
  50. package/lib-es/bridge/getTransactionStatus.js.map +1 -1
  51. package/lib-es/bridge/utils.d.ts.map +1 -1
  52. package/lib-es/bridge/utils.js +2 -2
  53. package/lib-es/bridge/utils.js.map +1 -1
  54. package/lib-es/logic/listOperations.d.ts +1 -3
  55. package/lib-es/logic/listOperations.d.ts.map +1 -1
  56. package/lib-es/logic/listOperations.js +4 -4
  57. package/lib-es/logic/listOperations.js.map +1 -1
  58. package/lib-es/logic/listOperations.test.js +44 -40
  59. package/lib-es/logic/listOperations.test.js.map +1 -1
  60. package/lib-es/network/index.d.ts +1 -1
  61. package/lib-es/network/sdk.d.ts +9 -5
  62. package/lib-es/network/sdk.d.ts.map +1 -1
  63. package/lib-es/network/sdk.js +54 -27
  64. package/lib-es/network/sdk.js.map +1 -1
  65. package/lib-es/network/sdk.test.js +272 -170
  66. package/lib-es/network/sdk.test.js.map +1 -1
  67. package/package.json +5 -5
  68. package/src/api/index.integration.test.ts +10 -2
  69. package/src/api/index.test.ts +3 -3
  70. package/src/api/index.ts +4 -3
  71. package/src/bridge/estimateMaxSpendable.test.ts +2 -2
  72. package/src/bridge/estimateMaxSpendable.ts +9 -8
  73. package/src/bridge/getTransactionStatus.ts +4 -2
  74. package/src/bridge/utils.ts +2 -2
  75. package/src/logic/listOperations.test.ts +45 -41
  76. package/src/logic/listOperations.ts +4 -4
  77. package/src/network/sdk.test.ts +312 -207
  78. package/src/network/sdk.ts +70 -32
@@ -24,6 +24,7 @@ import type {
24
24
  BlockTransaction,
25
25
  BlockOperation,
26
26
  Operation as Op,
27
+ Page,
27
28
  Stake,
28
29
  StakeState,
29
30
  AssetInfo,
@@ -318,6 +319,7 @@ export function transactionToOperation(
318
319
  return {
319
320
  id: encodeOperationId(accountId, hash, type),
320
321
  accountId,
322
+ // warning this is false:
321
323
  blockHash: hash,
322
324
  blockHeight: BLOCK_HEIGHT,
323
325
  date: getOperationDate(transaction),
@@ -347,7 +349,6 @@ export function transactionToOp(address: string, transaction: SuiTransactionBloc
347
349
  block: {
348
350
  // agreed to return bigint
349
351
  height: BigInt(transaction.checkpoint || "") as unknown as number,
350
- hash,
351
352
  time: getOperationDate(transaction),
352
353
  },
353
354
  },
@@ -449,14 +450,22 @@ export const getOperations = async (
449
450
  accountId: string,
450
451
  addr: string,
451
452
  cursor?: QueryTransactionBlocksParams["cursor"],
453
+ order?: "asc" | "desc",
452
454
  ): Promise<Operation[]> =>
453
455
  withApi(async api => {
454
- const sentOps = await loadOperations({
456
+ let rpcOrder: "ascending" | "descending";
457
+ if (order) {
458
+ rpcOrder = order === "asc" ? "ascending" : "descending";
459
+ } else {
460
+ rpcOrder = cursor ? "ascending" : "descending";
461
+ }
462
+
463
+ const sendOps = await loadOperations({
455
464
  api,
456
465
  addr,
457
466
  type: "OUT",
458
467
  cursor,
459
- order: cursor ? "ascending" : "descending",
468
+ order: rpcOrder,
460
469
  operations: [],
461
470
  });
462
471
  const receivedOps = await loadOperations({
@@ -464,38 +473,51 @@ export const getOperations = async (
464
473
  addr,
465
474
  type: "IN",
466
475
  cursor,
467
- order: cursor ? "ascending" : "descending",
476
+ order: rpcOrder,
468
477
  operations: [],
469
478
  });
470
- const rawTransactions = filterOperations(sentOps, receivedOps, cursor);
479
+ // When restoring state (no cursor provided) we filter out extra operations to maintain correct chronological order
480
+ const rawTransactions = filterOperations(sendOps, receivedOps, rpcOrder, !cursor);
471
481
 
472
- return rawTransactions.map(transaction => transactionToOperation(accountId, addr, transaction));
482
+ return rawTransactions.operations.map(transaction =>
483
+ transactionToOperation(accountId, addr, transaction),
484
+ );
473
485
  });
474
486
 
475
487
  export const filterOperations = (
476
- operationList1: SuiTransactionBlockResponse[],
477
- operationList2: SuiTransactionBlockResponse[],
478
- cursor: string | null | undefined,
479
- ) => {
480
- let filterTimestamp = 0;
481
-
482
- // When restoring state (no cursor provided) and we've reached the limit for either sent or received operations,
488
+ sendOps: LoadOperationResponse,
489
+ receiveOps: LoadOperationResponse,
490
+ order: "ascending" | "descending",
491
+ shouldFilter: boolean = true,
492
+ ): LoadOperationResponse => {
493
+ let filterTimestamp: number = 0;
494
+ let nextCursor: string | null | undefined = undefined;
495
+ // When we've reached the limit for either sent or received operations,
483
496
  // we filter out extra operations to maintain correct chronological order
484
497
  if (
485
- !cursor &&
486
- operationList1.length &&
487
- operationList2.length &&
488
- (operationList1.length === TRANSACTIONS_LIMIT || operationList2.length === TRANSACTIONS_LIMIT)
498
+ shouldFilter &&
499
+ sendOps.operations.length &&
500
+ receiveOps.operations.length &&
501
+ (sendOps.operations.length === TRANSACTIONS_LIMIT ||
502
+ receiveOps.operations.length === TRANSACTIONS_LIMIT)
489
503
  ) {
490
- const aTime = operationList1[operationList1.length - 1].timestampMs ?? 0;
491
- const bTime = operationList2[operationList2.length - 1].timestampMs ?? 0;
492
- filterTimestamp = Math.max(Number(aTime), Number(bTime));
504
+ const sendTime = Number(sendOps.operations[sendOps.operations.length - 1].timestampMs ?? 0);
505
+ const receiveTime = Number(
506
+ receiveOps.operations[receiveOps.operations.length - 1].timestampMs ?? 0,
507
+ );
508
+ if (sendTime >= receiveTime) {
509
+ nextCursor = sendOps.cursor;
510
+ filterTimestamp = sendTime;
511
+ } else {
512
+ nextCursor = receiveOps.cursor;
513
+ filterTimestamp = receiveTime;
514
+ }
493
515
  }
494
- const result = [...operationList1, ...operationList2]
516
+ const result = [...sendOps.operations, ...receiveOps.operations]
495
517
  .sort((a, b) => Number(b.timestampMs) - Number(a.timestampMs))
496
518
  .filter(op => Number(op.timestampMs) >= filterTimestamp);
497
519
 
498
- return uniqBy(result, tx => tx.digest);
520
+ return { operations: uniqBy(result, tx => tx.digest), cursor: nextCursor };
499
521
  };
500
522
 
501
523
  /**
@@ -505,14 +527,22 @@ export const getListOperations = async (
505
527
  addr: string,
506
528
  cursor: QueryTransactionBlocksParams["cursor"] = null,
507
529
  withApiImpl: typeof withApi = withApi,
508
- ): Promise<Op[]> =>
530
+ order?: "asc" | "desc",
531
+ ): Promise<Page<Op>> =>
509
532
  withApiImpl(async api => {
533
+ let rpcOrder: "ascending" | "descending";
534
+ if (order) {
535
+ rpcOrder = order === "asc" ? "ascending" : "descending";
536
+ } else {
537
+ rpcOrder = cursor ? "ascending" : "descending";
538
+ }
539
+
510
540
  const opsOut = await loadOperations({
511
541
  api,
512
542
  addr,
513
543
  type: "OUT",
514
544
  cursor,
515
- order: cursor ? "ascending" : "descending",
545
+ order: rpcOrder,
516
546
  operations: [],
517
547
  });
518
548
  const opsIn = await loadOperations({
@@ -520,12 +550,15 @@ export const getListOperations = async (
520
550
  addr,
521
551
  type: "IN",
522
552
  cursor,
523
- order: cursor ? "ascending" : "descending",
553
+ order: rpcOrder,
524
554
  operations: [],
525
555
  });
526
- const list = filterOperations(opsIn, opsOut, cursor);
556
+ const list = filterOperations(opsIn, opsOut, rpcOrder, true);
527
557
 
528
- return list.map(t => transactionToOp(addr, t));
558
+ return {
559
+ items: list.operations.map(t => transactionToOp(addr, t)),
560
+ next: list.cursor ?? undefined,
561
+ };
529
562
  });
530
563
 
531
564
  /**
@@ -715,6 +748,11 @@ export const executeTransactionBlock = async (params: ExecuteTransactionBlockPar
715
748
  return api.executeTransactionBlock(params);
716
749
  });
717
750
 
751
+ type LoadOperationResponse = {
752
+ operations: SuiTransactionBlockResponse[];
753
+ cursor?: QueryTransactionBlocksParams["cursor"];
754
+ };
755
+
718
756
  /**
719
757
  * Fetch operations for a specific address and type until the limit is reached
720
758
  */
@@ -730,10 +768,10 @@ export const loadOperations = async ({
730
768
  operations: PaginatedTransactionResponse["data"];
731
769
  order: "ascending" | "descending";
732
770
  cursor?: QueryTransactionBlocksParams["cursor"];
733
- }): Promise<PaginatedTransactionResponse["data"]> => {
771
+ }): Promise<LoadOperationResponse> => {
734
772
  try {
735
- if (order === "descending" && operations.length >= TRANSACTIONS_LIMIT) {
736
- return operations;
773
+ if (operations.length >= TRANSACTIONS_LIMIT) {
774
+ return { operations, cursor };
737
775
  }
738
776
 
739
777
  const { data, nextCursor, hasNextPage } = await queryTransactions({
@@ -744,7 +782,7 @@ export const loadOperations = async ({
744
782
 
745
783
  operations.push(...data);
746
784
  if (!hasNextPage) {
747
- return operations;
785
+ return { operations: operations, cursor: nextCursor };
748
786
  }
749
787
 
750
788
  await loadOperations({ ...params, cursor: nextCursor, operations, order });
@@ -759,7 +797,7 @@ export const loadOperations = async ({
759
797
  }
760
798
  }
761
799
 
762
- return operations;
800
+ return { operations: operations, cursor: cursor };
763
801
  };
764
802
 
765
803
  /**