@ledgerhq/coin-aptos 1.10.0-nightly.0 → 2.0.0-next.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 (210) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.unimportedrc.json +18 -6
  3. package/CHANGELOG.md +31 -9
  4. package/jest.config.js +1 -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/types/assets.d.ts +12 -0
  84. package/lib/types/assets.d.ts.map +1 -0
  85. package/lib/types/assets.js +3 -0
  86. package/lib/types/assets.js.map +1 -0
  87. package/lib-es/__tests__/api/index.integ.test.d.ts +2 -0
  88. package/lib-es/__tests__/api/index.integ.test.d.ts.map +1 -0
  89. package/lib-es/__tests__/api/index.integ.test.js +50 -0
  90. package/lib-es/__tests__/api/index.integ.test.js.map +1 -0
  91. package/lib-es/__tests__/api/index.test.js +52 -464
  92. package/lib-es/__tests__/api/index.test.js.map +1 -1
  93. package/lib-es/__tests__/bridge/broadcast.test.js +2 -2
  94. package/lib-es/__tests__/bridge/broadcast.test.js.map +1 -1
  95. package/lib-es/__tests__/bridge/buildTransaction.test.js +2 -2
  96. package/lib-es/__tests__/bridge/buildTransaction.test.js.map +1 -1
  97. package/lib-es/__tests__/bridge/createTransaction.test.js +1 -1
  98. package/lib-es/__tests__/bridge/createTransaction.test.js.map +1 -1
  99. package/lib-es/__tests__/bridge/getFeesForTransaction.test.js +2 -2
  100. package/lib-es/__tests__/bridge/getFeesForTransaction.test.js.map +1 -1
  101. package/lib-es/__tests__/bridge/prepareTransaction.test.js +2 -2
  102. package/lib-es/__tests__/bridge/prepareTransaction.test.js.map +1 -1
  103. package/lib-es/__tests__/bridge/signOperation.test.js +3 -2
  104. package/lib-es/__tests__/bridge/signOperation.test.js.map +1 -1
  105. package/lib-es/__tests__/bridge/synchronisation.test.js +3 -3
  106. package/lib-es/__tests__/bridge/synchronisation.test.js.map +1 -1
  107. package/lib-es/__tests__/network/client.test.d.ts +2 -0
  108. package/lib-es/__tests__/network/client.test.d.ts.map +1 -0
  109. package/lib-es/__tests__/network/client.test.js +515 -0
  110. package/lib-es/__tests__/network/client.test.js.map +1 -0
  111. package/lib-es/api/index.d.ts +4 -33
  112. package/lib-es/api/index.d.ts.map +1 -1
  113. package/lib-es/api/index.js +22 -194
  114. package/lib-es/api/index.js.map +1 -1
  115. package/lib-es/bridge/broadcast.d.ts +3 -2
  116. package/lib-es/bridge/broadcast.d.ts.map +1 -1
  117. package/lib-es/bridge/broadcast.js +4 -3
  118. package/lib-es/bridge/broadcast.js.map +1 -1
  119. package/lib-es/bridge/buildTransaction.d.ts +1 -1
  120. package/lib-es/bridge/buildTransaction.d.ts.map +1 -1
  121. package/lib-es/bridge/buildTransaction.js.map +1 -1
  122. package/lib-es/bridge/createTransaction.js +1 -1
  123. package/lib-es/bridge/createTransaction.js.map +1 -1
  124. package/lib-es/bridge/estimateMaxSpendable.d.ts.map +1 -1
  125. package/lib-es/bridge/estimateMaxSpendable.js +3 -2
  126. package/lib-es/bridge/estimateMaxSpendable.js.map +1 -1
  127. package/lib-es/bridge/getFeesForTransaction.d.ts +1 -1
  128. package/lib-es/bridge/getFeesForTransaction.d.ts.map +1 -1
  129. package/lib-es/bridge/getFeesForTransaction.js +2 -1
  130. package/lib-es/bridge/getFeesForTransaction.js.map +1 -1
  131. package/lib-es/bridge/index.d.ts +1 -1
  132. package/lib-es/bridge/index.d.ts.map +1 -1
  133. package/lib-es/bridge/logic.d.ts +0 -3
  134. package/lib-es/bridge/logic.d.ts.map +1 -1
  135. package/lib-es/bridge/logic.js +0 -3
  136. package/lib-es/bridge/logic.js.map +1 -1
  137. package/lib-es/bridge/prepareTransaction.d.ts.map +1 -1
  138. package/lib-es/bridge/prepareTransaction.js +3 -2
  139. package/lib-es/bridge/prepareTransaction.js.map +1 -1
  140. package/lib-es/bridge/signOperation.js +1 -1
  141. package/lib-es/bridge/signOperation.js.map +1 -1
  142. package/lib-es/bridge/synchronisation.js +1 -1
  143. package/lib-es/bridge/synchronisation.js.map +1 -1
  144. package/lib-es/config.d.ts +13 -0
  145. package/lib-es/config.d.ts.map +1 -0
  146. package/lib-es/config.js +4 -0
  147. package/lib-es/config.js.map +1 -0
  148. package/lib-es/constants.d.ts +4 -0
  149. package/lib-es/constants.d.ts.map +1 -1
  150. package/lib-es/constants.js +4 -0
  151. package/lib-es/constants.js.map +1 -1
  152. package/lib-es/network/client.d.ts +36 -0
  153. package/lib-es/network/client.d.ts.map +1 -0
  154. package/lib-es/network/client.js +240 -0
  155. package/lib-es/network/client.js.map +1 -0
  156. package/lib-es/network/graphql/queries.d.ts.map +1 -0
  157. package/lib-es/network/graphql/queries.js.map +1 -0
  158. package/lib-es/network/graphql/types.d.ts.map +1 -0
  159. package/lib-es/{api → network}/graphql/types.js.map +1 -1
  160. package/lib-es/network/index.d.ts +2 -1
  161. package/lib-es/network/index.d.ts.map +1 -1
  162. package/lib-es/network/index.js +1 -0
  163. package/lib-es/network/index.js.map +1 -1
  164. package/lib-es/types/assets.d.ts +12 -0
  165. package/lib-es/types/assets.d.ts.map +1 -0
  166. package/lib-es/types/assets.js +2 -0
  167. package/lib-es/types/assets.js.map +1 -0
  168. package/package.json +9 -8
  169. package/src/__tests__/api/index.integ.test.ts +58 -0
  170. package/src/__tests__/api/index.test.ts +60 -547
  171. package/src/__tests__/bridge/broadcast.test.ts +2 -2
  172. package/src/__tests__/bridge/buildTransaction.test.ts +2 -2
  173. package/src/__tests__/bridge/createTransaction.test.ts +1 -1
  174. package/src/__tests__/bridge/getFeesForTransaction.test.ts +2 -2
  175. package/src/__tests__/bridge/prepareTransaction.test.ts +2 -2
  176. package/src/__tests__/bridge/signOperation.test.ts +6 -3
  177. package/src/__tests__/bridge/synchronisation.test.ts +4 -4
  178. package/src/__tests__/network/client.test.ts +618 -0
  179. package/src/api/index.ts +31 -250
  180. package/src/bridge/broadcast.ts +7 -7
  181. package/src/bridge/buildTransaction.ts +1 -1
  182. package/src/bridge/createTransaction.ts +1 -1
  183. package/src/bridge/estimateMaxSpendable.ts +3 -2
  184. package/src/bridge/getFeesForTransaction.ts +3 -2
  185. package/src/bridge/index.ts +1 -1
  186. package/src/bridge/logic.ts +0 -4
  187. package/src/bridge/prepareTransaction.ts +4 -2
  188. package/src/bridge/signOperation.ts +1 -1
  189. package/src/bridge/synchronisation.ts +1 -1
  190. package/src/config.ts +19 -0
  191. package/src/constants.ts +5 -0
  192. package/src/network/client.ts +315 -0
  193. package/src/network/index.ts +2 -1
  194. package/src/types/assets.ts +15 -0
  195. package/lib/api/graphql/queries.d.ts.map +0 -1
  196. package/lib/api/graphql/queries.js.map +0 -1
  197. package/lib/api/graphql/types.d.ts.map +0 -1
  198. package/lib-es/api/graphql/queries.d.ts.map +0 -1
  199. package/lib-es/api/graphql/queries.js.map +0 -1
  200. package/lib-es/api/graphql/types.d.ts.map +0 -1
  201. /package/lib/{api → network}/graphql/queries.d.ts +0 -0
  202. /package/lib/{api → network}/graphql/queries.js +0 -0
  203. /package/lib/{api → network}/graphql/types.d.ts +0 -0
  204. /package/lib/{api → network}/graphql/types.js +0 -0
  205. /package/lib-es/{api → network}/graphql/queries.d.ts +0 -0
  206. /package/lib-es/{api → network}/graphql/queries.js +0 -0
  207. /package/lib-es/{api → network}/graphql/types.d.ts +0 -0
  208. /package/lib-es/{api → network}/graphql/types.js +0 -0
  209. /package/src/{api → network}/graphql/queries.ts +0 -0
  210. /package/src/{api → network}/graphql/types.ts +0 -0
