@ledgerhq/coin-evm 9.0.1 → 10.1.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 (275) hide show
  1. package/.oxlintrc.json +2 -6
  2. package/CHANGELOG.md +18 -0
  3. package/lib/abis/index.d.ts +8 -0
  4. package/lib/abis/index.d.ts.map +1 -1
  5. package/lib/abis/index.js +30 -0
  6. package/lib/abis/index.js.map +1 -1
  7. package/lib/adapters/blockOperations.js +2 -2
  8. package/lib/adapters/blockOperations.js.map +1 -1
  9. package/lib/api/index.d.ts.map +1 -1
  10. package/lib/api/index.js +2 -1
  11. package/lib/api/index.js.map +1 -1
  12. package/lib/internalTxSources.d.ts.map +1 -1
  13. package/lib/internalTxSources.js +21 -3
  14. package/lib/internalTxSources.js.map +1 -1
  15. package/lib/logic/common.d.ts.map +1 -1
  16. package/lib/logic/common.js +11 -3
  17. package/lib/logic/common.js.map +1 -1
  18. package/lib/logic/craftTransaction.d.ts.map +1 -1
  19. package/lib/logic/craftTransaction.js +4 -1
  20. package/lib/logic/craftTransaction.js.map +1 -1
  21. package/lib/logic/estimateFees.d.ts.map +1 -1
  22. package/lib/logic/estimateFees.js +4 -2
  23. package/lib/logic/estimateFees.js.map +1 -1
  24. package/lib/logic/getBalance.js +1 -1
  25. package/lib/logic/getBalance.js.map +1 -1
  26. package/lib/logic/internalTransactionsFetcher.js +2 -2
  27. package/lib/logic/internalTransactionsFetcher.js.map +1 -1
  28. package/lib/logic/listOperations.js +1 -1
  29. package/lib/logic/listOperations.js.map +1 -1
  30. package/lib/logic/predicates.d.ts +17 -0
  31. package/lib/logic/predicates.d.ts.map +1 -0
  32. package/lib/logic/predicates.js +126 -0
  33. package/lib/logic/predicates.js.map +1 -0
  34. package/lib/logic/validateIntent.d.ts.map +1 -1
  35. package/lib/logic/validateIntent.js +4 -6
  36. package/lib/logic/validateIntent.js.map +1 -1
  37. package/lib/network/explorer/index.d.ts +1 -1
  38. package/lib/network/explorer/index.d.ts.map +1 -1
  39. package/lib/network/explorer/index.js +2 -2
  40. package/lib/network/explorer/index.js.map +1 -1
  41. package/lib/network/explorer/ledger.js +3 -3
  42. package/lib/network/explorer/ledger.js.map +1 -1
  43. package/lib/network/explorer/types.d.ts.map +1 -1
  44. package/lib/network/explorer/types.js +11 -1
  45. package/lib/network/explorer/types.js.map +1 -1
  46. package/lib/network/gasTracker/ledger.d.ts +1 -2
  47. package/lib/network/gasTracker/ledger.d.ts.map +1 -1
  48. package/lib/network/gasTracker/ledger.js +3 -5
  49. package/lib/network/gasTracker/ledger.js.map +1 -1
  50. package/lib/network/gasTracker/types.d.ts +1 -2
  51. package/lib/network/gasTracker/types.d.ts.map +1 -1
  52. package/lib/network/gasTracker/types.js.map +1 -1
  53. package/lib/network/node/explorerProxy.d.ts.map +1 -1
  54. package/lib/network/node/explorerProxy.js +10 -5
  55. package/lib/network/node/explorerProxy.js.map +1 -1
  56. package/lib/network/node/gethCallTracerToTraceBlockItems.d.ts.map +1 -1
  57. package/lib/network/node/gethCallTracerToTraceBlockItems.js +10 -12
  58. package/lib/network/node/gethCallTracerToTraceBlockItems.js.map +1 -1
  59. package/lib/network/node/index.js +1 -1
  60. package/lib/network/node/index.js.map +1 -1
  61. package/lib/network/node/ledger.d.ts.map +1 -1
  62. package/lib/network/node/ledger.js +11 -7
  63. package/lib/network/node/ledger.js.map +1 -1
  64. package/lib/network/node/rpc.common.d.ts.map +1 -1
  65. package/lib/network/node/rpc.common.js +60 -70
  66. package/lib/network/node/rpc.common.js.map +1 -1
  67. package/lib/network/node/types.d.ts +6 -7
  68. package/lib/network/node/types.d.ts.map +1 -1
  69. package/lib/network/node/types.js +0 -27
  70. package/lib/network/node/types.js.map +1 -1
  71. package/lib/nodeSources.d.ts.map +1 -1
  72. package/lib/nodeSources.js +11 -3
  73. package/lib/nodeSources.js.map +1 -1
  74. package/lib/staking/contracts.d.ts.map +1 -1
  75. package/lib/staking/contracts.js +1 -3
  76. package/lib/staking/contracts.js.map +1 -1
  77. package/lib/staking/detectOperationType.d.ts +0 -3
  78. package/lib/staking/detectOperationType.d.ts.map +1 -1
  79. package/lib/staking/detectOperationType.js +15 -14
  80. package/lib/staking/detectOperationType.js.map +1 -1
  81. package/lib/staking/fetchers.d.ts.map +1 -1
  82. package/lib/staking/fetchers.js +2 -3
  83. package/lib/staking/fetchers.js.map +1 -1
  84. package/lib/staking/operations/index.d.ts.map +1 -1
  85. package/lib/staking/operations/index.js +18 -1
  86. package/lib/staking/operations/index.js.map +1 -1
  87. package/lib/staking/prepareIntents.d.ts.map +1 -1
  88. package/lib/staking/prepareIntents.js +4 -3
  89. package/lib/staking/prepareIntents.js.map +1 -1
  90. package/lib/staking/redelegations.d.ts.map +1 -1
  91. package/lib/staking/redelegations.js +4 -7
  92. package/lib/staking/redelegations.js.map +1 -1
  93. package/lib/staking/unbondings.d.ts.map +1 -1
  94. package/lib/staking/unbondings.js +3 -4
  95. package/lib/staking/unbondings.js.map +1 -1
  96. package/lib/staking/utils.d.ts.map +1 -1
  97. package/lib/staking/utils.js +22 -27
  98. package/lib/staking/utils.js.map +1 -1
  99. package/lib/staking/validators/monad.d.ts.map +1 -1
  100. package/lib/staking/validators/monad.js +16 -8
  101. package/lib/staking/validators/monad.js.map +1 -1
  102. package/lib/staking/validators/monadResolver.d.ts.map +1 -1
  103. package/lib/staking/validators/monadResolver.js +4 -3
  104. package/lib/staking/validators/monadResolver.js.map +1 -1
  105. package/lib/staking/validators/somnia.js +5 -5
  106. package/lib/staking/validators/somnia.js.map +1 -1
  107. package/lib/staking/validators/zero_gravity.js +5 -5
  108. package/lib/staking/validators/zero_gravity.js.map +1 -1
  109. package/lib-es/abis/index.d.ts +8 -0
  110. package/lib-es/abis/index.d.ts.map +1 -1
  111. package/lib-es/abis/index.js +29 -0
  112. package/lib-es/abis/index.js.map +1 -1
  113. package/lib-es/adapters/blockOperations.js +1 -1
  114. package/lib-es/adapters/blockOperations.js.map +1 -1
  115. package/lib-es/api/index.d.ts.map +1 -1
  116. package/lib-es/api/index.js +2 -1
  117. package/lib-es/api/index.js.map +1 -1
  118. package/lib-es/internalTxSources.d.ts.map +1 -1
  119. package/lib-es/internalTxSources.js +21 -3
  120. package/lib-es/internalTxSources.js.map +1 -1
  121. package/lib-es/logic/common.d.ts.map +1 -1
  122. package/lib-es/logic/common.js +11 -3
  123. package/lib-es/logic/common.js.map +1 -1
  124. package/lib-es/logic/craftTransaction.d.ts.map +1 -1
  125. package/lib-es/logic/craftTransaction.js +4 -1
  126. package/lib-es/logic/craftTransaction.js.map +1 -1
  127. package/lib-es/logic/estimateFees.d.ts.map +1 -1
  128. package/lib-es/logic/estimateFees.js +4 -2
  129. package/lib-es/logic/estimateFees.js.map +1 -1
  130. package/lib-es/logic/getBalance.js +1 -1
  131. package/lib-es/logic/getBalance.js.map +1 -1
  132. package/lib-es/logic/internalTransactionsFetcher.js +2 -2
  133. package/lib-es/logic/internalTransactionsFetcher.js.map +1 -1
  134. package/lib-es/logic/listOperations.js +1 -1
  135. package/lib-es/logic/listOperations.js.map +1 -1
  136. package/lib-es/logic/predicates.d.ts +17 -0
  137. package/lib-es/logic/predicates.d.ts.map +1 -0
  138. package/lib-es/logic/predicates.js +114 -0
  139. package/lib-es/logic/predicates.js.map +1 -0
  140. package/lib-es/logic/validateIntent.d.ts.map +1 -1
  141. package/lib-es/logic/validateIntent.js +4 -6
  142. package/lib-es/logic/validateIntent.js.map +1 -1
  143. package/lib-es/network/explorer/index.d.ts +1 -1
  144. package/lib-es/network/explorer/index.d.ts.map +1 -1
  145. package/lib-es/network/explorer/index.js +2 -2
  146. package/lib-es/network/explorer/index.js.map +1 -1
  147. package/lib-es/network/explorer/ledger.js +3 -3
  148. package/lib-es/network/explorer/ledger.js.map +1 -1
  149. package/lib-es/network/explorer/types.d.ts.map +1 -1
  150. package/lib-es/network/explorer/types.js +11 -1
  151. package/lib-es/network/explorer/types.js.map +1 -1
  152. package/lib-es/network/gasTracker/ledger.d.ts +1 -2
  153. package/lib-es/network/gasTracker/ledger.d.ts.map +1 -1
  154. package/lib-es/network/gasTracker/ledger.js +3 -5
  155. package/lib-es/network/gasTracker/ledger.js.map +1 -1
  156. package/lib-es/network/gasTracker/types.d.ts +1 -2
  157. package/lib-es/network/gasTracker/types.d.ts.map +1 -1
  158. package/lib-es/network/gasTracker/types.js.map +1 -1
  159. package/lib-es/network/node/explorerProxy.d.ts.map +1 -1
  160. package/lib-es/network/node/explorerProxy.js +10 -5
  161. package/lib-es/network/node/explorerProxy.js.map +1 -1
  162. package/lib-es/network/node/gethCallTracerToTraceBlockItems.d.ts.map +1 -1
  163. package/lib-es/network/node/gethCallTracerToTraceBlockItems.js +4 -6
  164. package/lib-es/network/node/gethCallTracerToTraceBlockItems.js.map +1 -1
  165. package/lib-es/network/node/index.js +1 -1
  166. package/lib-es/network/node/index.js.map +1 -1
  167. package/lib-es/network/node/ledger.d.ts.map +1 -1
  168. package/lib-es/network/node/ledger.js +11 -7
  169. package/lib-es/network/node/ledger.js.map +1 -1
  170. package/lib-es/network/node/rpc.common.d.ts.map +1 -1
  171. package/lib-es/network/node/rpc.common.js +58 -68
  172. package/lib-es/network/node/rpc.common.js.map +1 -1
  173. package/lib-es/network/node/types.d.ts +6 -7
  174. package/lib-es/network/node/types.d.ts.map +1 -1
  175. package/lib-es/network/node/types.js +0 -25
  176. package/lib-es/network/node/types.js.map +1 -1
  177. package/lib-es/nodeSources.d.ts.map +1 -1
  178. package/lib-es/nodeSources.js +11 -3
  179. package/lib-es/nodeSources.js.map +1 -1
  180. package/lib-es/staking/contracts.d.ts.map +1 -1
  181. package/lib-es/staking/contracts.js +1 -3
  182. package/lib-es/staking/contracts.js.map +1 -1
  183. package/lib-es/staking/detectOperationType.d.ts +0 -3
  184. package/lib-es/staking/detectOperationType.d.ts.map +1 -1
  185. package/lib-es/staking/detectOperationType.js +15 -14
  186. package/lib-es/staking/detectOperationType.js.map +1 -1
  187. package/lib-es/staking/fetchers.d.ts.map +1 -1
  188. package/lib-es/staking/fetchers.js +2 -3
  189. package/lib-es/staking/fetchers.js.map +1 -1
  190. package/lib-es/staking/operations/index.d.ts.map +1 -1
  191. package/lib-es/staking/operations/index.js +18 -1
  192. package/lib-es/staking/operations/index.js.map +1 -1
  193. package/lib-es/staking/prepareIntents.d.ts.map +1 -1
  194. package/lib-es/staking/prepareIntents.js +2 -1
  195. package/lib-es/staking/prepareIntents.js.map +1 -1
  196. package/lib-es/staking/redelegations.d.ts.map +1 -1
  197. package/lib-es/staking/redelegations.js +1 -4
  198. package/lib-es/staking/redelegations.js.map +1 -1
  199. package/lib-es/staking/unbondings.d.ts.map +1 -1
  200. package/lib-es/staking/unbondings.js +3 -4
  201. package/lib-es/staking/unbondings.js.map +1 -1
  202. package/lib-es/staking/utils.d.ts.map +1 -1
  203. package/lib-es/staking/utils.js +20 -25
  204. package/lib-es/staking/utils.js.map +1 -1
  205. package/lib-es/staking/validators/monad.d.ts.map +1 -1
  206. package/lib-es/staking/validators/monad.js +13 -5
  207. package/lib-es/staking/validators/monad.js.map +1 -1
  208. package/lib-es/staking/validators/monadResolver.d.ts.map +1 -1
  209. package/lib-es/staking/validators/monadResolver.js +2 -1
  210. package/lib-es/staking/validators/monadResolver.js.map +1 -1
  211. package/lib-es/staking/validators/somnia.js +3 -3
  212. package/lib-es/staking/validators/somnia.js.map +1 -1
  213. package/lib-es/staking/validators/zero_gravity.js +3 -3
  214. package/lib-es/staking/validators/zero_gravity.js.map +1 -1
  215. package/package.json +2 -2
  216. package/src/abis/index.test.ts +36 -0
  217. package/src/abis/index.ts +30 -0
  218. package/src/adapters/blockOperations.ts +1 -1
  219. package/src/api/index.cronos.integ.test.ts +28 -0
  220. package/src/api/index.integ.test.ts +57 -94
  221. package/src/api/index.ts +3 -2
  222. package/src/internalTxSources.ts +20 -3
  223. package/src/logic/common.ts +13 -3
  224. package/src/logic/craftTransaction.ts +4 -2
  225. package/src/logic/estimateFees.ts +4 -3
  226. package/src/logic/getBalance.ts +1 -1
  227. package/src/logic/internalTransactionsFetcher.ts +2 -2
  228. package/src/logic/listOperations.ts +1 -1
  229. package/src/logic/predicates.ts +124 -0
  230. package/src/logic/tests/craftTransaction.test.ts +1 -1
  231. package/src/logic/tests/getStakes.test.ts +1 -0
  232. package/src/logic/tests/validateIntent.test.ts +4 -1
  233. package/src/logic/validateIntent.ts +2 -6
  234. package/src/network/explorer/index.test.ts +9 -16
  235. package/src/network/explorer/index.ts +2 -2
  236. package/src/network/explorer/ledger.test.ts +21 -15
  237. package/src/network/explorer/ledger.ts +3 -3
  238. package/src/network/explorer/types.ts +12 -3
  239. package/src/network/gasTracker/ledger.test.ts +0 -10
  240. package/src/network/gasTracker/ledger.ts +2 -7
  241. package/src/network/gasTracker/types.ts +0 -2
  242. package/src/network/node/explorerProxy.ts +11 -8
  243. package/src/network/node/gethCallTracerToTraceBlockItems.test.ts +1 -1
  244. package/src/network/node/gethCallTracerToTraceBlockItems.ts +6 -9
  245. package/src/network/node/index.ts +1 -1
  246. package/src/network/node/ledger.ts +15 -12
  247. package/src/network/node/rpc.common.ts +92 -88
  248. package/src/network/node/rpc.test.ts +119 -0
  249. package/src/network/node/types.ts +6 -35
  250. package/src/nodeSources.ts +11 -3
  251. package/src/staking/contracts.ts +1 -3
  252. package/src/staking/detectOperationType.ts +15 -14
  253. package/src/staking/fetchers.ts +2 -3
  254. package/src/staking/operations/index.test.ts +1 -0
  255. package/src/staking/operations/index.ts +26 -7
  256. package/src/staking/prepareIntents.ts +2 -1
  257. package/src/staking/redelegations.ts +1 -5
  258. package/src/staking/unbondings.ts +2 -4
  259. package/src/staking/utils.ts +19 -26
  260. package/src/staking/validators/monad.test.ts +6 -4
  261. package/src/staking/validators/monad.ts +12 -5
  262. package/src/staking/validators/monadResolver.ts +2 -1
  263. package/src/staking/validators/somnia.test.ts +1 -0
  264. package/src/staking/validators/somnia.ts +5 -5
  265. package/src/staking/validators/zero_gravity.test.ts +1 -0
  266. package/src/staking/validators/zero_gravity.ts +3 -3
  267. package/lib/logic/evmUnit.d.ts +0 -3
  268. package/lib/logic/evmUnit.d.ts.map +0 -1
  269. package/lib/logic/evmUnit.js +0 -94
  270. package/lib/logic/evmUnit.js.map +0 -1
  271. package/lib-es/logic/evmUnit.d.ts +0 -3
  272. package/lib-es/logic/evmUnit.d.ts.map +0 -1
  273. package/lib-es/logic/evmUnit.js +0 -91
  274. package/lib-es/logic/evmUnit.js.map +0 -1
  275. package/src/logic/evmUnit.ts +0 -93
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/coin-evm",
3
- "version": "9.0.1",
3
+ "version": "10.1.0",
4
4
  "description": "Ledger EVM Coin integration",
