@ledgerhq/coin-sui 0.13.0-nightly.6 → 0.13.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 (39) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +23 -47
  3. package/lib/api/index.d.ts.map +1 -1
  4. package/lib/api/index.integration.test.js +4 -28
  5. package/lib/api/index.integration.test.js.map +1 -1
  6. package/lib/api/index.js +4 -1
  7. package/lib/api/index.js.map +1 -1
  8. package/lib/bridge/preload.js +2 -2
  9. package/lib/bridge/preload.js.map +1 -1
  10. package/lib/logic/listOperations.js +1 -1
  11. package/lib/logic/listOperations.js.map +1 -1
  12. package/lib/logic/listOperations.test.js +2 -2
  13. package/lib/logic/listOperations.test.js.map +1 -1
  14. package/lib/network/sdk.d.ts +1 -24
  15. package/lib/network/sdk.d.ts.map +1 -1
  16. package/lib/network/sdk.js +25 -68
  17. package/lib/network/sdk.js.map +1 -1
  18. package/lib-es/api/index.d.ts.map +1 -1
  19. package/lib-es/api/index.integration.test.js +4 -28
  20. package/lib-es/api/index.integration.test.js.map +1 -1
  21. package/lib-es/api/index.js +5 -2
  22. package/lib-es/api/index.js.map +1 -1
  23. package/lib-es/bridge/preload.js +2 -2
  24. package/lib-es/bridge/preload.js.map +1 -1
  25. package/lib-es/logic/listOperations.js +1 -1
  26. package/lib-es/logic/listOperations.js.map +1 -1
  27. package/lib-es/logic/listOperations.test.js +2 -2
  28. package/lib-es/logic/listOperations.test.js.map +1 -1
  29. package/lib-es/network/sdk.d.ts +1 -24
  30. package/lib-es/network/sdk.d.ts.map +1 -1
  31. package/lib-es/network/sdk.js +25 -68
  32. package/lib-es/network/sdk.js.map +1 -1
  33. package/package.json +7 -8
  34. package/src/api/index.integration.test.ts +6 -39
  35. package/src/api/index.ts +8 -2
  36. package/src/bridge/preload.ts +2 -2
  37. package/src/logic/listOperations.test.ts +2 -2
  38. package/src/logic/listOperations.ts +1 -1
  39. package/src/network/sdk.ts +27 -82
@@ -46,7 +46,7 @@ const mockOperations: Page<Op> = {
46
46
  next: "0x1234567890abcdef",
47
47
  };
48
48
 
49
- const mockGetListOperations = jest.mocked(getListOperations);
49
+ const mockGetListOperations = getListOperations as jest.Mock;
50
50
  mockGetListOperations.mockResolvedValue(mockOperations);
51
51
 
