@ledgerhq/coin-evm 16.2.5 → 16.4.2

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 (118) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/lib/adapters/blockscoutV2.d.ts +40 -0
  3. package/lib/adapters/blockscoutV2.d.ts.map +1 -0
  4. package/lib/adapters/blockscoutV2.js +177 -0
  5. package/lib/adapters/blockscoutV2.js.map +1 -0
  6. package/lib/adapters/index.d.ts +1 -0
  7. package/lib/adapters/index.d.ts.map +1 -1
  8. package/lib/adapters/index.js +1 -0
  9. package/lib/adapters/index.js.map +1 -1
  10. package/lib/chains.d.ts +1 -1
  11. package/lib/chains.js +1 -1
  12. package/lib/chains.js.map +1 -1
  13. package/lib/config.d.ts +51 -24
  14. package/lib/config.d.ts.map +1 -1
  15. package/lib/config.js +21 -19
  16. package/lib/config.js.map +1 -1
  17. package/lib/logic/getBlock.d.ts.map +1 -1
  18. package/lib/logic/getBlock.js +14 -1
  19. package/lib/logic/getBlock.js.map +1 -1
  20. package/lib/logic/internalTransactionsFetcher.d.ts.map +1 -1
  21. package/lib/logic/internalTransactionsFetcher.js +58 -20
  22. package/lib/logic/internalTransactionsFetcher.js.map +1 -1
  23. package/lib/network/blockscoutV2.d.ts +35 -0
  24. package/lib/network/blockscoutV2.d.ts.map +1 -0
  25. package/lib/network/blockscoutV2.js +311 -0
  26. package/lib/network/blockscoutV2.js.map +1 -0
  27. package/lib/network/explorer/etherscan.d.ts.map +1 -1
  28. package/lib/network/explorer/etherscan.js +11 -28
  29. package/lib/network/explorer/etherscan.js.map +1 -1
  30. package/lib/network/explorer/index.d.ts.map +1 -1
  31. package/lib/network/explorer/index.js +5 -0
  32. package/lib/network/explorer/index.js.map +1 -1
  33. package/lib/network/explorer/types.d.ts +6 -0
  34. package/lib/network/explorer/types.d.ts.map +1 -1
  35. package/lib/network/explorer/types.js +8 -1
  36. package/lib/network/explorer/types.js.map +1 -1
  37. package/lib/network/rewards.d.ts +11 -0
  38. package/lib/network/rewards.d.ts.map +1 -0
  39. package/lib/network/rewards.js +32 -0
  40. package/lib/network/rewards.js.map +1 -0
  41. package/lib/types/blockscoutV2.d.ts +80 -0
  42. package/lib/types/blockscoutV2.d.ts.map +1 -0
  43. package/lib/types/blockscoutV2.js +5 -0
  44. package/lib/types/blockscoutV2.js.map +1 -0
  45. package/lib/types/index.d.ts +1 -0
  46. package/lib/types/index.d.ts.map +1 -1
  47. package/lib/types/index.js +1 -0
  48. package/lib/types/index.js.map +1 -1
  49. package/lib-es/adapters/blockscoutV2.d.ts +40 -0
  50. package/lib-es/adapters/blockscoutV2.d.ts.map +1 -0
  51. package/lib-es/adapters/blockscoutV2.js +168 -0
  52. package/lib-es/adapters/blockscoutV2.js.map +1 -0
  53. package/lib-es/adapters/index.d.ts +1 -0
  54. package/lib-es/adapters/index.d.ts.map +1 -1
  55. package/lib-es/adapters/index.js +1 -0
  56. package/lib-es/adapters/index.js.map +1 -1
  57. package/lib-es/chains.d.ts +1 -1
  58. package/lib-es/chains.js +1 -1
  59. package/lib-es/chains.js.map +1 -1
  60. package/lib-es/config.d.ts +51 -24
  61. package/lib-es/config.d.ts.map +1 -1
  62. package/lib-es/config.js +21 -19
  63. package/lib-es/config.js.map +1 -1
  64. package/lib-es/logic/getBlock.d.ts.map +1 -1
  65. package/lib-es/logic/getBlock.js +14 -1
  66. package/lib-es/logic/getBlock.js.map +1 -1
  67. package/lib-es/logic/internalTransactionsFetcher.d.ts.map +1 -1
  68. package/lib-es/logic/internalTransactionsFetcher.js +59 -21
  69. package/lib-es/logic/internalTransactionsFetcher.js.map +1 -1
  70. package/lib-es/network/blockscoutV2.d.ts +35 -0
  71. package/lib-es/network/blockscoutV2.d.ts.map +1 -0
  72. package/lib-es/network/blockscoutV2.js +271 -0
  73. package/lib-es/network/blockscoutV2.js.map +1 -0
  74. package/lib-es/network/explorer/etherscan.d.ts.map +1 -1
  75. package/lib-es/network/explorer/etherscan.js +2 -19
  76. package/lib-es/network/explorer/etherscan.js.map +1 -1
  77. package/lib-es/network/explorer/index.d.ts.map +1 -1
  78. package/lib-es/network/explorer/index.js +5 -0
  79. package/lib-es/network/explorer/index.js.map +1 -1
  80. package/lib-es/network/explorer/types.d.ts +6 -0
  81. package/lib-es/network/explorer/types.d.ts.map +1 -1
  82. package/lib-es/network/explorer/types.js +6 -0
  83. package/lib-es/network/explorer/types.js.map +1 -1
  84. package/lib-es/network/rewards.d.ts +11 -0
  85. package/lib-es/network/rewards.d.ts.map +1 -0
  86. package/lib-es/network/rewards.js +29 -0
  87. package/lib-es/network/rewards.js.map +1 -0
  88. package/lib-es/types/blockscoutV2.d.ts +80 -0
  89. package/lib-es/types/blockscoutV2.d.ts.map +1 -0
  90. package/lib-es/types/blockscoutV2.js +4 -0
  91. package/lib-es/types/blockscoutV2.js.map +1 -0
  92. package/lib-es/types/index.d.ts +1 -0
  93. package/lib-es/types/index.d.ts.map +1 -1
  94. package/lib-es/types/index.js +1 -0
  95. package/lib-es/types/index.js.map +1 -1
  96. package/package.json +3 -3
  97. package/src/adapters/blockscoutV2.test.ts +296 -0
  98. package/src/adapters/blockscoutV2.ts +244 -0
  99. package/src/adapters/index.ts +1 -0
  100. package/src/api/tests/index.alpen_testnet.integ.test.ts +73 -0
  101. package/src/api/tests/index.integ.test.ts +4 -2
  102. package/src/api/tests/index.scroll.integ.test.ts +168 -0
  103. package/src/api/tests/index.shape.integ.test.ts +34 -0
  104. package/src/chains.ts +1 -1
  105. package/src/config.ts +46 -19
  106. package/src/logic/getBlock.ts +18 -1
  107. package/src/logic/internalTransactionsFetcher.ts +73 -24
  108. package/src/logic/tests/getBlock.test.ts +59 -3
  109. package/src/logic/tests/internalTransactionsFetcher.test.ts +98 -1
  110. package/src/network/blockscoutV2.ts +460 -0
  111. package/src/network/explorer/etherscan.ts +2 -29
  112. package/src/network/explorer/index.ts +5 -0
  113. package/src/network/explorer/types.ts +9 -0
  114. package/src/network/rewards.ts +47 -0
  115. package/src/network/tests/blockscoutV2.test.ts +565 -0
  116. package/src/network/tests/explorer/index.test.ts +21 -0
  117. package/src/types/blockscoutV2.ts +85 -0
  118. package/src/types/index.ts +1 -0
