@ledgerhq/coin-evm 5.3.4 → 5.3.9

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 (97) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/lib/adapters/etherscan.d.ts.map +1 -1
  3. package/lib/adapters/etherscan.js +2 -1
  4. package/lib/adapters/etherscan.js.map +1 -1
  5. package/lib/config.d.ts +4 -0
  6. package/lib/config.d.ts.map +1 -1
  7. package/lib/config.js.map +1 -1
  8. package/lib/errors.d.ts +6 -0
  9. package/lib/errors.d.ts.map +1 -1
  10. package/lib/errors.js +12 -1
  11. package/lib/errors.js.map +1 -1
  12. package/lib/logic/getBlock.js +2 -2
  13. package/lib/logic/getBlock.js.map +1 -1
  14. package/lib/network/explorer/etherscan.d.ts.map +1 -1
  15. package/lib/network/explorer/etherscan.js +38 -24
  16. package/lib/network/explorer/etherscan.js.map +1 -1
  17. package/lib/network/explorer/ledger.d.ts.map +1 -1
  18. package/lib/network/explorer/ledger.js +6 -4
  19. package/lib/network/explorer/ledger.js.map +1 -1
  20. package/lib/network/node/rpc.common.d.ts.map +1 -1
  21. package/lib/network/node/rpc.common.js +30 -6
  22. package/lib/network/node/rpc.common.js.map +1 -1
  23. package/lib/network/node/types.d.ts +3 -1
  24. package/lib/network/node/types.d.ts.map +1 -1
  25. package/lib/network/node/types.js.map +1 -1
  26. package/lib/network/withRetries.d.ts.map +1 -1
  27. package/lib/network/withRetries.js +13 -0
  28. package/lib/network/withRetries.js.map +1 -1
  29. package/lib/types/etherscan.d.ts +2 -0
  30. package/lib/types/etherscan.d.ts.map +1 -1
  31. package/lib/utils.d.ts +0 -2
  32. package/lib/utils.d.ts.map +1 -1
  33. package/lib/utils.js +0 -30
  34. package/lib/utils.js.map +1 -1
  35. package/lib-es/adapters/etherscan.d.ts.map +1 -1
  36. package/lib-es/adapters/etherscan.js +2 -1
  37. package/lib-es/adapters/etherscan.js.map +1 -1
  38. package/lib-es/config.d.ts +4 -0
  39. package/lib-es/config.d.ts.map +1 -1
  40. package/lib-es/config.js.map +1 -1
  41. package/lib-es/errors.d.ts +6 -0
  42. package/lib-es/errors.d.ts.map +1 -1
  43. package/lib-es/errors.js +10 -0
  44. package/lib-es/errors.js.map +1 -1
  45. package/lib-es/logic/getBlock.js +2 -2
  46. package/lib-es/logic/getBlock.js.map +1 -1
  47. package/lib-es/network/explorer/etherscan.d.ts.map +1 -1
  48. package/lib-es/network/explorer/etherscan.js +39 -25
  49. package/lib-es/network/explorer/etherscan.js.map +1 -1
  50. package/lib-es/network/explorer/ledger.d.ts.map +1 -1
  51. package/lib-es/network/explorer/ledger.js +7 -5
  52. package/lib-es/network/explorer/ledger.js.map +1 -1
  53. package/lib-es/network/node/rpc.common.d.ts.map +1 -1
  54. package/lib-es/network/node/rpc.common.js +30 -6
  55. package/lib-es/network/node/rpc.common.js.map +1 -1
  56. package/lib-es/network/node/types.d.ts +3 -1
  57. package/lib-es/network/node/types.d.ts.map +1 -1
  58. package/lib-es/network/node/types.js.map +1 -1
  59. package/lib-es/network/withRetries.d.ts.map +1 -1
  60. package/lib-es/network/withRetries.js +10 -0
  61. package/lib-es/network/withRetries.js.map +1 -1
  62. package/lib-es/types/etherscan.d.ts +2 -0
  63. package/lib-es/types/etherscan.d.ts.map +1 -1
  64. package/lib-es/utils.d.ts +0 -2
  65. package/lib-es/utils.d.ts.map +1 -1
  66. package/lib-es/utils.js +0 -28
  67. package/lib-es/utils.js.map +1 -1
  68. package/package.json +1 -1
  69. package/src/adapters/etherscan.test.ts +67 -0
  70. package/src/adapters/etherscan.ts +3 -1
  71. package/src/api/getBlock.integ.test.ts +1 -1
  72. package/src/api/index.avalanche_c_chain_fuji.integ.test.ts +1 -1
  73. package/src/api/index.integ.test.ts +7 -12
  74. package/src/api/index.linea.integ.test.ts +1 -1
  75. package/src/api/index.monad_testnet.integ.test.ts +1 -1
  76. package/src/api/index.optimism.integ.test.ts +30 -1
  77. package/src/api/index.rsk.integ.test.ts +1 -1
  78. package/src/api/index.shape.integ.test.ts +1 -1
  79. package/src/api/index.somnia.integ.test.ts +40 -0
  80. package/src/api/index.zksync.integ.test.ts +42 -0
  81. package/src/config.ts +4 -0
  82. package/src/errors.ts +10 -0
  83. package/src/logic/getBlock.test.ts +49 -0
  84. package/src/logic/getBlock.ts +3 -2
  85. package/src/network/explorer/etherscan.test.ts +407 -91
  86. package/src/network/explorer/etherscan.ts +69 -33
  87. package/src/network/explorer/ledger.test.ts +24 -13
  88. package/src/network/explorer/ledger.ts +27 -22
  89. package/src/network/node/rpc.common.ts +35 -6
  90. package/src/network/node/rpc.test.ts +73 -0
  91. package/src/network/node/types.ts +10 -1
  92. package/src/network/withRetries.test.ts +63 -0
  93. package/src/network/withRetries.ts +12 -0
  94. package/src/staking/validators.integ.test.ts +1 -1
  95. package/src/types/etherscan.ts +2 -0
  96. package/src/utils.test.ts +0 -86
  97. package/src/utils.ts +0 -41
