@ledgerhq/coin-aptos 2.0.0-nightly.4 → 2.0.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 (217) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.unimportedrc.json +18 -6
  3. package/CHANGELOG.md +156 -29
  4. package/jest.config.js +2 -1
  5. package/jest.integ.config.js +8 -0
  6. package/lib/__tests__/api/index.integ.test.d.ts +2 -0
  7. package/lib/__tests__/api/index.integ.test.d.ts.map +1 -0
  8. package/lib/__tests__/api/index.integ.test.js +52 -0
  9. package/lib/__tests__/api/index.integ.test.js.map +1 -0
  10. package/lib/__tests__/api/index.test.js +48 -460
  11. package/lib/__tests__/api/index.test.js.map +1 -1
  12. package/lib/__tests__/bridge/broadcast.test.js +5 -5
  13. package/lib/__tests__/bridge/broadcast.test.js.map +1 -1
  14. package/lib/__tests__/bridge/buildTransaction.test.js +6 -6
  15. package/lib/__tests__/bridge/buildTransaction.test.js.map +1 -1
  16. package/lib/__tests__/bridge/createTransaction.test.js +1 -1
  17. package/lib/__tests__/bridge/createTransaction.test.js.map +1 -1
  18. package/lib/__tests__/bridge/getFeesForTransaction.test.js +9 -9
  19. package/lib/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
  20. package/lib/__tests__/bridge/prepareTransaction.test.js +3 -3
  21. package/lib/__tests__/bridge/prepareTransaction.test.js.map +1 -1
  22. package/lib/__tests__/bridge/signOperation.test.js +3 -2
  23. package/lib/__tests__/bridge/signOperation.test.js.map +1 -1
  24. package/lib/__tests__/bridge/synchronisation.test.js +6 -6
  25. package/lib/__tests__/bridge/synchronisation.test.js.map +1 -1
  26. package/lib/__tests__/network/client.test.d.ts +2 -0
  27. package/lib/__tests__/network/client.test.d.ts.map +1 -0
  28. package/lib/__tests__/network/client.test.js +520 -0
  29. package/lib/__tests__/network/client.test.js.map +1 -0
  30. package/lib/api/index.d.ts +4 -33
  31. package/lib/api/index.d.ts.map +1 -1
  32. package/lib/api/index.js +24 -196
  33. package/lib/api/index.js.map +1 -1
  34. package/lib/bridge/broadcast.d.ts +3 -2
  35. package/lib/bridge/broadcast.d.ts.map +1 -1
  36. package/lib/bridge/broadcast.js +4 -3
  37. package/lib/bridge/broadcast.js.map +1 -1
  38. package/lib/bridge/buildTransaction.d.ts +1 -1
  39. package/lib/bridge/buildTransaction.d.ts.map +1 -1
  40. package/lib/bridge/buildTransaction.js.map +1 -1
  41. package/lib/bridge/createTransaction.js +3 -3
  42. package/lib/bridge/createTransaction.js.map +1 -1
  43. package/lib/bridge/estimateMaxSpendable.d.ts.map +1 -1
  44. package/lib/bridge/estimateMaxSpendable.js +5 -4
  45. package/lib/bridge/estimateMaxSpendable.js.map +1 -1
  46. package/lib/bridge/getFeesForTransaction.d.ts +1 -1
  47. package/lib/bridge/getFeesForTransaction.d.ts.map +1 -1
  48. package/lib/bridge/getFeesForTransaction.js +7 -6
  49. package/lib/bridge/getFeesForTransaction.js.map +1 -1
  50. package/lib/bridge/index.d.ts +1 -1
  51. package/lib/bridge/index.d.ts.map +1 -1
  52. package/lib/bridge/logic.d.ts +0 -3
  53. package/lib/bridge/logic.d.ts.map +1 -1
  54. package/lib/bridge/logic.js +1 -4
  55. package/lib/bridge/logic.js.map +1 -1
  56. package/lib/bridge/prepareTransaction.d.ts.map +1 -1
  57. package/lib/bridge/prepareTransaction.js +4 -3
  58. package/lib/bridge/prepareTransaction.js.map +1 -1
  59. package/lib/bridge/signOperation.js +3 -3
  60. package/lib/bridge/signOperation.js.map +1 -1
  61. package/lib/bridge/synchronisation.js +3 -3
  62. package/lib/bridge/synchronisation.js.map +1 -1
  63. package/lib/config.d.ts +13 -0
  64. package/lib/config.d.ts.map +1 -0
  65. package/lib/config.js +9 -0
  66. package/lib/config.js.map +1 -0
  67. package/lib/constants.d.ts +4 -0
  68. package/lib/constants.d.ts.map +1 -1
  69. package/lib/constants.js +8 -1
  70. package/lib/constants.js.map +1 -1
  71. package/lib/network/client.d.ts +36 -0
  72. package/lib/network/client.d.ts.map +1 -0
  73. package/lib/network/client.js +247 -0
  74. package/lib/network/client.js.map +1 -0
  75. package/lib/network/graphql/queries.d.ts.map +1 -0
  76. package/lib/network/graphql/queries.js.map +1 -0
  77. package/lib/network/graphql/types.d.ts.map +1 -0
  78. package/lib/{api → network}/graphql/types.js.map +1 -1
  79. package/lib/network/index.d.ts +2 -1
  80. package/lib/network/index.d.ts.map +1 -1
  81. package/lib/network/index.js +15 -0
  82. package/lib/network/index.js.map +1 -1
  83. package/lib/test/bridgeDatasetTest.d.ts.map +1 -1
  84. package/lib/test/bridgeDatasetTest.js +1 -0
  85. package/lib/test/bridgeDatasetTest.js.map +1 -1
  86. package/lib/types/assets.d.ts +12 -0
  87. package/lib/types/assets.d.ts.map +1 -0
  88. package/lib/types/assets.js +3 -0
  89. package/lib/types/assets.js.map +1 -0
  90. package/lib-es/__tests__/api/index.integ.test.d.ts +2 -0
  91. package/lib-es/__tests__/api/index.integ.test.d.ts.map +1 -0
  92. package/lib-es/__tests__/api/index.integ.test.js +50 -0
  93. package/lib-es/__tests__/api/index.integ.test.js.map +1 -0
  94. package/lib-es/__tests__/api/index.test.js +52 -464
  95. package/lib-es/__tests__/api/index.test.js.map +1 -1
  96. package/lib-es/__tests__/bridge/broadcast.test.js +2 -2
  97. package/lib-es/__tests__/bridge/broadcast.test.js.map +1 -1
  98. package/lib-es/__tests__/bridge/buildTransaction.test.js +2 -2
  99. package/lib-es/__tests__/bridge/buildTransaction.test.js.map +1 -1
  100. package/lib-es/__tests__/bridge/createTransaction.test.js +1 -1
  101. package/lib-es/__tests__/bridge/createTransaction.test.js.map +1 -1
  102. package/lib-es/__tests__/bridge/getFeesForTransaction.test.js +2 -2
  103. package/lib-es/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
  104. package/lib-es/__tests__/bridge/prepareTransaction.test.js +2 -2
  105. package/lib-es/__tests__/bridge/prepareTransaction.test.js.map +1 -1
  106. package/lib-es/__tests__/bridge/signOperation.test.js +3 -2
  107. package/lib-es/__tests__/bridge/signOperation.test.js.map +1 -1
  108. package/lib-es/__tests__/bridge/synchronisation.test.js +3 -3
  109. package/lib-es/__tests__/bridge/synchronisation.test.js.map +1 -1
  110. package/lib-es/__tests__/network/client.test.d.ts +2 -0
  111. package/lib-es/__tests__/network/client.test.d.ts.map +1 -0
  112. package/lib-es/__tests__/network/client.test.js +515 -0
  113. package/lib-es/__tests__/network/client.test.js.map +1 -0
  114. package/lib-es/api/index.d.ts +4 -33
  115. package/lib-es/api/index.d.ts.map +1 -1
  116. package/lib-es/api/index.js +22 -194
  117. package/lib-es/api/index.js.map +1 -1
  118. package/lib-es/bridge/broadcast.d.ts +3 -2
  119. package/lib-es/bridge/broadcast.d.ts.map +1 -1
  120. package/lib-es/bridge/broadcast.js +4 -3
  121. package/lib-es/bridge/broadcast.js.map +1 -1
  122. package/lib-es/bridge/buildTransaction.d.ts +1 -1
  123. package/lib-es/bridge/buildTransaction.d.ts.map +1 -1
  124. package/lib-es/bridge/buildTransaction.js.map +1 -1
  125. package/lib-es/bridge/createTransaction.js +1 -1
  126. package/lib-es/bridge/createTransaction.js.map +1 -1
  127. package/lib-es/bridge/estimateMaxSpendable.d.ts.map +1 -1
  128. package/lib-es/bridge/estimateMaxSpendable.js +3 -2
  129. package/lib-es/bridge/estimateMaxSpendable.js.map +1 -1
  130. package/lib-es/bridge/getFeesForTransaction.d.ts +1 -1
  131. package/lib-es/bridge/getFeesForTransaction.d.ts.map +1 -1
  132. package/lib-es/bridge/getFeesForTransaction.js +2 -1
  133. package/lib-es/bridge/getFeesForTransaction.js.map +1 -1
  134. package/lib-es/bridge/index.d.ts +1 -1
  135. package/lib-es/bridge/index.d.ts.map +1 -1
  136. package/lib-es/bridge/logic.d.ts +0 -3
  137. package/lib-es/bridge/logic.d.ts.map +1 -1
  138. package/lib-es/bridge/logic.js +0 -3
  139. package/lib-es/bridge/logic.js.map +1 -1
  140. package/lib-es/bridge/prepareTransaction.d.ts.map +1 -1
  141. package/lib-es/bridge/prepareTransaction.js +3 -2
  142. package/lib-es/bridge/prepareTransaction.js.map +1 -1
  143. package/lib-es/bridge/signOperation.js +1 -1
  144. package/lib-es/bridge/signOperation.js.map +1 -1
  145. package/lib-es/bridge/synchronisation.js +1 -1
  146. package/lib-es/bridge/synchronisation.js.map +1 -1
  147. package/lib-es/config.d.ts +13 -0
  148. package/lib-es/config.d.ts.map +1 -0
  149. package/lib-es/config.js +4 -0
  150. package/lib-es/config.js.map +1 -0
  151. package/lib-es/constants.d.ts +4 -0
  152. package/lib-es/constants.d.ts.map +1 -1
  153. package/lib-es/constants.js +4 -0
  154. package/lib-es/constants.js.map +1 -1
  155. package/lib-es/network/client.d.ts +36 -0
  156. package/lib-es/network/client.d.ts.map +1 -0
  157. package/lib-es/network/client.js +240 -0
  158. package/lib-es/network/client.js.map +1 -0
  159. package/lib-es/network/graphql/queries.d.ts.map +1 -0
  160. package/lib-es/network/graphql/queries.js.map +1 -0
  161. package/lib-es/network/graphql/types.d.ts.map +1 -0
  162. package/lib-es/{api → network}/graphql/types.js.map +1 -1
  163. package/lib-es/network/index.d.ts +2 -1
  164. package/lib-es/network/index.d.ts.map +1 -1
  165. package/lib-es/network/index.js +1 -0
  166. package/lib-es/network/index.js.map +1 -1
  167. package/lib-es/test/bridgeDatasetTest.d.ts.map +1 -1
  168. package/lib-es/test/bridgeDatasetTest.js +1 -0
  169. package/lib-es/test/bridgeDatasetTest.js.map +1 -1
  170. package/lib-es/types/assets.d.ts +12 -0
  171. package/lib-es/types/assets.d.ts.map +1 -0
  172. package/lib-es/types/assets.js +2 -0
  173. package/lib-es/types/assets.js.map +1 -0
  174. package/package.json +12 -10
  175. package/src/__tests__/api/index.integ.test.ts +58 -0
  176. package/src/__tests__/api/index.test.ts +60 -547
  177. package/src/__tests__/bridge/broadcast.test.ts +2 -2
  178. package/src/__tests__/bridge/buildTransaction.test.ts +2 -2
  179. package/src/__tests__/bridge/createTransaction.test.ts +1 -1
  180. package/src/__tests__/bridge/getFeesForTransaction.test.ts +2 -2
  181. package/src/__tests__/bridge/prepareTransaction.test.ts +2 -2
  182. package/src/__tests__/bridge/signOperation.test.ts +6 -3
  183. package/src/__tests__/bridge/synchronisation.test.ts +4 -4
  184. package/src/__tests__/network/client.test.ts +618 -0
  185. package/src/api/index.ts +31 -250
  186. package/src/bridge/broadcast.ts +7 -7
  187. package/src/bridge/buildTransaction.ts +1 -1
  188. package/src/bridge/createTransaction.ts +1 -1
  189. package/src/bridge/estimateMaxSpendable.ts +3 -2
  190. package/src/bridge/getFeesForTransaction.ts +3 -2
  191. package/src/bridge/index.ts +1 -1
  192. package/src/bridge/logic.ts +0 -4
  193. package/src/bridge/prepareTransaction.ts +4 -2
  194. package/src/bridge/signOperation.ts +1 -1
  195. package/src/bridge/synchronisation.ts +1 -1
  196. package/src/config.ts +19 -0
  197. package/src/constants.ts +5 -0
  198. package/src/network/client.ts +315 -0
  199. package/src/network/index.ts +2 -1
  200. package/src/test/bridgeDatasetTest.ts +1 -0
  201. package/src/types/assets.ts +15 -0
  202. package/lib/api/graphql/queries.d.ts.map +0 -1
  203. package/lib/api/graphql/queries.js.map +0 -1
  204. package/lib/api/graphql/types.d.ts.map +0 -1
  205. package/lib-es/api/graphql/queries.d.ts.map +0 -1
  206. package/lib-es/api/graphql/queries.js.map +0 -1
  207. package/lib-es/api/graphql/types.d.ts.map +0 -1
  208. /package/lib/{api → network}/graphql/queries.d.ts +0 -0
  209. /package/lib/{api → network}/graphql/queries.js +0 -0
  210. /package/lib/{api → network}/graphql/types.d.ts +0 -0
  211. /package/lib/{api → network}/graphql/types.js +0 -0
  212. /package/lib-es/{api → network}/graphql/queries.d.ts +0 -0
  213. /package/lib-es/{api → network}/graphql/queries.js +0 -0
  214. /package/lib-es/{api → network}/graphql/types.d.ts +0 -0
  215. /package/lib-es/{api → network}/graphql/types.js +0 -0
  216. /package/src/{api → network}/graphql/queries.ts +0 -0
  217. /package/src/{api → network}/graphql/types.ts +0 -0