@@ -0,0 +1,73 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { EvmConfigInfo } from '../../config'
5
+ import { internalTxSourcesFromList } from '../../config'
6
+ import { createMockEvmContext } from '../../fixtures/context.fixtures'
7
+ import { createApi } from '../index'
8
+
9
+ // Alpen Testnet runs Blockscout v9.0.2: its v2 token transfers report the token standard only in
10
+ // `token.type`, not in `token_type`.
11
+ describe('EVM Alpen Testnet (blockscout-v2 explorer, Blockscout v9)', () => {
12
+ const config: Partial<EvmConfigInfo> = {
13
+ chainId: 20310,
14
+ node: { type: 'external', uri: 'https://alpen.testnet.alpen.org' },
15
+ explorer: { type: 'blockscout-v2', uri: 'https://explorer.testnet.alpen.org/api/v2' },
16
+ }
17
+ const module = createApi()
18
+ const context = createMockEvmContext(config)
19
+
20
+ const usdc = '0xd7cB0E0692f2D55A17bA81c1fE5501D66774fC4A'
21
+
22
+ it('listOperations lists the ERC-20 transfers with the block hash of getBlock', async () => {
23
+ const address = '0xDeaD1F5aF792afc125812E875A891b038f888258'
24
+ const height = 224172
25
+ const { items } = await module.listOperations(context, address, {
26
+ minHeight: 0,
27
+ cursor: String(height),
28
+ limit: 5,
29
+ order: 'desc',
30
+ })
31
+ const block = await module.getBlock(context, height)
32
+
33
+ expect(items.filter((o) => o.asset.type === 'erc20').length).toBeGreaterThan(0)
34
+ expect(
35
+ items.find(
36
+ (o) =>
37
+ o.tx.hash === '0xe27e595026596fb2b08bf2c4857817afab00a926483e2f16649dbb8749a34826' &&
38
+ o.asset.type === 'erc20'
39
+ )
40
+ ).toMatchObject({
41
+ type: 'OUT',
42
+ value: 8_000_000n,
43
+ asset: { type: 'erc20', assetReference: usdc },
44
+ senders: [address],
45
+ recipients: ['0x25547a75818577AF0579308bDDC59Cb3E9E26d5b'],
46
+ tx: { block: { height, hash: block.info.hash } },
47
+ })
48
+ })
49
+
50
+ it('getBlock reads internal transactions from the Blockscout v2 API', async () => {
51
+ // Only the explorer source: the node refuses `trace_*` and `debug_*`
52
+ const explorerOnly = createMockEvmContext({
53
+ ...config,
54
+ getBlockInternalTxsSources: internalTxSourcesFromList(['explorer']),
55
+ })
56
+
57
+ // Faucet call: the faucet contract sends 0.1 tBTC to the caller's recipient
58
+ const block = await module.getBlock(explorerOnly, 221244)
59
+ const operations = block.transactions.find(
60
+ (t) => t.hash === '0xd8e37946b7f290047ae751c09e12dd53468a5b25df188ce3fcaf1ab078daee4b'
61
+ )?.operations
62
+
63
+ expect(operations).toContainEqual(
64
+ expect.objectContaining({
65
+ type: 'transfer',
66
+ address: '0xe59fC04559382d8d22235Cf4C7766e1fff5850C2',
67
+ peer: '0xAd77b4f3A3eA33A0201754CCE8683704B4A10575',
68
+ asset: { type: 'native' },
69
+ amount: 100_000_000_000_000_000n,
70
+ })
71
+ )
72
+ })
73
+ })
@@ -809,7 +809,7 @@ describe.each([
809
809
  type: 'send-legacy',
810
810
  intentType: 'transaction',
811
811
  amount: 1000000n, // 1 USDC (6 decimals)
812
- sender: '0xDb3812cD8d20F27888677dDC97dF1241850f8c48',
812
+ sender: '0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640', // Uniswap v3 USDC/WETH pool, always holds USDC
813
813
  recipient: '0x7b2C7232f9E38F30E2868f0E5Bf311Cd83554b5A',
814
814
  data: { type: 'buffer', value: Buffer.from([]) },
815
815
  asset: {
@@ -829,6 +829,7 @@ describe.each([
829
829
  type: 0,
830
830
  },
831
831
  })
832
+ expect(result.parameters?.gasLimit).toBeGreaterThan(0)
832
833
  expect(result.value).toBeGreaterThan(0)
833
834
  expect(result.parameters?.gasPrice).toBeGreaterThan(0)
834
835
  })
