@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
@@ -1,14 +1,22 @@
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 { TransactionReceipt, TransactionResponse, ethers } from 'ethers'
6
- import { AssertionError, fail } from 'assert'
4
+ import type { PerformActionRequest } from 'ethers'
7
5
  import { delay } from '@ledgerhq/coin-module-framework/promises'
8
6
  import BigNumber from 'bignumber.js'
9
- import { FetchRequest, JsonRpcProvider, Network, Transaction } from 'ethers'
7
+ import { FeeData, FetchRequest, JsonRpcProvider, Network, Transaction } from 'ethers'
10
8
  import { getCoinConfig } from '../../config'
11
9
  import { GasEstimationError, InsufficientFunds, UnsupportedRpcMethodError } from '../../errors'
10
+ import {
11
+ createMockEvmCoinConfig,
12
+ createMockEvmConfigInfo,
13
+ mkExternalNode,
14
+ } from '../../fixtures/context.fixtures'
15
+ import {
16
+ mkBlock,
17
+ mkTransactionReceipt,
18
+ mkTransactionResponse,
19
+ } from '../../fixtures/ethers.fixtures'
12
20
  import {
13
21
  createNodeApi,
14
22
  DEFAULT_RETRIES_RPC_METHODS,
@@ -22,11 +30,11 @@ import {
22
30
 
23
31
  const currencyId = 'my_new_chain'
24
32
 
25
- const evmConfig = {
33
+ const evmConfig = createMockEvmConfigInfo({
26
34
  chainId: 1,
27
35
  name: 'My New Chain',
28
- node: { type: 'external', uri: 'http://test' },
29
- } as EvmConfigInfo
36
+ node: mkExternalNode({ uri: 'http://test' }),
37
+ })
30
38
 
31
39
  const nodeApi = createNodeApi(evmConfig, {
32
40
  type: 'external',
@@ -44,7 +52,7 @@ const now = Math.floor(Date.now() / 1000)
44
52
  const address = '0x6cBCD73CD8e8a42844662f0A0e76D7F79Afd933d'
45
53
 
46
54
  jest.mock('@ledgerhq/coin-module-framework/promises')
47
- ;(delay as jest.Mock).mockImplementation(
55
+ jest.mocked(delay).mockImplementation(
48
56
  () => new Promise((resolve) => setTimeout(resolve, 1)) // mocking the delay supposed to happen after each try
49
57
  )
50
58
 
@@ -55,85 +63,76 @@ describe('EVM Family', () => {
55
63
 
56
64
  beforeEach(() => {
57
65
  jest.resetAllMocks()
58
- jest
59
- .spyOn(JsonRpcProvider.prototype as any, '_detectNetwork')
60
- .mockResolvedValue(Network.from(1))
61
-
62
- mockGetConfig.mockImplementation((): any => {
63
- return {
64
- info: {
65
- node: {
66
- type: 'external',
67
- uri: 'my-rpc.com',
68
- },
69
- },
70
- }
71
- })
66
+ jest.spyOn(JsonRpcProvider.prototype, '_detectNetwork').mockResolvedValue(Network.from(1))
72
67
 
73
- jest.spyOn(JsonRpcProvider.prototype, 'getFeeData').mockResolvedValue({
74
- gasPrice: BigInt(666),
75
- maxFeePerGas: 0n,
76
- maxPriorityFeePerGas: 0n,
77
- } as ethers.FeeData)
68
+ mockGetConfig.mockImplementation(() =>
69
+ createMockEvmCoinConfig({ chainId: 1, node: mkExternalNode({ uri: 'my-rpc.com' }) })
70
+ )
71
+
72
+ jest
73
+ .spyOn(JsonRpcProvider.prototype, 'getFeeData')
74
+ .mockResolvedValue(new FeeData(BigInt(666), 0n, 0n))
78
75
 
79
76
  jest.spyOn(JsonRpcProvider.prototype, 'getBalance').mockResolvedValue(BigInt(420))
80
77
  jest
81
78
  .spyOn(JsonRpcProvider.prototype, 'call')
82
79
  .mockResolvedValue('0x000000000000000000000000000000000000000000000000000000000000A455')
83
- jest.spyOn(JsonRpcProvider.prototype, 'getBlock').mockResolvedValue({
84
- hash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
85
- parentHash: '0xfc900c22725f9c0843c9cf7d2c47f4b61b246bd21e18e99f709aebaefc8aff14',
86
- number: 1,
87
- difficulty: 0n,
88
- gasLimit: BigInt(1),
89
- gasUsed: BigInt(2),
90
- extraData: '0x',
91
- baseFeePerGas: BigInt(123),
92
- timestamp: now,
93
- } as Partial<ethers.Block> as ethers.Block)
80
+ jest.spyOn(JsonRpcProvider.prototype, 'getBlock').mockResolvedValue(
81
+ mkBlock({
82
+ hash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
83
+ parentHash: '0xfc900c22725f9c0843c9cf7d2c47f4b61b246bd21e18e99f709aebaefc8aff14',
84
+ number: 1,
85
+ difficulty: 0n,
86
+ gasLimit: BigInt(1),
87
+ gasUsed: BigInt(2),
88
+ extraData: '0x',
89
+ baseFeePerGas: BigInt(123),
90
+ timestamp: now,
91
+ })
92
+ )
94
93
 
95
94
  jest.spyOn(JsonRpcProvider.prototype, 'getTransactionCount').mockResolvedValue(5)
96
95
 
97
- jest.spyOn(JsonRpcProvider.prototype, 'getTransaction').mockResolvedValue({
98
- hash: '0x435b00d28a10febbcfefbdea080134d08ef843df122d5bc9174b09de7fce6a59',
99
- blockNumber: 69,
100
- blockHash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
101
- nonce: 123,
102
- value: BigInt(456),
103
- gasLimit: BigInt(123),
104
- gasPrice: BigInt(789),
105
- data: '0x',
106
- to: '0xC2907EFccE4011C491BbedA8A0fA63BA7aab596C',
107
- from: '0x6cbcd73cd8e8a42844662f0a0e76d7f79afd933d',
108
- type: 0,
109
- confirmations: jest.fn().mockResolvedValue(1),
110
- provider: {} as any,
111
- } as Partial<TransactionResponse> as TransactionResponse)
112
-
113
- jest.spyOn(JsonRpcProvider.prototype, 'getTransactionReceipt').mockResolvedValue({
114
- blockHash: '0x8a179bc6cb299f936c4fd614995e62d597ec6108b579c23034fb220967ceaa94',
115
- blockNumber: 69,
116
- byzantium: true,
117
- confirmations: jest.fn().mockResolvedValue(100),
118
- contractAddress: null,
119
- cumulativeGasUsed: BigInt(121),
120
- effectiveGasPrice: BigInt(789),
121
- from: '0x6cbcd73cd8e8a42844662f0a0e76d7f79afd933d',
122
- gasUsed: BigInt(122),
123
- gasLimit: BigInt(21000),
124
- gasPrice: BigInt(789),
125
- logs: [],
126
- logsBloom: '0x',
127
- status: 1,
128
- to: '0xC2907EFccE4011C491BbedA8A0fA63BA7aab596C',
129
- transactionHash: '0x435b00d28a10febbcfefbdea080134d08ef843df122d5bc9174b09de7fce6a59',
130
- transactionIndex: 123,
131
- type: 0,
132
- } as Partial<TransactionReceipt> as TransactionReceipt)
96
+ jest.spyOn(JsonRpcProvider.prototype, 'getTransaction').mockResolvedValue(
97
+ mkTransactionResponse({
98
+ hash: '0x435b00d28a10febbcfefbdea080134d08ef843df122d5bc9174b09de7fce6a59',
99
+ blockNumber: 69,
100
+ blockHash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
101
+ nonce: 123,
102
+ value: BigInt(456),
103
+ gasLimit: BigInt(123),
104
+ gasPrice: BigInt(789),
105
+ data: '0x',
106
+ to: '0xC2907EFccE4011C491BbedA8A0fA63BA7aab596C',
107
+ from: '0x6cbcd73cd8e8a42844662f0a0e76d7f79afd933d',
108
+ type: 0,
109
+ confirmations: jest.fn().mockResolvedValue(1),
110
+ })
111
+ )
112
+
113
+ jest.spyOn(JsonRpcProvider.prototype, 'getTransactionReceipt').mockResolvedValue(
114
+ mkTransactionReceipt({
115
+ blockHash: '0x8a179bc6cb299f936c4fd614995e62d597ec6108b579c23034fb220967ceaa94',
116
+ blockNumber: 69,
117
+ confirmations: jest.fn().mockResolvedValue(100),
118
+ contractAddress: null,
119
+ cumulativeGasUsed: BigInt(121),
120
+ from: '0x6cbcd73cd8e8a42844662f0a0e76d7f79afd933d',
121
+ gasUsed: BigInt(122),
122
+ gasPrice: BigInt(789),
123
+ logs: [],
124
+ logsBloom: '0x',
125
+ status: 1,
126
+ to: '0xC2907EFccE4011C491BbedA8A0fA63BA7aab596C',
127
+ hash: '0x435b00d28a10febbcfefbdea080134d08ef843df122d5bc9174b09de7fce6a59',
128
+ index: 123,
129
+ type: 0,
130
+ })
131
+ )
133
132
 
134
133
  jest
135
134
  .spyOn(JsonRpcProvider.prototype, 'broadcastTransaction')
136
- .mockImplementation(async (signedTx: string): Promise<any> => {
135
+ .mockImplementation(async (signedTx: string) => {
137
136
  if (
138
137
  signedTx ===
139
138
  '0x02f873012d85010c388d0085077715912682520894c2907efcce4011c491bbeda8a0fa63ba7aab596c87038d7ea4c6800080c001a0bbffe7ba303ab03f697d64672c4a288ae863df8a62ffc67ba72872ce8c227f6fa01261e7c9f06af13631f03fad9b88d3c48931d353b6b41b4072fddcca5ec41628'
@@ -149,9 +148,9 @@ describe('EVM Family', () => {
149
148
  throw new Error('any error')
150
149
  }
151
150
 
152
- return {
151
+ return mkTransactionResponse({
153
152
  hash: '0x435b00d28a10febbcfefbdea080134d08ef843df122d5bc9174b09de7fce6a59',
154
- }
153
+ })
155
154
  })
156
155
  })
157
156
 
@@ -192,15 +191,7 @@ describe('EVM Family', () => {
192
191
  retries: DEFAULT_RETRIES_RPC_METHODS,
193
192
  }
194
193
 
195
- try {
196
- await withApi(evmConfig, currencyId, spy, nodeConfig)
197
- fail('Promise should have been rejected')
198
- } catch (e) {
199
- if (e instanceof AssertionError) {
200
- throw e
201
- }
202
- expect(e).toBeInstanceOf(SpyError)
203
- }
194
+ await expect(withApi(evmConfig, currencyId, spy, nodeConfig)).rejects.toThrow(SpyError)
204
195
  })
205
196
 
206
197
  it('provider cache should reuse the same JsonRpcProvider for the same currency id and same uri', async () => {
@@ -696,8 +687,8 @@ describe('EVM Family', () => {
696
687
  describe('getGasEstimation', () => {
697
688
  it('should return the expected payload', async () => {
698
689
  jest
699
- .spyOn(JsonRpcProvider.prototype as any, '_perform')
700
- .mockImplementation(async (req: any) => {
690
+ .spyOn(JsonRpcProvider.prototype, '_perform')
691
+ .mockImplementation(async (req: PerformActionRequest) => {
701
692
  if (req.method === 'estimateGas') {
702
693
  return 5n
703
694
  }
@@ -714,8 +705,8 @@ describe('EVM Family', () => {
714
705
 
715
706
  it('should throw a GasEstimationError in case of error', async () => {
716
707
  jest
717
- .spyOn(JsonRpcProvider.prototype as any, '_perform')
718
- .mockImplementation(async (req: any) => {
708
+ .spyOn(JsonRpcProvider.prototype, '_perform')
709
+ .mockImplementation(async (req: PerformActionRequest) => {
719
710
  if (req.method === 'estimateGas') {
720
711
  throw new Error('fail')
721
712
  }
@@ -897,12 +888,13 @@ describe('EVM Family', () => {
897
888
  ])(
898
889
  'forwards feeHistoryRewardPercentile %i to eth_feeHistory',
899
890
  async (configured, expected) => {
900
- mockGetConfig.mockImplementation((): any => ({
901
- info: {
902
- node: { type: 'external', uri: 'my-rpc.com' },
891
+ mockGetConfig.mockImplementation(() =>
892
+ createMockEvmCoinConfig({
893
+ chainId: 1,
894
+ node: mkExternalNode({ uri: 'my-rpc.com' }),
903
895
  feeHistoryRewardPercentile: configured,
904
- },
905
- }))
896
+ })
897
+ )
906
898
  const sendSpy = stubFeeHistory()
907
899
 
908
900
  await nodeApi.getFeeData(getCoinConfig(currencyId).info, currencyId, eip1559Tx)
@@ -912,13 +904,14 @@ describe('EVM Family', () => {
912
904
  )
913
905
 
914
906
  it('combines feeHistoryBlockCount and feeHistoryRewardPercentile when both configured', async () => {
915
- mockGetConfig.mockImplementation((): any => ({
916
- info: {
917
- node: { type: 'external', uri: 'my-rpc.com' },
907
+ mockGetConfig.mockImplementation(() =>
908
+ createMockEvmCoinConfig({
909
+ chainId: 1,
910
+ node: mkExternalNode({ uri: 'my-rpc.com' }),
918
911
  feeHistoryBlockCount: 20,
919
912
  feeHistoryRewardPercentile: 75,
920
- },
921
- }))
913
+ })
914
+ )
922
915
  const sendSpy = stubFeeHistory()
923
916
 
924
917
  await nodeApi.getFeeData(getCoinConfig(currencyId).info, currencyId, eip1559Tx)
@@ -935,12 +928,13 @@ describe('EVM Family', () => {
935
928
  ])(
936
929
  'forwards block count %i to eth_feeHistory as %s (hex-encoded)',
937
930
  async (blockCount, expectedHex) => {
938
- mockGetConfig.mockImplementation((): any => ({
939
- info: {
940
- node: { type: 'external', uri: 'my-rpc.com' },
931
+ mockGetConfig.mockImplementation(() =>
932
+ createMockEvmCoinConfig({
933
+ chainId: 1,
934
+ node: mkExternalNode({ uri: 'my-rpc.com' }),
941
935
  feeHistoryBlockCount: blockCount,
942
- },
943
- }))
936
+ })
937
+ )
944
938
  const sendSpy = stubFeeHistory()
945
939
 
946
940
  await nodeApi.getFeeData(getCoinConfig(currencyId).info, currencyId, eip1559Tx)
@@ -950,14 +944,15 @@ describe('EVM Family', () => {
950
944
  )
951
945
 
952
946
  it('is not used on the legacy path (eth_feeHistory is never called)', async () => {
953
- mockGetConfig.mockImplementation((): any => ({
954
- info: {
955
- node: { type: 'external', uri: 'my-rpc.com' },
947
+ mockGetConfig.mockImplementation(() =>
948
+ createMockEvmCoinConfig({
949
+ chainId: 1,
950
+ node: mkExternalNode({ uri: 'my-rpc.com' }),
956
951
  feeHistoryBlockCount: 50,
957
- },
958
- }))
952
+ })
953
+ )
959
954
  const sendSpy = jest
960
- .spyOn(JsonRpcProvider.prototype as any, 'send')
955
+ .spyOn(JsonRpcProvider.prototype, 'send')
961
956
  .mockImplementation(async (method) => {
962
957
  if (method === 'eth_getBlockByNumber') {
963
958
  return {
@@ -978,13 +973,14 @@ describe('EVM Family', () => {
978
973
  })
979
974
 
980
975
  it('combines block count and minGasPrice floor correctly', async () => {
981
- mockGetConfig.mockImplementation((): any => ({
982
- info: {
983
- node: { type: 'external', uri: 'my-rpc.com' },
976
+ mockGetConfig.mockImplementation(() =>
977
+ createMockEvmCoinConfig({
978
+ chainId: 1,
979
+ node: mkExternalNode({ uri: 'my-rpc.com' }),
984
980
  feeHistoryBlockCount: 20,
985
981
  minGasPrice: '20000000000', // 20 Gwei
986
- },
987
- }))
982
+ })
983
+ )
988
984
  const sendSpy = jest
989
985
  .spyOn(JsonRpcProvider.prototype, 'send')
990
986
  .mockImplementation(async (method) => {
@@ -1010,12 +1006,13 @@ describe('EVM Family', () => {
1010
1006
  })
1011
1007
 
1012
1008
  it('applies minGasPrice floor to maxPriorityFeePerGas (EIP-1559) when feeHistory reports a lower value', async () => {
1013
- mockGetConfig.mockImplementation((): any => ({
1014
- info: {
1015
- node: { type: 'external', uri: 'my-rpc.com' },
1009
+ mockGetConfig.mockImplementation(() =>
1010
+ createMockEvmCoinConfig({
1011
+ chainId: 1,
1012
+ node: mkExternalNode({ uri: 'my-rpc.com' }),
1016
1013
  minGasPrice: '20000000000', // 20 Gwei floor
1017
- },
1018
- }))
1014
+ })
1015
+ )
1019
1016
 
1020
1017
  jest.spyOn(JsonRpcProvider.prototype, 'send').mockImplementationOnce(async (method) => {
1021
1018
  if (method === 'eth_feeHistory') {
@@ -1036,12 +1033,13 @@ describe('EVM Family', () => {
1036
1033
  })
1037
1034
 
1038
1035
  it('does not lower the priority fee when feeHistory already reports above the configured floor', async () => {
1039
- mockGetConfig.mockImplementation((): any => ({
1040
- info: {
1041
- node: { type: 'external', uri: 'my-rpc.com' },
1036
+ mockGetConfig.mockImplementation(() =>
1037
+ createMockEvmCoinConfig({
1038
+ chainId: 1,
1039
+ node: mkExternalNode({ uri: 'my-rpc.com' }),
1042
1040
  minGasPrice: '1000000000', // 1 Gwei floor
1043
- },
1044
- }))
1041
+ })
1042
+ )
1045
1043
 
1046
1044
  jest.spyOn(JsonRpcProvider.prototype, 'send').mockImplementationOnce(async (method) => {
1047
1045
  if (method === 'eth_feeHistory') {
@@ -1060,35 +1058,32 @@ describe('EVM Family', () => {
1060
1058
  })
1061
1059
 
1062
1060
  it('applies minGasPrice floor from coin config for legacy txs when node reports lower', async () => {
1063
- mockGetConfig.mockImplementation((): any => ({
1064
- info: {
1065
- node: { type: 'external', uri: 'my-rpc.com' },
1061
+ mockGetConfig.mockImplementation(() =>
1062
+ createMockEvmCoinConfig({
1063
+ chainId: 1,
1064
+ node: mkExternalNode({ uri: 'my-rpc.com' }),
1066
1065
  minGasPrice: '20000000000', // 20 Gwei
1067
- },
1068
- }))
1066
+ })
1067
+ )
1069
1068
 
1070
- jest
1071
- .spyOn(JsonRpcProvider.prototype as any, 'send')
1072
- .mockImplementationOnce(async (method) => {
1073
- if (method === 'eth_getBlockByNumber') {
1074
- return {
1075
- parentHash: '0x0',
1076
- number: 1,
1077
- timestamp: 123,
1078
- difficulty: 0n,
1079
- gasLimit: 1n,
1080
- gasUsed: 2n,
1081
- extraData: '0x',
1082
- }
1069
+ jest.spyOn(JsonRpcProvider.prototype, 'send').mockImplementationOnce(async (method) => {
1070
+ if (method === 'eth_getBlockByNumber') {
1071
+ return {
1072
+ parentHash: '0x0',
1073
+ number: 1,
1074
+ timestamp: 123,
1075
+ difficulty: 0n,
1076
+ gasLimit: 1n,
1077
+ gasUsed: 2n,
1078
+ extraData: '0x',
1083
1079
  }
1084
- })
1080
+ }
1081
+ })
1085
1082
  // node reports 1 Gwei via the getFeeData mock at the top of the suite (666 wei), but
1086
1083
  // we override here to assert the floor wins
1087
- jest.spyOn(JsonRpcProvider.prototype, 'getFeeData').mockResolvedValueOnce({
1088
- gasPrice: BigInt('1000000000'), // 1 Gwei
1089
- maxFeePerGas: 0n,
1090
- maxPriorityFeePerGas: 0n,
1091
- } as ethers.FeeData)
1084
+ jest.spyOn(JsonRpcProvider.prototype, 'getFeeData').mockResolvedValueOnce(
1085
+ new FeeData(BigInt('1000000000'), 0n, 0n) // 1 Gwei
1086
+ )
1092
1087
 
1093
1088
  const result = await nodeApi.getFeeData(getCoinConfig(currencyId).info, currencyId, legacyTx)
1094
1089
 
@@ -1135,24 +1130,22 @@ describe('EVM Family', () => {
1135
1130
  }
1136
1131
 
1137
1132
  it('should return the expected payload for a legacy tx', async () => {
1138
- jest
1139
- .spyOn(JsonRpcProvider.prototype as any, 'send')
1140
- .mockImplementationOnce(async (method) => {
1141
- switch (method) {
1142
- case 'eth_getBlockByNumber':
1143
- return {
1144
- parentHash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
1145
- number: 1,
1146
- timestamp: 123,
1147
- difficulty: 0n,
1148
- gasLimit: 1n,
1149
- gasUsed: 2n,
1150
- extraData: '0x',
1151
- }
1152
- default:
1153
- throw new Error(`Method not mocked: ${method}`)
1154
- }
1155
- })
1133
+ jest.spyOn(JsonRpcProvider.prototype, 'send').mockImplementationOnce(async (method) => {
1134
+ switch (method) {
1135
+ case 'eth_getBlockByNumber':
1136
+ return {
1137
+ parentHash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
1138
+ number: 1,
1139
+ timestamp: 123,
1140
+ difficulty: 0n,
1141
+ gasLimit: 1n,
1142
+ gasUsed: 2n,
1143
+ extraData: '0x',
1144
+ }
1145
+ default:
1146
+ throw new Error(`Method not mocked: ${method}`)
1147
+ }
1148
+ })
1156
1149
 
1157
1150
  expect(
1158
1151
  await nodeApi.getFeeData(getCoinConfig('optimism').info, 'optimism', legacyTx)
@@ -1178,30 +1171,18 @@ describe('EVM Family', () => {
1178
1171
  const serializedTransaction =
1179
1172
  '0x02f873012d85010c388d0085077715912682520894c2907efcce4011c491bbeda8a0fa63ba7aab596c87038d7ea4c6800080c001a0bbffe7ba303ab03f697d64672c4a288ae863df8a62ffc67ba72872ce8c227f6fa01261e7c9f06af13631f03fad9b88d3c48931d353b6b41b4072fddcca5ec41628'
1180
1173
 
1181
- try {
1182
- await nodeApi.broadcastTransaction(currencyId, serializedTransaction)
1183
- fail('Promise should have been rejected')
1184
- } catch (e) {
1185
- if (e instanceof AssertionError) {
1186
- throw e
1187
- }
1188
- expect(e).toBeInstanceOf(InsufficientFunds)
1189
- }
1174
+ await expect(nodeApi.broadcastTransaction(currencyId, serializedTransaction)).rejects.toThrow(
1175
+ InsufficientFunds
1176
+ )
1190
1177
  })
1191
1178
 
1192
1179
  it('should throw any other error', async () => {
1193
1180
  const serializedTransaction =
1194
1181
  '0x02f873012d85010c388d0085077715912682520894c2907efcce4011c491bbeda8a0fa63ba7aab596c87038d7ea4c6800080c001a0bbffe7ba303ab03f697d64672c4a288ae863df8a62ffc67ba72872ce8c227f6fa01261e7c9f06af13631f03fad9b88d3c48931d353b6b41b4072fddcca5ec41625'
1195
1182
 
1196
- try {
1197
- await nodeApi.broadcastTransaction(currencyId, serializedTransaction)
1198
- fail('Promise should have been rejected')
1199
- } catch (e) {
1200
- if (e instanceof AssertionError) {
1201
- throw e
1202
- }
1203
- expect((e as Error).message).toEqual('any error')
1204
- }
1183
+ await expect(nodeApi.broadcastTransaction(currencyId, serializedTransaction)).rejects.toThrow(
1184
+ 'any error'
1185
+ )
1205
1186
  })
1206
1187
  })
1207
1188
 
@@ -1219,22 +1200,24 @@ describe('EVM Family', () => {
1219
1200
  })
1220
1201
 
1221
1202
  it('should prefetch block transactions when requested', async () => {
1222
- jest.spyOn(JsonRpcProvider.prototype, 'getBlock').mockResolvedValueOnce({
1223
- hash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
1224
- parentHash: '0xfc900c22725f9c0843c9cf7d2c47f4b61b246bd21e18e99f709aebaefc8aff14',
1225
- number: 1,
1226
- timestamp: now,
1227
- transactions: ['0xtx1'],
1228
- prefetchedTransactions: [
1229
- {
1230
- hash: '0xtx1',
1231
- value: 1n,
1232
- from: '0x6cbcd73cd8e8a42844662f0a0e76d7f79afd933d',
1233
- to: '0xc2907efcce4011c491bbeda8a0fa63ba7aab596c',
1234
- gasPrice: '0',
1235
- },
1236
- ],
1237
- } as any)
1203
+ jest.spyOn(JsonRpcProvider.prototype, 'getBlock').mockResolvedValueOnce(
1204
+ mkBlock({
1205
+ hash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
1206
+ parentHash: '0xfc900c22725f9c0843c9cf7d2c47f4b61b246bd21e18e99f709aebaefc8aff14',
1207
+ number: 1,
1208
+ timestamp: now,
1209
+ transactions: ['0xtx1'],
1210
+ prefetchedTransactions: [
1211
+ mkTransactionResponse({
1212
+ hash: '0xtx1',
1213
+ value: 1n,
1214
+ from: '0x6cbcd73cd8e8a42844662f0a0e76d7f79afd933d',
1215
+ to: '0xc2907efcce4011c491bbeda8a0fa63ba7aab596c',
1216
+ gasPrice: 0n,
1217
+ }),
1218
+ ],
1219
+ })
1220
+ )
1238
1221
 
1239
1222
  expect(await nodeApi.getBlockByHeight(currencyId, 1, true)).toEqual({
1240
1223
  hash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
@@ -1255,18 +1238,23 @@ describe('EVM Family', () => {
1255
1238
  })
1256
1239
 
1257
1240
  it('should fallback to transaction hashes when prefetched transactions are unavailable', async () => {
1258
- jest.spyOn(JsonRpcProvider.prototype, 'getBlock').mockResolvedValueOnce({
1241
+ const blockWithThrowingGetter = mkBlock({
1259
1242
  hash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
1260
1243
  parentHash: '0xfc900c22725f9c0843c9cf7d2c47f4b61b246bd21e18e99f709aebaefc8aff14',
1261
1244
  number: 1,
1262
1245
  timestamp: now,
1263
1246
  transactions: ['0xtx1'],
1264
- get prefetchedTransactions() {
1247
+ })
1248
+ Object.defineProperty(blockWithThrowingGetter, 'prefetchedTransactions', {
1249
+ get() {
1265
1250
  const error = new Error('transactions were not prefetched with block request')
1266
- ;(error as Error & { code?: string }).code = 'UNSUPPORTED_OPERATION'
1251
+ Object.assign(error, { code: 'UNSUPPORTED_OPERATION' })
1267
1252
  throw error
1268
1253
  },
1269
- } as any)
1254
+ })
1255
+ jest
1256
+ .spyOn(JsonRpcProvider.prototype, 'getBlock')
1257
+ .mockResolvedValueOnce(blockWithThrowingGetter)
1270
1258
 
1271
1259
  expect(await nodeApi.getBlockByHeight(currencyId, 1, true)).toEqual({
1272
1260
  hash: '0x474dee0136108e9412e9d84197b468bb057a8dad0f2024fc55adebc4a28fa8c5',
@@ -1478,7 +1466,7 @@ describe('EVM Family', () => {
1478
1466
  (e: unknown) => e
1479
1467
  )
1480
1468
  expect(err).toBeInstanceOf(UnsupportedRpcMethodError)
1481
- expect((err as { method?: string }).method).toBe('trace_block')
1469
+ expect(err).toMatchObject({ method: 'trace_block' })
1482
1470
  }
1483
1471
  )
1484
1472
  })
@@ -1548,7 +1536,7 @@ describe('EVM Family', () => {
1548
1536
  (e: unknown) => e
1549
1537
  )
1550
1538
  expect(err).toBeInstanceOf(UnsupportedRpcMethodError)
1551
- expect((err as { method?: string }).method).toBe('debug_traceBlockByNumber')
1539
+ expect(err).toMatchObject({ method: 'debug_traceBlockByNumber' })
1552
1540
  }
1553
1541
  )
1554
1542
 
@@ -1568,7 +1556,7 @@ describe('EVM Family', () => {
1568
1556
  (e: unknown) => e
1569
1557
  )
1570
1558
  expect(err).toBeInstanceOf(UnsupportedRpcMethodError)
1571
- expect((err as { method?: string }).method).toBe('debug_traceBlockByNumber')
1559
+ expect(err).toMatchObject({ method: 'debug_traceBlockByNumber' })
1572
1560
  })
1573
1561
  })
1574
1562
 
@@ -1747,16 +1735,13 @@ describe('EVM Family', () => {
1747
1735
  const NORMALIZED_RECIPIENT = '0xc2907efcce4011c491bbeda8a0fa63ba7aab596c'
1748
1736
 
1749
1737
  let capturedTransaction: { from?: string; to?: string } | undefined
1750
- jest
1751
- .spyOn(JsonRpcProvider.prototype as any, '_perform')
1752
- .mockImplementation(async (arg: unknown) => {
1753
- const req = arg as { method: string; transaction?: typeof capturedTransaction }
1754
- if (req.method === 'estimateGas') {
1755
- capturedTransaction = req.transaction
1756
- return 21000n
1757
- }
1758
- return null
1759
- })
1738
+ jest.spyOn(JsonRpcProvider.prototype, '_perform').mockImplementation(async (arg) => {
1739
+ if (arg.method === 'estimateGas') {
1740
+ capturedTransaction = arg.transaction
1741
+ return 21000n
1742
+ }
1743
+ return null
1744
+ })
1760
1745
 
1761
1746
  await nodeApi.getGasEstimation(currencyId, address, {
1762
1747
  recipient: EIP1191_RECIPIENT,