@ledgerhq/coin-evm 9.0.0 → 10.0.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 (145) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/logic/estimateFees.d.ts.map +1 -1
  3. package/lib/logic/estimateFees.js +0 -1
  4. package/lib/logic/estimateFees.js.map +1 -1
  5. package/lib/logic/getBalance.js +1 -1
  6. package/lib/logic/getBalance.js.map +1 -1
  7. package/lib/logic/internalTransactionsFetcher.js +1 -1
  8. package/lib/logic/internalTransactionsFetcher.js.map +1 -1
  9. package/lib/logic/listOperations.js +1 -1
  10. package/lib/logic/listOperations.js.map +1 -1
  11. package/lib/logic/validateIntent.d.ts.map +1 -1
  12. package/lib/logic/validateIntent.js +4 -6
  13. package/lib/logic/validateIntent.js.map +1 -1
  14. package/lib/network/explorer/index.d.ts +1 -1
  15. package/lib/network/explorer/index.d.ts.map +1 -1
  16. package/lib/network/explorer/index.js +2 -2
  17. package/lib/network/explorer/index.js.map +1 -1
  18. package/lib/network/explorer/ledger.js +3 -3
  19. package/lib/network/explorer/ledger.js.map +1 -1
  20. package/lib/network/gasTracker/ledger.d.ts +1 -2
  21. package/lib/network/gasTracker/ledger.d.ts.map +1 -1
  22. package/lib/network/gasTracker/ledger.js +2 -2
  23. package/lib/network/gasTracker/ledger.js.map +1 -1
  24. package/lib/network/gasTracker/types.d.ts +1 -2
  25. package/lib/network/gasTracker/types.d.ts.map +1 -1
  26. package/lib/network/gasTracker/types.js.map +1 -1
  27. package/lib/network/node/index.js +1 -1
  28. package/lib/network/node/index.js.map +1 -1
  29. package/lib/network/node/ledger.d.ts.map +1 -1
  30. package/lib/network/node/ledger.js +1 -2
  31. package/lib/network/node/ledger.js.map +1 -1
  32. package/lib/network/node/rpc.common.d.ts.map +1 -1
  33. package/lib/network/node/rpc.common.js +15 -2
  34. package/lib/network/node/rpc.common.js.map +1 -1
  35. package/lib/network/node/rpc.errors.d.ts +1 -3
  36. package/lib/network/node/rpc.errors.d.ts.map +1 -1
  37. package/lib/network/node/rpc.errors.js +21 -2
  38. package/lib/network/node/rpc.errors.js.map +1 -1
  39. package/lib/staking/fetchers.d.ts.map +1 -1
  40. package/lib/staking/fetchers.js +2 -3
  41. package/lib/staking/fetchers.js.map +1 -1
  42. package/lib/staking/unbondings.d.ts.map +1 -1
  43. package/lib/staking/unbondings.js +3 -4
  44. package/lib/staking/unbondings.js.map +1 -1
  45. package/lib/staking/validators/monad.d.ts.map +1 -1
  46. package/lib/staking/validators/monad.js +1 -2
  47. package/lib/staking/validators/monad.js.map +1 -1
  48. package/lib/staking/validators/somnia.d.ts.map +1 -1
  49. package/lib/staking/validators/somnia.js +1 -2
  50. package/lib/staking/validators/somnia.js.map +1 -1
  51. package/lib/staking/validators/zero_gravity.d.ts.map +1 -1
  52. package/lib/staking/validators/zero_gravity.js +1 -2
  53. package/lib/staking/validators/zero_gravity.js.map +1 -1
  54. package/lib-es/logic/estimateFees.d.ts.map +1 -1
  55. package/lib-es/logic/estimateFees.js +0 -1
  56. package/lib-es/logic/estimateFees.js.map +1 -1
  57. package/lib-es/logic/getBalance.js +1 -1
  58. package/lib-es/logic/getBalance.js.map +1 -1
  59. package/lib-es/logic/internalTransactionsFetcher.js +1 -1
  60. package/lib-es/logic/internalTransactionsFetcher.js.map +1 -1
  61. package/lib-es/logic/listOperations.js +1 -1
  62. package/lib-es/logic/listOperations.js.map +1 -1
  63. package/lib-es/logic/validateIntent.d.ts.map +1 -1
  64. package/lib-es/logic/validateIntent.js +4 -6
  65. package/lib-es/logic/validateIntent.js.map +1 -1
  66. package/lib-es/network/explorer/index.d.ts +1 -1
  67. package/lib-es/network/explorer/index.d.ts.map +1 -1
  68. package/lib-es/network/explorer/index.js +2 -2
  69. package/lib-es/network/explorer/index.js.map +1 -1
  70. package/lib-es/network/explorer/ledger.js +3 -3
  71. package/lib-es/network/explorer/ledger.js.map +1 -1
  72. package/lib-es/network/gasTracker/ledger.d.ts +1 -2
  73. package/lib-es/network/gasTracker/ledger.d.ts.map +1 -1
  74. package/lib-es/network/gasTracker/ledger.js +2 -2
  75. package/lib-es/network/gasTracker/ledger.js.map +1 -1
  76. package/lib-es/network/gasTracker/types.d.ts +1 -2
  77. package/lib-es/network/gasTracker/types.d.ts.map +1 -1
  78. package/lib-es/network/gasTracker/types.js.map +1 -1
  79. package/lib-es/network/node/index.js +1 -1
  80. package/lib-es/network/node/index.js.map +1 -1
  81. package/lib-es/network/node/ledger.d.ts.map +1 -1
  82. package/lib-es/network/node/ledger.js +1 -2
  83. package/lib-es/network/node/ledger.js.map +1 -1
  84. package/lib-es/network/node/rpc.common.d.ts.map +1 -1
  85. package/lib-es/network/node/rpc.common.js +16 -3
  86. package/lib-es/network/node/rpc.common.js.map +1 -1
  87. package/lib-es/network/node/rpc.errors.d.ts +1 -3
  88. package/lib-es/network/node/rpc.errors.d.ts.map +1 -1
  89. package/lib-es/network/node/rpc.errors.js +20 -2
  90. package/lib-es/network/node/rpc.errors.js.map +1 -1
  91. package/lib-es/staking/fetchers.d.ts.map +1 -1
  92. package/lib-es/staking/fetchers.js +2 -3
  93. package/lib-es/staking/fetchers.js.map +1 -1
  94. package/lib-es/staking/unbondings.d.ts.map +1 -1
  95. package/lib-es/staking/unbondings.js +3 -4
  96. package/lib-es/staking/unbondings.js.map +1 -1
  97. package/lib-es/staking/validators/monad.d.ts.map +1 -1
  98. package/lib-es/staking/validators/monad.js +1 -2
  99. package/lib-es/staking/validators/monad.js.map +1 -1
  100. package/lib-es/staking/validators/somnia.d.ts.map +1 -1
  101. package/lib-es/staking/validators/somnia.js +1 -2
  102. package/lib-es/staking/validators/somnia.js.map +1 -1
  103. package/lib-es/staking/validators/zero_gravity.d.ts.map +1 -1
  104. package/lib-es/staking/validators/zero_gravity.js +1 -2
  105. package/lib-es/staking/validators/zero_gravity.js.map +1 -1
  106. package/package.json +3 -2
  107. package/src/api/index.cronos.integ.test.ts +28 -0
  108. package/src/api/index.integ.test.ts +57 -94
  109. package/src/logic/estimateFees.ts +0 -1
  110. package/src/logic/getBalance.ts +1 -1
  111. package/src/logic/internalTransactionsFetcher.ts +1 -1
  112. package/src/logic/listOperations.ts +1 -1
  113. package/src/logic/tests/craftTransaction.test.ts +1 -1
  114. package/src/logic/tests/getStakes.test.ts +1 -0
  115. package/src/logic/validateIntent.ts +2 -6
  116. package/src/network/explorer/index.test.ts +9 -16
  117. package/src/network/explorer/index.ts +2 -2
  118. package/src/network/explorer/ledger.ts +3 -3
  119. package/src/network/gasTracker/ledger.test.ts +0 -10
  120. package/src/network/gasTracker/ledger.ts +1 -3
  121. package/src/network/gasTracker/types.ts +0 -2
  122. package/src/network/node/index.ts +1 -1
  123. package/src/network/node/ledger.ts +1 -2
  124. package/src/network/node/rpc.common.ts +23 -2
  125. package/src/network/node/rpc.errors.test.ts +53 -1
  126. package/src/network/node/rpc.errors.ts +25 -2
  127. package/src/network/node/rpc.test.ts +61 -16
  128. package/src/staking/fetchers.ts +2 -3
  129. package/src/staking/operations/index.test.ts +1 -0
  130. package/src/staking/unbondings.ts +2 -4
  131. package/src/staking/validators/monad.test.ts +6 -4
  132. package/src/staking/validators/monad.ts +1 -2
  133. package/src/staking/validators/somnia.test.ts +1 -0
  134. package/src/staking/validators/somnia.ts +1 -2
  135. package/src/staking/validators/zero_gravity.test.ts +1 -0
  136. package/src/staking/validators/zero_gravity.ts +1 -2
  137. package/lib/logic/evmUnit.d.ts +0 -3
  138. package/lib/logic/evmUnit.d.ts.map +0 -1
  139. package/lib/logic/evmUnit.js +0 -94
  140. package/lib/logic/evmUnit.js.map +0 -1
  141. package/lib-es/logic/evmUnit.d.ts +0 -3
  142. package/lib-es/logic/evmUnit.d.ts.map +0 -1
  143. package/lib-es/logic/evmUnit.js +0 -91
  144. package/lib-es/logic/evmUnit.js.map +0 -1
  145. package/src/logic/evmUnit.ts +0 -93