@@ -1,12 +1,12 @@
1
1
  import prepareTransaction from "../../bridge/prepareTransaction";
2
- import { AptosAPI } from "../../api";
2
+ import { AptosAPI } from "../../network";
3
3
  import { getEstimatedGas } from "../../bridge/getFeesForTransaction";
4
4
  import { getMaxSendBalance } from "../../bridge/logic";
5
5
  import BigNumber from "bignumber.js";
6
6
  import type { Account } from "@ledgerhq/types-live";
7
7
  import type { Transaction } from "../../types";
8
8
 
9
- jest.mock("../../api");
9
+ jest.mock("../../network");
10
10
  jest.mock("../../bridge/getFeesForTransaction");
11
11
  jest.mock("../../bridge/logic");
12
12
 
@@ -1,6 +1,6 @@
1
1
  import BigNumber from "bignumber.js";
2
2
  import { Observable } from "rxjs";
3
- import { SignerContext } from "@ledgerhq/coin-framework/lib/signer";
3
+ import { SignerContext } from "@ledgerhq/coin-framework/signer";
4
4
  import { createFixtureAccount, createFixtureTransaction } from "../../bridge/bridge.fixture";
5
5
  import buildSignOperation, { getAddress } from "../../bridge/signOperation";
6
6
  import { AptosSigner } from "../../types";