@@ -1,569 +1,82 @@
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, AptosExtra, AptosFeeParameters, AptosSender } 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, AptosExtra, AptosSender, AptosFeeParameters> =
37
+ createApi(mockAptosConfig);
299
38
 
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
- });
39
+ // Check that methods are set with what we expect
40
+ expect(api.broadcast).toBeDefined();
41
+ expect(api.combine).toBeDefined();
42
+ expect(api.craftTransaction).toBeDefined();
43
+ expect(api.estimateFees).toBeDefined();
44
+ expect(api.getBalance).toBeDefined();
45
+ expect(api.lastBlock).toBeDefined();
46
+ expect(api.listOperations).toBeDefined();
409
47
  });
48
+ });
410
49
 
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
- });
50
+ describe("lastBlock", () => {
51
+ beforeEach(() => {
52
+ mockedAptos = jest.mocked(Aptos);
506
53
  });
507
54
 
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
- });
55
+ afterEach(() => {
56
+ jest.resetAllMocks();
546
57
  });
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
58
 
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
- });
59
+ it("returns the last block information", async () => {
60
+ mockedAptos.mockImplementation(() => ({
61
+ getLedgerInfo: jest.fn().mockReturnValue({
62
+ block_height: "123",
63
+ }),
64
+ getBlockByHeight: jest.fn().mockReturnValue({
65
+ block_height: "123",
66
+ block_hash: "123hash",
67
+ block_timestamp: "1746021098623892",
68
+ first_version: "1",
69
+ last_version: "1",
70
+ }),
71
+ }));
72
+
73
+ const api: Api<AptosAsset, AptosExtra, AptosSender, AptosFeeParameters> =
74
+ createApi(mockAptosConfig);
75
+
76
+ expect(await api.lastBlock()).toStrictEqual({
77
+ height: 123,
78
+ hash: "123hash",
79
+ time: new Date(1746021098623892),
567
80
  });
568
81
  });
569
82
  });
@@ -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 {
@@ -1,7 +1,7 @@
1
1
  import BigNumber from "bignumber.js";
2
2
  import createTransaction from "../../bridge/createTransaction";
3
3
 
4
- jest.mock("../../bridge/logic", () => ({
4
+ jest.mock("../../constants", () => ({
5
5
  DEFAULT_GAS: 100,
6
6
  DEFAULT_GAS_PRICE: 200,
7
7
  }));
@@ -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 {