@ledgerhq/coin-sui 0.12.0-nightly.4 → 0.12.0

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