@@ -838,7 +839,7 @@ describe.each([
838
839
  type: 'send-eip1559',
839
840
  intentType: 'transaction',
840
841
  amount: 1000000n, // 1 USDC (6 decimals)
841
- sender: '0xDb3812cD8d20F27888677dDC97dF1241850f8c48',
842
+ sender: '0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640', // Uniswap v3 USDC/WETH pool, always holds USDC
842
843
  recipient: '0x7b2C7232f9E38F30E2868f0E5Bf311Cd83554b5A',
843
844
  data: { type: 'buffer', value: Buffer.from([]) },
844
845
  asset: {
@@ -858,6 +859,7 @@ describe.each([
858
859
  type: 2,
859
860
  },
860
861
  })
862
+ expect(result.parameters?.gasLimit).toBeGreaterThan(0)
861
863
  expect(result.value).toBeGreaterThan(0)
862
864
  expect(result.parameters?.maxFeePerGas).toBeGreaterThan(0)
863
865
  expect(result.parameters?.maxPriorityFeePerGas).toBeGreaterThan(0)
@@ -0,0 +1,168 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { EvmConfigInfo } from '../../config'
5
+ import type { MemoNotSupported, Operation } from '@ledgerhq/coin-module-framework/api/types'
6
+ import { internalTxSourcesFromList } from '../../config'
7
+ import { createMockEvmContext } from '../../fixtures/context.fixtures'
8
+ import { createApi } from '../index'
9
+
10
+ describe('EVM Scroll Network (blockscout-v2 explorer)', () => {
11
+ const config: Partial<EvmConfigInfo> = {
12
+ chainId: 534352,
13
+ node: {
14
+ type: 'external',
15
+ uri: 'https://scroll.alpaca.api.ledger.com',
16
+ },
17
+ explorer: {
18
+ type: 'blockscout-v2',
19
+ uri: 'https://proxyblockscout.api.live.ledger.com/534352/api/v2',
20
+ },
21
+ supportedTokens: ['erc721', 'erc1155'],
22
+ }
23
+ const module = createApi()
24
+ const context = createMockEvmContext(config)
25
+
26
+ // Account with transactions, ERC-20 transfers and internal transactions. It is still active, so
27
+ // the tests read it from a fixed block (the cursor is the highest block of the page).
28
+ const address = '0x0459a84952C85c6E9345e5d22d9B110790a5e1Ba'
29
+ const txHash = '0x43e90e6c63094c216268f02fea72f798ecbea691f208c4143d2aac322f6690ff'
30
+ const txHeight = 10582834
31
+ // Fee actually paid, mostly the L1 data fee: gasUsed × gasPrice alone is far lower, see LIVE-38797
32
+ const expectedFees = 6_863_830_062_607n
33
+
34
+ it('getBlock reports the fee including the L1 data fee', async () => {
35
+ const block = await module.getBlock(context, txHeight)
36
+ expect(block.transactions.find((t) => t.hash === txHash)?.fees).toEqual(expectedFees)
37
+ })
38
+
39
+ it('listOperations reports the same fee and block hash as getBlock', async () => {
40
+ const { items } = await module.listOperations(context, address, {
41
+ minHeight: 0,
42
+ cursor: String(txHeight),
43
+ limit: 5,
44
+ order: 'desc',
45
+ })
46
+ const operation = items.find((o) => o.tx.hash === txHash)
47
+ const block = await module.getBlock(context, txHeight)
48
+
49
+ expect(operation?.tx.fees).toEqual(expectedFees)
50
+ expect(operation?.tx.block.hash).toEqual(block.info.hash)
51
+ })
52
+
53
+ it('pages in descending order without gaps or duplicates', async () => {
54
+ const isInRange = (o: Operation<MemoNotSupported>): boolean => o.tx.block.height <= txHeight
55
+ const all = (await module.listOperations(context, address, { minHeight: 0 })).items.filter(
56
+ isInRange
57
+ )
58
+
59
+ const paged: Array<Operation<MemoNotSupported>> = []
60
+ let cursor: string | undefined = String(txHeight)
61
+ while (cursor) {
62
+ const page = await module.listOperations(context, address, {
63
+ minHeight: 0,
64
+ cursor,
65
+ limit: 5,
66
+ order: 'desc',
67
+ })
68
+ paged.push(...page.items)
69
+ cursor = page.next || undefined
70
+ }
71
+
72
+ const heights = paged.map((o) => o.tx.block.height)
73
+ expect(heights).toEqual([...heights].sort((a, b) => b - a))
74
+ expect(paged.map((o) => o.id).sort()).toEqual(all.map((o) => o.id).sort())
75
+ expect(paged.some((o) => o.asset.type === 'erc20')).toBe(true)
76
+ expect(paged.some((o) => o.details?.internal === true)).toBe(true)
77
+ })
78
+
79
+ it('lists an ERC-721 transfer', async () => {
80
+ const recipient = '0x2a10ed109Fe3088383D6b4d50C326D2DdeCE9923'
81
+ const height = 35442515
82
+ const { items } = await module.listOperations(context, recipient, {
83
+ minHeight: 0,
84
+ cursor: String(height),
85
+ limit: 5,
86
+ order: 'desc',
87
+ })
88
+ const operation = items.find(
89
+ (o) =>
90
+ o.tx.hash === '0x7dd31b6af23800b24311974d21d1ad3da4fbdc296879c40d33d4641ee7470eb2' &&
91
+ o.asset.type === 'erc721'
92
+ )
93
+ const block = await module.getBlock(context, height)
94
+
95
+ expect(operation).toMatchObject({
96
+ type: 'NFT_IN',
97
+ value: 1n,
98
+ asset: { type: 'erc721', assetReference: '0xcE8c1FcDD440b4Fd6e6c236568C74e6022767c97' },
99
+ senders: ['0x0000000000000000000000000000000000000000'],
100
+ recipients: [recipient],
101
+ tx: { block: { height, hash: block.info.hash } },
102
+ details: { tokenId: '1288' },
103
+ })
104
+ })
105
+
106
+ it('lists ERC-1155 transfers', async () => {
107
+ const recipient = '0xe71e46A8dd85BB3818fbBDaF8179da50A12D7166'
108
+ const height = 35419686
109
+ const { items } = await module.listOperations(context, recipient, {
110
+ minHeight: 0,
111
+ cursor: String(height),
112
+ limit: 5,
113
+ order: 'desc',
114
+ })
115
+ const operations = items.filter(
116
+ (o) => o.tx.hash === '0x37f8029709b9d4cce91881dee890c14ae8cd251495aef17f312a409e163b092c'
117
+ )
118
+ const block = await module.getBlock(context, height)
119
+
120
+ expect(
121
+ operations.find(
122
+ (o) =>
123
+ o.asset.type === 'erc1155' &&
124
+ o.asset.assetReference === '0x9D17534FaFf815D629abCfEF0d45D2Da7652D1eB'
125
+ )
126
+ ).toMatchObject({
127
+ type: 'NFT_IN',
128
+ value: 1n,
129
+ senders: ['0xcA11bde05977b3631167028862bE2a173976CA11'],
130
+ recipients: [recipient],
131
+ tx: { block: { height, hash: block.info.hash } },
132
+ details: { tokenId: '0' },
133
+ })
134
+ // One operation per transfer of the batch, with unique ids
135
+ const nftOperations = operations.filter((o) => o.asset.type === 'erc1155')
136
+ expect(nftOperations.length).toBeGreaterThan(1)
137
+ expect(new Set(nftOperations.map((o) => o.id)).size).toEqual(nftOperations.length)
138
+ })
139
+
140
+ it('getBlock reads internal transactions from the Blockscout v2 API', async () => {
141
+ // Only the explorer source, so that a failure cannot fall back to the node traces
142
+ const explorerOnly = createMockEvmContext({
143
+ ...config,
144
+ getBlockInternalTxsSources: internalTxSourcesFromList(['explorer']),
145
+ })
146
+
147
+ const block = await module.getBlock(explorerOnly, 33240184)
148
+ const operations = block.transactions.find(
149
+ (t) => t.hash === '0xda0d5242920bb16027cea259849a8f3f7f0e0c7b0cd9277fc0a78e4cf86e1900'
150
+ )?.operations
151
+
152
+ expect(operations).toContainEqual(
153
+ expect.objectContaining({
154
+ type: 'transfer',
155
+ address: '0x127B15f37aCBeaA4188A3388689445Ae892787bC',
156
+ peer: '0xbdA143d49da40C2cDA27c40edfBbe8A0D4AE0cBc',
157
+ asset: { type: 'native' },
158
+ amount: 3_000_000_000_000_000_000n,
159
+ })
160
+ )
161
+ })
162
+
163
+ it('rejects ascending pages', async () => {
164
+ await expect(
165
+ module.listOperations(context, address, { minHeight: 0, limit: 5, order: 'asc' })
166
+ ).rejects.toThrow('Ascending order is not supported')
167
+ })
168
+ })
@@ -47,4 +47,38 @@ describe('Shape (external node)', () => {
47
47
  )
48
48
  })
49
49
  })
