@ledgerhq/coin-aptos 1.10.0-nightly.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 (175) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.unimportedrc.json +18 -6
  3. package/CHANGELOG.md +6 -0
  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 -460
  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 +6 -6
  14. package/lib/__tests__/bridge/buildTransaction.test.js.map +1 -1
  15. package/lib/__tests__/bridge/getFeesForTransaction.test.js +9 -9
  16. package/lib/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
  17. package/lib/__tests__/bridge/prepareTransaction.test.js +3 -3
  18. package/lib/__tests__/bridge/prepareTransaction.test.js.map +1 -1
  19. package/lib/__tests__/bridge/signOperation.test.js +3 -2
  20. package/lib/__tests__/bridge/signOperation.test.js.map +1 -1
  21. package/lib/__tests__/bridge/synchronisation.test.js +6 -6
  22. package/lib/__tests__/bridge/synchronisation.test.js.map +1 -1
  23. package/lib/__tests__/network/client.test.d.ts +2 -0
  24. package/lib/__tests__/network/client.test.d.ts.map +1 -0
  25. package/lib/__tests__/network/client.test.js +478 -0
  26. package/lib/__tests__/network/client.test.js.map +1 -0
  27. package/lib/api/index.d.ts +4 -33
  28. package/lib/api/index.d.ts.map +1 -1
  29. package/lib/api/index.js +26 -196
  30. package/lib/api/index.js.map +1 -1
  31. package/lib/bridge/broadcast.d.ts +3 -2
  32. package/lib/bridge/broadcast.d.ts.map +1 -1
  33. package/lib/bridge/broadcast.js +4 -3
  34. package/lib/bridge/broadcast.js.map +1 -1
  35. package/lib/bridge/buildTransaction.d.ts +1 -1
  36. package/lib/bridge/buildTransaction.d.ts.map +1 -1
  37. package/lib/bridge/buildTransaction.js.map +1 -1
  38. package/lib/bridge/estimateMaxSpendable.js +2 -2
  39. package/lib/bridge/estimateMaxSpendable.js.map +1 -1
  40. package/lib/bridge/getFeesForTransaction.d.ts +1 -1
  41. package/lib/bridge/getFeesForTransaction.d.ts.map +1 -1
  42. package/lib/bridge/index.d.ts +1 -1
  43. package/lib/bridge/index.d.ts.map +1 -1
  44. package/lib/bridge/prepareTransaction.d.ts.map +1 -1
  45. package/lib/bridge/prepareTransaction.js +2 -2
  46. package/lib/bridge/prepareTransaction.js.map +1 -1
  47. package/lib/bridge/signOperation.js +3 -3
  48. package/lib/bridge/signOperation.js.map +1 -1
  49. package/lib/bridge/synchronisation.js +3 -3
  50. package/lib/bridge/synchronisation.js.map +1 -1
  51. package/lib/config.d.ts +13 -0
  52. package/lib/config.d.ts.map +1 -0
  53. package/lib/config.js +9 -0
  54. package/lib/config.js.map +1 -0
  55. package/lib/network/client.d.ts +34 -0
  56. package/lib/network/client.d.ts.map +1 -0
  57. package/lib/network/client.js +213 -0
  58. package/lib/network/client.js.map +1 -0
  59. package/lib/network/graphql/queries.d.ts.map +1 -0
  60. package/lib/network/graphql/queries.js.map +1 -0
  61. package/lib/network/graphql/types.d.ts.map +1 -0
  62. package/lib/{api → network}/graphql/types.js.map +1 -1
  63. package/lib/network/index.d.ts +2 -1
  64. package/lib/network/index.d.ts.map +1 -1
  65. package/lib/network/index.js +15 -0
  66. package/lib/network/index.js.map +1 -1
  67. package/lib/types/assets.d.ts +3 -0
  68. package/lib/types/assets.d.ts.map +1 -0
  69. package/lib/types/assets.js +3 -0
  70. package/lib/types/assets.js.map +1 -0
  71. package/lib-es/__tests__/api/index.integ.test.d.ts +2 -0
  72. package/lib-es/__tests__/api/index.integ.test.d.ts.map +1 -0
  73. package/lib-es/__tests__/api/index.integ.test.js +20 -0
  74. package/lib-es/__tests__/api/index.integ.test.js.map +1 -0
  75. package/lib-es/__tests__/api/index.test.js +52 -464
  76. package/lib-es/__tests__/api/index.test.js.map +1 -1
  77. package/lib-es/__tests__/bridge/broadcast.test.js +2 -2
  78. package/lib-es/__tests__/bridge/broadcast.test.js.map +1 -1
  79. package/lib-es/__tests__/bridge/buildTransaction.test.js +2 -2
  80. package/lib-es/__tests__/bridge/buildTransaction.test.js.map +1 -1
  81. package/lib-es/__tests__/bridge/getFeesForTransaction.test.js +2 -2
  82. package/lib-es/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
  83. package/lib-es/__tests__/bridge/prepareTransaction.test.js +2 -2
  84. package/lib-es/__tests__/bridge/prepareTransaction.test.js.map +1 -1
  85. package/lib-es/__tests__/bridge/signOperation.test.js +3 -2
  86. package/lib-es/__tests__/bridge/signOperation.test.js.map +1 -1
  87. package/lib-es/__tests__/bridge/synchronisation.test.js +3 -3
  88. package/lib-es/__tests__/bridge/synchronisation.test.js.map +1 -1
  89. package/lib-es/__tests__/network/client.test.d.ts +2 -0
  90. package/lib-es/__tests__/network/client.test.d.ts.map +1 -0
  91. package/lib-es/__tests__/network/client.test.js +473 -0
  92. package/lib-es/__tests__/network/client.test.js.map +1 -0
  93. package/lib-es/api/index.d.ts +4 -33
  94. package/lib-es/api/index.d.ts.map +1 -1
  95. package/lib-es/api/index.js +24 -194
  96. package/lib-es/api/index.js.map +1 -1
  97. package/lib-es/bridge/broadcast.d.ts +3 -2
  98. package/lib-es/bridge/broadcast.d.ts.map +1 -1
  99. package/lib-es/bridge/broadcast.js +4 -3
  100. package/lib-es/bridge/broadcast.js.map +1 -1
  101. package/lib-es/bridge/buildTransaction.d.ts +1 -1
  102. package/lib-es/bridge/buildTransaction.d.ts.map +1 -1
  103. package/lib-es/bridge/buildTransaction.js.map +1 -1
  104. package/lib-es/bridge/estimateMaxSpendable.js +1 -1
  105. package/lib-es/bridge/estimateMaxSpendable.js.map +1 -1
  106. package/lib-es/bridge/getFeesForTransaction.d.ts +1 -1
  107. package/lib-es/bridge/getFeesForTransaction.d.ts.map +1 -1
  108. package/lib-es/bridge/index.d.ts +1 -1
  109. package/lib-es/bridge/index.d.ts.map +1 -1
  110. package/lib-es/bridge/prepareTransaction.d.ts.map +1 -1
  111. package/lib-es/bridge/prepareTransaction.js +1 -1
  112. package/lib-es/bridge/prepareTransaction.js.map +1 -1
  113. package/lib-es/bridge/signOperation.js +1 -1
  114. package/lib-es/bridge/signOperation.js.map +1 -1
  115. package/lib-es/bridge/synchronisation.js +1 -1
  116. package/lib-es/bridge/synchronisation.js.map +1 -1
  117. package/lib-es/config.d.ts +13 -0
  118. package/lib-es/config.d.ts.map +1 -0
  119. package/lib-es/config.js +4 -0
  120. package/lib-es/config.js.map +1 -0
  121. package/lib-es/network/client.d.ts +34 -0
  122. package/lib-es/network/client.d.ts.map +1 -0
  123. package/lib-es/network/client.js +206 -0
  124. package/lib-es/network/client.js.map +1 -0
  125. package/lib-es/network/graphql/queries.d.ts.map +1 -0
  126. package/lib-es/network/graphql/queries.js.map +1 -0
  127. package/lib-es/network/graphql/types.d.ts.map +1 -0
  128. package/lib-es/{api → network}/graphql/types.js.map +1 -1
  129. package/lib-es/network/index.d.ts +2 -1
  130. package/lib-es/network/index.d.ts.map +1 -1
  131. package/lib-es/network/index.js +1 -0
  132. package/lib-es/network/index.js.map +1 -1
  133. package/lib-es/types/assets.d.ts +3 -0
  134. package/lib-es/types/assets.d.ts.map +1 -0
  135. package/lib-es/types/assets.js +2 -0
  136. package/lib-es/types/assets.js.map +1 -0
  137. package/package.json +2 -1
  138. package/src/__tests__/api/index.integ.test.ts +24 -0
  139. package/src/__tests__/api/index.test.ts +58 -547
  140. package/src/__tests__/bridge/broadcast.test.ts +2 -2
  141. package/src/__tests__/bridge/buildTransaction.test.ts +2 -2
  142. package/src/__tests__/bridge/getFeesForTransaction.test.ts +2 -2
  143. package/src/__tests__/bridge/prepareTransaction.test.ts +2 -2
  144. package/src/__tests__/bridge/signOperation.test.ts +6 -3
  145. package/src/__tests__/bridge/synchronisation.test.ts +4 -4
  146. package/src/__tests__/network/client.test.ts +569 -0
  147. package/src/api/index.ts +30 -250
  148. package/src/bridge/broadcast.ts +7 -7
  149. package/src/bridge/buildTransaction.ts +1 -1
  150. package/src/bridge/estimateMaxSpendable.ts +1 -1
  151. package/src/bridge/getFeesForTransaction.ts +1 -1
  152. package/src/bridge/index.ts +1 -1
  153. package/src/bridge/prepareTransaction.ts +2 -1
  154. package/src/bridge/signOperation.ts +1 -1
  155. package/src/bridge/synchronisation.ts +1 -1
  156. package/src/config.ts +19 -0
  157. package/src/network/client.ts +265 -0
  158. package/src/network/index.ts +2 -1
  159. package/src/types/assets.ts +3 -0
  160. package/lib/api/graphql/queries.d.ts.map +0 -1
  161. package/lib/api/graphql/queries.js.map +0 -1
  162. package/lib/api/graphql/types.d.ts.map +0 -1
  163. package/lib-es/api/graphql/queries.d.ts.map +0 -1
  164. package/lib-es/api/graphql/queries.js.map +0 -1
  165. package/lib-es/api/graphql/types.d.ts.map +0 -1
  166. /package/lib/{api → network}/graphql/queries.d.ts +0 -0
  167. /package/lib/{api → network}/graphql/queries.js +0 -0
  168. /package/lib/{api → network}/graphql/types.d.ts +0 -0
  169. /package/lib/{api → network}/graphql/types.js +0 -0
  170. /package/lib-es/{api → network}/graphql/queries.d.ts +0 -0
  171. /package/lib-es/{api → network}/graphql/queries.js +0 -0
  172. /package/lib-es/{api → network}/graphql/types.d.ts +0 -0
  173. /package/lib-es/{api → network}/graphql/types.js +0 -0
  174. /package/src/{api → network}/graphql/queries.ts +0 -0
  175. /package/src/{api → network}/graphql/types.ts +0 -0