5
5
  "keywords": [
6
6
  "Ledger",
@@ -112,7 +112,7 @@
112
112
  "license": "Apache-2.0",
113
113
  "dependencies": {
114
114
  "@ethersproject/shims": "^5.7.0",
115
- "axios": "1.13.5",
115
+ "axios": "1.20.0",
116
116
  "bignumber.js": "^9.1.2",
117
117
  "eip55": "^2.1.1",
118
118
  "ethers": "6.15.0",
@@ -0,0 +1,36 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Fragment, Interface } from 'ethers'
5
+ import ERC20_ABI from './erc20.abi.json'
6
+ import { toInterfaceAbi } from './index'
7
+
8
+ describe('toInterfaceAbi', () => {
9
+ it('parses every JSON ABI entry into a Fragment', () => {
10
+ const abi = toInterfaceAbi(ERC20_ABI)
11
+
12
+ expect(Array.isArray(abi)).toBe(true)
13
+ if (!Array.isArray(abi)) return
14
+ expect(abi).toHaveLength(ERC20_ABI.length)
15
+ expect(abi.every((entry) => entry instanceof Fragment)).toBe(true)
16
+ expect(new Interface(abi).getFunction('transfer')?.name).toBe('transfer')
17
+ })
18
+
19
+ it('parses a JSON ABI string and human-readable fragments', () => {
20
+ const fromJson = toInterfaceAbi(JSON.stringify(ERC20_ABI))
21
+ const fromSignature = toInterfaceAbi(['function balanceOf(address) view returns (uint256)'])
22
+
23
+ expect(new Interface(fromJson).getFunction('approve')?.name).toBe('approve')
24
+ expect(new Interface(fromSignature).getFunction('balanceOf')?.name).toBe('balanceOf')
25
+ })
26
+
27
+ it.each([
28
+ ['a number', 42],
29
+ ['a plain object', { type: 'function', name: 'foo' }],
30
+ ['invalid JSON', '{'],
31
+ ['an entry that is not a fragment', [42]],
32
+ ['a null entry', [null]],
33
+ ])('rejects %s', (_label, value) => {
34
+ expect(() => toInterfaceAbi(value)).toThrow('invalid contract ABI')
35
+ })
36
+ })
package/src/abis/index.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import type { InterfaceAbi } from 'ethers'
5
+ import { Fragment } from 'ethers'
4
6
  import ERC20_ABI from './erc20.abi.json'
5
7
  import ERC721_ABI from './erc721.abi.json'
6
8
  import ERC1155_ABI from './erc1155.abi.json'
@@ -14,3 +16,31 @@ export {
14
16
  OPTIMISM_GAS_PRICE_ORACLE_ABI,
15
17
  SCROLL_GAS_PRICE_ORACLE_ABI,
16
18
  }
19
+
20
+ function parseAbiJson(value: string): unknown {
21
+ try {
22
+ return JSON.parse(value)
23
+ } catch {
24
+ throw new Error('invalid contract ABI')
25
+ }
26
+ }
27
+
28
+ function parseAbiFragment(entry: unknown): Fragment {
29
+ try {
30
+ return Fragment.from(entry)
31
+ } catch {
32
+ throw new Error('invalid contract ABI')
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Parse a contract ABI into {@link Fragment} instances.
38
+ *
39
+ * `Array.isArray` only narrows to `any[]`, so each entry is parsed before the
40
+ * value is returned as an `InterfaceAbi`.
41
+ */
42
+ export function toInterfaceAbi(value: unknown): InterfaceAbi {
43
+ const entries = typeof value === 'string' ? parseAbiJson(value) : value
44
+ if (!Array.isArray(entries)) throw new Error('invalid contract ABI')
45
+ return entries.map(parseAbiFragment)
46
+ }
@@ -10,7 +10,7 @@ import type {
10
10
  } from '@ledgerhq/coin-module-framework/api/index'
11
11
  import type { Logger } from '@ledgerhq/coin-module-framework/config'
12
12
  import { safeEncodeEIP55 } from '../logic-public'
13
- import { isTraceBlockCallAction } from '../network/node/types'
13
+ import { isTraceBlockCallAction } from '../logic/predicates'
14
14
  import { NON_VALUE_TRANSFER_CALL_TYPES } from './nonValueTransferCallTypes'
15
15
 
16
16
  /**
@@ -24,6 +24,34 @@ describe('EVM Cronos Network (blockscout explorer)', () => {
24
24
  })
25
25
 
26
26
  describe('listOperations', () => {
27
+ /**
28
+ * FIXME: Cronos's Etherscan-compatible proxy does not honour ascending block-range
29
+ * pagination. Its `txlist` response is descending even with `sort=asc`; after the first
30
+ * cursor (`74012601-0001`), the next request starts at 74013395, so block 61849051 is
31
+ * skipped forever. This is intentionally opt-in (`*.integ.test.ts`) and skipped: unskip it
32
+ * when the explorer is repaired to verify that the pinned historical operation is reachable.
33
+ */
34
+ it.skip('FIXME: reaches the pinned historical operation when paging from genesis', async () => {
35
+ const context = createMockEvmContext(config)
36
+ const address = '0x06B68eAFd87Ce84b2c6A47F0e6bfa6074c042Ab9'
37
+ const options = { minHeight: 0, order: 'asc' as const, limit: 200 }
38
+
39
+ const firstPage = await module.listOperations(context, address, options)
40
+ const firstPageHeights = firstPage.items.map((operation) => operation.tx.block.height)
41
+
42
+ // Pinned historical rows returned by the broken first page.
43
+ expect(firstPageHeights).toEqual(expect.arrayContaining([29433129, 74012600]))
44
+ expect(firstPage.next).toBe('74012601-0001')
45
+
46
+ const secondPage = await module.listOperations(context, address, {
47
+ ...options,
48
+ cursor: firstPage.next,
49
+ })
50
+
51
+ // The current proxy instead starts this page at 74013395, bypassing 61849051.
52
+ expect(secondPage.items.map((operation) => operation.tx.block.height)).toContain(61849051)
53
+ })
54
+
27
55
  it('paginates with distinct cursors across pages', async () => {
28
56
  const page1 = await module.listOperations(
29
57
  createMockEvmContext(config),
@@ -1,7 +1,9 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import type { Operation } from '@ledgerhq/coin-module-framework/api/types'
4
+ import type { MemoNotSupported } from '@ledgerhq/coin-module-framework/api/index'
5
+ import type { BufferTxData, Operation } from '@ledgerhq/coin-module-framework/api/types'
6
+ import { createStakingTransactionIntentMockData } from '@ledgerhq/coin-module-framework/test-utils'
5
7
  import { ethers } from 'ethers'
6
8
  import {
7
9
  createMockEvmConfigInfo,
@@ -19,7 +21,7 @@ describe.each([
19
21
  'external node and explorer',
20
22
  createMockEvmConfigInfo({
21
23
  chainId: 1,
22
- node: mkExternalNode({ uri: 'https://ethereum-rpc.publicnode.com' }),
24
+ node: mkExternalNode({ uri: 'https://gateway.tenderly.co/public/mainnet' }),
23
25
  explorer: mkEtherscanExplorer({ uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1' }),
24
26
  supportedTokens: ['erc721', 'erc1155'],
25
27
  }),
@@ -869,7 +871,7 @@ describe('EVM Api: external node and explorer ONLY', () => {
869
871
  const context = createMockEvmContext(
870
872
  createMockEvmConfigInfo({
871
873
  chainId: 1,
872
- node: mkExternalNode({ uri: 'https://ethereum-rpc.publicnode.com' }),
874
+ node: mkExternalNode({ uri: 'https://gateway.tenderly.co/public/mainnet' }),
873
875
  explorer: mkEtherscanExplorer({ uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1' }),
874
876
  supportedTokens: ['erc721', 'erc1155'],
875
877
  })
@@ -952,53 +954,6 @@ describe('EVM Api: external node and explorer ONLY', () => {
952
954
  })
953
955
  })
954
956
 
955
- describe('EVM Api (Moonbeam Network)', () => {
956
- let module: ReturnType<typeof createApi>
957
- const context = createMockEvmContext(
958
- createMockEvmConfigInfo({
959
- chainId: 1284,
960
- node: mkExternalNode({ uri: 'https://moonbeam.coin.ledger.com' }),
961
- explorer: mkEtherscanExplorer({
962
- uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1284',
963
- }),
964
- })
965
- )
966
-
967
- beforeAll(() => {
968
- module = createApi('moonbeam')
969
- })
970
-
971
- describe('listOperations', () => {
972
- /**
973
- * Non-regression: some Moonbeam transactions (e.g. contract creations) have
974
- * an empty `to` field in the Etherscan API response. The adapter used to emit
975
- * `recipients: [""]` (array with one empty string) instead of `recipients: []`.
976
- * @see https://coin-service.api.ledger.com/v1/moonbeam/account/0x2a9c55b6dc56da178f9f9a566f1161237b73ba66/operations?limit=100
977
- */
978
- it('returns no operations with empty string recipients or senders', async () => {
979
- const { items } = await module.listOperations(
980
- context,
981
- '0x2a9c55b6dc56da178f9f9a566f1161237b73ba66',
982
- {
983
- minHeight: 0,
984
- order: 'desc',
985
- limit: 100,
986
- }
987
- )
988
-
989
- expect(items.length).toBeGreaterThan(0)
990
- items.forEach((op) => {
991
- op.recipients.forEach((r) => {
992
- expect(r).not.toBe('')
993
- })
994
- op.senders.forEach((s) => {
995
- expect(s).not.toBe('')
996
- })
997
- })
998
- })
999
- })
1000
- })
1001
-
1002
957
  describe('EVM Api (SEI Network)', () => {
1003
958
  let module: ReturnType<typeof createApi>
1004
959
  const seiStakingSender = '0x66c4371aE8FFeD2ec1c2EBbbcCfb7E494181E1E3'
@@ -1020,17 +975,19 @@ describe('EVM Api (SEI Network)', () => {
1020
975
 
1021
976
  describe('craftTransaction', () => {
1022
977
  it('crafts a legacy delegate transaction', async () => {
1023
- const { transaction: result } = await module.craftTransaction(context, {
1024
- type: 'staking-legacy',
1025
- intentType: 'staking' as const,
1026
- amount: seiStakingDelegationAmount,
1027
- mode: 'delegate',
1028
- sender: seiStakingSender,
1029
- recipient: seiStakingRecipient,
1030
- valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1031
- data: { type: 'buffer', value: Buffer.from([]) },
1032
- asset: { type: 'native' },
1033
- })
978
+ const { transaction: result } = await module.craftTransaction(
979
+ context,
980
+ createStakingTransactionIntentMockData<MemoNotSupported, BufferTxData>({
981
+ type: 'staking-legacy',
982
+ amount: seiStakingDelegationAmount,
983
+ mode: 'delegate',
984
+ sender: seiStakingSender,
985
+ recipient: seiStakingRecipient,
986
+ valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
987
+ data: { type: 'buffer', value: Buffer.from([]) },
988
+ asset: { type: 'native' },
989
+ })
990
+ )
1034
991
 
1035
992
  expect(result).toMatch(/^0x[A-Fa-f0-9]+$/)
1036
993
  expect(ethers.Transaction.from(result)).toMatchObject({
@@ -1044,17 +1001,19 @@ describe('EVM Api (SEI Network)', () => {
1044
1001
  })
1045
1002
 
1046
1003
  it('crafts an eip1559 delegate transaction', async () => {
1047
- const { transaction: result } = await module.craftTransaction(context, {
1048
- type: 'staking-eip1559',
1049
- intentType: 'staking' as const,
1050
- amount: seiStakingDelegationAmount,
1051
- mode: 'delegate',
1052
- sender: seiStakingSender,
1053
- recipient: seiStakingRecipient,
1054
- valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1055
- data: { type: 'buffer', value: Buffer.from([]) },
1056
- asset: { type: 'native' },
1057
- })
1004
+ const { transaction: result } = await module.craftTransaction(
1005
+ context,
1006
+ createStakingTransactionIntentMockData<MemoNotSupported, BufferTxData>({
1007
+ type: 'staking-eip1559',
1008
+ amount: seiStakingDelegationAmount,
1009
+ mode: 'delegate',
1010
+ sender: seiStakingSender,
1011
+ recipient: seiStakingRecipient,
1012
+ valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1013
+ data: { type: 'buffer', value: Buffer.from([]) },
1014
+ asset: { type: 'native' },
1015
+ })
1016
+ )
1058
1017
 
1059
1018
  expect(result).toMatch(/^0x[A-Fa-f0-9]+$/)
1060
1019
  expect(ethers.Transaction.from(result)).toMatchObject({
@@ -1073,17 +1032,19 @@ describe('EVM Api (SEI Network)', () => {
1073
1032
 
1074
1033
  describe('estimateFees', () => {
1075
1034
  it('estimates fees for legacy staking delegation', async () => {
1076
- const result = await module.estimateFees(context, {
1077
- type: 'staking-legacy',
1078
- intentType: 'staking' as const,
1079
- amount: seiStakingDelegationAmount,
1080
- mode: 'delegate',
1081
- sender: seiStakingSender,
1082
- recipient: seiStakingRecipient,
1083
- valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1084
- data: { type: 'buffer', value: Buffer.from([]) },
1085
- asset: { type: 'native' },
1086
- })
1035
+ const result = await module.estimateFees(
1036
+ context,
1037
+ createStakingTransactionIntentMockData<MemoNotSupported, BufferTxData>({
1038
+ type: 'staking-legacy',
1039
+ amount: seiStakingDelegationAmount,
1040
+ mode: 'delegate',
1041
+ sender: seiStakingSender,
1042
+ recipient: seiStakingRecipient,
1043
+ valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1044
+ data: { type: 'buffer', value: Buffer.from([]) },
1045
+ asset: { type: 'native' },
1046
+ })
1047
+ )
1087
1048
 
1088
1049
  expect(result).toEqual({
1089
1050
  value: expect.any(BigInt),
@@ -1101,17 +1062,19 @@ describe('EVM Api (SEI Network)', () => {
1101
1062
  })
1102
1063
 
1103
1064
  it('estimates fees for eip1559 staking delegation', async () => {
1104
- const result = await module.estimateFees(context, {
1105
- type: 'staking-eip1559',
1106
- intentType: 'staking' as const,
1107
- amount: seiStakingDelegationAmount,
1108
- mode: 'delegate',
1109
- sender: seiStakingSender,
1110
- recipient: seiStakingRecipient,
1111
- valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1112
- data: { type: 'buffer', value: Buffer.from([]) },
1113
- asset: { type: 'native' },
1114
- })
1065
+ const result = await module.estimateFees(
1066
+ context,
1067
+ createStakingTransactionIntentMockData<MemoNotSupported, BufferTxData>({
1068
+ type: 'staking-eip1559',
1069
+ amount: seiStakingDelegationAmount,
1070
+ mode: 'delegate',
1071
+ sender: seiStakingSender,
1072
+ recipient: seiStakingRecipient,
1073
+ valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1074
+ data: { type: 'buffer', value: Buffer.from([]) },
1075
+ asset: { type: 'native' },
1076
+ })
1077
+ )
1115
1078
 
1116
1079
  expect(result).toEqual({
1117
1080
  value: expect.any(BigInt),
package/src/api/index.ts CHANGED
@@ -38,6 +38,7 @@ import {
38
38
  listOperations,
39
39
  validateIntent,
40
40
  } from '../logic/index'
41
+ import { isRecord } from '../logic/predicates'
41
42
  import { validateAddress } from '../logic/validateAddress'
42
43
  import { getNodeApi } from '../network/node'
43
44
  import { getValidatorsPage } from '../staking/validators'
@@ -142,11 +143,11 @@ export function createApi(currencyId: string) {
142
143
  }
143
144
 
144
145
  export function parseCallParams(params: unknown): EvmCallParams {
145
- if (Array.isArray(params) || params === null || typeof params !== 'object') {
146
+ if (!isRecord(params)) {
146
147
  throw new TypeError('Invalid EVM call params: expected an object')
147
148
  }
148
149
 
149
- const { to, data, block } = params as Record<string, unknown>
150
+ const { to, data, block } = params
150
151
  if (typeof to !== 'string' || !isHexString(to, 20)) {
151
152
  throw new TypeError('Invalid EVM call params: "to" must be a 0x-prefixed 20-byte hex address')
152
153
  }
@@ -31,7 +31,8 @@ export function internalTxSourcesFromList(sources: readonly string[]): InternalT
31
31
  throw new Error('internalTxSources: at least one source is required')
32
32
  }
33
33
  const seen = new Set<string>()
34
- sources.forEach((source, index) => {
34
+ const validated: InternalTxSource[] = []
35
+ for (const [index, source] of sources.entries()) {
35
36
  if (!isInternalTxSource(source)) {
36
37
  throw new Error(`internalTxSources: invalid source "${source}"`)
37
38
  }
@@ -42,8 +43,24 @@ export function internalTxSourcesFromList(sources: readonly string[]): InternalT
42
43
  if (seen.has(source)) throw new Error(`internalTxSources: duplicate source "${source}"`)
43
44
  seen.add(source)
44
45
  }
45
- })
46
- return Object.freeze([...sources]) as unknown as InternalTxSourceList
46
+ validated.push(source)
47
+ }
48
+ const frozen = Object.freeze(validated)
49
+ if (isInternalTxSourceList(frozen)) return frozen
50
+ throw new Error('internalTxSources: invalid list')
51
+ }
52
+
53
+ function isInternalTxSourceList(
54
+ sources: readonly InternalTxSource[]
55
+ ): sources is InternalTxSourceList {
56
+ if (sources.length === 0) return false
57
+ const seen = new Set<string>()
58
+ for (const [index, source] of sources.entries()) {
59
+ if (source === 'empty') return index === sources.length - 1
60
+ if (seen.has(source)) return false
61
+ seen.add(source)
62
+ }
63
+ return true
47
64
  }
48
65
 
49
66
  export const DEFAULT_INTERNAL_TX_SOURCES = internalTxSourcesFromList([
@@ -21,9 +21,10 @@ import { getNodeApi } from '../network/node'
21
21
  import { buildStakingTransactionParams, prepareStakingIntent, STAKING_CONTRACTS } from '../staking'
22
22
  import { isNative, TransactionTypes } from '../types'
23
23
  import { getErc20Data } from './getErc20Data'
24
+ import { isRecord } from './predicates'
24
25
 
25
26
  export function isApiGasOptions(options: unknown): options is ApiGasOptions {
26
- if (!options || typeof options !== 'object') return false
27
+ if (!isRecord(options)) return false
27
28
 
28
29
  return (
29
30
  'slow' in options &&
@@ -36,7 +37,7 @@ export function isApiGasOptions(options: unknown): options is ApiGasOptions {
36
37
  }
37
38
 
38
39
  export function isApiFeeData(fees: unknown): fees is ApiFeeData {
39
- if (!fees || typeof fees !== 'object') return false
40
+ if (!isRecord(fees)) return false
40
41
 
41
42
  const isBigIntOrNull = (value: unknown): boolean => value === null || typeof value === 'bigint'
42
43
 
@@ -101,6 +102,15 @@ export function getCallData(intent: TransactionIntent<MemoNotSupported, BufferTx
101
102
  : getErc20Data(intent.recipient, intent.amount)
102
103
  }
103
104
 
105
+ function transactionRecipient(
106
+ asset: TransactionIntent<MemoNotSupported, BufferTxData>['asset'],
107
+ recipient: string
108
+ ): string {
109
+ if (isNative(asset)) return recipient
110
+ if (typeof asset.assetReference === 'string') return asset.assetReference
111
+ throw new Error('token asset is missing assetReference')
112
+ }
113
+
104
114
  function buildSendTxParams(intent: TransactionIntent<MemoNotSupported, BufferTxData>): {
105
115
  to: string
106
116
  data: Buffer
@@ -108,7 +118,7 @@ function buildSendTxParams(intent: TransactionIntent<MemoNotSupported, BufferTxD
108
118
  } {
109
119
  const { amount, asset, recipient } = intent
110
120
  return {
111
- to: isNative(asset) ? recipient : (asset.assetReference as string),
121
+ to: transactionRecipient(asset, recipient),
112
122
  data: getCallData(intent),
113
123
  value: isNative(asset) ? amount : 0n,
114
124
  }
@@ -6,7 +6,6 @@ import type {
6
6
  BufferTxData,
7
7
  CraftedTransaction,
8
8
  FeeEstimation,
9
- FeesStrategy,
10
9
  MemoNotSupported,
11
10
  TransactionIntent,
12
11
  } from '@ledgerhq/coin-module-framework/api/types'
@@ -17,6 +16,7 @@ import { getNodeApi } from '../network/node'
17
16
  import { TransactionTypes } from '../types'
18
17
  import { prepareUnsignedTxParams } from './common'
19
18
  import { getNextSequence } from './getNextSequence'
19
+ import { isFeesStrategy } from './predicates'
20
20
 
21
21
  export async function craftTransaction(
22
22
  context: EvmContext,
@@ -83,7 +83,9 @@ export async function craftTransaction(
83
83
  const node = getNodeApi(config, currencyId, context.logger)
84
84
  const feeData = await node.getFeeData(config, currencyId, {
85
85
  type,
86
- feesStrategy: customFees?.parameters?.feesStrategy as FeesStrategy | undefined,
86
+ feesStrategy: isFeesStrategy(customFees?.parameters?.feesStrategy)
87
+ ? customFees.parameters.feesStrategy
88
+ : undefined,
87
89
  })
88
90
 
89
91
  if (type === TransactionTypes.legacy && feeData.gasPrice) {
@@ -7,7 +7,6 @@ import type { ApiFeeData, ApiGasOptions, FeeData, GasOptions } from '../types'
7
7
  import type {
8
8
  BufferTxData,
9
9
  FeeEstimation,
10
- FeesStrategy,
11
10
  MemoNotSupported,
12
11
  TransactionIntent,
13
12
  } from '@ledgerhq/coin-module-framework/api/index'
@@ -23,6 +22,7 @@ import { STAKING_CONTRACTS } from '../staking'
23
22
  import { TransactionTypes } from '../types'
24
23
  import { getTransactionType, prepareUnsignedTxParams } from './common'
25
24
  import { getNextSequence } from './getNextSequence'
25
+ import { isFeesStrategy } from './predicates'
26
26
 
27
27
  const SEND_MAX_L1_FEE_BUFFER = new BigNumber(2)
28
28
 
@@ -159,7 +159,9 @@ export async function estimateFees(
159
159
  finalFeeData: FeeData
160
160
  finalGasOptions?: GasOptions
161
161
  }> => {
162
- const feesStrategy = customFeesParameters?.feesStrategy as FeesStrategy | undefined
162
+ const feesStrategy = isFeesStrategy(customFeesParameters?.feesStrategy)
163
+ ? customFeesParameters.feesStrategy
164
+ : undefined
163
165
 
164
166
  if (feesStrategy === 'custom') {
165
167
  return { finalFeeData: extractFeeData(customFeesParameters) }
@@ -173,7 +175,6 @@ export async function estimateFees(
173
175
 
174
176
  const gasTracker = getGasTracker(config)
175
177
  const remoteGasOptions = await gasTracker?.getGasOptions({
176
- currencyId,
177
178
  config,
178
179
  options: { useEIP1559: txType === TransactionTypes.eip1559 },
179
180
  })
@@ -28,7 +28,7 @@ export async function getBalance(
28
28
  ): Promise<Balance[]> {
29
29
  const config = await context.config(currencyId)
30
30
  const nodeApi = getNodeApi(config, currencyId, context.logger)
31
- const explorerApi = getExplorerApi(config, currencyId)
31
+ const explorerApi = getExplorerApi(config)
32
32
 
33
33
  const [nativeBalance, tokensBalances] = await Promise.all([
34
34
  getNativeBalance(currencyId, address, nodeApi),
@@ -42,7 +42,7 @@ async function runInternalTxSources(
42
42
  try {
43
43
  return await fetchers[source](height)
44
44
  } catch (error) {
45
- if ((error as { name?: string })?.name === 'SourceUnavailableError') {
45
+ if (error instanceof SourceUnavailableError) {
46
46
  hadUnavailableSource = true
47
47
  continue
48
48
  }
@@ -111,7 +111,7 @@ export function makeSourceFetchers(
111
111
  if (!isEtherscanLikeExplorerConfig(explorer)) {
112
112
  return Promise.reject(
113
113
  new SourceUnavailableError(
114
- `explorer internal txs not configured for currency ${currencyId}`
114
+ `explorer internal txs not configured for currency ${config.name}`
115
115
  )
116
116
  )
117
117
  }
@@ -60,7 +60,7 @@ export async function listOperations(
60
60
  }
61
61
 
62
62
  const config = await context.config(currencyId)
63
- const explorerApi = getExplorerApi(config, currencyId)
63
+ const explorerApi = getExplorerApi(config)
64
64
  const explorerOrder = options.limit === undefined ? 'desc' : (options.order ?? 'desc')
65
65
  const {
66
66
  lastCoinOperations,