@@ -9,11 +9,13 @@ import { TokenAccount } from "@ledgerhq/types-live";
9
9
  import { faker } from "@faker-js/faker";
10
10
  import { emptyHistoryCache } from "@ledgerhq/coin-framework/account/index";
11
11
 
12
- jest.mock("../../api", () => {
12
+ const generateTransaction = jest.fn(() => "tx");
13
+
14
+ jest.mock("../../network/client", () => {
13
15
  return {
14
16
  AptosAPI: function () {
15
17
  return {
16
- generateTransaction: jest.fn(() => "tx"),
18
+ generateTransaction,
17
19
  };
18
20
  },
19
21
  };
@@ -41,6 +43,7 @@ describe("buildSignOperation", () => {
41
43
  beforeEach(() => {
42
44
  mockedSignTransaction = jest.mocked(signTransaction);
43
45
  });
46
+
44
47
  afterEach(() => jest.clearAllMocks());
45
48
 
46
49
  it("should thrown an error", async () => {
@@ -3,7 +3,7 @@ import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets/index";
3
3
  import { Account, Operation, SyncConfig, TokenAccount } from "@ledgerhq/types-live";
4
4
  import { decodeTokenAccountId } from "@ledgerhq/coin-framework/account";
5
5
  import { emptyHistoryCache } from "@ledgerhq/coin-framework/account/index";
6
- import { AptosAPI } from "../../api";
6
+ import { AptosAPI } from "../../network";
7
7
  import {
8
8
  getAccountShape,
9
9
  mergeSubAccounts,
@@ -35,12 +35,12 @@ jest.mock("@ledgerhq/coin-framework/account", () => {
35
35
 
36
36
  const mockedDecodeTokenAccountId = jest.mocked(decodeTokenAccountId);
37
37
 
38
+ jest.mock("../../network");
39
+ let mockedAptosAPI: jest.Mocked<any>;
40
+
38
41
  jest.mock("@ledgerhq/coin-framework/bridge/jsHelpers");
39
42
  jest.mock("invariant", () => jest.fn());
40
43
 
41
- jest.mock("../../api");
42
- let mockedAptosAPI: jest.Mocked<any>;
43
-
44
44
  jest.mock("../../bridge/logic");
45
45
 
46
46
  describe("getAccountShape", () => {
@@ -0,0 +1,618 @@
1
+ import { ApolloClient } from "@apollo/client";
2
+ import {
3
+ AccountAddress,
4
+ Aptos,
5
+ ChainId,
6
+ Ed25519PublicKey,
7
+ InputEntryFunctionData,
8
+ RawTransaction,
9
+ Serializable,
10
+ post,
11
+ } from "@aptos-labs/ts-sdk";
12
+ import network from "@ledgerhq/live-network";
13
+ import BigNumber from "bignumber.js";
14
+ import { AptosAPI } from "../../network";
15
+ import { AptosAsset, AptosExtra, AptosSender } from "../../types/assets";
16
+ import { TransactionIntent } from "@ledgerhq/coin-framework/api/types";
17
+ import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
18
+
19
+ jest.mock("@aptos-labs/ts-sdk");
20
+ jest.mock("@apollo/client");
21
+ let mockedAptos: jest.Mocked<any>;
22
+ let mockedApolloClient: jest.Mocked<any>;
23
+ let mockedPost = jest.fn();
24
+
25
+ jest.mock("@ledgerhq/live-network/network");
26
+ const mockedNetwork = jest.mocked(network);
27
+
28
+ describe("Aptos API", () => {
29
+ beforeEach(() => {
30
+ mockedAptos = jest.mocked(Aptos);
31
+ mockedApolloClient = jest.mocked(ApolloClient);
32
+ mockedPost = jest.mocked(post);
33
+ });
34
+
35
+ afterEach(() => jest.clearAllMocks());
36
+
37
+ it("builds the client properly for mainnet", () => {
38
+ const api = new AptosAPI("aptos");
39
+
40
+ expect(api.broadcast).toBeDefined();
41
+ expect(typeof api.broadcast).toBe("function");
42
+ expect(api.estimateGasPrice).toBeDefined();
43
+ expect(typeof api.estimateGasPrice).toBe("function");
44
+ expect(api.generateTransaction).toBeDefined();
45
+ expect(typeof api.generateTransaction).toBe("function");
46
+ expect(api.getAccount).toBeDefined();
47
+ expect(typeof api.getAccount).toBe("function");
48
+ expect(api.getAccountInfo).toBeDefined();
49
+ expect(typeof api.getAccountInfo).toBe("function");
50
+ expect(api.simulateTransaction).toBeDefined();
51
+ expect(typeof api.simulateTransaction).toBe("function");
52
+ });
53
+
54
+ it("builds the client properly for testnet", () => {
55
+ const api = new AptosAPI("aptos_testnet");
56
+
57
+ expect(api.broadcast).toBeDefined();
58
+ expect(typeof api.broadcast).toBe("function");
59
+ expect(api.estimateGasPrice).toBeDefined();
60
+ expect(typeof api.estimateGasPrice).toBe("function");
61
+ expect(api.generateTransaction).toBeDefined();
62
+ expect(typeof api.generateTransaction).toBe("function");
63
+ expect(api.getAccount).toBeDefined();
64
+ expect(typeof api.getAccount).toBe("function");
65
+ expect(api.getAccountInfo).toBeDefined();
66
+ expect(typeof api.getAccountInfo).toBe("function");
67
+ expect(api.simulateTransaction).toBeDefined();
68
+ expect(typeof api.simulateTransaction).toBe("function");
69
+ });
70
+
71
+ describe("getAccount", () => {
72
+ it("calls getAccountInfo", async () => {
73
+ const mockGetAccountInfo = jest.fn();
74
+ mockedAptos.mockImplementation(() => ({
75
+ getAccountInfo: mockGetAccountInfo,
76
+ }));
77
+
78
+ const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount");
79
+
80
+ const api = new AptosAPI("aptos");
81
+ await api.getAccount("address");
82
+
83
+ expect(mockGetAccountSpy).toHaveBeenCalledWith({
84
+ accountAddress: "address",
85
+ });
86
+ });
87
+ });
88
+
89
+ describe("getBalance", () => {
90
+ let token: TokenCurrency;
91
+
92
+ beforeEach(() => {
93
+ token = {
94
+ type: "TokenCurrency",
95
+ id: "aptos_token",
96
+ name: "Aptos Token",
97
+ ticker: "APT",
98
+ units: [{ name: "APT", code: "APT", magnitude: 6 }],
99
+ contractAddress: "APTOS_1_ADDRESS",
100
+ tokenType: "fungible_asset",
101
+ parentCurrency: {
102
+ type: "CryptoCurrency",
103
+ id: "aptos",
104
+ name: "Aptos",
105
+ ticker: "APT",
106
+ units: [{ name: "APT", code: "APT", magnitude: 6 }],
107
+ color: "#000000",
108
+ family: "aptos",
109
+ scheme: "aptos",
110
+ explorerViews: [],
111
+ managerAppName: "Aptos",
112
+ coinType: 637,
113
+ },
114
+ };
115
+ });
116
+
117
+ it("get coin balance", async () => {
118
+ mockedAptos.mockImplementation(() => ({
119
+ view: jest.fn().mockReturnValue(["1234"]),
120
+ }));
121
+
122
+ token.tokenType = "coin";
123
+ const api = new AptosAPI("aptos");
124
+ const balance = await api.getBalance("address", token);
125
+
126
+ expect(balance).toEqual(new BigNumber(1234));
127
+ });
128
+
129
+ it("get fungible assets balance", async () => {
130
+ mockedAptos.mockImplementation(() => ({
131
+ view: jest.fn().mockReturnValue(["12345"]),
132
+ }));
133
+
134
+ token.tokenType = "fungible_asset";
135
+
136
+ const api = new AptosAPI("aptos");
137
+ const balance = await api.getBalance("address", token);
138
+
139
+ expect(balance).toEqual(new BigNumber(12345));
140
+ });
141
+
142
+ it("return 0 balace if could not retrieve proper balance of fungible assets", async () => {
143
+ mockedAptos.mockImplementation(() => ({
144
+ view: jest.fn().mockImplementation(() => {
145
+ throw new Error("error");
146
+ }),
147
+ }));
148
+
149
+ token.tokenType = "fungible_asset";
150
+
151
+ const api = new AptosAPI("aptos");
152
+ const balance = await api.getBalance("address", token);
153
+
154
+ expect(balance).toEqual(new BigNumber(0));
155
+ });
156
+ });
157
+
158
+ describe("getAccountInfo", () => {
159
+ it("calls getCoinBalance, fetchTransactions and getHeight", async () => {
160
+ mockedAptos.mockImplementation(() => ({
161
+ view: jest.fn().mockReturnValue(["123"]),
162
+ getTransactionByVersion: jest.fn().mockReturnValue({
163
+ type: "user_transaction",
164
+ version: "v1",
165
+ }),
166
+ getBlockByVersion: jest.fn().mockReturnValue({
167
+ block_height: "1",
168
+ block_hash: "83ca6d",
169
+ }),
170
+ }));
171
+
172
+ mockedNetwork.mockResolvedValue(
173
+ Promise.resolve({
174
+ data: {
175
+ account: {
176
+ account_number: 1,
177
+ sequence: 0,
178
+ pub_key: { key: "k", "@type": "type" },
179
+ base_account: {
180
+ account_number: 2,
181
+ sequence: 42,
182
+ pub_key: { key: "k2", "@type": "type2" },
183
+ },
184
+ },
185
+ block_height: "999",
186
+ },
187
+ status: 200,
188
+ headers: {} as any,
189
+ statusText: "",
190
+ config: {
191
+ headers: {} as any,
192
+ },
193
+ }),
194
+ );
195
+
196
+ mockedApolloClient.mockImplementation(() => ({
197
+ query: async () => ({
198
+ data: {
199
+ account_transactions: [{ transaction_version: 1 }],
200
+ },
201
+ loading: false,
202
+ networkStatus: 7,
203
+ }),
204
+ }));
205
+
206
+ const api = new AptosAPI("aptos");
207
+ const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
208
+
209
+ expect(accountInfo.balance).toEqual(new BigNumber(123));
210
+ expect(accountInfo.transactions).toEqual([
211
+ {
212
+ type: "user_transaction",
213
+ version: "v1",
214
+ block: {
215
+ height: 1,
216
+ hash: "83ca6d",
217
+ },
218
+ },
219
+ ]);
220
+ expect(accountInfo.blockHeight).toEqual(999);
221
+ });
222
+
223
+ it("return balance = 0 if it fails to fetch balance", async () => {
224
+ mockedAptos.mockImplementation(() => ({
225
+ view: jest.fn().mockImplementation(() => {
226
+ throw new Error("error");
227
+ }),
228
+ getTransactionByVersion: jest.fn().mockReturnValue({
229
+ type: "user_transaction",
230
+ version: "v1",
231
+ }),
232
+ getBlockByVersion: jest.fn().mockReturnValue({
233
+ block_height: "1",
234
+ block_hash: "83ca6d",
235
+ }),
236
+ }));
237
+
238
+ mockedNetwork.mockResolvedValue(
239
+ Promise.resolve({
240
+ data: {
241
+ account: {
242
+ account_number: 1,
243
+ sequence: 0,
244
+ pub_key: { key: "k", "@type": "type" },
245
+ base_account: {
246
+ account_number: 2,
247
+ sequence: 42,
248
+ pub_key: { key: "k2", "@type": "type2" },
249
+ },
250
+ },
251
+ block_height: "999",
252
+ },
253
+ status: 200,
254
+ headers: {} as any,
255
+ statusText: "",
256
+ config: {
257
+ headers: {} as any,
258
+ },
259
+ }),
260
+ );
261
+
262
+ mockedApolloClient.mockImplementation(() => ({
263
+ query: async () => ({
264
+ data: {
265
+ account_transactions: [{ transaction_version: 1 }],
266
+ },
267
+ loading: false,
268
+ networkStatus: 7,
269
+ }),
270
+ }));
271
+
272
+ const api = new AptosAPI("aptos");
273
+ const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
274
+
275
+ expect(accountInfo.balance).toEqual(new BigNumber(0));
276
+ expect(accountInfo.transactions).toEqual([
277
+ {
278
+ type: "user_transaction",
279
+ version: "v1",
280
+ block: {
281
+ height: 1,
282
+ hash: "83ca6d",
283
+ },
284
+ },
285
+ ]);
286
+ expect(accountInfo.blockHeight).toEqual(999);
287
+ });
288
+
289
+ it("returns no transactions if it the address is empty", async () => {
290
+ mockedAptos.mockImplementation(() => ({
291
+ view: jest.fn().mockReturnValue(["123"]),
292
+ getTransactionByVersion: jest.fn().mockReturnValue({
293
+ type: "user_transaction",
294
+ version: "v1",
295
+ }),
296
+ getBlockByVersion: jest.fn().mockReturnValue({
297
+ block_height: "1",
298
+ block_hash: "83ca6d",
299
+ }),
300
+ }));
301
+
302
+ mockedNetwork.mockResolvedValue(
303
+ Promise.resolve({
304
+ data: {
305
+ account: {
306
+ account_number: 1,
307
+ sequence: 0,
308
+ pub_key: { key: "k", "@type": "type" },
309
+ base_account: {
310
+ account_number: 2,
311
+ sequence: 42,
312
+ pub_key: { key: "k2", "@type": "type2" },
313
+ },
314
+ },
315
+ block_height: "999",
316
+ },
317
+ status: 200,
318
+ headers: {} as any,
319
+ statusText: "",
320
+ config: {
321
+ headers: {} as any,
322
+ },
323
+ }),
324
+ );
325
+
326
+ mockedApolloClient.mockImplementation(() => ({
327
+ query: async () => ({
328
+ data: {
329
+ account_transactions: [{ transaction_version: 1 }],
330
+ },
331
+ loading: false,
332
+ networkStatus: 7,
333
+ }),
334
+ }));
335
+
336
+ const api = new AptosAPI("aptos");
337
+ const accountInfo = await api.getAccountInfo("", "1");
338
+
339
+ expect(accountInfo.balance).toEqual(new BigNumber(123));
340
+ expect(accountInfo.transactions).toEqual([]);
341
+ expect(accountInfo.blockHeight).toEqual(999);
342
+ });
343
+
344
+ it("returns a null transaction if it fails to getTransactionByVersion", async () => {
345
+ mockedAptos.mockImplementation(() => ({
346
+ view: jest.fn().mockReturnValue(["123"]),
347
+ getTransactionByVersion: jest.fn().mockImplementation(() => {
348
+ throw new Error("error");
349
+ }),
350
+ getBlockByVersion: jest.fn().mockReturnValue({
351
+ block_height: "1",
352
+ block_hash: "83ca6d",
353
+ }),
354
+ }));
355
+
356
+ mockedNetwork.mockResolvedValue(
357
+ Promise.resolve({
358
+ data: {
359
+ account: {
360
+ account_number: 1,
361
+ sequence: 0,
362
+ pub_key: { key: "k", "@type": "type" },
363
+ base_account: {
364
+ account_number: 2,
365
+ sequence: 42,
366
+ pub_key: { key: "k2", "@type": "type2" },
367
+ },
368
+ },
369
+ block_height: "999",
370
+ },
371
+ status: 200,
372
+ headers: {} as any,
373
+ statusText: "",
374
+ config: {
375
+ headers: {} as any,
376
+ },
377
+ }),
378
+ );
379
+
380
+ mockedApolloClient.mockImplementation(() => ({
381
+ query: async () => ({
382
+ data: {
383
+ account_transactions: [{ transaction_version: 1 }],
384
+ },
385
+ loading: false,
386
+ networkStatus: 7,
387
+ }),
388
+ }));
389
+
390
+ const api = new AptosAPI("aptos");
391
+ const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
392
+
393
+ expect(accountInfo.balance).toEqual(new BigNumber(123));
394
+ expect(accountInfo.transactions).toEqual([null]);
395
+ expect(accountInfo.blockHeight).toEqual(999);
396
+ });
397
+ });
398
+
399
+ describe("estimateGasPrice", () => {
400
+ it("estimates the gas price", async () => {
401
+ const gasEstimation = { gas_estimate: 100 };
402
+ mockedAptos.mockImplementation(() => ({
403
+ getGasPriceEstimation: jest.fn().mockReturnValue(gasEstimation),
404
+ }));
405
+
406
+ const api = new AptosAPI("aptos");
407
+ const gasPrice = await api.estimateGasPrice();
408
+
409
+ expect(gasPrice.gas_estimate).toEqual(100);
410
+ });
411
+ });
412
+
413
+ describe("generateTransaction", () => {
414
+ const payload: InputEntryFunctionData = {
415
+ function: "0x1::coin::transfer",
416
+ functionArguments: ["0x13", 1],
417
+ };
418
+
419
+ it("generates a transaction without expireTimestamp", async () => {
420
+ const options = {
421
+ maxGasAmount: "100",
422
+ gasUnitPrice: "50",
423
+ };
424
+
425
+ const mockSimple = jest.fn().mockImplementation(async () => ({
426
+ rawTransaction: null,
427
+ }));
428
+ mockedAptos.mockImplementation(() => ({
429
+ transaction: {
430
+ build: {
431
+ simple: mockSimple,
432
+ },
433
+ },
434
+ }));
435
+
436
+ const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
437
+
438
+ const api = new AptosAPI("aptos");
439
+ await api.generateTransaction("APTOS_1_ADDRESS", payload, options);
440
+
441
+ expect(mockSimpleSpy).toHaveBeenCalledWith({
442
+ data: payload,
443
+ options: {
444
+ maxGasAmount: Number(options.maxGasAmount),
445
+ gasUnitPrice: Number(options.gasUnitPrice),
446
+ },
447
+ sender: "APTOS_1_ADDRESS",
448
+ });
449
+ });
450
+
451
+ it("generates a transaction with default expireTimestamp", async () => {
452
+ const options = {
453
+ maxGasAmount: "100",
454
+ gasUnitPrice: "50",
455
+ };
456
+
457
+ const mockSimple = jest.fn().mockImplementation(async () => ({
458
+ rawTransaction: null,
459
+ }));
460
+ const mockGetLedgerInfo = jest.fn().mockImplementation(async () => ({
461
+ ledger_timestamp: "0",
462
+ }));
463
+ mockedAptos.mockImplementation(() => ({
464
+ transaction: {
465
+ build: {
466
+ simple: mockSimple,
467
+ },
468
+ },
469
+ getLedgerInfo: mockGetLedgerInfo,
470
+ }));
471
+
472
+ const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
473
+
474
+ const api = new AptosAPI("aptos");
475
+ await api.generateTransaction("APTOS_1_ADDRESS", payload, options);
476
+
477
+ expect(mockSimpleSpy).toHaveBeenCalledWith({
478
+ data: payload,
479
+ options: {
480
+ maxGasAmount: Number(options.maxGasAmount),
481
+ gasUnitPrice: Number(options.gasUnitPrice),
482
+ expireTimestamp: 120,
483
+ },
484
+ sender: "APTOS_1_ADDRESS",
485
+ });
486
+ });
487
+
488
+ it("throws an error when failing to build a transaction", async () => {
489
+ const options = {
490
+ maxGasAmount: "100",
491
+ gasUnitPrice: "50",
492
+ };
493
+
494
+ const mockSimple = jest.fn().mockImplementation(async () => null);
495
+ mockedAptos.mockImplementation(() => ({
496
+ transaction: {
497
+ build: {
498
+ simple: mockSimple,
499
+ },
500
+ },
501
+ }));
502
+
503
+ const api = new AptosAPI("aptos");
504
+ expect(
505
+ async () => await api.generateTransaction("APTOS_1_ADDRESS", payload, options),
506
+ ).rejects.toThrow();
507
+ });
508
+ });
509
+
510
+ describe("simulateTransaction", () => {
511
+ it("simulates a transaction with the correct options", async () => {
512
+ const mockSimple = jest.fn().mockImplementation(async () => ({
513
+ rawTransaction: null,
514
+ }));
515
+ mockedAptos.mockImplementation(() => ({
516
+ transaction: {
517
+ simulate: {
518
+ simple: mockSimple,
519
+ },
520
+ },
521
+ }));
522
+
523
+ const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
524
+
525
+ const api = new AptosAPI("aptos");
526
+ const address = new Ed25519PublicKey("APTOS_1_ADDRESS");
527
+ const tx = new RawTransaction(
528
+ new AccountAddress(Uint8Array.from(Buffer.from("APTOS_2_ADDRESS"))),
529
+ BigInt(1),
530
+ "" as unknown as Serializable,
531
+ BigInt(100),
532
+ BigInt(50),
533
+ BigInt(1),
534
+ { chainId: 1 } as ChainId,
535
+ );
536
+ await api.simulateTransaction(address, tx);
537
+
538
+ expect(mockSimpleSpy).toHaveBeenCalledWith({
539
+ signerPublicKey: address,
540
+ transaction: { rawTransaction: tx },
541
+ options: {
542
+ estimateGasUnitPrice: true,
543
+ estimateMaxGasAmount: true,
544
+ estimatePrioritizedGasUnitPrice: false,
545
+ },
546
+ });
547
+ });
548
+ });
549
+ describe("broadcast", () => {
550
+ it("broadcasts the transaction", async () => {
551
+ mockedPost.mockImplementation(async () => ({ data: { hash: "ok" } }));
552
+ const mockedPostSpy = jest.spyOn({ post: mockedPost }, "post");
553
+
554
+ mockedAptos.mockImplementation(() => ({
555
+ config: "config",
556
+ }));
557
+
558
+ const api = new AptosAPI("aptos");
559
+ await api.broadcast("signature");
560
+
561
+ expect(mockedPostSpy).toHaveBeenCalledWith({
562
+ contentType: "application/x.aptos.signed_transaction+bcs",
563
+ aptosConfig: "config",
564
+ body: Uint8Array.from(Buffer.from("signature", "hex")),
565
+ path: "transactions",
566
+ type: "Fullnode",
567
+ originMethod: "",
568
+ });
569
+ });
570
+ });
571
+
572
+ describe("estimateFees", () => {
573
+ it("estimates the fees", async () => {
574
+ const gasEstimation = { gas_estimate: 100 };
575
+ mockedAptos.mockImplementation(() => ({
576
+ getLedgerInfo: jest.fn().mockResolvedValue({
577
+ ledger_timestamp: Date.now(),
578
+ }),
579
+ transaction: {
580
+ build: {
581
+ simple: jest.fn().mockResolvedValue({ rawTransaction: {} }),
582
+ },
583
+ simulate: {
584
+ simple: jest.fn().mockResolvedValue([
585
+ {
586
+ gas_used: 10,
587
+ gas_unit_price: 4,
588
+ },
589
+ ]),
590
+ },
591
+ },
592
+ getGasPriceEstimation: jest.fn().mockReturnValue(gasEstimation),
593
+ }));
594
+
595
+ const amount = BigInt(100);
596
+ const sender: AptosSender = {
597
+ xpub: "xpub",
598
+ freshAddress: "address1",
599
+ };
600
+ const recipient = "address2";
601
+
602
+ const api = new AptosAPI("aptos");
603
+ const transactionIntent: TransactionIntent<AptosAsset, AptosExtra, AptosSender> = {
604
+ asset: {
605
+ type: "native",
606
+ },
607
+ type: "send",
608
+ sender,
609
+ amount,
610
+ recipient,
611
+ };
612
+
613
+ const fees = await api.estimateFees(transactionIntent);
614
+
615
+ expect(fees.value.toString()).toEqual("40");
616
+ });
617
+ });
618
+ });