@ledgerhq/coin-evm 5.3.2 → 5.3.4

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 (48) hide show
  1. package/.oxlintrc.json +2 -2
  2. package/CHANGELOG.md +13 -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 +59 -79
  26. package/src/network/explorer/etherscan.test.ts +95 -136
  27. package/src/network/explorer/index.test.ts +51 -21
  28. package/src/network/explorer/ledger.test.ts +51 -44
  29. package/src/network/gasTracker/index.test.ts +14 -8
  30. package/src/network/gasTracker/ledger.test.ts +43 -61
  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 +204 -219
  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
  48. package/src/api/index.fantom.integ.test.ts +0 -103
package/.oxlintrc.json CHANGED
@@ -13,8 +13,8 @@
13
13
  "plugins": ["jest"],
14
14
  "rules": {
15
15
  "typescript/explicit-function-return-type": "off",
16
- "typescript/consistent-type-assertions": "warn",
17
- "typescript/no-explicit-any": "warn"
16
+ "typescript/consistent-type-assertions": ["error", { "assertionStyle": "never" }],
17
+ "typescript/no-explicit-any": "error"
18
18
  }
19
19
  }
20
20
  ]
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @ledgerhq/coin-evm
2
2
 
3
+ ## 5.3.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#902](https://github.com/LedgerHQ/coin-modules/pull/902) [`b4e0f42`](https://github.com/LedgerHQ/coin-modules/commit/b4e0f42febb9ce3b4f170a8facdeae1ec5052513) Thanks [@jnicoulaud-ledger](https://github.com/jnicoulaud-ledger)! - chore: drop fantom network (shut down)
8
+
9
+ ## 5.3.3
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [[`9c2ae60`](https://github.com/LedgerHQ/coin-modules/commit/9c2ae60cd78d21b58af56f5926b3e141f5631b76)]:
14
+ - @ledgerhq/coin-module-framework@9.0.0
15
+
3
16
  ## 5.3.2
4
17
 
5
18
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/coin-evm",
3
- "version": "5.3.2",
3
+ "version": "5.3.4",
4
4
  "description": "Ledger EVM Coin integration",
5
5
  "keywords": [
6
6
  "Ledger",
@@ -51,7 +51,7 @@
51
51
  "eip55": "^2.1.1",
52
52
  "ethers": "6.15.0",
53
53
  "react-native-get-random-values": "^1.8.0",
54
- "@ledgerhq/coin-module-framework": "^8.5.0"
54
+ "@ledgerhq/coin-module-framework": "^9.1.0"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "@ledgerhq/live-network": "^3.0.0",
@@ -161,40 +161,38 @@ describe('EVM Family', () => {
161
161
 
162
162
  const makeTraceItem = (
163
163
  overrides: Partial<TraceBlockItem> & { transactionHash: string }
164
- ): TraceBlockItem =>
165
- ({
166
- action: makeAction(overrides?.action || {}),
167
- result: { gasUsed: '0x5208', output: '0x' },
168
- blockHash: '0xabc',
169
- blockNumber: 1,
170
- transactionPosition: 0,
171
- traceAddress: [0],
172
- subtraces: 0,
173
- type: 'call',
174
- ...overrides,
175
- }) as TraceBlockItem
164
+ ): TraceBlockItem => ({
165
+ action: makeAction(overrides?.action || {}),
166
+ result: { gasUsed: '0x5208', output: '0x' },
167
+ blockHash: '0xabc',
168
+ blockNumber: 1,
169
+ transactionPosition: 0,
170
+ traceAddress: [0],
171
+ subtraces: 0,
172
+ type: 'call',
173
+ ...overrides,
174
+ })
176
175
 
177
176
  const makeOtherAction = (overrides: TraceBlockOtherAction = {}): TraceBlockOtherAction => ({
178
177
  ...overrides,
179
178
  })
180
179
 
181
- const makeRewardTraceItem = (overrides: Partial<TraceBlockItem> = {}): TraceBlockItem =>
182
- ({
183
- action: makeOtherAction({
184
- author: '0x8f81e2e3f8b46467523463835f965ffe476e1c9e',
185
- rewardType: 'block',
186
- value: '0x0',
187
- }),
188
- blockHash: '0x6c508acd5fb899025f59582d097b7d693e2efd538576d9709747272960e76663',
189
- blockNumber: 19500620,
190
- result: null,
191
- subtraces: 0,
192
- traceAddress: [],
193
- transactionHash: null,
194
- transactionPosition: null,
195
- type: 'reward',
196
- ...overrides,
197
- }) as TraceBlockItem
180
+ const makeRewardTraceItem = (overrides: Partial<TraceBlockItem> = {}): TraceBlockItem => ({
181
+ action: makeOtherAction({
182
+ author: '0x8f81e2e3f8b46467523463835f965ffe476e1c9e',
183
+ rewardType: 'block',
184
+ value: '0x0',
185
+ }),
186
+ blockHash: '0x6c508acd5fb899025f59582d097b7d693e2efd538576d9709747272960e76663',
187
+ blockNumber: 19500620,
188
+ result: null,
189
+ subtraces: 0,
190
+ traceAddress: [],
191
+ transactionHash: null,
192
+ transactionPosition: null,
193
+ type: 'reward',
194
+ ...overrides,
195
+ })
198
196
 
199
197
  it('should group native value transfers by transaction hash', () => {
200
198
  const items: TraceBlockItem[] = [
@@ -30,19 +30,7 @@ describe('Flare (external node)', () => {
30
30
  // - test on the tip of the chain without guarantees there is actually internal txs
31
31
  // - test that the call is not failing (getBlock should not reject)
32
32
  const info = await module.lastBlock(createMockEvmContext(flareConfig))
33
- const block = await module
34
- .getBlock(createMockEvmContext(flareConfig), info.height)
35
- .catch((e: unknown) => {
36
- if (
37
- e &&
38
- typeof e === 'object' &&
39
- 'rawError' in e &&
40
- (e as { rawError?: unknown }).rawError
41
- ) {
42
- throw (e as { rawError: unknown }).rawError
43
- }
44
- throw e
45
- })
33
+ const block = await module.getBlock(createMockEvmContext(flareConfig), info.height)
46
34
  expect(block.info.height).toBe(info.height)
47
35
  })
48
36
  })
@@ -1,45 +1,42 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import type { EvmConfig, EvmConfigInfo } from '../config'
5
- import type {
6
- BufferTxData,
7
- MemoNotSupported,
8
- Operation,
9
- StakingTransactionIntent,
10
- } from '@ledgerhq/coin-module-framework/api/types'
4
+ import type { Operation } from '@ledgerhq/coin-module-framework/api/types'
11
5
  import { ethers } from 'ethers'
12
- import { createMockEvmContext } from '../fixtures/context.fixtures'
6
+ import {
7
+ createMockEvmConfigInfo,
8
+ createMockEvmContext,
9
+ mkEtherscanExplorer,
10
+ mkExternalNode,
11
+ mkLedgerExplorer,
12
+ mkLedgerNode,
13
+ } from '../fixtures/context.fixtures'
13
14
  import { createApi } from './index'
14
15
 
15
16
  describe.each([
16
17
  [
17
18
  'external node and explorer',
18
- {
19
- node: { type: 'external', uri: 'https://ethereum-rpc.publicnode.com' },
20
- explorer: {
21
- type: 'etherscan',
22
- uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1',
23
- },
19
+ createMockEvmConfigInfo({
20
+ chainId: 1,
21
+ node: mkExternalNode({ uri: 'https://ethereum-rpc.publicnode.com' }),
22
+ explorer: mkEtherscanExplorer({ uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1' }),
24
23
  supportedTokens: ['erc721', 'erc1155'],
25
- },
24
+ }),
26
25
  ],
27
26
  [
28
27
  'ledger node and explorer',
29
- {
30
- node: { type: 'ledger', explorerId: 'eth' },
31
- explorer: {
32
- type: 'ledger',
33
- explorerId: 'eth',
34
- },
28
+ createMockEvmConfigInfo({
29
+ chainId: 1,
30
+ node: mkLedgerNode({ explorerId: 'eth' }),
31
+ explorer: mkLedgerExplorer({ explorerId: 'eth' }),
35
32
  supportedTokens: ['erc721', 'erc1155'],
36
- },
33
+ }),
37
34
  ],
38
35
  ])('EVM Api (%s)', (_, config) => {
39
36
  let module: ReturnType<typeof createApi>
40
37
  // The logic layer resolves config from the threaded context (ADR-019), so the context must
41
38
  // carry this suite's node/explorer config — not the empty default fixture.
42
- const context = createMockEvmContext(config as Partial<EvmConfigInfo>)
39
+ const context = createMockEvmContext(config)
43
40
  beforeAll(() => {
44
41
  module = createApi('ethereum')
45
42
  })
@@ -341,19 +338,14 @@ describe.each([
341
338
  * "To send batches over 10 items, consider using a dedicated API provider"
342
339
  */
343
340
  it('returns at least 10 token balances on Optimisim', async () => {
344
- const config = {
345
- node: {
346
- type: 'external',
347
- uri: 'https://mainnet.optimism.io',
348
- },
349
- explorer: {
350
- type: 'blockscout',
351
- uri: 'https://optimism.blockscout.com/api',
352
- },
353
- }
354
341
  const module = createApi('optimism')
355
-
356
- const context = createMockEvmContext(config as Partial<EvmConfigInfo>)
342
+ const context = createMockEvmContext(
343
+ createMockEvmConfigInfo({
344
+ chainId: 10,
345
+ node: mkExternalNode({ uri: 'https://mainnet.optimism.io' }),
346
+ explorer: { type: 'blockscout' as const, uri: 'https://optimism.blockscout.com/api' },
347
+ })
348
+ )
357
349
  const result = await module.getBalance(context, '0x1CDDb825910426644e00e769072Ce1Ea7d4e34BB')
358
350
 
359
351
  expect(result.length).toBeGreaterThan(10)
@@ -418,7 +410,7 @@ describe.each([
418
410
  }
419
411
  )
420
412
 
421
- const isEtherscanLike = (config as EvmConfig).explorer?.type === 'etherscan'
413
+ const isEtherscanLike = config.explorer?.type === 'etherscan'
422
414
 
423
415
  describe('pagination', () => {
424
416
  const expectUniqueOperationIds = (ops: Operation[]) => {
@@ -653,14 +645,13 @@ describe.each([
653
645
  asset: { type: 'erc20' },
654
646
  tx: { feesPayer: expect.any(String) },
655
647
  })
656
- const senderAsset = senderTxOps[0].asset as {
657
- assetReference?: string
658
- assetOwner?: string
648
+ const senderAsset = senderTxOps[0].asset
649
+ if (senderAsset.type !== 'native') {
650
+ expect(senderAsset.assetReference?.toLowerCase()).toBe(contract.toLowerCase())
651
+ expectAddressEq(senderTxOps[0].senders[0], sender)
652
+ expectAddressEq(senderTxOps[0].recipients[0], recipient)
653
+ expectAddressEq(senderAsset.assetOwner, sender)
659
654
  }
660
- expect(senderAsset.assetReference?.toLowerCase()).toBe(contract.toLowerCase())
661
- expectAddressEq(senderTxOps[0].senders[0], sender)
662
- expectAddressEq(senderTxOps[0].recipients[0], recipient)
663
- expectAddressEq(senderAsset.assetOwner!, sender)
664
655
  expectAddressEq(senderTxOps[0].tx.feesPayer!, sender)
665
656
  expect(senderTxOps[0].value).toBeGreaterThan(0n)
666
657
 
@@ -669,14 +660,13 @@ describe.each([
669
660
  type: 'IN',
670
661
  asset: { type: 'erc20' },
671
662
  })
672
- const recipientAsset = recipientTxOps[0].asset as {
673
- assetReference?: string
674
- assetOwner?: string
663
+ const recipientAsset = recipientTxOps[0].asset
664
+ if (recipientAsset.type !== 'native') {
665
+ expect(recipientAsset.assetReference?.toLowerCase()).toBe(contract.toLowerCase())
666
+ expectAddressEq(recipientTxOps[0].senders[0], sender)
667
+ expectAddressEq(recipientTxOps[0].recipients[0], recipient)
668
+ expectAddressEq(recipientAsset.assetOwner, recipient)
675
669
  }
676
- expect(recipientAsset.assetReference?.toLowerCase()).toBe(contract.toLowerCase())
677
- expectAddressEq(recipientTxOps[0].senders[0], sender)
678
- expectAddressEq(recipientTxOps[0].recipients[0], recipient)
679
- expectAddressEq(recipientAsset.assetOwner!, recipient)
680
670
  expect(recipientTxOps[0].value).toBe(senderTxOps[0].value)
681
671
  })
682
672
 
@@ -717,9 +707,11 @@ describe.each([
717
707
  asset: { type: 'erc20', assetOwner: expect.any(String) },
718
708
  tx: { feesPayer: expect.any(String) },
719
709
  })
720
- const tokenOpAsset = tokenOp!.asset as { assetReference?: string; assetOwner?: string }
721
- expect(tokenOpAsset.assetReference?.toLowerCase()).toBe(lidoContract.toLowerCase())
722
- expectAddressEq(tokenOpAsset.assetOwner!, sender)
710
+ const tokenOpAsset = tokenOp!.asset
711
+ if (tokenOpAsset.type !== 'native') {
712
+ expect(tokenOpAsset.assetReference?.toLowerCase()).toBe(lidoContract.toLowerCase())
713
+ expectAddressEq(tokenOpAsset.assetOwner, sender)
714
+ }
723
715
  expect(tokenOp!.senders[0].toLowerCase()).toBe(zeroAddress.toLowerCase())
724
716
  expectAddressEq(tokenOp!.recipients[0], sender)
725
717
  expectAddressEq(tokenOp!.tx.feesPayer!, sender)
@@ -878,15 +870,14 @@ describe.each([
878
870
 
879
871
  describe('EVM Api: external node and explorer ONLY', () => {
880
872
  let module: ReturnType<typeof createApi>
881
- const config = {
882
- node: { type: 'external', uri: 'https://ethereum-rpc.publicnode.com' },
883
- explorer: {
884
- type: 'etherscan',
885
- uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1',
886
- },
887
- supportedTokens: ['erc721', 'erc1155'],
888
- } as EvmConfig
889
- const context = createMockEvmContext(config)
873
+ const context = createMockEvmContext(
874
+ createMockEvmConfigInfo({
875
+ chainId: 1,
876
+ node: mkExternalNode({ uri: 'https://ethereum-rpc.publicnode.com' }),
877
+ explorer: mkEtherscanExplorer({ uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1' }),
878
+ supportedTokens: ['erc721', 'erc1155'],
879
+ })
880
+ )
890
881
 
891
882
  beforeAll(() => {
892
883
  module = createApi('ethereum')
@@ -967,14 +958,15 @@ describe('EVM Api: external node and explorer ONLY', () => {
967
958
 
968
959
  describe('EVM Api (Moonbeam Network)', () => {
969
960
  let module: ReturnType<typeof createApi>
970
- const config = {
971
- node: { type: 'external', uri: 'https://rpc.api.moonbeam.network' },
972
- explorer: {
973
- type: 'etherscan',
974
- uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1284',
975
- },
976
- } as EvmConfig
977
- const context = createMockEvmContext(config)
961
+ const context = createMockEvmContext(
962
+ createMockEvmConfigInfo({
963
+ chainId: 1284,
964
+ node: mkExternalNode({ uri: 'https://rpc.api.moonbeam.network' }),
965
+ explorer: mkEtherscanExplorer({
966
+ uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1284',
967
+ }),
968
+ })
969
+ )
978
970
 
979
971
  beforeAll(() => {
980
972
  module = createApi('moonbeam')
@@ -1016,17 +1008,15 @@ describe('EVM Api (SEI Network)', () => {
1016
1008
  const seiStakingSender = '0x66c4371aE8FFeD2ec1c2EBbbcCfb7E494181E1E3'
1017
1009
  const seiStakingRecipient = '0x0000000000000000000000000000000000001005'
1018
1010
  const seiStakingDelegationAmount = 1000000000000000n // 0.001 SEI
1019
- const config = {
1020
- node: {
1021
- type: 'external',
1022
- uri: 'https://sei-evm-rpc.publicnode.com',
1023
- },
1024
- explorer: {
1025
- type: 'etherscan',
1026
- uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1329',
1027
- },
1028
- } as EvmConfig
1029
- const context = createMockEvmContext(config)
1011
+ const context = createMockEvmContext(
1012
+ createMockEvmConfigInfo({
1013
+ chainId: 1329,
1014
+ node: mkExternalNode({ uri: 'https://sei-evm-rpc.publicnode.com' }),
1015
+ explorer: mkEtherscanExplorer({
1016
+ uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/1329',
1017
+ }),
1018
+ })
1019
+ )
1030
1020
 
1031
1021
  beforeAll(() => {
1032
1022
  module = createApi('sei_evm')
@@ -1044,7 +1034,7 @@ describe('EVM Api (SEI Network)', () => {
1044
1034
  valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1045
1035
  data: { type: 'buffer', value: Buffer.from([]) },
1046
1036
  asset: { type: 'native' },
1047
- } as StakingTransactionIntent<MemoNotSupported, BufferTxData>)
1037
+ })
1048
1038
 
1049
1039
  expect(result).toMatch(/^0x[A-Fa-f0-9]+$/)
1050
1040
  expect(ethers.Transaction.from(result)).toMatchObject({
@@ -1068,7 +1058,7 @@ describe('EVM Api (SEI Network)', () => {
1068
1058
  valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1069
1059
  data: { type: 'buffer', value: Buffer.from([]) },
1070
1060
  asset: { type: 'native' },
1071
- } as StakingTransactionIntent<MemoNotSupported, BufferTxData>)
1061
+ })
1072
1062
 
1073
1063
  expect(result).toMatch(/^0x[A-Fa-f0-9]+$/)
1074
1064
  expect(ethers.Transaction.from(result)).toMatchObject({
@@ -1097,7 +1087,7 @@ describe('EVM Api (SEI Network)', () => {
1097
1087
  valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1098
1088
  data: { type: 'buffer', value: Buffer.from([]) },
1099
1089
  asset: { type: 'native' },
1100
- } as StakingTransactionIntent<MemoNotSupported, BufferTxData>)
1090
+ })
1101
1091
 
1102
1092
  expect(result).toEqual({
1103
1093
  value: expect.any(BigInt),
@@ -1125,7 +1115,7 @@ describe('EVM Api (SEI Network)', () => {
1125
1115
  valAddress: 'seivaloper1ummny4p645xraxc4m7nphf7vxawfzt3p5hn47t',
1126
1116
  data: { type: 'buffer', value: Buffer.from([]) },
1127
1117
  asset: { type: 'native' },
1128
- } as StakingTransactionIntent<MemoNotSupported, BufferTxData>)
1118
+ })
1129
1119
 
1130
1120
  expect(result).toEqual({
1131
1121
  value: expect.any(BigInt),
@@ -1148,18 +1138,17 @@ describe('EVM Api (SEI Network)', () => {
1148
1138
 
1149
1139
  describe('EVM Api (Zero Gravity)', () => {
1150
1140
  let module: ReturnType<typeof createApi>
1151
- const config = {
1152
- node: {
1153
- type: 'external',
1154
- uri: 'https://evmrpc.0g.ai',
1155
- },
1156
- explorer: {
1157
- type: 'etherscan',
1158
- uri: 'https://chainscan.0g.ai/open/api',
1159
- maxLimit: 99, // use 99 so internal `limit + 1` probing stays within the explorer's hard max of 100
1160
- },
1161
- } as EvmConfig
1162
- const context = createMockEvmContext(config)
1141
+ const context = createMockEvmContext(
1142
+ createMockEvmConfigInfo({
1143
+ chainId: 16661,
1144
+ node: mkExternalNode({ uri: 'https://evmrpc.0g.ai' }),
1145
+ explorer: {
1146
+ type: 'etherscan' as const,
1147
+ uri: 'https://chainscan.0g.ai/open/api',
1148
+ maxLimit: 99, // use 99 so internal `limit + 1` probing stays within the explorer's hard max of 100
1149
+ },
1150
+ })
1151
+ )
1163
1152
 
1164
1153
  beforeAll(() => {
1165
1154
  module = createApi('zero_gravity')
@@ -1,10 +1,88 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import type { EvmConfigInfo, EvmContext } from '../config'
4
+ import type {
5
+ EvmCoinConfig,
6
+ EvmConfig,
7
+ EvmConfigInfo,
8
+ EvmContext,
9
+ ExternalNodeConfig,
10
+ LedgerNodeConfig,
11
+ } from '../config'
5
12
  import type { Logger } from '@ledgerhq/coin-module-framework/config'
6
13
  import { log } from '@ledgerhq/logs'
7
14
 
15
+ /** Returns an external (JSON-RPC) node config for use in tests. */
16
+ export const mkExternalNode = (partial?: {
17
+ uri?: string
18
+ retries?: number
19
+ }): ExternalNodeConfig => ({
20
+ type: 'external',
21
+ uri: partial?.uri ?? 'http://node.test',
22
+ ...(typeof partial?.retries === 'number' && { retries: partial.retries }),
23
+ })
24
+
25
+ /** Returns a Ledger-hosted node config for use in tests. */
26
+ export const mkLedgerNode = (partial?: {
27
+ explorerId?: string
28
+ retries?: number
29
+ }): LedgerNodeConfig => ({
30
+ type: 'ledger',
31
+ explorerId: partial?.explorerId ?? 'test-explorer-id',
32
+ ...(typeof partial?.retries === 'number' && { retries: partial.retries }),
33
+ })
34
+
35
+ /** Returns an Etherscan-compatible explorer config for use in tests. */
36
+ export const mkEtherscanExplorer = (partial?: {
37
+ uri?: string
38
+ noCache?: boolean
39
+ }): EvmConfig['explorer'] => ({
40
+ type: 'etherscan',
41
+ uri: partial?.uri ?? 'http://explorer.test',
42
+ ...(typeof partial?.noCache === 'boolean' && { noCache: partial.noCache }),
43
+ })
44
+
45
+ /** Returns a Ledger explorer config for use in tests. */
46
+ export const mkLedgerExplorer = (partial?: { explorerId?: string }): EvmConfig['explorer'] => ({
47
+ type: 'ledger',
48
+ explorerId: partial?.explorerId ?? 'test-explorer-id',
49
+ })
50
+
51
+ /** Returns a Ledger gas tracker config for use in tests. */
52
+ export const mkLedgerGasTracker = (partial?: {
53
+ explorerId?: string
54
+ }): NonNullable<EvmConfig['gasTracker']> => ({
55
+ type: 'ledger',
56
+ explorerId: partial?.explorerId ?? 'test-explorer-id',
57
+ })
58
+
59
+ /**
60
+ * Builds a complete {@link EvmConfigInfo} for tests.
61
+ *
62
+ * `chainId` and `node` are required — use {@link mkExternalNode} or {@link mkLedgerNode}
63
+ * to compose the node. All other fields are optional and default to `name: 'test'` and
64
+ * `explorer: { type: 'none' }`.
65
+ */
66
+ export const createMockEvmConfigInfo = (
67
+ config: Pick<EvmConfigInfo, 'chainId' | 'node'> & Partial<Omit<EvmConfigInfo, 'chainId' | 'node'>>
68
+ ): EvmConfigInfo => ({
69
+ status: { type: 'active' },
70
+ name: 'test',
71
+ explorer: { type: 'none' },
72
+ ...config,
73
+ })
74
+
75
+ /**
76
+ * Builds an {@link EvmCoinConfig} for tests.
77
+ *
78
+ * Wraps {@link createMockEvmConfigInfo} in the `{ info }` shape expected by `setCoinConfig`.
79
+ */
80
+ export const createMockEvmCoinConfig = (
81
+ config: Pick<EvmConfigInfo, 'chainId' | 'node'> & Partial<Omit<EvmConfigInfo, 'chainId' | 'node'>>
82
+ ): EvmCoinConfig => ({
83
+ info: createMockEvmConfigInfo(config),
84
+ })
85
+
8
86
  /**
9
87
  * Builds a mock {@link EvmContext} for tests.
10
88
  *
@@ -0,0 +1,26 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Block, JsonRpcProvider, Result, TransactionReceipt, TransactionResponse } from 'ethers'
5
+
6
+ export const mkJsonRpcProvider = (): JsonRpcProvider => Object.create(JsonRpcProvider.prototype)
7
+
8
+ export const mkResult = (...items: unknown[]): Result => {
9
+ return Object.setPrototypeOf(items, Result.prototype)
10
+ }
11
+
12
+ export const mkBlock = (partial: Partial<Block>): Block => {
13
+ const base = { transactions: undefined, prefetchedTransactions: undefined, ...partial }
14
+ return Object.create(Block.prototype, Object.getOwnPropertyDescriptors(base))
15
+ }
16
+
17
+ export const mkTransactionResponse = (
18
+ partial: Partial<TransactionResponse>
19
+ ): TransactionResponse => {
20
+ const tx: TransactionResponse = Object.create(TransactionResponse.prototype)
21
+ return Object.assign(tx, partial)
22
+ }
23
+
24
+ export const mkTransactionReceipt = (partial: Partial<TransactionReceipt>): TransactionReceipt => {
25
+ return Object.create(TransactionReceipt.prototype, Object.getOwnPropertyDescriptors(partial))
26
+ }
@@ -3,6 +3,7 @@
3
3
 
4
4
  import { Wallet } from 'ethers'
5
5
  import { createContext, setCoinConfig } from '../config'
6
+ import { createMockEvmCoinConfig, mkExternalNode, mkLedgerNode } from '../fixtures/context.fixtures'
6
7
  import broadcast from './broadcast'
7
8
 
8
9
  describe('Broadcast', () => {
@@ -10,25 +11,12 @@ describe('Broadcast', () => {
10
11
  setCoinConfig((currency) => {
11
12
  switch (currency) {
12
13
  case 'ethereum':
13
- return {
14
- info: {
15
- status: { type: 'active' },
16
- node: {
17
- type: 'ledger',
18
- explorerId: 'eth',
19
- },
20
- },
21
- } as any
14
+ return createMockEvmCoinConfig({ chainId: 1, node: mkLedgerNode({ explorerId: 'eth' }) })
22
15
  case 'sonic':
23
- return {
24
- info: {
25
- status: { type: 'active' },
26
- node: {
27
- type: 'external',
28
- uri: 'https://sonic.coin.ledger.com',
29
- },
30
- },
31
- } as any
16
+ return createMockEvmCoinConfig({
17
+ chainId: 146,
18
+ node: mkExternalNode({ uri: 'https://sonic.coin.ledger.com' }),
19
+ })
32
20
  default:
33
21
  throw new Error('Unhandled currency')
34
22
  }