@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
@@ -1,499 +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";
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";
15
7
 
16
8
  jest.mock("@aptos-labs/ts-sdk");
17
- jest.mock("@apollo/client");
18
9
  let mockedAptos: jest.Mocked<any>;
19
- let mockedApolloClient: jest.Mocked<any>;
20
- let mockedPost = jest.fn();
21
10
 
22
- jest.mock("@ledgerhq/live-network/network");
23
- const mockedNetwork = jest.mocked(network);
11
+ jest.mock("../../config", () => ({
12
+ setCoinConfig: jest.fn(),
13
+ }));
24
14
 
25
- describe("Aptos API", () => {
26
- beforeEach(() => {
27
- mockedAptos = jest.mocked(Aptos);
28
- mockedApolloClient = jest.mocked(ApolloClient);
29
- mockedPost = jest.mocked(post);
30
- });
15
+ const mockAptosConfig: AptosConfig = {} as AptosConfig;
31
16
 
32
- afterEach(() => jest.clearAllMocks());
17
+ describe("createApi", () => {
18
+ it("should set the coin config value", () => {
19
+ const setCoinConfigSpy = jest.spyOn(coinConfig, "setCoinConfig");
33
20
 
34
- it("builds the client properly for mainnet", () => {
35
- const api = new AptosAPI("aptos");
21
+ createApi(mockAptosConfig);
36
22
 
37
- expect(api.broadcast).toBeDefined();
38
- expect(typeof api.broadcast).toBe("function");
39
- expect(api.estimateGasPrice).toBeDefined();
40
- expect(typeof api.estimateGasPrice).toBe("function");
41
- expect(api.generateTransaction).toBeDefined();
42
- expect(typeof api.generateTransaction).toBe("function");
43
- expect(api.getAccount).toBeDefined();
44
- expect(typeof api.getAccount).toBe("function");
45
- expect(api.getAccountInfo).toBeDefined();
46
- expect(typeof api.getAccountInfo).toBe("function");
47
- expect(api.simulateTransaction).toBeDefined();
48
- expect(typeof api.simulateTransaction).toBe("function");
49
- });
23
+ const config = setCoinConfigSpy.mock.calls[0][0]();
50
24
 
51
- it("builds the client properly for testnet", () => {
52
- const api = new AptosAPI("aptos_testnet");
25
+ expect(setCoinConfigSpy).toHaveBeenCalled();
53
26
 
54
- expect(api.broadcast).toBeDefined();
55
- expect(typeof api.broadcast).toBe("function");
56
- expect(api.estimateGasPrice).toBeDefined();
57
- expect(typeof api.estimateGasPrice).toBe("function");
58
- expect(api.generateTransaction).toBeDefined();
59
- expect(typeof api.generateTransaction).toBe("function");
60
- expect(api.getAccount).toBeDefined();
61
- expect(typeof api.getAccount).toBe("function");
62
- expect(api.getAccountInfo).toBeDefined();
63
- expect(typeof api.getAccountInfo).toBe("function");
64
- expect(api.simulateTransaction).toBeDefined();
65
- expect(typeof api.simulateTransaction).toBe("function");
27
+ expect(config).toEqual(
28
+ expect.objectContaining({
29
+ ...mockAptosConfig,
30
+ status: { type: "active" },
31
+ }),
32
+ );
66
33
  });
67
34
 
68
- describe("getAccount", () => {
69
- it("calls getAccountInfo", async () => {
70
- const mockGetAccountInfo = jest.fn();
71
- mockedAptos.mockImplementation(() => ({
72
- getAccountInfo: mockGetAccountInfo,
73
- }));
74
-
75
- const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount");
76
-
77
- const api = new AptosAPI("aptos");
78
- await api.getAccount("address");
79
-
80
- expect(mockGetAccountSpy).toHaveBeenCalledWith({
81
- accountAddress: "address",
82
- });
83
- });
84
- });
85
-
86
- describe("getAccountInfo", () => {
87
- it("calls getBalance, fetchTransactions and getHeight", async () => {
88
- mockedAptos.mockImplementation(() => ({
89
- view: jest.fn().mockReturnValue(["123"]),
90
- getTransactionByVersion: jest.fn().mockReturnValue({
91
- type: "user_transaction",
92
- version: "v1",
93
- }),
94
- getBlockByVersion: jest.fn().mockReturnValue({
95
- block_height: "1",
96
- block_hash: "83ca6d",
97
- }),
98
- }));
99
-
100
- mockedNetwork.mockResolvedValue(
101
- Promise.resolve({
102
- data: {
103
- account: {
104
- account_number: 1,
105
- sequence: 0,
106
- pub_key: { key: "k", "@type": "type" },
107
- base_account: {
108
- account_number: 2,
109
- sequence: 42,
110
- pub_key: { key: "k2", "@type": "type2" },
111
- },
112
- },
113
- block_height: "999",
114
- },
115
- status: 200,
116
- headers: {} as any,
117
- statusText: "",
118
- config: {
119
- headers: {} as any,
120
- },
121
- }),
122
- );
123
-
124
- mockedApolloClient.mockImplementation(() => ({
125
- query: async () => ({
126
- data: {
127
- address_version_from_move_resources: [{ transaction_version: "v1" }],
128
- },
129
- loading: false,
130
- networkStatus: 7,
131
- }),
132
- }));
133
-
134
- const api = new AptosAPI("aptos");
135
- const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
136
-
137
- expect(accountInfo.balance).toEqual(new BigNumber(123));
138
- expect(accountInfo.transactions).toEqual([
139
- {
140
- type: "user_transaction",
141
- version: "v1",
142
- block: {
143
- height: 1,
144
- hash: "83ca6d",
145
- },
146
- },
147
- ]);
148
- expect(accountInfo.blockHeight).toEqual(999);
149
- });
150
-
151
- it("return balance = 0 if it fails to fetch balance", async () => {
152
- mockedAptos.mockImplementation(() => ({
153
- view: jest.fn().mockImplementation(() => {
154
- throw new Error("error");
155
- }),
156
- getTransactionByVersion: jest.fn().mockReturnValue({
157
- type: "user_transaction",
158
- version: "v1",
159
- }),
160
- getBlockByVersion: jest.fn().mockReturnValue({
161
- block_height: "1",
162
- block_hash: "83ca6d",
163
- }),
164
- }));
165
-
166
- mockedNetwork.mockResolvedValue(
167
- Promise.resolve({
168
- data: {
169
- account: {
170
- account_number: 1,
171
- sequence: 0,
172
- pub_key: { key: "k", "@type": "type" },
173
- base_account: {
174
- account_number: 2,
175
- sequence: 42,
176
- pub_key: { key: "k2", "@type": "type2" },
177
- },
178
- },
179
- block_height: "999",
180
- },
181
- status: 200,
182
- headers: {} as any,
183
- statusText: "",
184
- config: {
185
- headers: {} as any,
186
- },
187
- }),
188
- );
189
-
190
- mockedApolloClient.mockImplementation(() => ({
191
- query: async () => ({
192
- data: {
193
- address_version_from_move_resources: [{ transaction_version: "v1" }],
194
- },
195
- loading: false,
196
- networkStatus: 7,
197
- }),
198
- }));
199
-
200
- const api = new AptosAPI("aptos");
201
- const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
202
-
203
- expect(accountInfo.balance).toEqual(new BigNumber(0));
204
- expect(accountInfo.transactions).toEqual([
205
- {
206
- type: "user_transaction",
207
- version: "v1",
208
- block: {
209
- height: 1,
210
- hash: "83ca6d",
211
- },
212
- },
213
- ]);
214
- expect(accountInfo.blockHeight).toEqual(999);
215
- });
216
-
217
- it("returns no transactions if it the address is empty", async () => {
218
- mockedAptos.mockImplementation(() => ({
219
- view: jest.fn().mockReturnValue(["123"]),
220
- getTransactionByVersion: jest.fn().mockReturnValue({
221
- type: "user_transaction",
222
- version: "v1",
223
- }),
224
- getBlockByVersion: jest.fn().mockReturnValue({
225
- block_height: "1",
226
- block_hash: "83ca6d",
227
- }),
228
- }));
229
-
230
- mockedNetwork.mockResolvedValue(
231
- Promise.resolve({
232
- data: {
233
- account: {
234
- account_number: 1,
235
- sequence: 0,
236
- pub_key: { key: "k", "@type": "type" },
237
- base_account: {
238
- account_number: 2,
239
- sequence: 42,
240
- pub_key: { key: "k2", "@type": "type2" },
241
- },
242
- },
243
- block_height: "999",
244
- },
245
- status: 200,
246
- headers: {} as any,
247
- statusText: "",
248
- config: {
249
- headers: {} as any,
250
- },
251
- }),
252
- );
253
-
254
- mockedApolloClient.mockImplementation(() => ({
255
- query: async () => ({
256
- data: {
257
- address_version_from_move_resources: [{ transaction_version: "v1" }],
258
- },
259
- loading: false,
260
- networkStatus: 7,
261
- }),
262
- }));
263
-
264
- const api = new AptosAPI("aptos");
265
- const accountInfo = await api.getAccountInfo("", "1");
266
-
267
- expect(accountInfo.balance).toEqual(new BigNumber(123));
268
- expect(accountInfo.transactions).toEqual([]);
269
- expect(accountInfo.blockHeight).toEqual(999);
270
- });
271
-
272
- it("returns a null transaction if it fails to getTransactionByVersion", async () => {
273
- mockedAptos.mockImplementation(() => ({
274
- view: jest.fn().mockReturnValue(["123"]),
275
- getTransactionByVersion: jest.fn().mockImplementation(() => {
276
- throw new Error("error");
277
- }),
278
- getBlockByVersion: jest.fn().mockReturnValue({
279
- block_height: "1",
280
- block_hash: "83ca6d",
281
- }),
282
- }));
283
-
284
- mockedNetwork.mockResolvedValue(
285
- Promise.resolve({
286
- data: {
287
- account: {
288
- account_number: 1,
289
- sequence: 0,
290
- pub_key: { key: "k", "@type": "type" },
291
- base_account: {
292
- account_number: 2,
293
- sequence: 42,
294
- pub_key: { key: "k2", "@type": "type2" },
295
- },
296
- },
297
- block_height: "999",
298
- },
299
- status: 200,
300
- headers: {} as any,
301
- statusText: "",
302
- config: {
303
- headers: {} as any,
304
- },
305
- }),
306
- );
307
-
308
- mockedApolloClient.mockImplementation(() => ({
309
- query: async () => ({
310
- data: {
311
- address_version_from_move_resources: [{ transaction_version: "v1" }],
312
- },
313
- loading: false,
314
- networkStatus: 7,
315
- }),
316
- }));
317
-
318
- const api = new AptosAPI("aptos");
319
- const accountInfo = await api.getAccountInfo("APTOS_1_ADDRESS", "1");
35
+ it("should return an API object with alpaca api methods", () => {
36
+ const api: Api<AptosAsset> = createApi(mockAptosConfig);
320
37
 
321
- expect(accountInfo.balance).toEqual(new BigNumber(123));
322
- expect(accountInfo.transactions).toEqual([null]);
323
- expect(accountInfo.blockHeight).toEqual(999);
324
- });
325
- });
326
-
327
- describe("estimateGasPrice", () => {
328
- it("estimates the gas price", async () => {
329
- const gasEstimation = { gas_estimate: 100 };
330
- mockedAptos.mockImplementation(() => ({
331
- getGasPriceEstimation: jest.fn().mockReturnValue(gasEstimation),
332
- }));
333
-
334
- const api = new AptosAPI("aptos");
335
- const gasPrice = await api.estimateGasPrice();
336
-
337
- expect(gasPrice.gas_estimate).toEqual(100);
338
- });
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();
339
46
  });
47
+ });
340
48
 
341
- describe("generateTransaction", () => {
342
- const payload: InputEntryFunctionData = {
343
- function: "0x1::coin::transfer",
344
- functionArguments: ["0x13", 1],
345
- };
346
-
347
- it("generates a transaction without expireTimestamp", async () => {
348
- const options = {
349
- maxGasAmount: "100",
350
- gasUnitPrice: "50",
351
- };
352
-
353
- const mockSimple = jest.fn().mockImplementation(async () => ({
354
- rawTransaction: null,
355
- }));
356
- mockedAptos.mockImplementation(() => ({
357
- transaction: {
358
- build: {
359
- simple: mockSimple,
360
- },
361
- },
362
- }));
363
-
364
- const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
365
-
366
- const api = new AptosAPI("aptos");
367
- await api.generateTransaction("APTOS_1_ADDRESS", payload, options);
368
-
369
- expect(mockSimpleSpy).toHaveBeenCalledWith({
370
- data: payload,
371
- options: {
372
- maxGasAmount: Number(options.maxGasAmount),
373
- gasUnitPrice: Number(options.gasUnitPrice),
374
- },
375
- sender: "APTOS_1_ADDRESS",
376
- });
377
- });
378
-
379
- it("generates a transaction with default expireTimestamp", async () => {
380
- const options = {
381
- maxGasAmount: "100",
382
- gasUnitPrice: "50",
383
- };
384
-
385
- const mockSimple = jest.fn().mockImplementation(async () => ({
386
- rawTransaction: null,
387
- }));
388
- const mockGetLedgerInfo = jest.fn().mockImplementation(async () => ({
389
- ledger_timestamp: "0",
390
- }));
391
- mockedAptos.mockImplementation(() => ({
392
- transaction: {
393
- build: {
394
- simple: mockSimple,
395
- },
396
- },
397
- getLedgerInfo: mockGetLedgerInfo,
398
- }));
399
-
400
- const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
401
-
402
- const api = new AptosAPI("aptos");
403
- await api.generateTransaction("APTOS_1_ADDRESS", payload, options);
404
-
405
- expect(mockSimpleSpy).toHaveBeenCalledWith({
406
- data: payload,
407
- options: {
408
- maxGasAmount: Number(options.maxGasAmount),
409
- gasUnitPrice: Number(options.gasUnitPrice),
410
- expireTimestamp: 120,
411
- },
412
- sender: "APTOS_1_ADDRESS",
413
- });
414
- });
415
-
416
- it("throws an error when failing to build a transaction", async () => {
417
- const options = {
418
- maxGasAmount: "100",
419
- gasUnitPrice: "50",
420
- };
421
-
422
- const mockSimple = jest.fn().mockImplementation(async () => null);
423
- mockedAptos.mockImplementation(() => ({
424
- transaction: {
425
- build: {
426
- simple: mockSimple,
427
- },
428
- },
429
- }));
430
-
431
- const api = new AptosAPI("aptos");
432
- expect(
433
- async () => await api.generateTransaction("APTOS_1_ADDRESS", payload, options),
434
- ).rejects.toThrow();
435
- });
49
+ describe("lastBlock", () => {
50
+ beforeEach(() => {
51
+ mockedAptos = jest.mocked(Aptos);
436
52
  });
437
53
 
438
- describe("simulateTransaction", () => {
439
- it("simulates a transaction with the correct options", async () => {
440
- const mockSimple = jest.fn().mockImplementation(async () => ({
441
- rawTransaction: null,
442
- }));
443
- mockedAptos.mockImplementation(() => ({
444
- transaction: {
445
- simulate: {
446
- simple: mockSimple,
447
- },
448
- },
449
- }));
450
-
451
- const mockSimpleSpy = jest.spyOn({ simple: mockSimple }, "simple");
452
-
453
- const api = new AptosAPI("aptos");
454
- const address = new Ed25519PublicKey("APTOS_1_ADDRESS");
455
- const tx = new RawTransaction(
456
- new AccountAddress(Uint8Array.from(Buffer.from("APTOS_2_ADDRESS"))),
457
- BigInt(1),
458
- "" as unknown as Serializable,
459
- BigInt(100),
460
- BigInt(50),
461
- BigInt(1),
462
- { chainId: 1 } as ChainId,
463
- );
464
- await api.simulateTransaction(address, tx);
465
-
466
- expect(mockSimpleSpy).toHaveBeenCalledWith({
467
- signerPublicKey: address,
468
- transaction: { rawTransaction: tx },
469
- options: {
470
- estimateGasUnitPrice: true,
471
- estimateMaxGasAmount: true,
472
- estimatePrioritizedGasUnitPrice: false,
473
- },
474
- });
475
- });
54
+ afterEach(() => {
55
+ jest.resetAllMocks();
476
56
  });
477
- describe("broadcast", () => {
478
- it("broadcasts the transaction", async () => {
479
- mockedPost.mockImplementation(async () => ({ data: { hash: "ok" } }));
480
- const mockedPostSpy = jest.spyOn({ post: mockedPost }, "post");
481
-
482
- mockedAptos.mockImplementation(() => ({
483
- config: "config",
484
- }));
485
-
486
- const api = new AptosAPI("aptos");
487
- await api.broadcast("signature");
488
57
 
489
- expect(mockedPostSpy).toHaveBeenCalledWith({
490
- contentType: "application/x.aptos.signed_transaction+bcs",
491
- aptosConfig: "config",
492
- body: Uint8Array.from(Buffer.from("signature", "hex")),
493
- path: "transactions",
494
- type: "Fullnode",
495
- originMethod: "",
496
- });
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),
497
78
  });
498
79
  });
499
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", () => {
@@ -1,6 +1,11 @@
1
- import { createFixtureAccount, createFixtureTransaction } from "../../bridge/bridge.fixture";
1
+ import {
2
+ createFixtureAccount,
3
+ createFixtureAccountWithSubAccount,
4
+ createFixtureTransaction,
5
+ createFixtureTransactionWithSubAccount,
6
+ } from "../../bridge/bridge.fixture";
2
7
  import buildTransaction from "../../bridge/buildTransaction";
3
- import { AptosAPI } from "../../api";
8
+ import { AptosAPI } from "../../network";
4
9
  import { normalizeTransactionOptions } from "../../bridge/logic";
5
10
  import { InputEntryFunctionData } from "@aptos-labs/ts-sdk";
6
11
  import { TransactionOptions } from "../../types";
@@ -16,7 +21,7 @@ jest.mock("../../bridge/logic", () => ({
16
21
  DEFAULT_GAS_PRICE: 200,
17
22
  }));
18
23
 
19
- jest.mock("../../api", () => {
24
+ jest.mock("../../network", () => {
20
25
  return {
21
26
  AptosAPI: function () {
22
27
  return {
@@ -26,8 +31,12 @@ jest.mock("../../api", () => {
26
31
  };
27
32
  });
28
33
 
34
+ afterEach(() => {
35
+ jest.clearAllMocks();
36
+ });
37
+
29
38
  describe("buildTransaction Test", () => {
30
- it("should return tx", async () => {
39
+ it("should return transaction for main account", async () => {
31
40
  const account = createFixtureAccount();
32
41
  const transaction = createFixtureTransaction();
33
42
  const aptosClient = new AptosAPI(account.currency.id);
@@ -54,8 +63,81 @@ describe("buildTransaction Test", () => {
54
63
  expect(generateTransactionArgs[1]).toEqual({
55
64
  function: "0x1::aptos_account::transfer_coins",
56
65
  typeArguments: ["0x1::aptos_coin::AptosCoin"],
57
- functionArguments: ["", "0"],
66
+ functionArguments: ["recipient", "0"],
67
+ });
68
+ expect(generateTransactionArgs[2]).toEqual({ maxGasAmount: "100", gasUnitPrice: "200" });
69
+ });
70
+
71
+ it("should return transaction for token account of type fungible asset", async () => {
72
+ const account = createFixtureAccountWithSubAccount("fungible_asset");
73
+ const transaction = createFixtureTransactionWithSubAccount();
74
+ const aptosClient = new AptosAPI(account.currency.id);
75
+ const result = await buildTransaction(account, transaction, aptosClient);
76
+
77
+ const expected = "tx";
78
+
79
+ expect(result).toBe(expected);
80
+
81
+ const mockedNormalizeTransactionOptions = jest.mocked(normalizeTransactionOptions);
82
+
83
+ expect(mockedNormalizeTransactionOptions).toHaveBeenCalledTimes(1);
84
+ expect(generateTransaction).toHaveBeenCalledTimes(1);
85
+
86
+ const generateTransactionArgs: [string, InputEntryFunctionData, TransactionOptions][] =
87
+ generateTransaction.mock.calls[0];
88
+
89
+ expect(mockedNormalizeTransactionOptions.mock.calls[0][0]).toEqual({
90
+ maxGasAmount: "0",
91
+ gasUnitPrice: "0",
92
+ });
93
+
94
+ expect(generateTransactionArgs[0]).toBe("address");
95
+ expect(generateTransactionArgs[1]).toEqual({
96
+ function: "0x1::primary_fungible_store::transfer",
97
+ typeArguments: ["0x1::fungible_asset::Metadata"],
98
+ functionArguments: ["contract_address", "recipient", "0"],
99
+ });
100
+ expect(generateTransactionArgs[2]).toEqual({ maxGasAmount: "100", gasUnitPrice: "200" });
101
+ });
102
+
103
+ it("should return transaction for token account of type coin", async () => {
104
+ const account = createFixtureAccountWithSubAccount("coin");
105
+ const transaction = createFixtureTransactionWithSubAccount();
106
+ const aptosClient = new AptosAPI(account.currency.id);
107
+ const result = await buildTransaction(account, transaction, aptosClient);
108
+
109
+ const expected = "tx";
110
+
111
+ expect(result).toBe(expected);
112
+
113
+ const mockedNormalizeTransactionOptions = jest.mocked(normalizeTransactionOptions);
114
+
115
+ expect(mockedNormalizeTransactionOptions).toHaveBeenCalledTimes(1);
116
+ expect(generateTransaction).toHaveBeenCalledTimes(1);
117
+
118
+ const generateTransactionArgs: [string, InputEntryFunctionData, TransactionOptions][] =
119
+ generateTransaction.mock.calls[0];
120
+
121
+ expect(mockedNormalizeTransactionOptions.mock.calls[0][0]).toEqual({
122
+ maxGasAmount: "0",
123
+ gasUnitPrice: "0",
124
+ });
125
+
126
+ expect(generateTransactionArgs[0]).toBe("address");
127
+ expect(generateTransactionArgs[1]).toEqual({
128
+ function: "0x1::aptos_account::transfer_coins",
129
+ typeArguments: ["contract_address"],
130
+ functionArguments: ["recipient", "0"],
58
131
  });
59
132
  expect(generateTransactionArgs[2]).toEqual({ maxGasAmount: "100", gasUnitPrice: "200" });
60
133
  });
134
+
135
+ it("should throw error if token is not supported", async () => {
136
+ const account = createFixtureAccountWithSubAccount("not_supported_token_type");
137
+ const transaction = createFixtureTransactionWithSubAccount();
138
+ const aptosClient = new AptosAPI(account.currency.id);
139
+ expect(async () => await buildTransaction(account, transaction, aptosClient)).rejects.toThrow(
140
+ "Token type not_supported_token_type not supported",
141
+ );
142
+ });
61
143
  });