@@ -23,7 +23,6 @@ const configFor = (currencyId: string): EvmConfigInfo => {
23
23
  return createMockEvmConfigInfo({
24
24
  chainId: 1,
25
25
  node,
26
- // @ts-expect-error — intentional invalid explorer type to test UnknownExplorer error path
27
26
  explorer: { type: 'anything', uri: 'notworking' },
28
27
  })
29
28
  case 'etherscan-coin':
@@ -68,7 +67,7 @@ describe('EVM Family', () => {
68
67
  describe('getExplorerApi', () => {
69
68
  it('should throw when requesting a non existing explorer', () => {
70
69
  try {
71
- getExplorerApi(configFor('anything-coin'), 'anything-coin')
70
+ getExplorerApi(configFor('anything-coin'))
72
71
  fail('Promise should have been rejected')
73
72
  } catch (e) {
74
73
  if (e instanceof AssertionError) {
@@ -79,35 +78,29 @@ describe('EVM Family', () => {
79
78
  })
80
79
 
81
80
  it('should return the etherscan api', () => {
82
- const explorerA = getExplorerApi(configFor('etherscan-coin'), 'etherscan-coin')
83
- const explorerB = getExplorerApi(configFor('blockscout-coin'), 'blockscout-coin')
81
+ const explorerA = getExplorerApi(configFor('etherscan-coin'))
82
+ const explorerB = getExplorerApi(configFor('blockscout-coin'))
84
83
 
85
84
  expect(explorerA).toBe(etherscanLikeApi.explorerApi)
86
85
  expect(explorerB).toBe(etherscanLikeApi.explorerApi)
87
86
  })
88
87
 
89
88
  it('should return the no cache etherscan api', () => {
90
- const explorerA = getExplorerApi(
91
- configFor('etherscan-coin-no-cache'),
92
- 'etherscan-coin-no-cache'
93
- )
94
- const explorerB = getExplorerApi(
95
- configFor('blockscout-coin-no-cache'),
96
- 'blockscout-coin-no-cache'
97
- )
89
+ const explorerA = getExplorerApi(configFor('etherscan-coin-no-cache'))
90
+ const explorerB = getExplorerApi(configFor('blockscout-coin-no-cache'))
98
91
 
99
92
  expect(explorerA).toBe(etherscanLikeApi.explorerApiNoCache)
100
93
  expect(explorerB).toBe(etherscanLikeApi.explorerApiNoCache)
101
94
  })
102
95
 
103
96
  it('should return the ledger api', () => {
104
- const explorerA = getExplorerApi(configFor('ledger-coin'), 'ledger-coin')
97
+ const explorerA = getExplorerApi(configFor('ledger-coin'))
105
98
 
106
99
  expect(explorerA).toBe(ledgerExplorerApi)
107
100
  })
108
101
 
109
102
  it('should return the none api', () => {
110
- const explorerA = getExplorerApi(configFor('none-coin'), 'none-coin')
103
+ const explorerA = getExplorerApi(configFor('none-coin'))
111
104
 
112
105
  expect(explorerA).toBe(noExplorerAPI)
113
106
  })
@@ -117,13 +110,13 @@ describe('EVM Family', () => {
117
110
  ['ledger', 'ledger-coin'],
118
111
  ['none', 'none-coin'],
119
112
  ])('%s arm exposes discoverTokenAssets', (_type, currencyId) => {
120
- const explorer = getExplorerApi(configFor(currencyId), currencyId)
113
+ const explorer = getExplorerApi(configFor(currencyId))
121
114
 
122
115
  expect(typeof explorer.discoverTokenAssets).toBe('function')
123
116
  })
124
117
 
125
118
  it('the none arm resolves discoverTokenAssets to an empty array', async () => {
126
- const explorer = getExplorerApi(configFor('none-coin'), 'none-coin')
119
+ const explorer = getExplorerApi(configFor('none-coin'))
127
120
 
128
121
  await expect(
129
122
  explorer.discoverTokenAssets(configFor('none-coin'), testLogger, 'none-coin', '0xaddress')
@@ -11,7 +11,7 @@ import noExplorerAPI from './none'
11
11
  /**
12
12
  * Switch to select one of the compatible explorer
13
13
  */
14
- export const getExplorerApi = (config: EvmConfigInfo, currencyId: string): ExplorerApi => {
14
+ export const getExplorerApi = (config: EvmConfigInfo): ExplorerApi => {
15
15
  switch (config?.explorer?.type) {
16
16
  case 'etherscan':
17
17
  case 'blockscout':
@@ -28,6 +28,6 @@ export const getExplorerApi = (config: EvmConfigInfo, currencyId: string): Explo
28
28
  return noExplorerAPI
29
29
 
30
30
  default:
31
- throw new UnknownExplorer(`Unknown explorer for currency: ${currencyId}`)
31
+ throw new UnknownExplorer(`Unknown explorer for currency: ${config.name}`)
32
32
  }
33
33
  }
@@ -422,7 +422,7 @@ export const getOperations: ExplorerApi['getOperations'] = async (
422
422
  const { explorer } = config || /* istanbul ignore next */ {}
423
423
  if (!isLedgerExplorerConfig(explorer)) {
424
424
  throw new LedgerExplorerUsedIncorrectly(
425
- `Ledger explorer used incorrectly with currency: ${currencyId}`
425
+ `Ledger explorer used incorrectly with currency: ${config?.name ?? 'unknown'}`
426
426
  )
427
427
  }
428
428
 
@@ -481,14 +481,14 @@ export const getOperations: ExplorerApi['getOperations'] = async (
481
481
  export const discoverTokenAssets: ExplorerApi['discoverTokenAssets'] = async (
482
482
  config,
483
483
  logger,
484
- currencyId,
484
+ _currencyId,
485
485
  address,
486
486
  fromBlock
487
487
  ) => {
488
488
  const { explorer } = config || /* istanbul ignore next */ {}
489
489
  if (!isLedgerExplorerConfig(explorer)) {
490
490
  throw new LedgerExplorerUsedIncorrectly(
491
- `Ledger explorer used incorrectly with currency: ${currencyId}`
491
+ `Ledger explorer used incorrectly with currency: ${config?.name ?? 'unknown'}`
492
492
  )
493
493
  }
494
494
 
@@ -42,7 +42,6 @@ describe('EVM Family', () => {
42
42
  describe('EIP-1559 gas options', () => {
43
43
  it('should return EIP-1559 gas options', async () => {
44
44
  const gasOptions: GasOptions = await getGasOptions({
45
- currencyId: 'ethereum',
46
45
  config: ledgerGasTrackerConfig,
47
46
  options: {
48
47
  useEIP1559: true,
@@ -81,7 +80,6 @@ describe('EVM Family', () => {
81
80
  )
82
81
 
83
82
  const gasOptions: GasOptions = await getGasOptions({
84
- currencyId: 'ethereum',
85
83
  config: { ...ledgerGasTrackerConfig, eip1559BaseFeeMultiplier: 1.5 },
86
84
  options: {
87
85
  useEIP1559: true,
@@ -103,7 +101,6 @@ describe('EVM Family', () => {
103
101
  )
104
102
 
105
103
  const gasOptions: GasOptions = await getGasOptions({
106
- currencyId: 'ethereum',
107
104
  config: ledgerGasTrackerConfig,
108
105
  options: {
109
106
  useEIP1559: true,
@@ -121,7 +118,6 @@ describe('EVM Family', () => {
121
118
 
122
119
  it('should use overrideGasTracker', async () => {
123
120
  await getGasOptions({
124
- currencyId: 'ethereum',
125
121
  config: createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() }),
126
122
  options: {
127
123
  useEIP1559: true,
@@ -139,7 +135,6 @@ describe('EVM Family', () => {
139
135
  describe('legacy gas options', () => {
140
136
  it('should return legacy gas options', async () => {
141
137
  const gasOptions: GasOptions = await getGasOptions({
142
- currencyId: 'ethereum',
143
138
  config: ledgerGasTrackerConfig,
144
139
  options: {
145
140
  useEIP1559: false,
@@ -172,7 +167,6 @@ describe('EVM Family', () => {
172
167
 
173
168
  it('should return legacy gas options when EIP-1559 not supported by gas tracker', async () => {
174
169
  const gasOptions: GasOptions = await getGasOptions({
175
- currencyId: 'ethereum_classic',
176
170
  config: createMockEvmConfigInfo({
177
171
  chainId: 1,
178
172
  node: mkExternalNode(),
@@ -213,7 +207,6 @@ describe('EVM Family', () => {
213
207
  )
214
208
 
215
209
  const gasOptions: GasOptions = await getGasOptions({
216
- currencyId: 'ethereum',
217
210
  config: ledgerGasTrackerConfig,
218
211
  options: {
219
212
  useEIP1559: false,
@@ -229,7 +222,6 @@ describe('EVM Family', () => {
229
222
 
230
223
  it('should use overrideGasTracker', async () => {
231
224
  await getGasOptions({
232
- currencyId: 'ethereum',
233
225
  config: createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() }),
234
226
  options: {
235
227
  useEIP1559: false,
@@ -247,7 +239,6 @@ describe('EVM Family', () => {
247
239
  it("should throw if the gas tracker type isn't ledger", async () => {
248
240
  try {
249
241
  await getGasOptions({
250
- currencyId: 'ethereum',
251
242
  config: createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() }),
252
243
  })
253
244
  fail('Promise should have been rejected')
@@ -262,7 +253,6 @@ describe('EVM Family', () => {
262
253
  it("should throw if the gas tracker explorerId doesn't exist", async () => {
263
254
  try {
264
255
  await getGasOptions({
265
- currencyId: 'ethereum',
266
256
  config: createMockEvmConfigInfo({
267
257
  chainId: 1,
268
258
  node: mkExternalNode(),
@@ -32,11 +32,9 @@ const explorerIdGasTrackerMap = new Map<string, GasTracker>([
32
32
  ])
33
33
 
34
34
  export const getGasOptions = async ({
35
- currencyId,
36
35
  config,
37
36
  options,
38
37
  }: {
39
- currencyId: string
40
38
  config: EvmConfigInfo
41
39
  options?: {
42
40
  useEIP1559: boolean
@@ -54,7 +52,7 @@ export const getGasOptions = async ({
54
52
 
55
53
  const gasTrackerConfig = explorerIdGasTrackerMap.get(gasTracker.explorerId)
56
54
  if (!gasTrackerConfig) {
57
- throw new NoGasTrackerFound(`No gas tracker found for ${currencyId}`)
55
+ throw new NoGasTrackerFound(`No gas tracker found for ${config.name}`)
58
56
  }
59
57
 
60
58
  // We use the eip1559 display parameter only if requested AND the currency supports it
@@ -6,11 +6,9 @@ import type { GasOptions } from '../../types'
6
6
 
7
7
  export type GasTrackerApi = {
8
8
  getGasOptions: ({
9
- currencyId,
10
9
  config,
11
10
  options,
12
11
  }: {
13
- currencyId: string
14
12
  config: EvmConfigInfo
15
13
  options?: {
16
14
  useEIP1559: boolean
@@ -34,7 +34,7 @@ export const getNodeApi = (config: EvmConfigInfo, currencyId: string, logger: Lo
34
34
  const type = node?.type
35
35
 
36
36
  if (type !== 'ledger' && type !== 'external') {
37
- throw new UnknownNode(`Unknown node "${type}" for currency: ${currencyId}`)
37
+ throw new UnknownNode(`Unknown node "${type}" for currency: ${config?.name}`)
38
38
  }
39
39
 
40
40
  const explorerUri = config.ledgerExplorerUri ?? DEFAULT_LEDGER_EXPLORER_URI
@@ -200,11 +200,10 @@ async function getFeeData(
200
200
  _fetch: LedgerFetch,
201
201
  config: LedgerNodeApiConfig,
202
202
  evmConfig: Parameters<NodeApi['getFeeData']>[0],
203
- currencyId: Parameters<NodeApi['getFeeData']>[1],
203
+ _currencyId: Parameters<NodeApi['getFeeData']>[1],
204
204
  transaction: Parameters<NodeApi['getFeeData']>[2]
205
205
  ): Promise<Awaited<ReturnType<NodeApi['getFeeData']>>> {
206
206
  const options = await getGasOptions({
207
- currencyId,
208
207
  config: evmConfig,
209
208
  options: {
210
209
  useEIP1559: evmConfig.forceLegacyTransactions ? false : transaction.type === 2,
@@ -29,6 +29,7 @@ import { withRetries } from '../withRetries'
29
29
  import { gethCallTracerToTraceBlockItems } from './gethCallTracerToTraceBlockItems'
30
30
  import {
31
31
  hasErrorCode,
32
+ isRpcMethodNotAvailableOnChainError,
32
33
  isRpcMethodNotImplementedError,
33
34
  isUnsupportedRpcMethodErrorMsg,
34
35
  isUnsupportedRpcMethodError,
@@ -531,6 +532,20 @@ async function broadcastTransaction(
531
532
  }
532
533
  }
533
534
 
535
+ /**
536
+ * Some pruned nodes (e.g. Alchemy on Astar) answer blocks they do not store with an empty stub block
537
+ * (zero timestamp, no transactions) instead of null. Only the genesis block can have a zero timestamp.
538
+ */
539
+ function assertNotStubBlock(
540
+ blockHeight: number | BlockFinalizationTag,
541
+ height: number,
542
+ timestampInSeconds: number
543
+ ): void {
544
+ if (height > 0 && timestampInSeconds === 0) {
545
+ throw new Error(`Block ${blockHeight} not found (the node returned an empty stub block)`)
546
+ }
547
+ }
548
+
534
549
  async function getBlockByHeight(
535
550
  api: JsonRpcProvider,
536
551
  _currencyId: string,
@@ -557,6 +572,7 @@ async function getBlockByHeight(
557
572
  if (!block) {
558
573
  throw new Error(`Block ${blockHeight} not found`)
559
574
  }
575
+ assertNotStubBlock(blockHeight, block.number, block.timestamp)
560
576
 
561
577
  if (!block.hash) {
562
578
  throw new Error(`Block ${blockHeight} is missing hash`)
@@ -657,6 +673,7 @@ async function getBlockByHeightFromRawRpc(
657
673
 
658
674
  const height = parseRpcHexQuantity(block.number, 'block.number')
659
675
  const timestampInSeconds = parseRpcHexQuantity(block.timestamp, 'block.timestamp')
676
+ assertNotStubBlock(blockHeight, height, timestampInSeconds)
660
677
  const rawTransactions = block.transactions
661
678
  if (rawTransactions !== undefined && !Array.isArray(rawTransactions))
662
679
  throw new Error('Malformed block.transactions in eth_getBlockByNumber response')
@@ -788,7 +805,11 @@ async function traceBlockGeth(
788
805
  const debugResults = await api
789
806
  .send('debug_traceBlockByNumber', [rpcBlockTag, { tracer: 'callTracer' }])
790
807
  .catch((error) => {
791
- if (isUnsupportedRpcMethodError(error) || isUnsupportedRpcMethodErrorMsg(error)) {
808
+ if (
809
+ isUnsupportedRpcMethodError(error) ||
810
+ isUnsupportedRpcMethodErrorMsg(error) ||
811
+ isRpcMethodNotAvailableOnChainError(error)
812
+ ) {
792
813
  throw new UnsupportedRpcMethodError(
793
814
  'debug_traceBlockByNumber is not supported by this RPC provider',
794
815
  {
@@ -810,7 +831,7 @@ async function traceBlockErigon(
810
831
  ): Promise<TraceBlockItem[]> {
811
832
  const blockTag = blockHeight === 'latest' ? 'latest' : ethers.toQuantity(blockHeight)
812
833
  const traces = await api.send('trace_block', [blockTag]).catch((error) => {
813
- if (isUnsupportedRpcMethodError(error)) {
834
+ if (isUnsupportedRpcMethodError(error) || isRpcMethodNotAvailableOnChainError(error)) {
814
835
  throw new UnsupportedRpcMethodError('trace_block is not supported by this RPC provider', {
815
836
  method: 'trace_block',
816
837
  rawError: error,
@@ -1,7 +1,11 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { isRpcMethodNotImplementedError } from './rpc.errors'
4
+ import {
5
+ isRpcMethodNotAvailableOnChainError,
6
+ isRpcMethodNotImplementedError,
7
+ isUnsupportedRpcMethodErrorMsg,
8
+ } from './rpc.errors'
5
9
 
6
10
  describe('isRpcMethodNotImplementedError', () => {
7
11
  it.each([
@@ -28,3 +32,51 @@ describe('isRpcMethodNotImplementedError', () => {
28
32
  expect(isRpcMethodNotImplementedError(error)).toBe(false)
29
33
  })
30
34
  })
35
+
36
+ describe('isUnsupportedRpcMethodErrorMsg', () => {
37
+ it.each([
38
+ [{ code: -32000, message: 'required historical state unavailable (reexec=0)' }],
39
+ [{ code: 27, message: 'Unknown state. First available state is 14823652' }],
40
+ ])('is true for %j', (error) => {
41
+ expect(isUnsupportedRpcMethodErrorMsg(error)).toBe(true)
42
+ })
43
+
44
+ it.each([
45
+ [{ code: -32000, message: 'execution reverted' }],
46
+ [{ code: 'TIMEOUT', message: 'timeout' }],
47
+ ])('is false for %j', (error) => {
48
+ expect(isUnsupportedRpcMethodErrorMsg(error)).toBe(false)
49
+ })
50
+ })
51
+
52
+ describe('isRpcMethodNotAvailableOnChainError', () => {
53
+ const alchemyMessage =
54
+ 'trace_block is not available on the ASTAR_MAINNET. For more information see our docs: https://docs.alchemy.com/alchemy/documentation/apis/ethereum'
55
+
56
+ it.each([
57
+ [{ code: -32600, message: alchemyMessage }],
58
+ [
59
+ {
60
+ code: 'SERVER_ERROR',
61
+ info: {
62
+ responseStatus: '400 Bad Request',
63
+ responseBody: JSON.stringify({
64
+ jsonrpc: '2.0',
65
+ id: 1,
66
+ error: { code: -32600, message: alchemyMessage },
67
+ }),
68
+ },
69
+ },
70
+ ],
71
+ ])('is true for %j', (error) => {
72
+ expect(isRpcMethodNotAvailableOnChainError(error)).toBe(true)
73
+ })
74
+
75
+ it.each([
76
+ [{ code: -32600, message: 'invalid request' }],
77
+ [{ code: -32000, message: 'header not found' }],
78
+ [{ code: 'TIMEOUT', message: 'timeout' }],
79
+ ])('is false for %j', (error) => {
80
+ expect(isRpcMethodNotAvailableOnChainError(error)).toBe(false)
81
+ })
82
+ })
@@ -43,10 +43,33 @@ export function isRpcMethodNotImplementedError(error: unknown): boolean {
43
43
 
44
44
  /**
45
45
  * call this function when RPC returns a generic code, like -32000 which can be used for different errors
46
+ *
47
+ * - "required historical state unavailable": geth node without the state for the requested block
48
+ * - "unknown state": Alchemy node without the state for the requested block
49
+ * (e.g. "Unknown state. First available state is 14823652")
46
50
  */
51
+ const HISTORICAL_STATE_UNAVAILABLE_MARKERS = [
52
+ 'required historical state unavailable',
53
+ 'unknown state',
54
+ ]
55
+
47
56
  export function isUnsupportedRpcMethodErrorMsg(error: unknown): boolean {
48
- const marker = 'required historical state unavailable'
49
- return collectRpcErrorFields(error).messages.some((m) => m.toLowerCase().includes(marker))
57
+ return collectRpcErrorFields(error).messages.some((m) => {
58
+ const message = m.toLowerCase()
59
+ return HISTORICAL_STATE_UNAVAILABLE_MARKERS.some((marker) => message.includes(marker))
60
+ })
61
+ }
62
+
63
+ /**
64
+ * Alchemy rejects methods it does not serve on a chain with a generic -32600 code and a message like
65
+ * "trace_block is not available on the ASTAR_MAINNET".
66
+ */
67
+ const RPC_METHOD_NOT_AVAILABLE_ON_CHAIN_MESSAGE = /\b\w+_\w+ is not available on\b/i
68
+
69
+ export function isRpcMethodNotAvailableOnChainError(error: unknown): boolean {
70
+ return collectRpcErrorFields(error).messages.some((m) =>
71
+ RPC_METHOD_NOT_AVAILABLE_ON_CHAIN_MESSAGE.test(m)
72
+ )
50
73
  }
51
74
 
52
75
  function extractRpcErrorCode(key: string, field: unknown): string | null {
@@ -1218,6 +1218,38 @@ describe('EVM Family', () => {
1218
1218
  })
1219
1219
  })
1220
1220
 
1221
+ it('should throw when the node returns an empty stub block (zero timestamp)', async () => {
1222
+ // not mockResolvedValueOnce: the node API retries on error
1223
+ jest.spyOn(JsonRpcProvider.prototype, 'getBlock').mockResolvedValue(
1224
+ mkBlock({
1225
+ hash: '0x9196eeae33b2e2a2080a9660a1b2554cf01260129756e26fc17e5cbd883bdb61',
1226
+ number: 956855,
1227
+ timestamp: 0,
1228
+ transactions: [],
1229
+ })
1230
+ )
1231
+
1232
+ await expect(nodeApi.getBlockByHeight(currencyId, 956855)).rejects.toThrow(
1233
+ 'Block 956855 not found (the node returned an empty stub block)'
1234
+ )
1235
+ })
1236
+
1237
+ it('should accept a zero timestamp on the genesis block', async () => {
1238
+ jest.spyOn(JsonRpcProvider.prototype, 'getBlock').mockResolvedValueOnce(
1239
+ mkBlock({
1240
+ hash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
1241
+ number: 0,
1242
+ timestamp: 0,
1243
+ transactions: [],
1244
+ })
1245
+ )
1246
+
1247
+ await expect(nodeApi.getBlockByHeight(currencyId, 0)).resolves.toMatchObject({
1248
+ height: 0,
1249
+ timestamp: 0,
1250
+ })
1251
+ })
1252
+
1221
1253
  it('should prefetch block transactions when requested', async () => {
1222
1254
  jest.spyOn(JsonRpcProvider.prototype, 'getBlock').mockResolvedValueOnce(
1223
1255
  mkBlock({
@@ -1499,6 +1531,12 @@ describe('EVM Family', () => {
1499
1531
  message: 'API key is not allowed to access method',
1500
1532
  label: '-32053 (e.g. API key cannot call trace_block)',
1501
1533
  },
1534
+ {
1535
+ code: -32600,
1536
+ message:
1537
+ 'trace_block is not available on the ASTAR_MAINNET. For more information see our docs: https://docs.alchemy.com/alchemy/documentation/apis/ethereum',
1538
+ label: '-32600 (e.g. Alchemy method not available on chain)',
1539
+ },
1502
1540
  ])(
1503
1541
  'should throw UnsupportedRpcMethodError when trace_block returns $label',
1504
1542
  async ({ code, message }) => {
@@ -1588,24 +1626,31 @@ describe('EVM Family', () => {
1588
1626
  }
1589
1627
  )
1590
1628
 
1591
- it('throws UnsupportedRpcMethodError when debug_traceBlockByNumber reports historical state unavailable', async () => {
1592
- jest.spyOn(JsonRpcProvider.prototype, 'send').mockImplementation(async (method: string) => {
1593
- if (method === 'debug_traceBlockByNumber') {
1594
- throw {
1595
- code: -32000,
1596
- message: 'required historical state unavailable (reexec=0)',
1629
+ it.each([
1630
+ { code: -32000, message: 'required historical state unavailable (reexec=0)', label: 'geth' },
1631
+ {
1632
+ code: 27,
1633
+ message: 'Unknown state. First available state is 14823652',
1634
+ label: 'Alchemy',
1635
+ },
1636
+ ])(
1637
+ 'throws UnsupportedRpcMethodError when debug_traceBlockByNumber reports historical state unavailable ($label)',
1638
+ async ({ code, message }) => {
1639
+ jest.spyOn(JsonRpcProvider.prototype, 'send').mockImplementation(async (method: string) => {
1640
+ if (method === 'debug_traceBlockByNumber') {
1641
+ throw { code, message }
1597
1642
  }
1598
- }
1599
- throw new Error(`Method not mocked: ${method}`)
1600
- })
1643
+ throw new Error(`Method not mocked: ${method}`)
1644
+ })
1601
1645
 
1602
- const err = await nodeApi.traceBlockGeth!(currencyId, 1).then(
1603
- () => null,
1604
- (e: unknown) => e
1605
- )
1606
- expect(err).toBeInstanceOf(UnsupportedRpcMethodError)
1607
- expect(err).toMatchObject({ method: 'debug_traceBlockByNumber' })
1608
- })
1646
+ const err = await nodeApi.traceBlockGeth!(currencyId, 1).then(
1647
+ () => null,
1648
+ (e: unknown) => e
1649
+ )
1650
+ expect(err).toBeInstanceOf(UnsupportedRpcMethodError)
1651
+ expect(err).toMatchObject({ method: 'debug_traceBlockByNumber' })
1652
+ }
1653
+ )
1609
1654
  })
1610
1655
 
1611
1656
  describe('getOptimismAdditionalFees', () => {
@@ -11,7 +11,6 @@ import type {
11
11
  import type { Stake, Validator } from '@ledgerhq/coin-module-framework/api/types'
12
12
  import type { Logger } from '@ledgerhq/coin-module-framework/config'
13
13
  import { delay } from '@ledgerhq/coin-module-framework/promises'
14
- import { evmUnit } from '../logic/evmUnit'
15
14
  import { withApi } from '../network/node/rpc.common'
16
15
  import { isExternalNodeConfig } from '../network/node/types'
17
16
  import { encodeStakingData, decodeStakingResult } from './encoder'
@@ -175,7 +174,7 @@ const createStakeFromContract = async (
175
174
  asset: {
176
175
  type: 'native',
177
176
  name: evmConfig.name,
178
- unit: evmUnit[currencyId],
177
+ unit: evmConfig.unit,
179
178
  },
180
179
  amount,
181
180
  actions: [],
@@ -229,7 +228,7 @@ const getStakesForValidators = async (
229
228
  logger: Logger
230
229
  ): Promise<Stake[]> => {
231
230
  if (validators.length === 0) {
232
- console.error('No validators available', { currencyId })
231
+ console.error('No validators available', { currency: evmConfig.name })
233
232
  return []
234
233
  }
235
234
 
@@ -283,6 +283,7 @@ describe('buildTransactionParams', () => {
283
283
 
284
284
  it('should throw error for unsupported operation type', () => {
285
285
  expect(() => {
286
+ // @ts-expect-error invalid operation
286
287
  buildTransactionParams('sei_evm', 'invalidOperation', {
287
288
  valAddress: validatorAddress,
288
289
  amount,
@@ -6,7 +6,6 @@ import type { StakingContractConfig, UnbondingStrategy } from '../types/staking'
6
6
  import type { Stake } from '@ledgerhq/coin-module-framework/api/types'
7
7
  import type { Logger } from '@ledgerhq/coin-module-framework/config'
8
8
  import network from '@ledgerhq/live-network'
9
- import { evmUnit } from '../logic/evmUnit'
10
9
  import { isExternalNodeConfig } from '../network/node/types'
11
10
  import { STAKING_CONTRACTS } from './contracts'
12
11
  import { getCosmosAddr } from './redelegations'
@@ -34,7 +33,6 @@ async function fetchCosmosRestUnbondings(
34
33
  evmConfig: EvmConfigInfo,
35
34
  config: StakingContractConfig,
36
35
  strategy: Extract<UnbondingStrategy, { type: 'cosmos-rest' }>,
37
- currencyId: string,
38
36
  evmAddress: string,
39
37
  logger: Logger
40
38
  ): Promise<Stake[]> {
@@ -101,7 +99,7 @@ async function fetchCosmosRestUnbondings(
101
99
  asset: {
102
100
  type: 'native',
103
101
  name: evmConfig.name,
104
- unit: evmUnit[currencyId],
102
+ unit: evmConfig.unit,
105
103
  },
106
104
  amount,
107
105
  actions: [],
@@ -132,7 +130,7 @@ export async function fetchUnbondings(
132
130
 
133
131
  switch (strategy.type) {
134
132
  case 'cosmos-rest':
135
- return fetchCosmosRestUnbondings(evmConfig, config, strategy, currencyId, evmAddress, logger)
133
+ return fetchCosmosRestUnbondings(evmConfig, config, strategy, evmAddress, logger)
136
134
  default:
137
135
  return []
138
136
  }
@@ -34,6 +34,7 @@ const mockedNetwork = jest.mocked(network)
34
34
  const mockConfig = createMockEvmConfigInfo({
35
35
  chainId: 10143,
36
36
  name: 'Monad',
37
+ unit: { name: 'MON', code: 'MON', magnitude: 18 },
37
38
  node: mkExternalNode({ uri: 'https://monad.rpc/' }),
38
39
  })
39
40
  const ledgerConfig = createMockEvmConfigInfo({ chainId: 10143, node: mkLedgerNode() })
@@ -69,6 +70,9 @@ type CallHandler = (request: {
69
70
  data?: string | Uint8Array | null
70
71
  }) => Promise<string>
71
72
 
73
+ const parseCalldata = (data: string | Uint8Array) =>
74
+ monadIface.parseTransaction({ data: ethers.hexlify(data) })
75
+
72
76
  const setupRpc = (handler: CallHandler) => {
73
77
  const callMock = jest.fn(handler)
74
78
  mockedWithApi.mockImplementation(async (_config, _currencyId, fn) => {
@@ -93,7 +97,7 @@ const routeByName = (responses: {
93
97
  }): CallHandler => {
94
98
  return async ({ data }) => {
95
99
  if (!data) throw new Error('missing data')
96
- const desc = monadIface.parseTransaction({ data })
100
+ const desc = parseCalldata(data)
97
101
  if (!desc) throw new Error('could not parse calldata')
98
102
  if (desc.name === 'getExecutionValidatorSet') {
99
103
  const startIndex: bigint = desc.args[0]
@@ -492,9 +496,7 @@ describe('staking/validators/monad', () => {
492
496
  monadIface.encodeFunctionResult('getEpoch', [epoch, false])
493
497
 
494
498
  const callNames = (callMock: ReturnType<typeof setupRpc>): (string | undefined)[] =>
495
- callMock.mock.calls.map(
496
- ([req]) => monadIface.parseTransaction({ data: req.data ?? '0x' })?.name
497
- )
499
+ callMock.mock.calls.map(([req]) => parseCalldata(req.data ?? '0x')?.name)
498
500
 
499
501
  it('returns one active stake for a single delegation', async () => {
500
502
  setupRpc(
@@ -18,7 +18,6 @@ import type { JsonRpcProvider } from 'ethers'
18
18
  import network from '@ledgerhq/live-network'
19
19
  import { makeLRUCache } from '@ledgerhq/live-network/cache'
20
20
  import { ethers } from 'ethers'
21
- import { evmUnit } from '../../logic/evmUnit'
22
21
  import { withApi } from '../../network/node/rpc.common'
23
22
  import { isExternalNodeConfig } from '../../network/node/types'
24
23
  import { getStakingABI } from '../abis'
@@ -343,7 +342,7 @@ const fetchStakeForValId = async (
343
342
  const asset: AssetInfo = {
344
343
  type: 'native',
345
344
  name: evmConfig.name,
346
- unit: evmUnit[currencyId],
345
+ unit: evmConfig.unit,
347
346
  }
348
347
  const details = {
349
348
  contractAddress,
@@ -28,6 +28,7 @@ const mockConfig = createMockEvmConfigInfo({
28
28
  chainId: 5031,
29
29
  node: mkExternalNode({ uri: 'https://somnia-rpc.publicnode.com' }),
30
30
  name: 'Somnia',
31
+ unit: { name: 'SOMI', code: 'SOMI', magnitude: 18 },
31
32
  })
32
33
  const ledgerConfig = createMockEvmConfigInfo({ chainId: 5031, node: mkLedgerNode() })
33
34