50
+
51
+ // Shape's Blockscout reports the L1 data fee in `txlist` (`L1FeesPaid`), so the v1 API is enough
52
+ describe('fees include the L1 data fee', () => {
53
+ const config: Partial<EvmConfigInfo> = {
54
+ chainId: 360,
55
+ node: {
56
+ type: 'external',
57
+ uri: 'https://mainnet.shape.network',
58
+ },
59
+ explorer: {
60
+ type: 'blockscout',
61
+ uri: 'https://proxyblockscout.api.live.ledger.com/360/api',
62
+ },
63
+ }
64
+ const module = createApi()
65
+
66
+ const txHash = '0x68d2739afa41ec0523e03eb672002152195af1cf928ba6afa4b48944462d6754'
67
+ // gasUsed × gasPrice (21000 × 1000252 = 21_005_292_000) + L1 data fee, see LIVE-38797
68
+ const expectedFees = 846_354_837_246n
69
+
70
+ it('getBlock', async () => {
71
+ const block = await module.getBlock(createMockEvmContext(config), 3505965)
72
+ expect(block.transactions.find((t) => t.hash === txHash)?.fees).toEqual(expectedFees)
73
+ })
74
+
75
+ it('listOperations', async () => {
76
+ const { items } = await module.listOperations(
77
+ createMockEvmContext(config),
78
+ '0x9425E212647518853a92ae7D2C1Ab09D343e70b6',
79
+ { minHeight: 3505965, order: 'asc', limit: 5 }
80
+ )
81
+ expect(items.find((o) => o.tx.hash === txHash)?.tx.fees).toEqual(expectedFees)
82
+ })
83
+ })
50
84
  })