@@ -0,0 +1,40 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { EvmConfigInfo } from '../config'
5
+ import { createMockEvmContext } from '../fixtures/context.fixtures'
6
+ import { createApi } from './index'
7
+
8
+ describe('EVM Api (Somnia)', () => {
9
+ let module: ReturnType<typeof createApi>
10
+
11
+ const config: Partial<EvmConfigInfo> = {
12
+ node: { type: 'external', uri: 'https://api.infra.mainnet.somnia.network' },
13
+ explorer: { type: 'blockscout', uri: 'https://explorer.somnia.network/api/' },
14
+ }
15
+
16
+ beforeAll(() => {
17
+ module = createApi('somnia')
18
+ })
19
+
20
+ describe('listOperations', () => {
21
+ it('keeps returning token operations on pages after the first when scanning from genesis', async () => {
22
+ const context = createMockEvmContext(config)
23
+ const address = '0x51d5ea8E6F98A73F823938Ff5731383aa92b0605'
24
+ const options = { minHeight: 0, order: 'asc' as const, limit: 3 }
25
+
26
+ const firstPage = await module.listOperations(context, address, options)
27
+ expect(firstPage.items.length).toBeGreaterThan(0)
28
+ expect(typeof firstPage.next).toBe('string')
29
+
30
+ const secondPage = await module.listOperations(context, address, {
31
+ ...options,
32
+ cursor: firstPage.next,
33
+ })
34
+
35
+ // The regression: without re-querying the token endpoint this page is native-only.
36
+ const erc20Ops = secondPage.items.filter((op) => op.asset.type === 'erc20')
37
+ expect(erc20Ops.length).toBeGreaterThan(0)
38
+ })
39
+ })
40
+ })
@@ -0,0 +1,42 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { createMockEvmContext, mkExternalNode } from '../fixtures/context.fixtures'
5
+ import { createApi } from './index'
6
+
7
+ /**
8
+ * Non regression for GET /v1/zksync/account/{address}/operations?minHeight=0&order=asc&limit=1,
9
+ * which was timing out because 429 responses from the blockscout upstream were silently
10
+ * retried until the request timed out.
11
+ *
12
+ * `limit` must be passed: listOperations forces the explorer order to `desc` when it is
13
+ * undefined, so omitting it would query a different code path than the reported one.
14
+ */
15
+ describe('ZkSync (blockscout explorer)', () => {
16
+ const ADDRESS = '0x060afdc8d583e134eebb50826695632f041c5153'
17
+
18
+ let module: ReturnType<typeof createApi>
19
+
20
+ beforeAll(() => {
21
+ module = createApi('zksync')
22
+ })
23
+
24
+ it('listOperations completes without timing out', async () => {
25
+ const { items } = await module.listOperations(
26
+ createMockEvmContext({
27
+ chainId: 324,
28
+ node: mkExternalNode({ uri: 'https://zksync.coin.ledger.com', retries: 0 }),
29
+ explorer: {
30
+ type: 'blockscout',
31
+ uri: 'https://proxyblockscout.api.live.ledger.com/324/api',
32
+ retries: 0,
33
+ },
34
+ }),
35
+ ADDRESS,
36
+ { minHeight: 0, order: 'asc', limit: 1 }
37
+ )
38
+
39
+ expect(Array.isArray(items)).toBe(true)
40
+ expect(items.length).toBeLessThanOrEqual(1)
41
+ })
42
+ })
package/src/config.ts CHANGED
@@ -52,11 +52,15 @@ export type EvmConfig = {
52
52
  */
53
53
  maxLimit?: number | undefined
54
54
  uri: string
55
+ /** Number of retries for explorer API calls. Defaults to 8 if not set. Set to 0 for no retries. */
56
+ retries?: number
55
57
  }
