@ledgerhq/coin-aptos 1.9.6-next.0 → 2.0.0-nightly.1

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