@ledgerhq/coin-aptos 1.9.6-next.0 → 1.10.0-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 (158) hide show
  1. package/CHANGELOG.md +13 -4
  2. package/lib/__tests__/api/index.test.js +61 -5
  3. package/lib/__tests__/api/index.test.js.map +1 -1
  4. package/lib/__tests__/bridge/buildTransaction.test.js +57 -2
  5. package/lib/__tests__/bridge/buildTransaction.test.js.map +1 -1
  6. package/lib/__tests__/bridge/getFeesForTransaction.test.js +109 -8
  7. package/lib/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
  8. package/lib/__tests__/bridge/getTransactionStatus.test.js +175 -37
  9. package/lib/__tests__/bridge/getTransactionStatus.test.js.map +1 -1
  10. package/lib/__tests__/bridge/logic.test.js +889 -115
  11. package/lib/__tests__/bridge/logic.test.js.map +1 -1
  12. package/lib/__tests__/bridge/signOperation.test.js +128 -2
  13. package/lib/__tests__/bridge/signOperation.test.js.map +1 -1
  14. package/lib/__tests__/bridge/synchronisation.test.js +1214 -67
  15. package/lib/__tests__/bridge/synchronisation.test.js.map +1 -1
  16. package/lib/api/graphql/queries.js +6 -6
  17. package/lib/api/graphql/types.d.ts +9 -9
  18. package/lib/api/graphql/types.d.ts.map +1 -1
  19. package/lib/api/index.d.ts +5 -2
  20. package/lib/api/index.d.ts.map +1 -1
  21. package/lib/api/index.js +30 -4
  22. package/lib/api/index.js.map +1 -1
  23. package/lib/bridge/bridge.fixture.d.ts +2 -0
  24. package/lib/bridge/bridge.fixture.d.ts.map +1 -1
  25. package/lib/bridge/bridge.fixture.js +85 -13
  26. package/lib/bridge/bridge.fixture.js.map +1 -1
  27. package/lib/bridge/buildTransaction.d.ts.map +1 -1
  28. package/lib/bridge/buildTransaction.js +29 -3
  29. package/lib/bridge/buildTransaction.js.map +1 -1
  30. package/lib/bridge/estimateMaxSpendable.js +1 -1
  31. package/lib/bridge/estimateMaxSpendable.js.map +1 -1
  32. package/lib/bridge/getFeesForTransaction.d.ts.map +1 -1
  33. package/lib/bridge/getFeesForTransaction.js +12 -7
  34. package/lib/bridge/getFeesForTransaction.js.map +1 -1
  35. package/lib/bridge/getTransactionStatus.d.ts.map +1 -1
  36. package/lib/bridge/getTransactionStatus.js +28 -14
  37. package/lib/bridge/getTransactionStatus.js.map +1 -1
  38. package/lib/bridge/logic.d.ts +13 -8
  39. package/lib/bridge/logic.d.ts.map +1 -1
  40. package/lib/bridge/logic.js +152 -54
  41. package/lib/bridge/logic.js.map +1 -1
  42. package/lib/bridge/prepareTransaction.d.ts.map +1 -1
  43. package/lib/bridge/prepareTransaction.js +2 -2
  44. package/lib/bridge/prepareTransaction.js.map +1 -1
  45. package/lib/bridge/signOperation.d.ts.map +1 -1
  46. package/lib/bridge/signOperation.js +17 -3
  47. package/lib/bridge/signOperation.js.map +1 -1
  48. package/lib/bridge/synchronisation.d.ts +15 -0
  49. package/lib/bridge/synchronisation.d.ts.map +1 -1
  50. package/lib/bridge/synchronisation.js +127 -4
  51. package/lib/bridge/synchronisation.js.map +1 -1
  52. package/lib/constants.d.ts +5 -1
  53. package/lib/constants.d.ts.map +1 -1
  54. package/lib/constants.js +6 -2
  55. package/lib/constants.js.map +1 -1
  56. package/lib/test/bot-specs.d.ts.map +1 -1
  57. package/lib/test/bot-specs.js +40 -1
  58. package/lib/test/bot-specs.js.map +1 -1
  59. package/lib/test/bridgeDatasetTest.d.ts.map +1 -1
  60. package/lib/test/bridgeDatasetTest.js +44 -62
  61. package/lib/test/bridgeDatasetTest.js.map +1 -1
  62. package/lib/test/speculos-deviceActions.d.ts +1 -0
  63. package/lib/test/speculos-deviceActions.d.ts.map +1 -1
  64. package/lib/test/speculos-deviceActions.js +37 -5
  65. package/lib/test/speculos-deviceActions.js.map +1 -1
  66. package/lib/types/index.d.ts +18 -7
  67. package/lib/types/index.d.ts.map +1 -1
  68. package/lib-es/__tests__/api/index.test.js +61 -5
  69. package/lib-es/__tests__/api/index.test.js.map +1 -1
  70. package/lib-es/__tests__/bridge/buildTransaction.test.js +58 -3
  71. package/lib-es/__tests__/bridge/buildTransaction.test.js.map +1 -1
  72. package/lib-es/__tests__/bridge/getFeesForTransaction.test.js +110 -9
  73. package/lib-es/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
  74. package/lib-es/__tests__/bridge/getTransactionStatus.test.js +177 -39
  75. package/lib-es/__tests__/bridge/getTransactionStatus.test.js.map +1 -1
  76. package/lib-es/__tests__/bridge/logic.test.js +891 -117
  77. package/lib-es/__tests__/bridge/logic.test.js.map +1 -1
  78. package/lib-es/__tests__/bridge/signOperation.test.js +128 -2
  79. package/lib-es/__tests__/bridge/signOperation.test.js.map +1 -1
  80. package/lib-es/__tests__/bridge/synchronisation.test.js +1213 -69
  81. package/lib-es/__tests__/bridge/synchronisation.test.js.map +1 -1
  82. package/lib-es/api/graphql/queries.js +6 -6
  83. package/lib-es/api/graphql/types.d.ts +9 -9
  84. package/lib-es/api/graphql/types.d.ts.map +1 -1
  85. package/lib-es/api/index.d.ts +5 -2
  86. package/lib-es/api/index.d.ts.map +1 -1
  87. package/lib-es/api/index.js +30 -4
  88. package/lib-es/api/index.js.map +1 -1
  89. package/lib-es/bridge/bridge.fixture.d.ts +2 -0
  90. package/lib-es/bridge/bridge.fixture.d.ts.map +1 -1
  91. package/lib-es/bridge/bridge.fixture.js +82 -12
  92. package/lib-es/bridge/bridge.fixture.js.map +1 -1
  93. package/lib-es/bridge/buildTransaction.d.ts.map +1 -1
  94. package/lib-es/bridge/buildTransaction.js +30 -4
  95. package/lib-es/bridge/buildTransaction.js.map +1 -1
  96. package/lib-es/bridge/estimateMaxSpendable.js +1 -1
  97. package/lib-es/bridge/estimateMaxSpendable.js.map +1 -1
  98. package/lib-es/bridge/getFeesForTransaction.d.ts.map +1 -1
  99. package/lib-es/bridge/getFeesForTransaction.js +13 -8
  100. package/lib-es/bridge/getFeesForTransaction.js.map +1 -1
  101. package/lib-es/bridge/getTransactionStatus.d.ts.map +1 -1
  102. package/lib-es/bridge/getTransactionStatus.js +28 -14
  103. package/lib-es/bridge/getTransactionStatus.js.map +1 -1
  104. package/lib-es/bridge/logic.d.ts +13 -8
  105. package/lib-es/bridge/logic.d.ts.map +1 -1
  106. package/lib-es/bridge/logic.js +146 -52
  107. package/lib-es/bridge/logic.js.map +1 -1
  108. package/lib-es/bridge/prepareTransaction.d.ts.map +1 -1
  109. package/lib-es/bridge/prepareTransaction.js +2 -2
  110. package/lib-es/bridge/prepareTransaction.js.map +1 -1
  111. package/lib-es/bridge/signOperation.d.ts.map +1 -1
  112. package/lib-es/bridge/signOperation.js +17 -3
  113. package/lib-es/bridge/signOperation.js.map +1 -1
  114. package/lib-es/bridge/synchronisation.d.ts +15 -0
  115. package/lib-es/bridge/synchronisation.d.ts.map +1 -1
  116. package/lib-es/bridge/synchronisation.js +123 -3
  117. package/lib-es/bridge/synchronisation.js.map +1 -1
  118. package/lib-es/constants.d.ts +5 -1
  119. package/lib-es/constants.d.ts.map +1 -1
  120. package/lib-es/constants.js +5 -1
  121. package/lib-es/constants.js.map +1 -1
  122. package/lib-es/test/bot-specs.d.ts.map +1 -1
  123. package/lib-es/test/bot-specs.js +41 -2
  124. package/lib-es/test/bot-specs.js.map +1 -1
  125. package/lib-es/test/bridgeDatasetTest.d.ts.map +1 -1
  126. package/lib-es/test/bridgeDatasetTest.js +44 -59
  127. package/lib-es/test/bridgeDatasetTest.js.map +1 -1
  128. package/lib-es/test/speculos-deviceActions.d.ts +1 -0
  129. package/lib-es/test/speculos-deviceActions.d.ts.map +1 -1
  130. package/lib-es/test/speculos-deviceActions.js +36 -4
  131. package/lib-es/test/speculos-deviceActions.js.map +1 -1
  132. package/lib-es/types/index.d.ts +18 -7
  133. package/lib-es/types/index.d.ts.map +1 -1
  134. package/package.json +9 -8
  135. package/src/__tests__/api/index.test.ts +75 -5
  136. package/src/__tests__/bridge/buildTransaction.test.ts +85 -3
  137. package/src/__tests__/bridge/getFeesForTransaction.test.ts +144 -9
  138. package/src/__tests__/bridge/getTransactionStatus.test.ts +217 -38
  139. package/src/__tests__/bridge/logic.test.ts +922 -118
  140. package/src/__tests__/bridge/signOperation.test.ts +141 -2
  141. package/src/__tests__/bridge/synchronisation.test.ts +1265 -71
  142. package/src/api/graphql/queries.ts +6 -6
  143. package/src/api/graphql/types.ts +9 -9
  144. package/src/api/index.ts +32 -5
  145. package/src/bridge/bridge.fixture.ts +91 -12
  146. package/src/bridge/buildTransaction.ts +39 -6
  147. package/src/bridge/estimateMaxSpendable.ts +1 -1
  148. package/src/bridge/getFeesForTransaction.ts +14 -9
  149. package/src/bridge/getTransactionStatus.ts +35 -13
  150. package/src/bridge/logic.ts +202 -63
  151. package/src/bridge/prepareTransaction.ts +4 -3
  152. package/src/bridge/signOperation.ts +19 -3
  153. package/src/bridge/synchronisation.ts +170 -3
  154. package/src/constants.ts +12 -1
  155. package/src/test/bot-specs.ts +63 -3
  156. package/src/test/bridgeDatasetTest.ts +46 -59
  157. package/src/test/speculos-deviceActions.ts +40 -4
  158. package/src/types/index.ts +15 -1