56
58
  | {
57
59
  type: 'ledger'
58
60
  explorerId: string
59
61
  batchSize?: number | undefined
62
+ /** Number of retries for explorer API calls. Defaults to 2 if not set. Set to 0 for no retries. */
63
+ retries?: number
60
64
  }
61
65
  | {
62
66
  type: 'none'
package/src/errors.ts CHANGED
@@ -44,6 +44,16 @@ export class InvalidExplorerResponse extends Error {
44
44
  }
45
45
  }
46
46
 
47
+ /** Rate limit error (HTTP 429) from an upstream API — explorer or RPC node. */
48
+ export class RateLimitError extends Error {
49
+ override name = 'RateLimitError'
50
+ readonly statusCode = 429
51
+ constructor(message?: string, fields?: Record<string, unknown>) {
52
+ super(message || 'Upstream rate limit exceeded (429), please retry later')
53
+ if (fields) Object.assign(this, fields)
54
+ }
55
+ }
56
+
47
57
  // Node
48
58
  export class UnknownNode extends Error {
49
59
  override name = 'UnknownNode'
@@ -1145,6 +1145,55 @@ describe('getBlock', () => {
1145
1145
  expect(result.transactions[0].fees).toEqual(21000n * 1000000000n)
1146
1146
  })
1147
1147
 
1148
+ it('adds l1Fee to fees when bulk receipt includes l1Fee (OP-stack bulk path)', async () => {
1149
+ setCoinConfig(() => createMockEvmCoinConfig({ chainId: 1, node: mkExternalNode() }))
1150
+ const gasUsed = 21000n
1151
+ const gasPrice = '1000000000'
1152
+ const l1Fee = '95394812830580'
1153
+ const mockGetNodeApi = jest.mocked(getNodeApi)
1154
+ mockGetNodeApi.mockReturnValue(
1155
+ mockNodeApi({
1156
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
1157
+ makeNodeBlock({
1158
+ transactions: [makeNodeBlockTx({ hash: '0xtx1', gasPrice })],
1159
+ })
1160
+ ),
1161
+ getBlockReceipts: jest.fn().mockResolvedValueOnce([
1162
+ makeNodeBlockReceipt({
1163
+ hash: '0xtx1',
1164
+ gasUsed: gasUsed.toString(),
1165
+ gasPrice,
1166
+ l1Fee,
1167
+ erc20Transfers: [],
1168
+ }),
1169
+ ]),
1170
+ getTransaction: jest.fn(),
1171
+ })
1172
+ )
1173
+ const result = await getBlock(createContext(), 'optimism', 12345)
1174
+ expect(result.transactions[0].fees).toEqual(gasUsed * BigInt(gasPrice) + BigInt(l1Fee))
1175
+ })
1176
+
1177
+ it('adds l1Fee to fees when getTransaction returns l1Fee (OP-stack per-tx fallback path)', async () => {
1178
+ setCoinConfig(() => createMockEvmCoinConfig({ chainId: 1, node: mkExternalNode() }))
1179
+ const gasUsed = '21000'
1180
+ const gasPrice = '1000000000'
1181
+ const l1Fee = '95394812830580'
1182
+ const mockGetNodeApi = jest.mocked(getNodeApi)
1183
+ mockGetNodeApi.mockReturnValue(
1184
+ mockNodeApi({
1185
+ getBlockByHeight: jest
1186
+ .fn()
1187
+ .mockResolvedValueOnce(makeNodeBlock({ transactionHashes: ['0xtx1'] })),
1188
+ getTransaction: jest
1189
+ .fn()
1190
+ .mockResolvedValueOnce(makeNodeTxInfo({ hash: '0xtx1', gasUsed, gasPrice, l1Fee })),
1191
+ })
1192
+ )
1193
+ const result = await getBlock(createContext(), 'optimism', 12345)
1194
+ expect(result.transactions[0].fees).toEqual(BigInt(gasUsed) * BigInt(gasPrice) + BigInt(l1Fee))
1195
+ })
1196
+
1148
1197
  describe('zkSync L1→L2 priority transactions (receipt type 0xff)', () => {
1149
1198
  const USER = '0xbEB30f27e61eFFF46Aa27dcE18E23ee8D5A7c6a4'
1150
1199
  const L2_BASE_TOKEN = '0x000000000000000000000000000000000000800A'
@@ -192,7 +192,7 @@ function prefetchedTransactionToBlockTransaction(
192
192
  // (the node layer provides `0` when the raw `eth_getBlockByNumber(_, true)` payload omits it).
193
193
  const receiptGasPrice = BigInt(receipt.gasPrice)
194
194
  const gasPrice = receiptGasPrice > 0n ? receiptGasPrice : BigInt(tx.gasPrice)
195
- const fees = BigInt(receipt.gasUsed) * gasPrice
195
+ const fees = BigInt(receipt.gasUsed) * gasPrice + (receipt.l1Fee ? BigInt(receipt.l1Fee) : 0n)
196
196
  const operations = rpcTransactionToBlockOperations({
197
197
  from: tx.from,
198
198
  to: tx.to,
@@ -220,7 +220,8 @@ async function getTransactionFromHash(
220
220
  const txInfo = await nodeApi.getTransaction(currencyId, txHash)
221
221
 
222
222
  const failed = txInfo.status === 0
223
- const fees = BigInt(txInfo.gasUsed) * BigInt(txInfo.gasPrice)
223
+ const fees =
224
+ BigInt(txInfo.gasUsed) * BigInt(txInfo.gasPrice) + (txInfo.l1Fee ? BigInt(txInfo.l1Fee) : 0n)
224
225
 
225
226
  const rawOperations = rpcTransactionToBlockOperations(txInfo)
226
227
  const operations = applyChainSpecificCorrections(currencyId, rawOperations, txInfo.type)