@@ -1,569 +1,80 @@
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 "../../api";
15
- import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
1
+ import { Aptos } from "@aptos-labs/ts-sdk";
2
+ import type { Api } from "@ledgerhq/coin-framework/api/types";
3
+ import type { AptosAsset } from "../../types/assets";
4
+ import type { AptosConfig } from "../../config";
5
+ import { createApi } from "../../api";
6
+ import coinConfig from "../../config";
16
7
 
17
8
  jest.mock("@aptos-labs/ts-sdk");
18
- jest.mock("@apollo/client");
19
9
  let mockedAptos: jest.Mocked<any>;
20
- let mockedApolloClient: jest.Mocked<any>;
21
- let mockedPost = jest.fn();
22
10
 
23
- jest.mock("@ledgerhq/live-network/network");
24
- const mockedNetwork = jest.mocked(network);
11
+ jest.mock("../../config", () => ({
12
+ setCoinConfig: jest.fn(),
13
+ }));
25
14
 
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
- });
15
+ const mockAptosConfig: AptosConfig = {} as AptosConfig;
114
16
 
115
- it("get coin balance", async () => {
116
- mockedAptos.mockImplementation(() => ({
117
- view: jest.fn().mockReturnValue(["1234"]),
118
- }));
17
+ describe("createApi", () => {
18
+ it("should set the coin config value", () => {
19
+ const setCoinConfigSpy = jest.spyOn(coinConfig, "setCoinConfig");
119
20
 
120
- token.tokenType = "coin";
121
- const api = new AptosAPI("aptos");
122
- const balance = await api.getBalance("address", token);
21
+ createApi(mockAptosConfig);
123
22
 
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
- });
23
+ const config = setCoinConfigSpy.mock.calls[0][0]();
139
24
 
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
- }));
25
+ expect(setCoinConfigSpy).toHaveBeenCalled();
146
26
 
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
- });
27
+ expect(config).toEqual(
28
+ expect.objectContaining({
29
+ ...mockAptosConfig,
30
+ status: { type: "active" },
31
+ }),
32
+ );
154
33
  });
