@ledgerhq/coin-sui 0.15.0 → 0.15.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 (61) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/lib/api/index.js +7 -2
  3. package/lib/api/index.js.map +1 -1
  4. package/lib/api/index.test.js +1 -1
  5. package/lib/api/index.test.js.map +1 -1
  6. package/lib/logic/craftTransaction.d.ts +1 -1
  7. package/lib/logic/craftTransaction.d.ts.map +1 -1
  8. package/lib/logic/craftTransaction.integration.test.js +38 -0
  9. package/lib/logic/craftTransaction.integration.test.js.map +1 -1
  10. package/lib/logic/craftTransaction.js +3 -4
  11. package/lib/logic/craftTransaction.js.map +1 -1
  12. package/lib/logic/craftTransaction.test.js +5 -5
  13. package/lib/logic/craftTransaction.test.js.map +1 -1
  14. package/lib/logic/estimateFees.integration.test.js +1 -1
  15. package/lib/network/index.d.ts +1 -1
  16. package/lib/network/sdk.d.ts +8 -4
  17. package/lib/network/sdk.d.ts.map +1 -1
  18. package/lib/network/sdk.integration.test.js +2 -2
  19. package/lib/network/sdk.integration.test.js.map +1 -1
  20. package/lib/network/sdk.js +78 -31
  21. package/lib/network/sdk.js.map +1 -1
  22. package/lib/network/sdk.test.js +148 -333
  23. package/lib/network/sdk.test.js.map +1 -1
  24. package/lib/types/model.d.ts +3 -0
  25. package/lib/types/model.d.ts.map +1 -1
  26. package/lib-es/api/index.js +7 -2
  27. package/lib-es/api/index.js.map +1 -1
  28. package/lib-es/api/index.test.js +1 -1
  29. package/lib-es/api/index.test.js.map +1 -1
  30. package/lib-es/logic/craftTransaction.d.ts +1 -1
  31. package/lib-es/logic/craftTransaction.d.ts.map +1 -1
  32. package/lib-es/logic/craftTransaction.integration.test.js +38 -0
  33. package/lib-es/logic/craftTransaction.integration.test.js.map +1 -1
  34. package/lib-es/logic/craftTransaction.js +3 -4
  35. package/lib-es/logic/craftTransaction.js.map +1 -1
  36. package/lib-es/logic/craftTransaction.test.js +5 -5
  37. package/lib-es/logic/craftTransaction.test.js.map +1 -1
  38. package/lib-es/logic/estimateFees.integration.test.js +1 -1
  39. package/lib-es/network/index.d.ts +1 -1
  40. package/lib-es/network/sdk.d.ts +8 -4
  41. package/lib-es/network/sdk.d.ts.map +1 -1
  42. package/lib-es/network/sdk.integration.test.js +2 -2
  43. package/lib-es/network/sdk.integration.test.js.map +1 -1
  44. package/lib-es/network/sdk.js +75 -29
  45. package/lib-es/network/sdk.js.map +1 -1
  46. package/lib-es/network/sdk.test.js +148 -333
  47. package/lib-es/network/sdk.test.js.map +1 -1
  48. package/lib-es/types/model.d.ts +3 -0
  49. package/lib-es/types/model.d.ts.map +1 -1
  50. package/package.json +4 -4
  51. package/src/api/index.test.ts +1 -1
  52. package/src/api/index.ts +7 -2
  53. package/src/logic/craftTransaction.integration.test.ts +48 -0
  54. package/src/logic/craftTransaction.test.ts +41 -25
  55. package/src/logic/craftTransaction.ts +25 -20
  56. package/src/logic/estimateFees.integration.test.ts +1 -1
  57. package/src/network/sdk.integration.test.ts +2 -2
  58. package/src/network/sdk.test.ts +210 -369
  59. package/src/network/sdk.ts +114 -36
  60. package/src/types/model.ts +4 -0
  61. package/index.d.ts +0 -0
@@ -4,6 +4,7 @@ import coinConfig from "../config";
4
4
  import { BigNumber } from "bignumber.js";
