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