155
34
 
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
- }));
35
+ it("should return an API object with alpaca api methods", () => {
36
+ const api: Api<AptosAsset> = createApi(mockAptosConfig);
299
37
 
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
- });
38
+ // Check that methods are set with what we expect
39
+ expect(api.broadcast).toBeDefined();
40
+ expect(api.combine).toBeDefined();
41
+ expect(api.craftTransaction).toBeDefined();
42
+ expect(api.estimateFees).toBeDefined();
43
+ expect(api.getBalance).toBeDefined();
44
+ expect(api.lastBlock).toBeDefined();
45
+ expect(api.listOperations).toBeDefined();
409
46
  });
47
+ });
410
48
 
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
- });
49
+ describe("lastBlock", () => {
50
+ beforeEach(() => {
51
+ mockedAptos = jest.mocked(Aptos);
506
52
  });
507
53
 
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
- });
54
+ afterEach(() => {
55
+ jest.resetAllMocks();
546
56
  });
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
57
 
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
- });
58
+ it("returns the last block information", async () => {
59
+ mockedAptos.mockImplementation(() => ({
60
+ getLedgerInfo: jest.fn().mockReturnValue({
61
+ block_height: "123",
62
+ }),
63
+ getBlockByHeight: jest.fn().mockReturnValue({
64
+ block_height: "123",
65
+ block_hash: "123hash",
66
+ block_timestamp: "1746021098623892",
67
+ first_version: "1",
68
+ last_version: "1",
69
+ }),
70
+ }));
71
+
72
+ const api: Api<AptosAsset> = createApi(mockAptosConfig);
73
+
74
+ expect(await api.lastBlock()).toStrictEqual({
75
+ height: 123,
76
+ hash: "123hash",
77
+ time: new Date(1746021098623892),
567
78
  });
568
79
  });