5
5
  import { SuiClient } from "@mysten/sui/client";
6
6
  import type { TransactionBlockData, SuiTransactionBlockResponse } from "@mysten/sui/client";
7
+ import assert from "assert";
7
8
 
8
9
  // Mock SUI client for tests
9
10
  jest.mock("@mysten/sui/client", () => {
@@ -185,7 +186,90 @@ const mockTransaction = {
185
186
  ],
186
187
  timestampMs: "1742294454878",
187
188
  checkpoint: "313024",
188
- };
189
+ } as SuiTransactionBlockResponse;
190
+
191
+ // Create a mock staking transaction
192
+ // amount must be a negative number
193
+ function mockStakingTx(address: string, amount: string) {
194
+ assert(new BigNumber(amount).lte(0), "amount must be a negative number");
195
+ return {
196
+ digest: "delegate_tx_digest_123",
197
+ transaction: {
198
+ data: {
199
+ sender: address,
200
+ transaction: {
201
+ kind: "ProgrammableTransaction",
202
+ inputs: [],
203
+ transactions: [
204
+ {
205
+ MoveCall: {
206
+ function: "request_add_stake",
207
+ },
208
+ },
209
+ ],
210
+ },
211
+ },
212
+ },
213
+ effects: {
214
+ status: { status: "success" },
215
+ gasUsed: {
216
+ computationCost: "1000000",
217
+ storageCost: "500000",
218
+ storageRebate: "450000",
219
+ },
220
+ },
221
+ balanceChanges: [
222
+ {
223
+ owner: { AddressOwner: address },
224
+ coinType: "0x2::sui::SUI",
225
+ amount: amount.startsWith("-") ? amount : `-${amount}`,
226
+ },
227
+ ],
228
+ timestampMs: "1742294454878",
229
+ checkpoint: "313024",
230
+ } as unknown as SuiTransactionBlockResponse;
231
+ }
232
+
233
+ // amount must be a positive number
234
+ function mockUnstakingTx(address: string, amount: string) {
235
+ assert(new BigNumber(amount).gte(0), "amount must be a positive number");
236
+ return {
237
+ digest: "undelegate_tx_digest_456",
238
+ transaction: {
239
+ data: {
240
+ sender: address,
241
+ transaction: {
242
+ kind: "ProgrammableTransaction",
243
+ inputs: [],
244
+ transactions: [
245
+ {
246
+ MoveCall: {
247
+ function: "request_withdraw_stake",
248
+ },
249
+ },
250
+ ],
251
+ },
252
+ },
253
+ },
254
+ effects: {
255
+ status: { status: "success" },
256
+ gasUsed: {
257
+ computationCost: "1000000",
258
+ storageCost: "500000",
259
+ storageRebate: "450000",
260
+ },
261
+ },
262
+ balanceChanges: [
263
+ {
264
+ owner: { AddressOwner: address },
265
+ coinType: "0x2::sui::SUI",
266
+ amount: amount,
267
+ },
268
+ ],
269
+ timestampMs: "1742294454878",
270
+ checkpoint: "313024",
271
+ } as unknown as SuiTransactionBlockResponse;
272
+ }
189
273
 
190
274
  const mockApi = new SuiClient({ url: "mock" }) as jest.Mocked<SuiClient>;
191
275
 
