@ledgerhq/coin-evm 5.3.2 → 5.3.3

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 (47) hide show
  1. package/.oxlintrc.json +1 -1
  2. package/CHANGELOG.md +7 -0
  3. package/package.json +2 -2
  4. package/src/adapters/blockOperations.test.ts +27 -29
  5. package/src/api/index.flare.integ.test.ts +1 -13
  6. package/src/api/index.integ.test.ts +86 -97
  7. package/src/fixtures/context.fixtures.ts +79 -1
  8. package/src/fixtures/ethers.fixtures.ts +26 -0
  9. package/src/logic/broadcast.integ.test.ts +6 -18
  10. package/src/logic/broadcast.test.ts +19 -31
  11. package/src/logic/broadcastSource.test.ts +6 -7
  12. package/src/logic/common.test.ts +120 -112
  13. package/src/logic/craftTransaction.test.ts +161 -199
  14. package/src/logic/craftTransactionData.test.ts +6 -6
  15. package/src/logic/estimateFees.test.ts +19 -15
  16. package/src/logic/getBalance.test.ts +15 -14
  17. package/src/logic/getBlock.test.ts +372 -382
  18. package/src/logic/getBlockInfo.test.ts +6 -6
  19. package/src/logic/getNextSequence.test.ts +5 -5
  20. package/src/logic/getStakes.test.ts +34 -34
  21. package/src/logic/internalTransactionsFetcher.test.ts +23 -19
  22. package/src/logic/lastBlock.test.ts +29 -21
  23. package/src/logic/listOperations.test.ts +43 -40
  24. package/src/logic/validateIntent.test.ts +151 -101
  25. package/src/logic.test.ts +13 -18
  26. package/src/network/explorer/etherscan.test.ts +5 -5
  27. package/src/network/explorer/index.test.ts +51 -21
  28. package/src/network/explorer/ledger.test.ts +15 -4
  29. package/src/network/gasTracker/index.test.ts +14 -8
  30. package/src/network/gasTracker/ledger.test.ts +25 -13
  31. package/src/network/node/index.memoization.test.ts +25 -31
  32. package/src/network/node/index.test.ts +14 -25
  33. package/src/network/node/ledger.test.ts +45 -60
  34. package/src/network/node/node.fixtures.ts +0 -2
  35. package/src/network/node/rpc.test.ts +152 -177
  36. package/src/staking/encoder.test.ts +13 -21
  37. package/src/staking/logic.test.ts +20 -14
  38. package/src/staking/operations/index.test.ts +28 -28
  39. package/src/staking/prepareIntents.test.ts +26 -20
  40. package/src/staking/redelegations.test.ts +27 -18
  41. package/src/staking/rewards.test.ts +23 -15
  42. package/src/staking/rewardsFromReceipt.test.ts +19 -20
  43. package/src/staking/transactionData.test.ts +22 -22
  44. package/src/staking/validators/monad.test.ts +65 -24
  45. package/src/staking/validators/somnia.test.ts +21 -13
  46. package/src/staking/validators/zero_gravity.test.ts +118 -48
  47. package/src/staking/validators.integ.test.ts +13 -25
@@ -1,8 +1,8 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import type { EvmConfigInfo, EvmContext } from '../config'
5
- import { createMockEvmContext } from '../fixtures/context.fixtures'
4
+ import type { EvmConfigInfo } from '../config'
5
+ import { createMockEvmContext, mkExternalNode, mkLedgerNode } from '../fixtures/context.fixtures'
6
6
  import { getNodeApi } from '../network/node'
7
7
  import { mockNodeApi } from '../network/node/node.fixtures'
8
8
  import { getBlockInfo } from './getBlockInfo'
@@ -26,10 +26,10 @@ describe('getBlockInfo', () => {
26
26
  })
27
27
 