569
80
  });
@@ -1,10 +1,10 @@
1
1
  import broadcast from "../../bridge/broadcast";
2
- import { AptosAPI } from "../../api";
2
+ import { AptosAPI } from "../../network";
3
3
  import { patchOperationWithHash } from "@ledgerhq/coin-framework/operation";
4
4
  import type { Account, Operation, SignedOperation } from "@ledgerhq/types-live";
5
5
  import BigNumber from "bignumber.js";
6
6
 
7
- jest.mock("../../api");
7
+ jest.mock("../../network");
8
8
  jest.mock("@ledgerhq/coin-framework/operation");
9
9
 
10
10
  describe("broadcast", () => {
@@ -5,7 +5,7 @@ import {
5
5
  createFixtureTransactionWithSubAccount,
6
6
  } from "../../bridge/bridge.fixture";
7
7
  import buildTransaction from "../../bridge/buildTransaction";
8
- import { AptosAPI } from "../../api";
8
+ import { AptosAPI } from "../../network";
9
9
  import { normalizeTransactionOptions } from "../../bridge/logic";
10
10
  import { InputEntryFunctionData } from "@aptos-labs/ts-sdk";
11
11
  import { TransactionOptions } from "../../types";
@@ -21,7 +21,7 @@ jest.mock("../../bridge/logic", () => ({
21
21
  DEFAULT_GAS_PRICE: 200,
22
22
  }));
23
23
 
24
- jest.mock("../../api", () => {
24
+ jest.mock("../../network", () => {
25
25
  return {
26
26
  AptosAPI: function () {
27
27
  return {
@@ -6,11 +6,11 @@ import {
6
6
  createFixtureTransactionWithSubAccount,
7
7
  } from "../../bridge/bridge.fixture";
8
8
  import * as getFeesForTransaction from "../../bridge/getFeesForTransaction";
9
- import { AptosAPI } from "../../api";
9
+ import { AptosAPI } from "../../network";
10
10
 
11
11
  let simulateTransaction = jest.fn();
12
12
 
13
- jest.mock("../../api", () => {
13
+ jest.mock("../../network", () => {
14
14
  return {
15
15
  AptosAPI: function () {
16
16
  return {
@@ -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