52
52
  describe("List Operations", () => {
@@ -65,7 +65,7 @@ describe("List Operations", () => {
65
65
 
66
66
  expect(operations).toEqual(mockOperations.items);
67
67
  expect(lastHash).toBe(mockOperations.items[0].tx.hash);
68
- expect(mockGetListOperations).toHaveBeenCalledWith(mockAddress, "asc", withApi, undefined);
68
+ expect(mockGetListOperations).toHaveBeenCalledWith(mockAddress, undefined, withApi, "asc");
69
69
  });
70
70
 
71
71
  it("should return empty array and empty string when no operations", async () => {
@@ -5,6 +5,6 @@ export const listOperations = async (
5
5
  address: string,
6
6
  { lastPagingToken, order }: Pagination,
7
7
  ): Promise<[Operation[], string]> => {
8
- const ops = await getListOperations(address, order ?? "asc", withApi, lastPagingToken);
8
+ const ops = await getListOperations(address, lastPagingToken, withApi, order);
9
9
  return [ops.items, ops.next || ""];
10
10
  };
@@ -41,7 +41,6 @@ import { getEnv } from "@ledgerhq/live-env";
41
41
  import { SUI_SYSTEM_STATE_OBJECT_ID } from "@mysten/sui/utils";
42
42
  import { getCurrentSuiPreloadData } from "../bridge/preload";
43
43
  import { ONE_SUI } from "../constants";
44
- import bs58 from "bs58";
45
44
 
46
45
  const apiMap: Record<string, SuiClient> = {};
47
46
  type AsyncApiFunction<T> = (api: SuiClient) => Promise<T>;
@@ -337,10 +336,6 @@ export function transactionToOperation(
337
336
  };
338
337
  }
339
338
 
340
- /**
341
- * @returns the operation converted. Note that if param `transaction` was retrieved as an "IN" operations, the type may be converted to "OUT".
342
- * It happens for most "OUT" operations because the sender receive a new version of the coin objects.
343
- */
344
339
  export function transactionToOp(address: string, transaction: SuiTransactionBlockResponse): Op {
345
340
  const type = getOperationType(address, transaction);
346
341
  const coinType = getOperationCoinType(transaction);
@@ -525,94 +520,44 @@ export const filterOperations = (
525
520
  return { operations: uniqBy(result, tx => tx.digest), cursor: nextCursor };
526
521
  };
527
522
 
528
- function convertApiOrderToSdkOrder(order: "asc" | "desc"): "ascending" | "descending" {
529
- return order === "asc" ? "ascending" : "descending";
530
- }
531
-
532
- type Cursor = {
533
- out?: string;
534
- in?: string;
535
- };
536
-
537
- function serializeCursor(cursor: Cursor): string {
538
- return bs58.encode(Buffer.from(JSON.stringify(cursor)));
539
- }
540
-
541
- function deserializeCursor(b58cursor: string | undefined): Cursor {
542
- return b58cursor
543
- ? (JSON.parse(Buffer.from(bs58.decode(b58cursor)).toString()) as Cursor)
544
- : ({} as Cursor);
545
- }
546
-
547
- function toSdkCursor(cursor: string | undefined): QueryTransactionBlocksParams["cursor"] {
548
- const ret: QueryTransactionBlocksParams["cursor"] = cursor;
549
- return ret;
550
- }
551
-
552
523
  /**
553
524
  * Fetch operations for Alpaca
554
- * It fetches separately the "OUT" and "IN" operations and then merge them.
555
- * The cursor is composed of the last "OUT" and "IN" operation cursors.
556
- *
557
- * Warning:
558
- * Some IN operations are also OUT operations because the sender receive a new version of the coin objects,
559
- * and the complexity of this function don't go that far to detect it.
560
- * IN calls and OUT calls are not disjoint
561
- * Consequence: 2 successive calls of this function when passing cursor may return an operation we already saw in previous calls,
562
- * fetched as an OUT operation.
563
- *
564
- * Note: I think it's possible to detect duplicated IN oprations:
565
- * - if the address is the sender of the tx
566
- * - and there is some transfer to other address
567
- * - and the address is the single only owner of mutated or deleted object
568
- * when all that conditions are met, the transaction will be fetched as an OUT operation,
569
- * and it can be filtered out from the IN operations results.
570
- *
571
- * @returns the operations.
572
- *
573
525
  */
574
526
  export const getListOperations = async (
575
527
  addr: string,
576
- order: "asc" | "desc",
528
+ cursor: QueryTransactionBlocksParams["cursor"] = null,
577
529
  withApiImpl: typeof withApi = withApi,
578
- cursor?: string,
530
+ order?: "asc" | "desc",
579
531
  ): Promise<Page<Op>> =>
580
532
  withApiImpl(async api => {
581
- const rpcOrder = convertApiOrderToSdkOrder(order);
582
- const { out: outCursor, in: inCursor } = deserializeCursor(cursor);
583
-
584
- const [opsOut, opsIn] = await Promise.all([
585
- queryTransactions({
586
- api,
587
- addr,
588
- type: "OUT",
589
- cursor: toSdkCursor(outCursor),
590
- order: rpcOrder,
591
- }),
592
- queryTransactions({
593
- api,
594
- addr,
595
- type: "IN",
596
- cursor: toSdkCursor(inCursor),
597
- order: rpcOrder,
598
- }),
599
- ]);
600
-
601
- const ops = [...opsOut.data, ...opsIn.data]
602
- .sort((a, b) => Number(b.timestampMs) - Number(a.timestampMs))
603
- .map(t => transactionToOp(addr, t));
604
-
605
- const nextCursor: Cursor = {};
606
- if (opsOut.hasNextPage && opsOut.nextCursor) {
607
- nextCursor.out = opsOut.nextCursor;
608
- }
609
- if (opsIn.hasNextPage && opsIn.nextCursor) {
610
- nextCursor.in = opsIn.nextCursor;
533
+ let rpcOrder: "ascending" | "descending";
534
+ if (order) {
535
+ rpcOrder = order === "asc" ? "ascending" : "descending";
536
+ } else {
537
+ rpcOrder = cursor ? "ascending" : "descending";
611
538
  }
612
539
 
540
+ const opsOut = await loadOperations({
541
+ api,
542
+ addr,
543
+ type: "OUT",
544
+ cursor,
545
+ order: rpcOrder,
546
+ operations: [],
547
+ });
548
+ const opsIn = await loadOperations({
549
+ api,
550
+ addr,
551
+ type: "IN",
552
+ cursor,
553
+ order: rpcOrder,
554
+ operations: [],
555
+ });
556
+ const list = filterOperations(opsIn, opsOut, rpcOrder, true);
557
+
613
558
  return {
614
- items: ops,
615
- next: serializeCursor(nextCursor),
559
+ items: list.operations.map(t => transactionToOp(addr, t)),
560
+ next: list.cursor ?? undefined,
616
561
  };
617
562
  });
618
563