@@ -6,25 +6,35 @@ import {
6
6
  } from "@aptos-labs/ts-sdk";
7
7
  import type { Operation, OperationType } from "@ledgerhq/types-live";
8
8
  import BigNumber from "bignumber.js";
9
- import { APTOS_ASSET_ID, APTOS_COIN_CHANGE, DIRECTION } from "../../constants";
9
+ import {
10
+ APTOS_ASSET_ID,
11
+ APTOS_COIN_CHANGE,
12
+ APTOS_FUNGIBLE_STORE,
13
+ APTOS_OBJECT_CORE,
14
+ DIRECTION,
15
+ } from "../../constants";
10
16
  import {
11
17
  calculateAmount,
12
18
  compareAddress,
13
- getAptosAmounts,
19
+ getCoinAndAmounts,
14
20
  getFunctionAddress,
15
- isChangeOfAptos,
21
+ getResourceAddress,
16
22
  isTestnet,
17
23
  processRecipients,
18
24
  getMaxSendBalance,
19
25
  normalizeTransactionOptions,
20
26
  getBlankOperation,
21
27
  txsToOps,
28
+ getEventCoinAddress,
29
+ getEventFAAddress,
22
30
  } from "../../bridge/logic";
23
31
  import type { AptosTransaction, TransactionOptions } from "../../types";
32
+ import { createFixtureAccount, createFixtureTransaction } from "../../bridge/bridge.fixture";
33
+ import { findTokenByAddressInCurrency } from "@ledgerhq/cryptoassets";
34
+ import { decodeTokenAccountId, encodeTokenAccountId } from "@ledgerhq/coin-framework/account/index";
24
35
 
25
- jest.mock("@ledgerhq/cryptoassets", () => ({
26
- getCryptoCurrencyById: jest.fn(),
27
- }));
36
+ jest.mock("@ledgerhq/cryptoassets");
37
+ jest.mock("@ledgerhq/coin-framework/account/index");
28
38
 