@@ -254,16 +338,12 @@ describe("SDK Functions", () => {
254
338
 
255
339
  test("getOperationType should return IN for incoming tx", () => {
256
340
  const address = "0x33444cf803c690db96527cec67e3c9ab512596f4ba2d4eace43f0b4f716e0164";
257
- expect(sdk.getOperationType(address, mockTransaction as SuiTransactionBlockResponse)).toBe(
258
- "IN",
259
- );
341
+ expect(sdk.getOperationType(address, mockTransaction)).toBe("IN");
260
342
  });
261
343
 
262
344
  test("getOperationType should return OUT for outgoing tx", () => {
263
345
  const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
264
- expect(sdk.getOperationType(address, mockTransaction as SuiTransactionBlockResponse)).toBe(
265
- "OUT",
266
- );
346
+ expect(sdk.getOperationType(address, mockTransaction)).toBe("OUT");
267
347
  });
268
348
 
269
349
  test("getOperationSenders should return sender address", () => {
@@ -278,36 +358,12 @@ describe("SDK Functions", () => {
278
358
  ).toEqual(["0x6e143fe0a8ca010a86580dafac44298e5b1b7d73efc345356a59a15f0d7824f0"]);
279
359
  });
280
360
 
281
- test("getOperationAmount should calculate amount correctly for SUI", () => {
282
- const address = "0x6e143fe0a8ca010a86580dafac44298e5b1b7d73efc345356a59a15f0d7824f0";
283
- expect(
284
- sdk.getOperationAmount(
285
- address,
286
- mockTransaction as SuiTransactionBlockResponse,
287
- sdk.DEFAULT_COIN_TYPE,
288
- ),
289
- ).toEqual(new BigNumber("9998990120"));
290
- });
291
-
292
- test("getOperationAmount should calculate amount correctly for tokens", () => {
293
- const address = "0x6e143fe0a8ca010a86580dafac44298e5b1b7d73efc345356a59a15f0d7824f0";
294
- expect(
295
- sdk.getOperationAmount(
296
- address,
297
- mockTransaction as SuiTransactionBlockResponse,
298
- "0x123::test::TOKEN",
299
- ),
300
- ).toEqual(new BigNumber("500000"));
301
- });
302
-
303
361
  test("getOperationFee should calculate fee correctly", () => {
304
- expect(sdk.getOperationFee(mockTransaction as SuiTransactionBlockResponse)).toEqual(
305
- new BigNumber(1009880),
306
- );
362
+ expect(sdk.getOperationFee(mockTransaction)).toEqual(new BigNumber(1009880));
307
363
  });
308
364
 
309
365
  test("getOperationDate should return correct date", () => {
310
- const date = sdk.getOperationDate(mockTransaction as SuiTransactionBlockResponse);
366
+ const date = sdk.getOperationDate(mockTransaction);
311
367
  expect(date).toBeDefined();
312
368
  expect(date).toBeInstanceOf(Date);
313
369
  });
@@ -477,7 +533,7 @@ describe("SDK Functions", () => {
477
533
  ],
478
534
  };
479
535
 
480
- const operation = sdk.transactionToOp(address, tokenTx as SuiTransactionBlockResponse);
536
+ const operation = sdk.alpacaTransactionToOp(address, tokenTx as SuiTransactionBlockResponse);
481
537
  expect(operation.id).toEqual("DhKLpX5kwuKuyRa71RGqpX5EY2M8Efw535ZVXYXsRiDt");
482
538
  expect(operation.type).toEqual("IN");
483
539
  expect(operation.senders).toEqual([
@@ -555,201 +611,89 @@ describe("Staking Operations", () => {
555
611
  describe("Operation Type Detection", () => {
556
612
  test("getOperationType should return DELEGATE for staking transaction", () => {
557
613
  const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
558
-
559
- // Create a mock staking transaction
560
- const mockStakingTx = {
561
- digest: "delegate_tx_digest_123",
562
- transaction: {
563
- data: {
564
- sender: address,
565
- transaction: {
566
- kind: "ProgrammableTransaction",
567
- inputs: [],
568
- transactions: [
569
- {
570
- MoveCall: {
571
- function: "request_add_stake",
572
- },
573
- },
574
- ],
575
- },
576
- },
577
- },
578
- effects: {
579
- status: { status: "success" },
580
- gasUsed: {
581
- computationCost: "1000000",
582
- storageCost: "500000",
583
- storageRebate: "450000",
584
- },
585
- },
586
- balanceChanges: [
587
- {
588
- owner: { AddressOwner: address },
589
- coinType: "0x2::sui::SUI",
590
- amount: "-1000000000",
591
- },
592
- ],
593
- timestampMs: "1742294454878",
594
- checkpoint: "313024",
595
- } as unknown as SuiTransactionBlockResponse;
596
-
597
- expect(sdk.getOperationType(address, mockStakingTx)).toBe("DELEGATE");
614
+ expect(sdk.getOperationType(address, mockStakingTx(address, "-1000000000"))).toBe("DELEGATE");
598
615
  });
599
616
 
600
617
  test("getOperationType should return UNDELEGATE for unstaking transaction", () => {
601
618
  const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
602
-
603
- // Create a mock unstaking transaction
604
- const mockUnstakingTx = {
605
- digest: "undelegate_tx_digest_456",
606
- transaction: {
607
- data: {
608
- sender: address,
609
- transaction: {
610
- kind: "ProgrammableTransaction",
611
- inputs: [],
612
- transactions: [
613
- {
614
- MoveCall: {
615
- function: "request_withdraw_stake",
616
- },
617
- },
618
- ],
619
- },
620
- },
621
- },
622
- effects: {
623
- status: { status: "success" },
624
- gasUsed: {
625
- computationCost: "1000000",
626
- storageCost: "500000",
627
- storageRebate: "450000",
628
- },
629
- },
630
- balanceChanges: [
631
- {
632
- owner: { AddressOwner: address },
633
- coinType: "0x2::sui::SUI",
634
- amount: "0",
635
- },
636
- ],
637
- timestampMs: "1742294454878",
638
- checkpoint: "313024",
639
- } as unknown as SuiTransactionBlockResponse;
640
-
641
- expect(sdk.getOperationType(address, mockUnstakingTx)).toBe("UNDELEGATE");
619
+ expect(sdk.getOperationType(address, mockUnstakingTx(address, "1000000000"))).toBe(
620
+ "UNDELEGATE",
621
+ );
642
622
  });
643
623
  });
644
624
 
645
625
  describe("Operation Amount Calculation", () => {
646
- test("getOperationAmount should calculate staking amount correctly", () => {
647
- const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
626
+ const address = "0x6e143fe0a8ca010a86580dafac44298e5b1b7d73efc345356a59a15f0d7824f0";
648
627
 
649
- const mockStakingTx = {
650
- transaction: {
651
- data: {
652
- transaction: {
653
- kind: "ProgrammableTransaction",
654
- transactions: [
655
- {
656
- MoveCall: {
657
- function: "request_add_stake",
658
- },
659
- },
660
- ],
661
- },
662
- },
663
- },
664
- balanceChanges: [
665
- {
666
- owner: { AddressOwner: address },
667
- coinType: "0x2::sui::SUI",
668
- amount: "-1000000000",
669
- },
670
- ],
671
- } as unknown as SuiTransactionBlockResponse;
628
+ function bridgeOperationAmount(
629
+ mock: SuiTransactionBlockResponse,
630
+ coinType: string = sdk.DEFAULT_COIN_TYPE,
631
+ ) {
632
+ return sdk.getOperationAmount(address, mock, coinType);
633
+ }
672
634
 
673
- const amount = sdk.getOperationAmount(address, mockStakingTx, sdk.DEFAULT_COIN_TYPE);
674
- expect(amount).toEqual(new BigNumber("1000000000")); // The function returns minus of the balance change
675
- });
635
+ test("getOperationAmount should calculate staking amount", () =>
636
+ expect(bridgeOperationAmount(mockStakingTx(address, "-1000000000"))).toEqual(
637
+ new BigNumber("1000000000"),
638
+ ));
676
639
 
677
- test("getOperationAmount should calculate unstaking amount correctly", () => {
678
- const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
640
+ test("getOperationAmount should calculate unstaking amount of 1000", () =>
641
+ expect(bridgeOperationAmount(mockUnstakingTx(address, "1000"))).toEqual(
642
+ new BigNumber("-1000"),
643
+ ));
679
644
 
680
- const mockUnstakingTx = {
681
- transaction: {
682
- data: {
683
- transaction: {
684
- kind: "ProgrammableTransaction",
685
- transactions: [
686
- {
687
- MoveCall: {
688
- function: "request_withdraw_stake",
689
- },
690
- },
691
- ],
692
- },
693
- },
694
- },
695
- balanceChanges: [
696
- {
697
- owner: { AddressOwner: address },
698
- coinType: "0x2::sui::SUI",
699
- amount: "0",
700
- },
701
- ],
702
- } as unknown as SuiTransactionBlockResponse;
645
+ test("getOperationAmount should calculate unstaking amount of 0", () =>
646
+ expect(bridgeOperationAmount(mockUnstakingTx(address, "0"))).toEqual(new BigNumber("0")));
703
647
 
704
- const amount = sdk.getOperationAmount(address, mockUnstakingTx, sdk.DEFAULT_COIN_TYPE);
705
- expect(amount).toEqual(new BigNumber("0"));
706
- });
648
+ test("getOperationAmount should calculate amount correctly for SUI", () =>
649
+ expect(bridgeOperationAmount(mockTransaction)).toEqual(new BigNumber("9998990120")));
650
+
651
+ test("getOperationAmount should calculate amount correctly for tokens", () =>
652
+ expect(bridgeOperationAmount(mockTransaction, "0x123::test::TOKEN")).toEqual(
653
+ new BigNumber("500000"),
654
+ ));
655
+
656
+ function alpacaOperationAmount(
657
+ mock: SuiTransactionBlockResponse,
658
+ coinType: string = sdk.DEFAULT_COIN_TYPE,
659
+ ) {
660
+ return sdk.alpacaGetOperationAmount(address, mock, coinType);
661
+ }
662
+
663
+ test("alpaca getOperationAmount should calculate staking amount", () =>
664
+ expect(alpacaOperationAmount(mockStakingTx(address, "-1000000000"))).toEqual(
665
+ new BigNumber("1000000000"),
666
+ ));
667
+
668
+ test("alpaca getOperationAmount should calculate unstaking amount of 1000", () =>
669
+ expect(alpacaOperationAmount(mockUnstakingTx(address, "1000"))).toEqual(
670
+ new BigNumber("1000"),
671
+ ));
672
+
673
+ test("alpaca getOperationAmount should calculate unstaking amount of 0", () =>
674
+ expect(alpacaOperationAmount(mockUnstakingTx(address, "0"))).toEqual(new BigNumber("0")));
675
+
676
+ test("alpaca getOperationAmount should calculate amount correctly for SUI", () =>
677
+ expect(alpacaOperationAmount(mockTransaction)).toEqual(new BigNumber("9998990120")));
678
+
679
+ test("alpaca getOperationAmount should calculate amount correctly for tokens", () =>
680
+ expect(alpacaOperationAmount(mockTransaction, "0x123::test::TOKEN")).toEqual(
681
+ new BigNumber("500000"),
682
+ ));
707
683
  });
708
684
 
709
685
  describe("Operation Recipients", () => {
710
686
  test("getOperationRecipients should return empty array for staking transaction", () => {
711
- const mockStakingTx = {
712
- transaction: {
713
- data: {
714
- transaction: {
715
- kind: "ProgrammableTransaction",
716
- inputs: [],
717
- transactions: [
718
- {
719
- MoveCall: {
720
- function: "request_add_stake",
721
- },
722
- },
723
- ],
724
- },
725
- },
726
- },
727
- } as unknown as SuiTransactionBlockResponse;
728
-
729
- const recipients = sdk.getOperationRecipients(mockStakingTx.transaction?.data);
687
+ const recipients = sdk.getOperationRecipients(
688
+ mockStakingTx("0xdeadbeef", "-1000000000").transaction?.data,
689
+ );
730
690
  expect(recipients).toEqual([]);
731
691
  });
732
692
 
733
693
  test("getOperationRecipients should return empty array for unstaking transaction", () => {
734
- const mockUnstakingTx = {
735
- transaction: {
736
- data: {
737
- transaction: {
738
- kind: "ProgrammableTransaction",
739
- inputs: [],
740
- transactions: [
741
- {
742
- MoveCall: {
743
- function: "request_withdraw_stake",
744
- },
745
- },
746
- ],
747
- },
748
- },
749
- },
750
- } as unknown as SuiTransactionBlockResponse;
751
-
752
- const recipients = sdk.getOperationRecipients(mockUnstakingTx.transaction?.data);
694
+ const recipients = sdk.getOperationRecipients(
695
+ mockUnstakingTx("0xdeadbeef", "1000000000").transaction?.data,
696
+ );
753
697
  expect(recipients).toEqual([]);
754
698
  });
755
699
  });
@@ -840,44 +784,11 @@ describe("Staking Operations", () => {
840
784
  const accountId = "mockAccountId";
841
785
  const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
842
786
 
843
- const mockStakingTx = {
844
- digest: "delegate_tx_digest_123",
845
- transaction: {
846
- data: {
847
- sender: address,
848
- transaction: {
849
- kind: "ProgrammableTransaction",
850
- inputs: [],
851
- transactions: [
852
- {
853
- MoveCall: {
854
- function: "request_add_stake",
855
- },
856
- },
857
- ],
858
- },
859
- },
860
- },
861
- effects: {
862
- status: { status: "success" },
863
- gasUsed: {
864
- computationCost: "1000000",
865
- storageCost: "500000",
866
- storageRebate: "450000",
867
- },
868
- },
869
- balanceChanges: [
870
- {
871
- owner: { AddressOwner: address },
872
- coinType: "0x2::sui::SUI",
873
- amount: "-1000000000",
874
- },
875
- ],
876
- timestampMs: "1742294454878",
877
- checkpoint: "313024",
878
- } as unknown as SuiTransactionBlockResponse;
879
-
880
- const operation = sdk.transactionToOperation(accountId, address, mockStakingTx);
787
+ const operation = sdk.transactionToOperation(
788
+ accountId,
789
+ address,
790
+ mockStakingTx(address, "-1000000000"),
791
+ );
881
792
 
882
793
  expect(operation).toHaveProperty("id");
883
794
  expect(operation).toHaveProperty("accountId", accountId);
@@ -893,45 +804,11 @@ describe("Staking Operations", () => {
893
804
  test("transactionToOperation should map unstaking transaction correctly", () => {
894
805
  const accountId = "mockAccountId";
895
806
  const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
896
-
897
- const mockUnstakingTx = {
898
- digest: "undelegate_tx_digest_456",
899
- transaction: {
900
- data: {
901
- sender: address,
902
- transaction: {
903
- kind: "ProgrammableTransaction",
904
- inputs: [],
905
- transactions: [
906
- {
907
- MoveCall: {
908
- function: "request_withdraw_stake",
909
- },
910
- },
911
- ],
912
- },
913
- },
914
- },
915
- effects: {
916
- status: { status: "success" },
917
- gasUsed: {
918
- computationCost: "1000000",
919
- storageCost: "500000",
920
- storageRebate: "450000",
921
- },
922
- },
923
- balanceChanges: [
924
- {
925
- owner: { AddressOwner: address },
926
- coinType: "0x2::sui::SUI",
927
- amount: "0",
928
- },
929
- ],
930
- timestampMs: "1742294454878",
931
- checkpoint: "313024",
932
- } as unknown as SuiTransactionBlockResponse;
933
-
934
- const operation = sdk.transactionToOperation(accountId, address, mockUnstakingTx);
807
+ const operation = sdk.transactionToOperation(
808
+ accountId,
809
+ address,
810
+ mockUnstakingTx(address, "1000000000"),
811
+ );
935
812
 
936
813
  expect(operation).toHaveProperty("id");
937
814
  expect(operation).toHaveProperty("accountId", accountId);
@@ -939,7 +816,7 @@ describe("Staking Operations", () => {
939
816
  expect(operation).toHaveProperty("hash", "undelegate_tx_digest_456");
940
817
  expect(operation).toHaveProperty("extra");
941
818
  expect((operation.extra as { coinType: string }).coinType).toBe(sdk.DEFAULT_COIN_TYPE);
942
- expect(operation.value).toEqual(new BigNumber("0"));
819
+ expect(operation.value).toEqual(new BigNumber("-1000000000"));
943
820
  expect(operation.recipients).toEqual([]);
944
821
  expect(operation.senders).toEqual([address]);
945
822
  });
@@ -947,44 +824,7 @@ describe("Staking Operations", () => {
947
824
  test("transactionToOp should map staking transaction correctly", () => {
948
825
  const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
949
826
 
950
- const mockStakingTx = {
951
- digest: "delegate_tx_digest_123",
952
- transaction: {
953
- data: {
954
- sender: address,
955
- transaction: {
956
- kind: "ProgrammableTransaction",
957
- inputs: [],
958
- transactions: [
959
- {
960
- MoveCall: {
961
- function: "request_add_stake",
962
- },
963
- },
964
- ],
965
- },
966
- },
967
- },
968
- effects: {
969
- status: { status: "success" },
970
- gasUsed: {
971
- computationCost: "1000000",
972
- storageCost: "500000",
973
- storageRebate: "450000",
974
- },
975
- },
976
- balanceChanges: [
977
- {
978
- owner: { AddressOwner: address },
979
- coinType: "0x2::sui::SUI",
980
- amount: "-1000000000",
981
- },
982
- ],
983
- timestampMs: "1742294454878",
984
- checkpoint: "313024",
985
- } as unknown as SuiTransactionBlockResponse;
986
-
987
- const operation = sdk.transactionToOp(address, mockStakingTx);
827
+ const operation = sdk.alpacaTransactionToOp(address, mockStakingTx(address, "-1000000000"));
988
828
 
989
829
  expect(operation.id).toEqual("delegate_tx_digest_123");
990
830
  expect(operation.type).toEqual("DELEGATE");
@@ -998,50 +838,13 @@ describe("Staking Operations", () => {
998
838
  test("transactionToOp should map unstaking transaction correctly", () => {
999
839
  const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
1000
840
 
1001
- const mockUnstakingTx = {
1002
- digest: "undelegate_tx_digest_456",
1003
- transaction: {
1004
- data: {
1005
- sender: address,
1006
- transaction: {
1007
- kind: "ProgrammableTransaction",
1008
- inputs: [],
1009
- transactions: [
1010
- {
1011
- MoveCall: {
1012
- function: "request_withdraw_stake",
1013
- },
1014
- },
1015
- ],
1016
- },
1017
- },
1018
- },
1019
- effects: {
1020
- status: { status: "success" },
1021
- gasUsed: {
1022
- computationCost: "1000000",
1023
- storageCost: "500000",
1024
- storageRebate: "450000",
1025
- },
1026
- },
1027
- balanceChanges: [
1028
- {
1029
- owner: { AddressOwner: address },
1030
- coinType: "0x2::sui::SUI",
1031
- amount: "0",
1032
- },
1033
- ],
1034
- timestampMs: "1742294454878",
1035
- checkpoint: "313024",
1036
- } as unknown as SuiTransactionBlockResponse;
1037
-
1038
- const operation = sdk.transactionToOp(address, mockUnstakingTx);
841
+ const operation = sdk.alpacaTransactionToOp(address, mockUnstakingTx(address, "1000000000"));
1039
842
 
1040
843
  expect(operation.id).toEqual("undelegate_tx_digest_456");
1041
844
  expect(operation.type).toEqual("UNDELEGATE");
1042
845
  expect(operation.senders).toEqual([address]);
1043
846
  expect(operation.recipients).toEqual([]);
1044
- expect(operation.value).toEqual(0n);
847
+ expect(operation.value).toEqual(1000000000n);
1045
848
  expect(operation.asset).toEqual({ type: "native" });
1046
849
  expect(operation.tx.block.hash).toBeUndefined();
1047
850
  });
@@ -1934,7 +1737,7 @@ describe("filterOperations", () => {
1934
1737
  describe("conversion methods", () => {
1935
1738
  test("toBlockOperation should map native transfers correctly", () => {
1936
1739
  expect(
1937
- sdk.toBlockOperation({
1740
+ sdk.toBlockOperation(mockTransaction, {
1938
1741
  owner: {
1939
1742
  AddressOwner: "0x65449f57946938c84c5127",
1940
1743
  },
@@ -1953,7 +1756,7 @@ describe("filterOperations", () => {
1953
1756
 
1954
1757
  test("toBlockOperation should ignore transfers from shared owner", () => {
1955
1758
  expect(
1956
- sdk.toBlockOperation({
1759
+ sdk.toBlockOperation(mockTransaction, {
1957
1760
  owner: {
1958
1761
  Shared: {
1959
1762
  initial_shared_version: "0",
@@ -1967,7 +1770,7 @@ describe("filterOperations", () => {
1967
1770
 
1968
1771
  test("toBlockOperation should ignore transfers from object owner", () => {
1969
1772
  expect(
1970
- sdk.toBlockOperation({
1773
+ sdk.toBlockOperation(mockTransaction, {
1971
1774
  owner: {
1972
1775
  ObjectOwner: "test",
1973
1776
  },
@@ -1979,7 +1782,7 @@ describe("filterOperations", () => {
1979
1782
 
1980
1783
  test("toBlockOperation should ignore transfers from immutable owner", () => {
1981
1784
  expect(
1982
- sdk.toBlockOperation({
1785
+ sdk.toBlockOperation(mockTransaction, {
1983
1786
  owner: "Immutable",
1984
1787
  coinType: sdk.DEFAULT_COIN_TYPE,
1985
1788
  amount: "-10000000000",
@@ -1989,7 +1792,7 @@ describe("filterOperations", () => {
1989
1792
 
1990
1793
  test("toBlockOperation should ignore transfers from consensus owner", () => {
1991
1794
  expect(
1992
- sdk.toBlockOperation({
1795
+ sdk.toBlockOperation(mockTransaction, {
1993
1796
  owner: {
1994
1797
  ConsensusAddressOwner: {
1995
1798
  owner: "test",
@@ -2004,7 +1807,7 @@ describe("filterOperations", () => {
2004
1807
 
2005
1808
  test("toBlockOperation should map token transfers correctly", () => {
2006
1809
  expect(
2007
- sdk.toBlockOperation({
1810
+ sdk.toBlockOperation(mockTransaction, {
2008
1811
  owner: {
2009
1812
  AddressOwner: "0x65449f57946938c84c5127",
2010
1813
  },
@@ -2024,6 +1827,44 @@ describe("filterOperations", () => {
2024
1827
  ]);
2025
1828
  });
2026
1829
 
1830
+ test("toBlockOperation should map staking operations correctly", () => {
1831
+ const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
1832
+ expect(
1833
+ sdk.toBlockOperation(mockStakingTx(address, "-1000000000"), {
1834
+ owner: { AddressOwner: address },
1835
+ coinType: sdk.DEFAULT_COIN_TYPE,
1836
+ amount: "-10000000000",
1837
+ }),
1838
+ ).toEqual([
1839
+ {
1840
+ type: "other",
1841
+ operationType: "DELEGATE",
1842
+ address: address,
1843
+ asset: { type: "native" },
1844
+ amount: 10000000000n,
1845
+ },
1846
+ ]);
1847
+ });
1848
+
1849
+ test("toBlockOperation should map unstaking operations correctly", () => {
1850
+ const address = "0x65449f57946938c84c512732f1d69405d1fce417d9c9894696ddf4522f479e24";
1851
+ expect(
1852
+ sdk.toBlockOperation(mockUnstakingTx(address, "1000000000"), {
1853
+ owner: { AddressOwner: address },
1854
+ coinType: sdk.DEFAULT_COIN_TYPE,
1855
+ amount: "10000000000",
1856
+ }),
1857
+ ).toEqual([
1858
+ {
1859
+ type: "other",
1860
+ operationType: "UNDELEGATE",
1861
+ address: address,
1862
+ asset: { type: "native" },
1863
+ amount: 10000000000n,
1864
+ },
1865
+ ]);
1866
+ });
1867
+
2027
1868
  test("toBlockInfo should map checkpoints correctly", () => {
2028
1869
  expect(
2029
1870
  sdk.toBlockInfo({