package/src/chains.ts CHANGED
@@ -32,7 +32,6 @@ export const CHAIN_ID = {
32
32
  BITTORRENT: 199,
33
33
  DEXON: 237,
34
34
  ENERGY_WEB: 246,
35
- FANTOM: 250,
36
35
  HPB: 269,
37
36
  BOBA: 288,
38
37
  ZKSYNC_SEPOLIA: 300,
@@ -65,6 +64,7 @@ export const CHAIN_ID = {
65
64
  MONAD_TESTNET: 10143,
66
65
  ZERO_GRAVITY: 16661,
67
66
  ALPEN_TESTNET: 20310,
67
+ ALPEN: 29032,
68
68
  ETHERSOCIAL: 31102,
69
69
  ADI: 36900,
70
70
  ARBITRUM: 42161,
package/src/config.ts CHANGED
@@ -48,6 +48,12 @@ export type EvmConfig = {
48
48
  explorer:
49
49
  | {
50
50
  type: 'etherscan' | 'blockscout' | 'teloscan' | 'klaytnfinder' | 'corescan' | 'cronos'
51
+ /**
52
+ * Disable the explorer response cache. By default, identical `getOperations` and
53
+ * `discoverTokenAssets` calls (same chain, address, block range, cursor, limit and order)
54
+ * made within 5 seconds share one explorer request; a failed request is not cached.
55
+ * Set to `true` to always send a new request.
56
+ */
51
57
  noCache?: boolean | undefined
52
58
  /**
53
59
  * Optional cap applied to the requested operation `limit` before the internal `limit + 1` probe.
@@ -66,6 +72,25 @@ export type EvmConfig = {
66
72
  /** Number of retries for explorer API calls. Defaults to 8 if not set. Set to 0 for no retries. */
67
73
  retries?: number
68
74
  }
75
+ | {
76
+ /**
77
+ * Blockscout v2 REST API. Unlike the Etherscan-compatible v1 API (`type: 'blockscout'`), it
78
+ * reports the fee actually paid, including the L1 data fee of rollups.
79
+ * Only serves operations in descending order.
80
+ */
81
+ type: 'blockscout-v2'
82
+ /** Base URI of the v2 API, e.g. `https://proxyblockscout.api.live.ledger.com/534352/api/v2` */
83
+ uri: string
84
+ /**
85
+ * Disable the explorer response cache. By default, identical `getOperations` and
86
+ * `discoverTokenAssets` calls (same chain, address, block range, cursor, limit and order)
87
+ * made within 5 seconds share one explorer request; a failed request is not cached.
88
+ * Set to `true` to always send a new request.
89
+ */
90
+ noCache?: boolean | undefined
91
+ /** Number of retries for explorer API calls. Defaults to 8 if not set. Set to 0 for no retries. */
92
+ retries?: number
93
+ }
69
94
  | {
70
95
  type: 'ledger'
71
96
  explorerId: string
@@ -382,22 +407,6 @@ export const evmDefaults = {
382
407
  type: 'none',
383
408
  },
384
409
  },
385
- fantom: {
386
- status: {
387
- type: 'active',
388
- features: [{ id: 'blockchain_txs', status: 'active' }],
389
- },
390
- chainId: 250,
391
- name: 'Fantom',
392
- unit: { name: 'FTM', code: 'FTM', magnitude: 18 },
393
- node: {
394
- type: 'external',
395
- uri: 'https://fantom.coin.ledger.com',
396
- },
397
- explorer: {
398
- type: 'none',
399
- },
400
- },
401
410
  flare: {
402
411
  status: {
403
412
  type: 'active',
@@ -1145,10 +1154,10 @@ export const evmDefaults = {
1145
1154
  chainId: 20310,
1146
1155
  name: 'Alpen Testnet',
1147
1156
  unit: { name: 'BTC', code: 'BTC', magnitude: 18 },
1148
- node: { type: 'external', uri: 'https://alpen.testnet.alpen.org' },
1157
+ node: { type: 'external', uri: 'https://alpen-testnet.coin.ledger.com' },
1149
1158
  explorer: {
1150
- type: 'blockscout',
1151
- uri: 'https://explorer.testnet.alpen.org/api',
1159
+ type: 'blockscout-v2',
1160
+ uri: 'https://alpen-testnet-explorer.api.ledger.com/api/v2',
1152
1161
  },
1153
1162
  // Alpen's fee market is sparse: the base fee holds at 0.000875-0.001 gwei and
1154
1163
  // eth_feeHistory reports no tips, so EIP-1559 estimation falls back to a 1 gwei tip
@@ -1157,4 +1166,22 @@ export const evmDefaults = {
1157
1166
  forceLegacyTransactions: true,
1158
1167
  minGasPrice: '2000000',
1159
1168
  },
1169
+ alpen: {
1170
+ status: {
1171
+ type: 'active',
1172
+ features: [{ id: 'blockchain_txs', status: 'active' }],
1173
+ },
1174
+ chainId: 29032,
1175
+ name: 'Alpen',
1176
+ unit: { name: 'BTC', code: 'BTC', magnitude: 18 },
1177
+ node: { type: 'external', uri: 'https://alpen.coin.ledger.com' },
1178
+ explorer: {
1179
+ type: 'blockscout-v2',
1180
+ uri: 'https://alpen-explorer.api.ledger.com/api/v2',
1181
+ },
1182
+ // Same sparse fee market as Alpen Testnet, with a base fee of 0.00025 gwei and no tips:
1183
+ // price as legacy from eth_gasPrice, with a 2x-base-fee floor.
1184
+ forceLegacyTransactions: true,
1185
+ minGasPrice: '500000',
1186
+ },
1160
1187
  } satisfies Record<string, EvmConfigInfo>
@@ -61,7 +61,7 @@ export async function getBlock(context: EvmContext, height: number): Promise<Blo
61
61
  // Internal transactions only exist inside transactions, so a block with none has nothing to
62
62
  // merge. Skipping the fetch avoids a request for a guaranteed-empty answer — on the `explorer`
63
63
  // source that is one metered `txlistinternal` call per empty block.
64
- const [transactions, internalTxs] = await Promise.all([
64
+ const [transactionsResult, internalTxsResult] = await Promise.allSettled([
65
65
  getTransactionsFromNode(
66
66
  config.chainId,
67
67
  config.name,
@@ -76,6 +76,19 @@ export async function getBlock(context: EvmContext, height: number): Promise<Blo
76
76
  : fetchInternalTxs(height),
77
77
  ])
78
78
 
79
+ // Wait for both calls, so that no error is lost when both fail.
80
+ if (transactionsResult.status === 'rejected' && internalTxsResult.status === 'rejected') {
81
+ throw new AggregateError(
82
+ [transactionsResult.reason, internalTxsResult.reason],
83
+ `getBlock ${height} failed: transactions: ${describeReason(transactionsResult.reason)}; internal transactions: ${describeReason(internalTxsResult.reason)}`
84
+ )
85
+ }
86
+ if (transactionsResult.status === 'rejected') throw transactionsResult.reason
87
+ if (internalTxsResult.status === 'rejected') throw internalTxsResult.reason
88
+
89
+ const transactions = transactionsResult.value
90
+ const internalTxs = internalTxsResult.value
91
+
79
92
  const mergedTransactions = mergeInternalTransactions(transactions, internalTxs)
80
93
 
81
94
  return {
@@ -84,6 +97,10 @@ export async function getBlock(context: EvmContext, height: number): Promise<Blo
84
97
  }
85
98
  }
86
99
 
100
+ function describeReason(reason: unknown): string {
101
+ return reason instanceof Error ? reason.message : String(reason)
102
+ }
103
+
87
104
  /**
88
105
  * Merges internal transaction operations into block transactions by matching tx hash.
89
106
  *
@@ -9,18 +9,64 @@ import type { Logger } from '@ledgerhq/coin-module-framework/config'
9
9
  import { traceBlockItemsToOperationsByHash } from '../adapters/blockOperations'
10
10
  import { internalTxsToOperationsByHash } from '../adapters/etherscan'
11
11
  import { SourceUnavailableError } from '../errors'
12
+ import { getInternalTransactionsByBlock as getBlockscoutV2InternalTransactionsByBlock } from '../network/blockscoutV2'
12
13
  import { getInternalTransactionsByBlock } from '../network/explorer/etherscan'
13
- import { isEtherscanLikeExplorerConfig } from '../network/explorer/types'
14
+ import {
15
+ isBlockscoutV2ExplorerConfig,
16
+ isEtherscanLikeExplorerConfig,
17
+ } from '../network/explorer/types'
14
18
 
15
19
  export type InternalTxsByHash = Map<string, BlockOperation[]>
16
20
 
17
21
  export type SourceFetcher = (height: number) => Promise<InternalTxsByHash>
18
22
 
19
- function throwCollectedRealErrors(errors: unknown[]): never {
20
- if (errors.length === 1) {
21
- throw errors[0]
23
+ type SourceFailure = { source: InternalTxSource; error: unknown }
24
+
25
+ function describeError(error: unknown): string {
26
+ if (!(error instanceof Error)) return String(error)
27
+ const { cause } = error
28
+ return cause === undefined ? error.message : `${error.message} (cause: ${describeError(cause)})`
29
+ }
30
+
31
+ function describeFailures(failures: readonly SourceFailure[]): string {
32
+ return failures.map(({ source, error }) => `[${source}] ${describeError(error)}`).join('; ')
33
+ }
34
+
35
+ function throwCollectedFailures(failures: readonly SourceFailure[]): never {
36
+ const [first] = failures
37
+ if (failures.length === 1 && first !== undefined) {
38
+ throw first.error
22
39
  }
23
- throw new AggregateError(errors, 'internal tx sources failed')
40
+ throw new AggregateError(
41
+ failures.map(({ error }) => error),
42
+ `internal tx sources failed: ${describeFailures(failures)}`
43
+ )
44
+ }
45
+
46
+ /**
47
+ * Records a failed source. Real errors are always kept. Unavailable errors are kept only when
48
+ * they carry a cause, because that cause is otherwise lost.
49
+ * Returns whether the error was real.
50
+ */
51
+ function recordFailure(
52
+ failures: SourceFailure[],
53
+ source: InternalTxSource,
54
+ error: unknown
55
+ ): boolean {
56
+ if (!(error instanceof SourceUnavailableError)) {
57
+ failures.push({ source, error })
58
+ return true
59
+ }
60
+ if (error.cause !== undefined) failures.push({ source, error })
61
+ return false
62
+ }
63
+
64
+ function throwExhausted(failures: readonly SourceFailure[], hadUnavailableSource: boolean): never {
65
+ if (hadUnavailableSource) {
66
+ const details = failures.length > 0 ? `: ${describeFailures(failures)}` : ''
67
+ throw new SourceUnavailableError(`all internal tx sources unavailable${details}`)
68
+ }
69
+ throw new Error('no internal tx sources configured')
24
70
  }
25
71
 
26
72
  async function runInternalTxSources(
@@ -28,37 +74,39 @@ async function runInternalTxSources(
28
74
  fetchers: Record<InternalTxSource, SourceFetcher>,
29
75
  height: number
30
76
  ): Promise<InternalTxsByHash> {
31
- const realErrors: unknown[] = []
77
+ // Real errors, plus unavailable errors that carry a cause, in source order.
78
+ const failures: SourceFailure[] = []
79
+ let hasRealError = false
32
80
  let hadUnavailableSource = false
33
81
 
34
82
  for (const source of sources) {
35
83
  if (source === 'empty') {
36
- if (realErrors.length > 0) {
37
- throwCollectedRealErrors(realErrors)
38
- }
84
+ if (hasRealError) throwCollectedFailures(failures)
39
85
  return new Map()
40
86
  }
41
87
 
42
88
  try {
43
89
  return await fetchers[source](height)
44
90
  } catch (error) {
45
- if (error instanceof SourceUnavailableError) {
46
- hadUnavailableSource = true
47
- continue
48
- }
49
- realErrors.push(error)
91
+ if (recordFailure(failures, source, error)) hasRealError = true
92
+ else hadUnavailableSource = true
50
93
  }
51
94
  }
52
95
 
53
- if (realErrors.length > 0) {
54
- throwCollectedRealErrors(realErrors)
55
- }
96
+ if (hasRealError) throwCollectedFailures(failures)
97
+ return throwExhausted(failures, hadUnavailableSource)
98
+ }
56
99
 
57
- if (hadUnavailableSource) {
58
- throw new SourceUnavailableError('all internal tx sources unavailable')
100
+ function explorerInternalTxsByBlock(
101
+ explorer: EvmConfigInfo['explorer']
102
+ ): typeof getInternalTransactionsByBlock | undefined {
103
+ if (isBlockscoutV2ExplorerConfig(explorer)) {
104
+ return getBlockscoutV2InternalTransactionsByBlock
59
105
  }
60
-
61
- throw new Error('no internal tx sources configured')
106
+ if (isEtherscanLikeExplorerConfig(explorer)) {
107
+ return getInternalTransactionsByBlock
108
+ }
109
+ return undefined
62
110
  }
63
111
 
64
112
  export function composeInternalTxsFetcher(
@@ -107,14 +155,15 @@ export function makeSourceFetchers(
107
155
  .then((items) => traceBlockItemsToOperationsByHash(items, logger))
108
156
  },
109
157
  explorer: (height: number) => {
110
- if (!isEtherscanLikeExplorerConfig(explorer)) {
158
+ const getByBlock = explorerInternalTxsByBlock(explorer)
159
+ if (getByBlock === undefined) {
111
160
  return Promise.reject(
112
161
  new SourceUnavailableError(
113
162
  `explorer internal txs not configured for currency ${config.name}`
114
163
  )
115
164
  )
116
165
  }
117
- return getInternalTransactionsByBlock(config, height)
166
+ return getByBlock(config, height)
118
167
  .then((txs) => internalTxsToOperationsByHash(txs, logger))
119
168
  .catch((error) => {
120
169
  logger('coin-evm', 'debug: explorer internal txs failed, falling through', {
@@ -122,7 +171,7 @@ export function makeSourceFetchers(
122
171
  blockHeight: height,
123
172
  error,
124
173
  })
125
- throw new SourceUnavailableError('explorer internal txs failed')
174
+ throw new SourceUnavailableError('explorer internal txs failed', { cause: error })
126
175
  })
127
176
  },
128
177
  empty: async () => new Map<string, BlockOperation[]>(),