29
39
  describe("Aptos logic ", () => {
30
40
  describe("isTestnet", () => {
@@ -40,41 +50,48 @@ describe("Aptos logic ", () => {
40
50
  describe("getMaxSendBalance", () => {
41
51
  it("should return the correct max send balance when amount is greater than total gas", () => {
42
52
  const amount = new BigNumber(1000000);
53
+ const account = createFixtureAccount({ balance: amount, spendableBalance: amount });
54
+ const transaction = createFixtureTransaction();
43
55
  const gas = new BigNumber(200);
44
56
  const gasPrice = new BigNumber(100);
45
- const result = getMaxSendBalance(amount, gas, gasPrice);
57
+ const result = getMaxSendBalance(gas, gasPrice, account, transaction);
46
58
  expect(result.isEqualTo(amount.minus(gas.multipliedBy(gasPrice)))).toBe(true);
47
59
  });
48
60
 
49
61
  it("should return zero when amount is less than total gas", () => {
50
- const amount = new BigNumber(1000);
62
+ const account = createFixtureAccount();
63
+ const transaction = createFixtureTransaction();
51
64
  const gas = new BigNumber(200);
52
65
  const gasPrice = new BigNumber(100);
53
- const result = getMaxSendBalance(amount, gas, gasPrice);
66
+ const result = getMaxSendBalance(gas, gasPrice, account, transaction);
54
67
  expect(result.isEqualTo(new BigNumber(0))).toBe(true);
55
68
  });
56
69
 
57
70
  it("should return zero when amount is equal to total gas", () => {
58
- const amount = new BigNumber(20000);
71
+ const account = createFixtureAccount();
72
+ const transaction = createFixtureTransaction();
59
73
  const gas = new BigNumber(200);
60
74
  const gasPrice = new BigNumber(100);
61
- const result = getMaxSendBalance(amount, gas, gasPrice);
75
+ const result = getMaxSendBalance(gas, gasPrice, account, transaction);
62
76
  expect(result.isEqualTo(new BigNumber(0))).toBe(true);
63
77
  });
64
78
 
65
79
  it("should handle zero amount", () => {
66
- const amount = new BigNumber(0);
80
+ const account = createFixtureAccount();
81
+ const transaction = createFixtureTransaction();
67
82
  const gas = new BigNumber(200);
68
83
  const gasPrice = new BigNumber(100);
69
- const result = getMaxSendBalance(amount, gas, gasPrice);
84
+ const result = getMaxSendBalance(gas, gasPrice, account, transaction);
70
85
  expect(result.isEqualTo(new BigNumber(0))).toBe(true);
71
86
  });
72
87
 
73
88
  it("should handle zero gas and gas price", () => {
74
89
  const amount = new BigNumber(1000000);
90
+ const account = createFixtureAccount({ balance: amount, spendableBalance: amount });
91
+ const transaction = createFixtureTransaction();
75
92
  const gas = new BigNumber(0);
76
93
  const gasPrice = new BigNumber(0);
77
- const result = getMaxSendBalance(amount, gas, gasPrice);
94
+ const result = getMaxSendBalance(gas, gasPrice, account, transaction);
78
95
  expect(result.isEqualTo(amount)).toBe(true);
79
96
  });
80
97
  });
@@ -114,7 +131,7 @@ describe("Aptos logic ", () => {
114
131
  version: "1",
115
132
  } as unknown as AptosTransaction;
116
133
 
117
- const id = "test-id";
134
+ const id = "test_id";
118
135
  const result = getBlankOperation(tx, id);
119
136
 
120
137
  expect(result).toEqual({
@@ -142,7 +159,7 @@ describe("Aptos logic ", () => {
142
159
  sequence_number: "1",
143
160
  } as unknown as AptosTransaction;
144
161
 
145
- const id = "test-id";
162
+ const id = "test_id";
146
163
  const result = getBlankOperation(tx, id);
147
164
 
148
165
  expect(result).toEqual({
@@ -292,10 +309,21 @@ describe("Aptos sync logic ", () => {
292
309
  processRecipients(payload, "0x11", op, "0x2");
293
310
  expect(op.recipients).toContain("0x2");
294
311
  });
312
+
313
+ it("should add recipient for fungible assets transfer-like functions", () => {
314
+ const payload: InputEntryFunctionData = {
315
+ function: "0x1::primary_fungible_store::transfer",
316
+ typeArguments: [],
317
+ functionArguments: [["0xfff"], "0x13"],
318
+ };
319
+
320
+ processRecipients(payload, "0x13", op, "0x1");
321
+ expect(op.recipients).toContain("0x13");
322
+ });
295
323
  });
296
324
 
297
- describe("isChangeOfAptos", () => {
298
- it("should return true for a valid change of Aptos", () => {
325
+ describe("getResourceAddress", () => {
326
+ it("should return coin name from the change", () => {
299
327
  const change = {
300
328
  type: "write_resource",
301
329
  data: {
@@ -313,6 +341,15 @@ describe("Aptos sync logic ", () => {
313
341
  },
314
342
  } as unknown as WriteSetChange;
315
343
 
344
+ const tx: AptosTransaction = {
345
+ hash: "0x123",
346
+ block: { hash: "0xabc", height: 1 },
347
+ timestamp: "1000000",
348
+ sequence_number: "1",
349
+ version: "1",
350
+ changes: [change],
351
+ } as unknown as AptosTransaction;
352
+
316
353
  const event = {
317
354
  guid: {
318
355
  account_address: "0x11",
@@ -321,11 +358,11 @@ describe("Aptos sync logic ", () => {
321
358
  type: "0x1::coin::WithdrawEvent",
322
359
  } as Event;
323
360
 
324
- const result = isChangeOfAptos(change, event, "withdraw_events");
325
- expect(result).toBe(true);
361
+ const result = getResourceAddress(tx, event, "withdraw_events", getEventCoinAddress);
362
+ expect(result).toEqual(APTOS_ASSET_ID);
326
363
  });
327
364
 
328
- it("should return false for an invalid change of Aptos", () => {
365
+ it("should return null for not finding the valid coin in change", () => {
329
366
  const change = {
330
367
  type: "write_resource",
331
368
  data: {
@@ -343,6 +380,15 @@ describe("Aptos sync logic ", () => {
343
380
  },
344
381
  } as unknown as WriteSetChange;
345
382
 
383
+ const tx: AptosTransaction = {
384
+ hash: "0x123",
385
+ block: { hash: "0xabc", height: 1 },
386
+ timestamp: "1000000",
387
+ sequence_number: "1",
388
+ version: "1",
389
+ changes: [change],
390
+ } as unknown as AptosTransaction;
391
+
346
392
  const event = {
347
393
  guid: {
348
394
  account_address: "0x11",
@@ -351,107 +397,169 @@ describe("Aptos sync logic ", () => {
351
397
  type: "0x1::coin::WithdrawEvent",
352
398
  } as Event;
353
399
 
354
- const result = isChangeOfAptos(change, event, "withdraw_events");
355
- expect(result).toBe(false);
400
+ const result = getResourceAddress(tx, event, "withdraw_events", getEventCoinAddress);
401
+ expect(result).toBe(null);
356
402
  });
357
403
 
358
- it("should return false for a change with a different WriteSet type", () => {
404
+ it("should return null for not finding the event name in change", () => {
359
405
  const change = {
360
- type: "write_module",
361
- data: {},
406
+ type: "write_resource",
407
+ data: {
408
+ type: APTOS_COIN_CHANGE,
409
+ data: {
410
+ other_events: {
411
+ guid: {
412
+ id: {
413
+ addr: "0x12",
414
+ creation_num: "2",
415
+ },
416
+ },
417
+ },
418
+ },
419
+ },
362
420
  } as unknown as WriteSetChange;
363
421
 
422
+ const tx: AptosTransaction = {
423
+ hash: "0x123",
424
+ block: { hash: "0xabc", height: 1 },
425
+ timestamp: "1000000",
426
+ sequence_number: "1",
427
+ version: "1",
428
+ changes: [change],
429
+ } as unknown as AptosTransaction;
430
+
364
431
  const event = {
365
432
  guid: {
366
- account_address: "0x1",
433
+ account_address: "0x11",
367
434
  creation_number: "1",
368
435
  },
369
436
  type: "0x1::coin::WithdrawEvent",
370
437
  } as Event;
371
438
 
372
- const result = isChangeOfAptos(change, event, "withdraw_events");
373
- expect(result).toBe(false);
439
+ const result = getResourceAddress(tx, event, "withdraw_events", getEventCoinAddress);
440
+ expect(result).toBe(null);
374
441
  });
375
442
 
376
- it("should return false if no data in WriteSet Change", () => {
443
+ it("should return fungible asset address", () => {
377
444
  const change = {
378
445
  type: "write_resource",
446
+ address: "0xsomeaddress",
447
+ data: {
448
+ type: APTOS_FUNGIBLE_STORE,
449
+ data: {
450
+ metadata: {
451
+ inner: "0xassetaddress",
452
+ },
453
+ },
454
+ },
379
455
  } as unknown as WriteSetChange;
380
456
 
457
+ const tx: AptosTransaction = {
458
+ hash: "0x123",
459
+ block: { hash: "0xabc", height: 1 },
460
+ timestamp: "1000000",
461
+ sequence_number: "1",
462
+ version: "1",
463
+ changes: [change],
464
+ } as unknown as AptosTransaction;
465
+
381
466
  const event = {
382
467
  guid: {
383
- account_address: "0x11",
384
- creation_number: "2",
468
+ account_address: "0x0",
469
+ creation_number: "0",
470
+ },
471
+ type: "0x1::fungible_asset::Deposit",
472
+ data: {
473
+ amount: "100",
474
+ store: "0xsomeaddress",
385
475
  },
386
- type: "0x1::coin::WithdrawEvent",
387
476
  } as Event;
388
477
 
389
- const result = isChangeOfAptos(change, event, "withdraw_events");
390
- expect(result).toBe(false);
478
+ const result = getResourceAddress(tx, event, "withdraw_events", getEventFAAddress);
479
+ expect(result).toEqual("0xassetaddress");
391
480
  });
392
481
 
393
- it("should return false if no type in change data", () => {
482
+ it("should return null address instead of fungible asset when wrong type", () => {
394
483
  const change = {
395
484
  type: "write_resource",
485
+ address: "0xsomeaddress",
396
486
  data: {
487
+ type: APTOS_COIN_CHANGE,
397
488
  data: {
398
- withdraw_events: {
399
- guid: {
400
- id: {
401
- addr: "0x11",
402
- creation_num: "2",
403
- },
404
- },
489
+ metadata: {
490
+ inner: "0xassetaddress",
405
491
  },
406
492
  },
407
493
  },
408
494
  } as unknown as WriteSetChange;
409
495
 
496
+ const tx: AptosTransaction = {
497
+ hash: "0x123",
498
+ block: { hash: "0xabc", height: 1 },
499
+ timestamp: "1000000",
500
+ sequence_number: "1",
501
+ version: "1",
502
+ changes: [change],
503
+ } as unknown as AptosTransaction;
504
+
410
505
  const event = {
411
506
  guid: {
412
- account_address: "0x11",
413
- creation_number: "2",
507
+ account_address: "0x0",
508
+ creation_number: "0",
509
+ },
510
+ type: "0x1::fungible_asset::Deposit",
511
+ data: {
512
+ amount: "100",
513
+ store: "0xsomeaddress",
414
514
  },
415
- type: "0x1::coin::WithdrawEvent",
416
515
  } as Event;
417
516
 
418
- const result = isChangeOfAptos(change, event, "withdraw_events");
419
- expect(result).toBe(false);
517
+ const result = getResourceAddress(tx, event, "withdraw_events", getEventFAAddress);
518
+ expect(result).toEqual(null);
420
519
  });
421
520
 
422
- it("should return false for a change with a different WriteSet Change type", () => {
521
+ it("should return null address instead of fungible asset when wrong event address", () => {
423
522
  const change = {
424
523
  type: "write_resource",
524
+ address: "0xsomeaddress",
425
525
  data: {
426
- type: "0x1::coin::CoinStore<0x1::aptos_coin::ANY_OTHER_COIN>",
526
+ type: APTOS_FUNGIBLE_STORE,
427
527
  data: {
428
- withdraw_events: {
429
- guid: {
430
- id: {
431
- addr: "0x11",
432
- creation_num: "2",
433
- },
434
- },
528
+ metadata: {
529
+ inner: "0xassetaddress",
435
530
  },
436
531
  },
437
532
  },
438
533
  } as unknown as WriteSetChange;
439
534
 
535
+ const tx: AptosTransaction = {
536
+ hash: "0x123",
537
+ block: { hash: "0xabc", height: 1 },
538
+ timestamp: "1000000",
539
+ sequence_number: "1",
540
+ version: "1",
541
+ changes: [change],
542
+ } as unknown as AptosTransaction;
543
+
440
544
  const event = {
441
545
  guid: {
442
- account_address: "0x11",
443
- creation_number: "2",
546
+ account_address: "0x0",
547
+ creation_number: "0",
548
+ },
549
+ type: "0x1::fungible_asset::Deposit",
550
+ data: {
551
+ amount: "100",
552
+ store: "0xwrongaddress",
444
553
  },
445
- type: "0x1::coin::WithdrawEvent",
446
554
  } as Event;
447
555
 
448
- const result = isChangeOfAptos(change, event, "withdraw_events");
449
- expect(result).toBe(false);
556
+ const result = getResourceAddress(tx, event, "withdraw_events", getEventFAAddress);
557
+ expect(result).toEqual(null);
450
558
  });
451
559
  });
452
560
 
453
- describe("getAptosAmounts", () => {
454
- it("should calculate the correct amounts for withdraw and deposit events", () => {
561
+ describe("getCoinAndAmounts", () => {
562
+ it("should calculate the correct legacy coins amounts for withdraw and deposit events", () => {
455
563
  const tx = {
456
564
  events: [
457
565
  {
@@ -504,37 +612,78 @@ describe("Aptos sync logic ", () => {
504
612
  } as unknown as AptosTransaction;
505
613
 
506
614
  const address = "0x11";
507
- const result = getAptosAmounts(tx, address);
615
+ const result = getCoinAndAmounts(tx, address);
508
616
 
509
617
  expect(result.amount_in).toEqual(new BigNumber(50));
510
618
  expect(result.amount_out).toEqual(new BigNumber(100));
619
+ expect(result.coin_id).toEqual(APTOS_ASSET_ID);
511
620
  });
512
621
 
513
- it("should return zero amounts if no matching events are found", () => {
622
+ it("should calculate the correct fungible asset amounts for withdraw and deposit events", () => {
514
623
  const tx = {
515
624
  events: [
516
625
  {
517
- type: "0x1::coin::WithdrawEvent",
626
+ type: "0x1::fungible_asset::Withdraw",
518
627
  guid: {
519
628
  account_address: "0x11",
520
629
  creation_number: "1",
521
630
  },
522
631
  data: {
523
632
  amount: "100",
633
+ store: "0x22",
524
634
  },
525
635
  },
526
636
  {
527
- type: "0x1::coin::DepositEvent",
637
+ type: "0x1::fungible_asset::Deposit",
528
638
  guid: {
529
639
  account_address: "0x11",
530
640
  creation_number: "2",
531
641
  },
532
642
  data: {
533
643
  amount: "50",
644
+ store: "0x33",
534
645
  },
535
646
  },
536
647
  ],
537
648
  changes: [
649
+ {
650
+ type: "write_resource",
651
+ address: "0x22",
652
+ data: {
653
+ type: APTOS_FUNGIBLE_STORE,
654
+ data: {
655
+ metadata: {
656
+ inner: "0x44",
657
+ },
658
+ transfer_events: {
659
+ guid: {
660
+ id: {
661
+ addr: "0x11",
662
+ creation_num: "2",
663
+ },
664
+ },
665
+ },
666
+ },
667
+ },
668
+ },
669
+ {
670
+ type: "write_resource",
671
+ address: "0x22",
672
+ data: {
673
+ type: APTOS_OBJECT_CORE,
674
+ data: {
675
+ owner: "0x11",
676
+ transfer_events: {
677
+ guid: {
678
+ id: {
679
+ addr: "0x22",
680
+ creation_num: "2",
681
+ },
682
+ },
683
+ },
684
+ },
685
+ },
686
+ },
538
687
  {
539
688
  type: "write_resource",
540
689
  data: {
@@ -543,7 +692,7 @@ describe("Aptos sync logic ", () => {
543
692
  withdraw_events: {
544
693
  guid: {
545
694
  id: {
546
- addr: "0x12", // should fail by address check
695
+ addr: "0x11",
547
696
  creation_num: "1",
548
697
  },
549
698
  },
@@ -552,7 +701,7 @@ describe("Aptos sync logic ", () => {
552
701
  guid: {
553
702
  id: {
554
703
  addr: "0x11",
555
- creation_num: "3", // should fail by number check
704
+ creation_num: "2",
556
705
  },
557
706
  },
558
707
  },
@@ -563,10 +712,11 @@ describe("Aptos sync logic ", () => {
563
712
  } as unknown as AptosTransaction;
564
713
 
565
714
  const address = "0x11";
566
- const result = getAptosAmounts(tx, address);
715
+ const result = getCoinAndAmounts(tx, address);
567
716
 
568
717
  expect(result.amount_in).toEqual(new BigNumber(0));
569
- expect(result.amount_out).toEqual(new BigNumber(0));
718
+ expect(result.amount_out).toEqual(new BigNumber(100));
719
+ expect(result.coin_id).toEqual("0x44");
570
720
  });
571
721
 
572
722
  it("should handle transactions with other events", () => {
@@ -586,10 +736,11 @@ describe("Aptos sync logic ", () => {
586
736
  } as unknown as AptosTransaction;
587
737
 
588
738
  const address = "0x1";
589
- const result = getAptosAmounts(tx, address);
739
+ const result = getCoinAndAmounts(tx, address);
590
740
 
591
741
  expect(result.amount_in).toEqual(new BigNumber(0));
592
742
  expect(result.amount_out).toEqual(new BigNumber(0));
743
+ expect(result.coin_id).toEqual(null);
593
744
  });
594
745
  });
595
746
 
@@ -597,24 +748,22 @@ describe("Aptos sync logic ", () => {
597
748
  it("should calculate the correct amount when the address is the sender", () => {
598
749
  const address = "0x11";
599
750
  const sender = "0x11";
600
- const fee = new BigNumber(10); // account pays fees
601
751
  const amount_in = new BigNumber(50);
602
752
  const amount_out = new BigNumber(100);
603
753
 
604
- const result = calculateAmount(sender, address, fee, amount_in, amount_out);
754
+ const result = calculateAmount(sender, address, amount_in, amount_out);
605
755
 
606
756
  // LL negates the amount for SEND transactions during output
607
- expect(result).toEqual(new BigNumber(60)); // -(50 - 100 - 10)
757
+ expect(result).toEqual(new BigNumber(50)); // -(50 - 100 - 10)
608
758
  });
609
759
 
610
760
  it("should calculate the correct amount when the address is not the sender", () => {
611
761
  const address = "0x11";
612
762
  const sender = "0x12";
613
- const fee = new BigNumber(10); // sender pays fees
614
763
  const amount_in = new BigNumber(100);
615
764
  const amount_out = new BigNumber(50);
616
765
 
617
- const result = calculateAmount(sender, address, fee, amount_in, amount_out);
766
+ const result = calculateAmount(sender, address, amount_in, amount_out);
618
767
 
619
768
  expect(result).toEqual(new BigNumber(50)); // 100 - 50
620
769
  });
@@ -622,34 +771,32 @@ describe("Aptos sync logic ", () => {
622
771
  it("should handle transactions with zero amounts", () => {
623
772
  const address = "0x11";
624
773
  const sender = "0x11";
625
- const fee = new BigNumber(10);
626
774
  const amount_in = new BigNumber(0);
627
775
  const amount_out = new BigNumber(0);
628
776
 
629
- const result = calculateAmount(sender, address, fee, amount_in, amount_out);
777
+ const result = calculateAmount(sender, address, amount_in, amount_out);
630
778
 
631
779
  // LL negates the amount for SEND transactions during output
632
- expect(result).toEqual(new BigNumber(10)); // -(0 - 0 - 10)
780
+ expect(result).toEqual(new BigNumber(0)); // -(0 - 0 - 10)
633
781
  });
634
782
 
635
783
  it("should get negative numbers (for send tx with deposit to account)", () => {
636
784
  const address = "0x11";
637
785
  const sender = "0x11";
638
- const fee = new BigNumber(10);
639
786
  const amount_in = new BigNumber(100);
640
787
  const amount_out = new BigNumber(0);
641
788
 
642
- const result = calculateAmount(sender, address, fee, amount_in, amount_out);
789
+ const result = calculateAmount(sender, address, amount_in, amount_out);
643
790
 
644
791
  // LL negates the amount for SEND transactions during output
645
- expect(result).toEqual(new BigNumber(90).negated()); // 100 - 10
792
+ expect(result).toEqual(new BigNumber(100).negated()); // 100 - 10
646
793
  });
647
794
  });
648
795
 
649
796
  describe("txsToOps", () => {
650
- it("should convert transactions to operations correctly", () => {
797
+ it("should convert Aptos transactions to operations correctly", () => {
651
798
  const address = "0x11";
652
- const id = "test-id";
799
+ const id = "test_id";
653
800
  const txs: AptosTransaction[] = [
654
801
  {
655
802
  hash: "0x123",
@@ -717,14 +864,14 @@ describe("Aptos sync logic ", () => {
717
864
  } as unknown as AptosTransaction,
718
865
  ];
719
866
 
720
- const result = txsToOps({ address }, id, txs);
867
+ const [result] = txsToOps({ address }, id, txs);
721
868
 
722
869
  expect(result).toHaveLength(1);
723
870
  expect(result[0]).toEqual({
724
871
  id: expect.any(String),
725
872
  hash: "0x123",
726
873
  type: DIRECTION.OUT,
727
- value: new BigNumber(20100),
874
+ value: new BigNumber(100),
728
875
  fee: new BigNumber(20000),
729
876
  blockHash: "0xabc",
730
877
  blockHeight: 1,
@@ -740,7 +887,7 @@ describe("Aptos sync logic ", () => {
740
887
 
741
888
  it("should skip transactions without functions in payload", () => {
742
889
  const address = "0x11";
743
- const id = "test-id";
890
+ const id = "test_id";
744
891
  const txs: AptosTransaction[] = [
745
892
  {
746
893
  hash: "0x123",
@@ -749,12 +896,6 @@ describe("Aptos sync logic ", () => {
749
896
  gas_unit_price: "100",
750
897
  success: true,
751
898
  payload: {} as EntryFunctionPayloadResponse,
752
- // payload: {
753
- // type: "entry_function_payload",
754
- // function: "0x1::coin::transfer",
755
- // type_arguments: [],
756
- // arguments: ["0x12", 100],
757
- // } as EntryFunctionPayloadResponse,
758
899
  events: [],
759
900
  changes: [],
760
901
  block: { hash: "0xabc", height: 1 },
@@ -763,14 +904,14 @@ describe("Aptos sync logic ", () => {
763
904
  } as unknown as AptosTransaction,
764
905
  ];
765
906
 
766
- const result = txsToOps({ address }, id, txs);
907
+ const [result] = txsToOps({ address }, id, txs);
767
908
 
768
909
  expect(result).toHaveLength(0);
769
910
  });
770
911
 
771
912
  it("should skip transactions that result in no Aptos change", () => {
772
913
  const address = "0x11";
773
- const id = "test-id";
914
+ const id = "test_id";
774
915
  const txs: AptosTransaction[] = [
775
916
  {
776
917
  hash: "0x123",
@@ -792,73 +933,719 @@ describe("Aptos sync logic ", () => {
792
933
  } as unknown as AptosTransaction,
793
934
  ];
794
935
 
795
- const result = txsToOps({ address }, id, txs);
936
+ const [result] = txsToOps({ address }, id, txs);
796
937
 
797
938
  expect(result).toHaveLength(0);
798
939
  });
799
940
 
800
941
  it("should handle failed transactions", () => {
801
- const address = "0x11";
802
- const id = "test-id";
942
+ const address = "0xa0d8";
943
+ const id = "test_id";
803
944
  const txs: AptosTransaction[] = [
804
945
  {
805
- hash: "0x123",
806
- sender: "0x11",
946
+ hash: "0x0189",
947
+ sender: "0xa0d8",
807
948
  gas_used: "200",
808
949
  gas_unit_price: "100",
809
950
  success: false,
810
951
  payload: {
811
- type: "entry_function_payload",
812
952
  function: "0x1::coin::transfer",
813
- type_arguments: [],
814
- arguments: ["0x12", 100],
953
+ type_arguments: ["0xd111::staked_coin::StakedAptos"],
954
+ arguments: ["0x4e5e", "50000000"],
955
+ type: "entry_function_payload",
815
956
  } as EntryFunctionPayloadResponse,
816
957
  events: [
817
958
  {
818
- type: "0x1::coin::WithdrawEvent",
819
959
  guid: {
820
- account_address: "0x11",
821
- creation_number: "1",
960
+ creation_number: "0",
961
+ account_address: "0x0",
822
962
  },
963
+ sequence_number: "0",
964
+ type: "0x1::transaction_fee::FeeStatement",
823
965
  data: {
824
- amount: "100",
966
+ execution_gas_units: "5",
967
+ io_gas_units: "4",
968
+ storage_fee_octas: "0",
969
+ storage_fee_refund_octas: "0",
970
+ total_charge_gas_units: "8",
825
971
  },
826
972
  },
973
+ ],
974
+ changes: [
827
975
  {
976
+ address: "0xa0d8",
977
+ state_key_hash: "0x1709",
978
+ data: {
979
+ type: "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
980
+ data: {
981
+ coin: {
982
+ value: "573163341",
983
+ },
984
+ deposit_events: {
985
+ counter: "45",
986
+ guid: {
987
+ id: {
988
+ addr: "0xa0d8",
989
+ creation_num: "2",
990
+ },
991
+ },
992
+ },
993
+ frozen: false,
994
+ withdraw_events: {
995
+ counter: "82",
996
+ guid: {
997
+ id: {
998
+ addr: "0xa0d8",
999
+ creation_num: "3",
1000
+ },
1001
+ },
1002
+ },
1003
+ },
1004
+ },
1005
+ type: "write_resource",
1006
+ },
1007
+ {
1008
+ address: "0xa0d8",
1009
+ state_key_hash: "0x6f1e",
1010
+ data: {
1011
+ type: "0x1::account::Account",
1012
+ data: {
1013
+ authentication_key: "0xa0d8",
1014
+ coin_register_events: {
1015
+ counter: "5",
1016
+ guid: {
1017
+ id: {
1018
+ addr: "0xa0d8",
1019
+ creation_num: "0",
1020
+ },
1021
+ },
1022
+ },
1023
+ guid_creation_num: "12",
1024
+ key_rotation_events: {
1025
+ counter: "0",
1026
+ guid: {
1027
+ id: {
1028
+ addr: "0xa0d8",
1029
+ creation_num: "1",
1030
+ },
1031
+ },
1032
+ },
1033
+ rotation_capability_offer: {
1034
+ for: {
1035
+ vec: [],
1036
+ },
1037
+ },
1038
+ sequence_number: "83",
1039
+ signer_capability_offer: {
1040
+ for: {
1041
+ vec: [],
1042
+ },
1043
+ },
1044
+ },
1045
+ },
1046
+ type: "write_resource",
1047
+ },
1048
+ {
1049
+ state_key_hash: "0x6e4b",
1050
+ handle: "0x1b85",
1051
+ key: "0x0619",
1052
+ value: "0x72c5e483c25c96010000000000000000",
1053
+ data: null,
1054
+ type: "write_table_item",
1055
+ },
1056
+ ],
1057
+ block: {
1058
+ hash: "0xc496",
1059
+ height: 1,
1060
+ },
1061
+ timestamp: "1000000",
1062
+ sequence_number: "1",
1063
+ } as unknown as AptosTransaction,
1064
+ ];
1065
+
1066
+ const [result] = txsToOps({ address }, id, txs);
1067
+
1068
+ expect(result).toHaveLength(1);
1069
+ expect(result[0]).toEqual({
1070
+ id: expect.any(String),
1071
+ hash: "0x0189",
1072
+ type: DIRECTION.OUT,
1073
+ value: new BigNumber(20000),
1074
+ fee: new BigNumber(20000),
1075
+ blockHash: "0xc496",
1076
+ blockHeight: 1,
1077
+ senders: ["0xa0d8"],
1078
+ recipients: ["0x4e5e"],
1079
+ accountId: id,
1080
+ date: new Date(1000),
1081
+ extra: { version: undefined },
1082
+ transactionSequenceNumber: 1,
1083
+ hasFailed: true,
1084
+ });
1085
+ });
1086
+
1087
+ it("should convert Aptos token transactions to operations correctly", () => {
1088
+ (findTokenByAddressInCurrency as jest.Mock).mockReturnValue({
1089
+ type: "TokenCurrency",
1090
+ id: "aptos/coin/dstapt::staked_coin::stakedaptos",
1091
+ contractAddress: "0xd111::staked_coin::StakedAptos",
1092
+ parentCurrency: {
1093
+ type: "CryptoCurrency",
1094
+ id: "aptos",
1095
+ coinType: 637,
1096
+ name: "Aptos",
1097
+ managerAppName: "Aptos",
1098
+ ticker: "APT",
1099
+ scheme: "aptos",
1100
+ color: "#231F20",
1101
+ family: "aptos",
1102
+ units: [
1103
+ {
1104
+ name: "APT",
1105
+ code: "APT",
1106
+ magnitude: 8,
1107
+ },
1108
+ ],
1109
+ explorerViews: [
1110
+ {
1111
+ address: "https://explorer.aptoslabs.com/account/$address?network=mainnet",
1112
+ tx: "https://explorer.aptoslabs.com/txn/$hash?network=mainnet",
1113
+ },
1114
+ ],
1115
+ },
1116
+ name: "dstAPT",
1117
+ tokenType: "coin",
1118
+ ticker: "dstAPT",
1119
+ disableCountervalue: false,
1120
+ delisted: false,
1121
+ units: [
1122
+ {
1123
+ name: "dstAPT",
1124
+ code: "dstAPT",
1125
+ magnitude: 8,
1126
+ },
1127
+ ],
1128
+ });
1129
+
1130
+ jest.mock("../../bridge/logic", () => ({
1131
+ ...jest.requireActual("../../bridge/logic"),
1132
+ getResourceAddress: jest.fn().mockReturnValue("0xd111::staked_coin::StakedAptos"),
1133
+ }));
1134
+
1135
+ (decodeTokenAccountId as jest.Mock).mockReturnValue({
1136
+ accountId: "token_account_id",
1137
+ });
1138
+
1139
+ const address = "0xa0d";
1140
+ const id = "test_id";
1141
+ const txs: AptosTransaction[] = [
1142
+ {
1143
+ hash: "0x123",
1144
+ sender: address,
1145
+ gas_used: "200",
1146
+ gas_unit_price: "100",
1147
+ success: true,
1148
+ payload: {
1149
+ function: "0x1::aptos_account::transfer_coins",
1150
+ type_arguments: ["0xd111::staked_coin::StakedAptos"],
1151
+ arguments: ["0x4e5", "1500000"],
1152
+ type: "entry_function_payload",
1153
+ } as EntryFunctionPayloadResponse,
1154
+ events: [
1155
+ {
1156
+ guid: {
1157
+ creation_number: "11",
1158
+ account_address: "0xa0d",
1159
+ },
1160
+ sequence_number: "12",
1161
+ type: "0x1::coin::WithdrawEvent",
1162
+ data: {
1163
+ amount: "1500000",
1164
+ },
1165
+ },
1166
+ {
1167
+ guid: {
1168
+ creation_number: "4",
1169
+ account_address: "0x4e5",
1170
+ },
1171
+ sequence_number: "8",
828
1172
  type: "0x1::coin::DepositEvent",
1173
+ data: {
1174
+ amount: "1500000",
1175
+ },
1176
+ },
1177
+ {
829
1178
  guid: {
830
- account_address: "0x12",
831
- creation_number: "2",
1179
+ creation_number: "0",
1180
+ account_address: "0x0",
832
1181
  },
1182
+ sequence_number: "0",
1183
+ type: "0x1::transaction_fee::FeeStatement",
833
1184
  data: {
834
- amount: "100",
1185
+ execution_gas_units: "6",
1186
+ io_gas_units: "6",
1187
+ storage_fee_octas: "0",
1188
+ storage_fee_refund_octas: "0",
1189
+ total_charge_gas_units: "12",
835
1190
  },
836
1191
  },
837
1192
  ],
838
1193
  changes: [
839
1194
  {
1195
+ address: "0x4e5",
1196
+ state_key_hash: "0x3c0",
1197
+ data: {
1198
+ type: "0x1::coin::CoinStore<0xd111::staked_coin::StakedAptos>",
1199
+ data: {
1200
+ coin: {
1201
+ value: "4000000",
1202
+ },
1203
+ deposit_events: {
1204
+ counter: "9",
1205
+ guid: {
1206
+ id: {
1207
+ addr: "0x4e5",
1208
+ creation_num: "4",
1209
+ },
1210
+ },
1211
+ },
1212
+ frozen: false,
1213
+ withdraw_events: {
1214
+ counter: "6",
1215
+ guid: {
1216
+ id: {
1217
+ addr: "0x4e5",
1218
+ creation_num: "5",
1219
+ },
1220
+ },
1221
+ },
1222
+ },
1223
+ },
840
1224
  type: "write_resource",
1225
+ },
1226
+ {
1227
+ address: "0xa0d",
1228
+ state_key_hash: "0x1709",
841
1229
  data: {
842
- type: APTOS_COIN_CHANGE,
1230
+ type: "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
843
1231
  data: {
1232
+ coin: {
1233
+ value: "68254118",
1234
+ },
1235
+ deposit_events: {
1236
+ counter: "46",
1237
+ guid: {
1238
+ id: {
1239
+ addr: "0xa0d",
1240
+ creation_num: "2",
1241
+ },
1242
+ },
1243
+ },
1244
+ frozen: false,
844
1245
  withdraw_events: {
1246
+ counter: "89",
845
1247
  guid: {
846
1248
  id: {
847
- addr: "0x11",
1249
+ addr: "0xa0d",
1250
+ creation_num: "3",
1251
+ },
1252
+ },
1253
+ },
1254
+ },
1255
+ },
1256
+ type: "write_resource",
1257
+ },
1258
+ {
1259
+ address: "0xa0d",
1260
+ state_key_hash: "0x5520",
1261
+ data: {
1262
+ type: "0x1::coin::CoinStore<0xd111::staked_coin::StakedAptos>",
1263
+ data: {
1264
+ coin: {
1265
+ value: "1000000",
1266
+ },
1267
+ deposit_events: {
1268
+ counter: "7",
1269
+ guid: {
1270
+ id: {
1271
+ addr: "0xa0d",
1272
+ creation_num: "10",
1273
+ },
1274
+ },
1275
+ },
1276
+ frozen: false,
1277
+ withdraw_events: {
1278
+ counter: "13",
1279
+ guid: {
1280
+ id: {
1281
+ addr: "0xa0d",
1282
+ creation_num: "11",
1283
+ },
1284
+ },
1285
+ },
1286
+ },
1287
+ },
1288
+ type: "write_resource",
1289
+ },
1290
+ {
1291
+ address: "0xa0d",
1292
+ state_key_hash: "0x6f1e",
1293
+ data: {
1294
+ type: "0x1::account::Account",
1295
+ data: {
1296
+ authentication_key: "0xa0d",
1297
+ coin_register_events: {
1298
+ counter: "5",
1299
+ guid: {
1300
+ id: {
1301
+ addr: "0xa0d",
1302
+ creation_num: "0",
1303
+ },
1304
+ },
1305
+ },
1306
+ guid_creation_num: "12",
1307
+ key_rotation_events: {
1308
+ counter: "0",
1309
+ guid: {
1310
+ id: {
1311
+ addr: "0xa0d",
848
1312
  creation_num: "1",
849
1313
  },
850
1314
  },
851
1315
  },
1316
+ rotation_capability_offer: {
1317
+ for: {
1318
+ vec: [],
1319
+ },
1320
+ },
1321
+ sequence_number: "122",
1322
+ signer_capability_offer: {
1323
+ for: {
1324
+ vec: [],
1325
+ },
1326
+ },
1327
+ },
1328
+ },
1329
+ type: "write_resource",
1330
+ },
1331
+ {
1332
+ state_key_hash: "0x6e4b",
1333
+ handle: "0x1b85",
1334
+ key: "0x0619",
1335
+ value: "0x1ddaf8da3b1497010000000000000000",
1336
+ data: null,
1337
+ type: "write_table_item",
1338
+ },
1339
+ ],
1340
+ block: { hash: "0xabc", height: 1 },
1341
+ timestamp: "1000000",
1342
+ sequence_number: "1",
1343
+ } as unknown as AptosTransaction,
1344
+ ];
1345
+
1346
+ const [ops, tokenOps] = txsToOps({ address }, id, txs);
1347
+
1348
+ expect(ops).toHaveLength(1);
1349
+ expect(ops[0]).toEqual({
1350
+ id: expect.any(String),
1351
+ hash: "0x123",
1352
+ type: "FEES",
1353
+ value: new BigNumber(20000),
1354
+ fee: new BigNumber(20000),
1355
+ blockHash: "0xabc",
1356
+ blockHeight: 1,
1357
+ senders: ["0xa0d"],
1358
+ recipients: ["0x4e5"],
1359
+ accountId: "token_account_id",
1360
+ date: new Date(1000),
1361
+ extra: { version: undefined },
1362
+ transactionSequenceNumber: 1,
1363
+ hasFailed: false,
1364
+ });
1365
+
1366
+ expect(tokenOps).toHaveLength(1);
1367
+ expect(tokenOps[0]).toEqual({
1368
+ id: expect.any(String),
1369
+ hash: "0x123",
1370
+ type: DIRECTION.OUT,
1371
+ value: new BigNumber(1500000),
1372
+ fee: new BigNumber(20000),
1373
+ blockHash: "0xabc",
1374
+ blockHeight: 1,
1375
+ senders: ["0xa0d"],
1376
+ recipients: ["0x4e5"],
1377
+ date: new Date(1000),
1378
+ extra: { version: undefined },
1379
+ transactionSequenceNumber: 1,
1380
+ hasFailed: false,
1381
+ });
1382
+ });
1383
+
1384
+ it("should convert Aptos token transactions to operations correctly", () => {
1385
+ (findTokenByAddressInCurrency as jest.Mock).mockReturnValue({
1386
+ type: "TokenCurrency",
1387
+ id: "aptos/fungible_asset/cellana_0x2ebb2ccac5e027a87fa0e2e5f656a3a4238d6a48d93ec9b610d570fc0aa0df12",
1388
+ contractAddress: "0x2ebb",
1389
+ parentCurrency: {
1390
+ type: "CryptoCurrency",
1391
+ id: "aptos",
1392
+ coinType: 637,
1393
+ name: "Aptos",
1394
+ managerAppName: "Aptos",
1395
+ ticker: "APT",
1396
+ scheme: "aptos",
1397
+ color: "#231F20",
1398
+ family: "aptos",
1399
+ units: [
1400
+ {
1401
+ name: "APT",
1402
+ code: "APT",
1403
+ magnitude: 8,
1404
+ },
1405
+ ],
1406
+ explorerViews: [
1407
+ {
1408
+ address: "https://explorer.aptoslabs.com/account/$address?network=mainnet",
1409
+ tx: "https://explorer.aptoslabs.com/txn/$hash?network=mainnet",
1410
+ },
1411
+ ],
1412
+ },
1413
+ name: "CELLANA",
1414
+ tokenType: "fungible_asset",
1415
+ ticker: "CELL",
1416
+ disableCountervalue: false,
1417
+ delisted: false,
1418
+ units: [
1419
+ {
1420
+ name: "CELLANA",
1421
+ code: "CELL",
1422
+ magnitude: 8,
1423
+ },
1424
+ ],
1425
+ });
1426
+
1427
+ jest.mock("../../bridge/logic", () => ({
1428
+ ...jest.requireActual("../../bridge/logic"),
1429
+ getResourceAddress: jest.fn().mockReturnValue("0x2ebb"),
1430
+ }));
1431
+
1432
+ (encodeTokenAccountId as jest.Mock).mockReturnValue("token_account_id");
1433
+
1434
+ const txs: AptosTransaction[] = [
1435
+ {
1436
+ hash: "0x10c9",
1437
+ sender: "0xa0d8",
1438
+ gas_used: "200",
1439
+ gas_unit_price: "100",
1440
+ success: true,
1441
+ payload: {
1442
+ function: "0x1::primary_fungible_store::transfer",
1443
+ type_arguments: ["0x1::fungible_asset::Metadata"],
1444
+ arguments: [
1445
+ {
1446
+ inner: "0x2ebb",
1447
+ },
1448
+ "0x6b8c",
1449
+ "193",
1450
+ ],
1451
+ type: "entry_function_payload",
1452
+ } as EntryFunctionPayloadResponse,
1453
+ events: [
1454
+ {
1455
+ guid: {
1456
+ creation_number: "0",
1457
+ account_address: "0x0",
1458
+ },
1459
+ sequence_number: "0",
1460
+ type: "0x1::fungible_asset::Withdraw",
1461
+ data: {
1462
+ amount: "193",
1463
+ store: "0xd475",
1464
+ },
1465
+ },
1466
+ {
1467
+ guid: {
1468
+ creation_number: "0",
1469
+ account_address: "0x0",
1470
+ },
1471
+ sequence_number: "0",
1472
+ type: "0x1::fungible_asset::Deposit",
1473
+ data: {
1474
+ amount: "193",
1475
+ store: "0xaaa9",
1476
+ },
1477
+ },
1478
+ {
1479
+ guid: {
1480
+ creation_number: "0",
1481
+ account_address: "0x0",
1482
+ },
1483
+ sequence_number: "0",
1484
+ type: "0x1::transaction_fee::FeeStatement",
1485
+ data: {
1486
+ execution_gas_units: "4",
1487
+ io_gas_units: "6",
1488
+ storage_fee_octas: "0",
1489
+ storage_fee_refund_octas: "0",
1490
+ total_charge_gas_units: "10",
1491
+ },
1492
+ },
1493
+ ],
1494
+ changes: [
1495
+ {
1496
+ address: "0xaaa9",
1497
+ state_key_hash: "0x9a17",
1498
+ data: {
1499
+ type: "0x1::fungible_asset::FungibleStore",
1500
+ data: {
1501
+ balance: "10044959",
1502
+ frozen: false,
1503
+ metadata: {
1504
+ inner: "0x2ebb",
1505
+ },
1506
+ },
1507
+ },
1508
+ type: "write_resource",
1509
+ },
1510
+ {
1511
+ address: "0xaaa9",
1512
+ state_key_hash: "0x9a17",
1513
+ data: {
1514
+ type: "0x1::object::ObjectCore",
1515
+ data: {
1516
+ allow_ungated_transfer: false,
1517
+ guid_creation_num: "1125899906842625",
1518
+ owner: "0x6b8c",
1519
+ transfer_events: {
1520
+ counter: "0",
1521
+ guid: {
1522
+ id: {
1523
+ addr: "0xaaa9",
1524
+ creation_num: "1125899906842624",
1525
+ },
1526
+ },
1527
+ },
1528
+ },
1529
+ },
1530
+ type: "write_resource",
1531
+ },
1532
+ {
1533
+ address: "0xa0d8",
1534
+ state_key_hash: "0x1709",
1535
+ data: {
1536
+ type: "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
1537
+ data: {
1538
+ coin: {
1539
+ value: "98423118",
1540
+ },
852
1541
  deposit_events: {
1542
+ counter: "46",
853
1543
  guid: {
854
1544
  id: {
855
- addr: "0x12",
1545
+ addr: "0xa0d8",
856
1546
  creation_num: "2",
857
1547
  },
858
1548
  },
859
1549
  },
1550
+ frozen: false,
1551
+ withdraw_events: {
1552
+ counter: "88",
1553
+ guid: {
1554
+ id: {
1555
+ addr: "0xa0d8",
1556
+ creation_num: "3",
1557
+ },
1558
+ },
1559
+ },
1560
+ },
1561
+ },
1562
+ type: "write_resource",
1563
+ },
1564
+ {
1565
+ address: "0xa0d8",
1566
+ state_key_hash: "0x6f1e",
1567
+ data: {
1568
+ type: "0x1::account::Account",
1569
+ data: {
1570
+ authentication_key: "0xa0d8",
1571
+ coin_register_events: {
1572
+ counter: "5",
1573
+ guid: {
1574
+ id: {
1575
+ addr: "0xa0d8",
1576
+ creation_num: "0",
1577
+ },
1578
+ },
1579
+ },
1580
+ guid_creation_num: "12",
1581
+ key_rotation_events: {
1582
+ counter: "0",
1583
+ guid: {
1584
+ id: {
1585
+ addr: "0xa0d8",
1586
+ creation_num: "1",
1587
+ },
1588
+ },
1589
+ },
1590
+ rotation_capability_offer: {
1591
+ for: {
1592
+ vec: [],
1593
+ },
1594
+ },
1595
+ sequence_number: "108",
1596
+ signer_capability_offer: {
1597
+ for: {
1598
+ vec: [],
1599
+ },
1600
+ },
1601
+ },
1602
+ },
1603
+ type: "write_resource",
1604
+ },
1605
+ {
1606
+ address: "0xd475",
1607
+ state_key_hash: "0x7567",
1608
+ data: {
1609
+ type: "0x1::fungible_asset::FungibleStore",
1610
+ data: {
1611
+ balance: "14000",
1612
+ frozen: false,
1613
+ metadata: {
1614
+ inner: "0x2ebb",
1615
+ },
860
1616
  },
861
1617
  },
1618
+ type: "write_resource",
1619
+ },
1620
+ {
1621
+ address: "0xd475",
1622
+ state_key_hash: "0x7567",
1623
+ data: {
1624
+ type: "0x1::object::ObjectCore",
1625
+ data: {
1626
+ allow_ungated_transfer: false,
1627
+ guid_creation_num: "1125899906842625",
1628
+ owner: "0xa0d8",
1629
+ transfer_events: {
1630
+ counter: "0",
1631
+ guid: {
1632
+ id: {
1633
+ addr: "0xd475",
1634
+ creation_num: "1125899906842624",
1635
+ },
1636
+ },
1637
+ },
1638
+ },
1639
+ },
1640
+ type: "write_resource",
1641
+ },
1642
+ {
1643
+ state_key_hash: "0x6e4b",
1644
+ handle: "0x1b85",
1645
+ key: "0x0619",
1646
+ value: "0xad4388dc7daf96010000000000000000",
1647
+ data: null,
1648
+ type: "write_table_item",
862
1649
  },
863
1650
  ],
864
1651
  block: { hash: "0xabc", height: 1 },
@@ -867,10 +1654,27 @@ describe("Aptos sync logic ", () => {
867
1654
  } as unknown as AptosTransaction,
868
1655
  ];
869
1656
 
870
- const result = txsToOps({ address }, id, txs);
1657
+ const [ops, tokenOps] = txsToOps({ address: "0x6b8c" }, "test_id", txs);
871
1658
 
872
- expect(result).toHaveLength(1);
873
- expect(result[0].hasFailed).toBe(true);
1659
+ expect(ops).toHaveLength(0);
1660
+
1661
+ expect(tokenOps).toHaveLength(1);
1662
+ expect(tokenOps[0]).toEqual({
1663
+ id: expect.any(String),
1664
+ accountId: "token_account_id",
1665
+ hash: "0x10c9",
1666
+ type: DIRECTION.IN,
1667
+ value: new BigNumber(193),
1668
+ fee: new BigNumber(20000),
1669
+ blockHash: "0xabc",
1670
+ blockHeight: 1,
1671
+ senders: ["0xa0d8"],
1672
+ recipients: ["0x6b8c"],
1673
+ date: new Date(1000),
1674
+ extra: { version: undefined },
1675
+ transactionSequenceNumber: 1,
1676
+ hasFailed: false,
1677
+ });
874
1678
  });
875
1679
  });
876
1680
  });