28
28
  describe.each([
29
- ['an external node', 'external', externalMocks],
30
- ['a ledger node', 'ledger', ledgerMocks],
31
- ])('using %s', (_, type, nodeApiMock) => {
32
- const context: EvmContext = createMockEvmContext({ node: { type } } as Partial<EvmConfigInfo>)
29
+ ['an external node', mkExternalNode(), externalMocks],
30
+ ['a ledger node', mkLedgerNode(), ledgerMocks],
31
+ ])('using %s', (_, node, nodeApiMock) => {
32
+ const context = createMockEvmContext({ node })
33
33
 
34
34
  it('returns block info', async () => {
35
35
  nodeApiMock.getBlockByHeight.mockResolvedValueOnce({
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import type { EvmConfigInfo } from '../config'
5
- import { createMockEvmContext } from '../fixtures/context.fixtures'
5
+ import { createMockEvmContext, mkExternalNode, mkLedgerNode } from '../fixtures/context.fixtures'
6
6
  import { getNodeApi } from '../network/node'
7
7
  import { mockNodeApi } from '../network/node/node.fixtures'
8
8
  import { getNextSequence } from './getNextSequence'
@@ -26,10 +26,10 @@ describe('getNextSequence', () => {
26
26
  })
27
27
 
28
28
  it.each([
29
- ['an external node', 'external', externalMocks],
30
- ['a ledger node', 'ledger', ledgerMocks],
31
- ])('returns next sequence for an address using %s', async (_, type, nodeApiMock) => {
32
- const context = createMockEvmContext({ node: { type } } as Partial<EvmConfigInfo>)
29
+ ['an external node', mkExternalNode(), externalMocks],
30
+ ['a ledger node', mkLedgerNode(), ledgerMocks],
31
+ ])('returns next sequence for an address using %s', async (_, node, nodeApiMock) => {
32
+ const context = createMockEvmContext({ node })
33
33
  nodeApiMock.getTransactionCount.mockResolvedValue(42)
34
34
 
35
35
  expect(await getNextSequence(context, '', '')).toEqual(42n)
@@ -1,9 +1,8 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import type { EvmConfigInfo } from '../config'
5
- import type { JsonRpcProvider } from 'ethers'
6
4
  import { createMockEvmContext } from '../fixtures/context.fixtures'
5
+ import { mkJsonRpcProvider, mkResult } from '../fixtures/ethers.fixtures'
7
6
  import { withApi } from '../network/node/rpc.common'
8
7
  import { encodeStakingData, decodeStakingResult } from '../staking/encoder'
9
8
  import { getValidators } from '../staking/validators'
@@ -23,16 +22,16 @@ jest.mock('../staking/validators', () => ({
23
22
  getValidators: jest.fn(),
24
23
  }))
25
24
 
26
- const mockWithApi = withApi as jest.Mock
27
- const mockEncodeStakingData = encodeStakingData as jest.Mock
28
- const mockDecodeStakingResult = decodeStakingResult as jest.Mock
29
- const mockGetValidators = getValidators as jest.Mock
25
+ const mockWithApi = jest.mocked(withApi)
26
+ const mockEncodeStakingData = jest.mocked(encodeStakingData)
27
+ const mockDecodeStakingResult = jest.mocked(decodeStakingResult)
28
+ const mockGetValidators = jest.mocked(getValidators)
30
29
 
31
30
  const externalNodeConfig = { type: 'external' as const, uri: 'https://test' }
32
31
  const context = createMockEvmContext({
33
32
  node: externalNodeConfig,
34
33
  name: 'Celo',
35
- } as Partial<EvmConfigInfo>)
34
+ })
36
35
  const makeValidator = (validatorAddress: string) => ({
37
36
  id: validatorAddress,
38
37
  address: validatorAddress,
@@ -49,12 +48,13 @@ describe('EVM Staking - getStakes', () => {
49
48
 
50
49
  it('should return stake objects with positive amounts for supported currencies', async () => {
51
50
  mockWithApi.mockImplementation(async (_cur, _currencyId, fn) => {
52
- const api = { call: jest.fn().mockResolvedValue('0x') } as unknown as JsonRpcProvider
51
+ const api = mkJsonRpcProvider()
52
+ jest.spyOn(api, 'call').mockResolvedValue('0x')
53
53
  return fn(api)
54
54
  })
55
55
 
56
56
  mockEncodeStakingData.mockReturnValue('0xdeadbeef')
57
- mockDecodeStakingResult.mockReturnValue([{ toString: (): string => '1000000' }] as any)
57
+ mockDecodeStakingResult.mockReturnValue(mkResult({ toString: (): string => '1000000' }))
58
58
 
59
59
  const result = await getStakes(context, 'celo', address)
60
60
 
@@ -81,11 +81,12 @@ describe('EVM Staking - getStakes', () => {
81
81
 
82
82
  it('should filter out stakes with zero amounts', async () => {
83
83
  mockWithApi.mockImplementation(async (_cur, _currencyId, fn) => {
84
- const api = { call: jest.fn().mockResolvedValue('0x') } as unknown as JsonRpcProvider
84
+ const api = mkJsonRpcProvider()
85
+ jest.spyOn(api, 'call').mockResolvedValue('0x')
85
86
  return fn(api)
86
87
  })
87
88
  mockEncodeStakingData.mockReturnValue('0xdeadbeef')
88
- mockDecodeStakingResult.mockReturnValue([{ toString: (): string => '0' }] as any)
89
+ mockDecodeStakingResult.mockReturnValue(mkResult({ toString: (): string => '0' }))
89
90
 
90
91
  const result = await getStakes(context, 'celo', address)
91
92
 
@@ -101,24 +102,25 @@ describe('EVM Staking - getStakes', () => {
101
102
  })
102
103
 
103
104
  mockWithApi.mockImplementation(async (_cur, _currencyId, fn) => {
104
- const api = { call: jest.fn().mockResolvedValue('0x') } as unknown as JsonRpcProvider
105
+ const api = mkJsonRpcProvider()
106
+ jest.spyOn(api, 'call').mockResolvedValue('0x')
105
107
  return fn(api)
106
108
  })
107
109
 
108
110
  mockEncodeStakingData.mockReturnValue('0xdeadbeef')
109
111
 
110
- mockDecodeStakingResult.mockReturnValueOnce([
111
- {
112
+ mockDecodeStakingResult.mockReturnValueOnce(
113
+ mkResult({
112
114
  balance: { amount: '0', denom: 'usei' },
113
115
  delegation: { delegator_address: address, validator_address: 'seivaloper1abc' },
114
- },
115
- ] as any)
116
- mockDecodeStakingResult.mockReturnValueOnce([
117
- {
116
+ })
117
+ )
118
+ mockDecodeStakingResult.mockReturnValueOnce(
119
+ mkResult({
118
120
  balance: { amount: '42', denom: 'usei' },
119
121
  delegation: { delegator_address: address, validator_address: 'seivaloper1def' },
120
- },
121
- ] as any)
122
+ })
123
+ )
122
124
 
123
125
  const result = await getStakes(context, 'sei_evm', address)
124
126
 
@@ -148,9 +150,8 @@ describe('EVM Staking - getStakes', () => {
148
150
 
149
151
  it('should handle RPC call failures gracefully without crashing', async () => {
150
152
  mockWithApi.mockImplementation(async (_cur, _currencyId, fn) => {
151
- const api = {
152
- call: jest.fn().mockRejectedValue(new Error('rpc error')),
153
- } as unknown as JsonRpcProvider
153
+ const api = mkJsonRpcProvider()
154
+ jest.spyOn(api, 'call').mockRejectedValue(new Error('rpc error'))
154
155
  return fn(api)
155
156
  })
156
157
  mockEncodeStakingData.mockReturnValue('0xdeadbeef')
@@ -168,17 +169,16 @@ describe('EVM Staking - getStakes', () => {
168
169
  next: undefined,
169
170
  })
170
171
  mockWithApi.mockImplementation(async (_cur, _currencyId, fn) => {
171
- const api = {
172
- call: jest.fn().mockRejectedValue(
173
- Object.assign(new Error('missing revert data'), {
174
- code: 'CALL_EXCEPTION',
175
- data: null,
176
- reason: null,
177
- revert: null,
178
- shortMessage: 'missing revert data',
179
- })
180
- ),
181
- }
172
+ const api = mkJsonRpcProvider()
173
+ jest.spyOn(api, 'call').mockRejectedValue(
174
+ Object.assign(new Error('missing revert data'), {
175
+ code: 'CALL_EXCEPTION',
176
+ data: null,
177
+ reason: null,
178
+ revert: null,
179
+ shortMessage: 'missing revert data',
180
+ })
181
+ )
182
182
  return fn(api)
183
183
  })
184
184
  mockEncodeStakingData.mockReturnValue('0xdeadbeef')
@@ -1,11 +1,16 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import type { EvmConfigInfo } from '../config'
5
4
  import type { SourceFetcher } from './internalTransactionsFetcher'
6
5
  import type { BlockOperation } from '@ledgerhq/coin-module-framework/api/index'
7
6
  import { internalTxSourcesFromList } from '../config'
8
7
  import { SourceUnavailableError } from '../errors'
8
+ import {
9
+ createMockEvmConfigInfo,
10
+ mkEtherscanExplorer,
11
+ mkExternalNode,
12
+ mkLedgerExplorer,
13
+ } from '../fixtures/context.fixtures'
9
14
  import { getInternalTransactionsByBlock } from '../network/explorer/etherscan'
10
15
  import { mockNodeApi } from '../network/node/node.fixtures'
11
16
  import { composeInternalTxsFetcher, makeSourceFetchers } from './internalTransactionsFetcher'
@@ -164,19 +169,18 @@ describe('makeSourceFetchers', () => {
164
169
  })
165
170
 
166
171
  it('rejects explorer when explorer config is not etherscan-like', async () => {
167
- const config = {
168
- node: { type: 'external' as const, retries: 0 },
169
- explorer: { type: 'ledger' },
170
- } as unknown as EvmConfigInfo
172
+ const config = createMockEvmConfigInfo({
173
+ chainId: 1,
174
+ node: mkExternalNode(),
175
+ explorer: mkLedgerExplorer(),
176
+ })
171
177
 
172
178
  const fetchers = makeSourceFetchers(config, mockNodeApi(), '')
173
179
  await expect(fetchers.explorer(1)).rejects.toBeInstanceOf(SourceUnavailableError)
174
180
  })
175
181
 
176
182
  it('rejects trace_block when traceBlockErigon is undefined', async () => {
177
- const config = {
178
- node: { type: 'external' as const, retries: 0 },
179
- } as unknown as EvmConfigInfo
183
+ const config = createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
180
184
 
181
185
  const { traceBlockErigon: _traceBlockErigon, ...nodeApiWithoutErigon } = mockNodeApi()
182
186
  const fetchers = makeSourceFetchers(config, nodeApiWithoutErigon, '')
@@ -184,10 +188,11 @@ describe('makeSourceFetchers', () => {
184
188
  })
185
189
 
186
190
  it('wraps explorer runtime failures as SourceUnavailableError', async () => {
187
- const config = {
188
- node: { type: 'external' as const, retries: 0 },
189
- explorer: { type: 'etherscan', uri: 'https://api.etherscan.io' },
190
- } as unknown as EvmConfigInfo
191
+ const config = createMockEvmConfigInfo({
192
+ chainId: 1,
193
+ node: mkExternalNode(),
194
+ explorer: mkEtherscanExplorer({ uri: 'https://api.etherscan.io' }),
195
+ })
191
196
 
192
197
  const mockGetInternalTransactionsByBlock = jest.mocked(getInternalTransactionsByBlock)
193
198
  mockGetInternalTransactionsByBlock.mockRejectedValueOnce(new Error('explorer error'))
@@ -197,10 +202,11 @@ describe('makeSourceFetchers', () => {
197
202
  })
198
203
 
199
204
  it('calls getInternalTransactionsByBlock for explorer when configured', async () => {
200
- const config = {
201
- node: { type: 'external' as const, retries: 0 },
202
- explorer: { type: 'etherscan', uri: 'https://api.etherscan.io' },
203
- } as unknown as EvmConfigInfo
205
+ const config = createMockEvmConfigInfo({
206
+ chainId: 1,
207
+ node: mkExternalNode(),
208
+ explorer: mkEtherscanExplorer({ uri: 'https://api.etherscan.io' }),
209
+ })
204
210
 
205
211
  const mockGetInternalTransactionsByBlock = jest.mocked(getInternalTransactionsByBlock)
206
212
  mockGetInternalTransactionsByBlock.mockResolvedValueOnce([])
@@ -212,9 +218,7 @@ describe('makeSourceFetchers', () => {
212
218
  })
213
219
 
214
220
  it('empty always resolves to an empty map', async () => {
215
- const config = {
216
- node: { type: 'external' as const, retries: 0 },
217
- } as unknown as EvmConfigInfo
221
+ const config = createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
218
222
 
219
223
  const fetchers = makeSourceFetchers(config, mockNodeApi(), '')
220
224
  await expect(fetchers.empty(1)).resolves.toEqual(new Map<string, BlockOperation[]>())
@@ -1,9 +1,13 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import type { BlockFinalizationTag } from '../config'
5
- import type { EvmConfigInfo } from '../config'
6
- import { createMockEvmContext } from '../fixtures/context.fixtures'
4
+ import type {
5
+ BlockFinalizationTag,
6
+ EvmConfigInfo,
7
+ ExternalNodeConfig,
8
+ LedgerNodeConfig,
9
+ } from '../config'
10
+ import { createMockEvmContext, mkExternalNode, mkLedgerNode } from '../fixtures/context.fixtures'
7
11
  import { getNodeApi } from '../network/node'
8
12
  import { mockNodeApi } from '../network/node/node.fixtures'
9
13
  import { lastBlock } from './lastBlock'
@@ -27,9 +31,9 @@ describe('lastBlock', () => {
27
31
  })
28
32
 
29
33
  it.each([
30
- ['an external node', 'external', externalMocks],
31
- ['a ledger node', 'ledger', ledgerMocks],
32
- ])('returns last block info using %s', async (_, type, nodeApiMock) => {
34
+ ['an external node', mkExternalNode(), externalMocks],
35
+ ['a ledger node', mkLedgerNode(), ledgerMocks],
36
+ ])('returns last block info using %s', async (_, node, nodeApiMock) => {
33
37
  const blockResult = {
34
38
  hash: 'hash',
35
39
  parentHash: 'parentHash',
@@ -38,16 +42,16 @@ describe('lastBlock', () => {
38
42
  }
39
43
  const expectedInfo = { hash: 'hash', height: 33, time: new Date('2025-12-31') }
40
44
 
41
- const context = createMockEvmContext({ node: { type } } as Partial<EvmConfigInfo>)
45
+ const context = createMockEvmContext({ node })
42
46
  nodeApiMock.getBlockByHeight.mockResolvedValue(blockResult)
43
47
 
44
48
  expect(await lastBlock(context, '')).toEqual(expectedInfo)
45
49
  })
46
50
 
47
51
  it.each([
48
- ['an external node', 'external', externalMocks],
49
- ['a ledger node', 'ledger', ledgerMocks],
50
- ])("passes 'latest' by default to getBlockByHeight using %s", async (_, type, nodeApiMock) => {
52
+ ['an external node', mkExternalNode(), externalMocks],
53
+ ['a ledger node', mkLedgerNode(), ledgerMocks],
54
+ ])("passes 'latest' by default to getBlockByHeight using %s", async (_, node, nodeApiMock) => {
51
55
  const blockResult = {
52
56
  hash: 'hash',
53
57
  parentHash: 'parentHash',
@@ -55,7 +59,7 @@ describe('lastBlock', () => {
55
59
  timestamp: new Date('2025-12-31').getTime(),
56
60
  }
57
61
 
58
- const context = createMockEvmContext({ node: { type } } as Partial<EvmConfigInfo>)
62
+ const context = createMockEvmContext({ node })
59
63
  nodeApiMock.getBlockByHeight.mockResolvedValue(blockResult)
60
64
 
61
65
  await lastBlock(context, '')
@@ -63,14 +67,21 @@ describe('lastBlock', () => {
63
67
  expect(nodeApiMock.getBlockByHeight).toHaveBeenCalledWith(expect.anything(), 'latest')
64
68
  })
65
69
 
66
- it.each<[BlockFinalizationTag, string, string, jest.Mocked<ReturnType<typeof mockNodeApi>>]>([
67
- ['finalized', 'an external node', 'external', externalMocks],
68
- ['finalized', 'a ledger node', 'ledger', ledgerMocks],
69
- ['safe', 'an external node', 'external', externalMocks],
70
- ['safe', 'a ledger node', 'ledger', ledgerMocks],
70
+ it.each<
71
+ [
72
+ BlockFinalizationTag,
73
+ string,
74
+ ExternalNodeConfig | LedgerNodeConfig,
75
+ jest.Mocked<ReturnType<typeof mockNodeApi>>,
76
+ ]
77
+ >([
78
+ ['finalized', 'an external node', mkExternalNode(), externalMocks],
79
+ ['finalized', 'a ledger node', mkLedgerNode(), ledgerMocks],
80
+ ['safe', 'an external node', mkExternalNode(), externalMocks],
81
+ ['safe', 'a ledger node', mkLedgerNode(), ledgerMocks],
71
82
  ])(
72
83
  "forwards finalizationLevel '%s' to getBlockByHeight using %s",
73
- async (finalizationLevel, _, type, nodeApiMock) => {
84
+ async (finalizationLevel, _, node, nodeApiMock) => {
74
85
  const blockResult = {
75
86
  hash: 'hash',
76
87
  parentHash: 'parentHash',
@@ -79,10 +90,7 @@ describe('lastBlock', () => {
79
90
  }
80
91
  const expectedInfo = { hash: 'hash', height: 33, time: new Date('2025-12-31') }
81
92
 
82
- const context = createMockEvmContext({
83
- node: { type },
84
- finalizationLevel,
85
- } as Partial<EvmConfigInfo>)
93
+ const context = createMockEvmContext({ node, finalizationLevel })
86
94
  nodeApiMock.getBlockByHeight.mockResolvedValue(blockResult)
87
95
 
88
96
  expect(await lastBlock(context, '')).toEqual(expectedInfo)