@ledgerhq/coin-evm 16.2.5 → 16.4.0

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 (110) hide show
  1. package/CHANGELOG.md +18 -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 +24 -23
  14. package/lib/config.d.ts.map +1 -1
  15. package/lib/config.js +0 -16
  16. package/lib/config.js.map +1 -1
  17. package/lib/logic/internalTransactionsFetcher.d.ts.map +1 -1
  18. package/lib/logic/internalTransactionsFetcher.js +13 -2
  19. package/lib/logic/internalTransactionsFetcher.js.map +1 -1
  20. package/lib/network/blockscoutV2.d.ts +35 -0
  21. package/lib/network/blockscoutV2.d.ts.map +1 -0
  22. package/lib/network/blockscoutV2.js +311 -0
  23. package/lib/network/blockscoutV2.js.map +1 -0
  24. package/lib/network/explorer/etherscan.d.ts.map +1 -1
  25. package/lib/network/explorer/etherscan.js +11 -28
  26. package/lib/network/explorer/etherscan.js.map +1 -1
  27. package/lib/network/explorer/index.d.ts.map +1 -1
  28. package/lib/network/explorer/index.js +5 -0
  29. package/lib/network/explorer/index.js.map +1 -1
  30. package/lib/network/explorer/types.d.ts +6 -0
  31. package/lib/network/explorer/types.d.ts.map +1 -1
  32. package/lib/network/explorer/types.js +8 -1
  33. package/lib/network/explorer/types.js.map +1 -1
  34. package/lib/network/rewards.d.ts +11 -0
  35. package/lib/network/rewards.d.ts.map +1 -0
  36. package/lib/network/rewards.js +32 -0
  37. package/lib/network/rewards.js.map +1 -0
  38. package/lib/types/blockscoutV2.d.ts +80 -0
  39. package/lib/types/blockscoutV2.d.ts.map +1 -0
  40. package/lib/types/blockscoutV2.js +5 -0
  41. package/lib/types/blockscoutV2.js.map +1 -0
  42. package/lib/types/index.d.ts +1 -0
  43. package/lib/types/index.d.ts.map +1 -1
  44. package/lib/types/index.js +1 -0
  45. package/lib/types/index.js.map +1 -1
  46. package/lib-es/adapters/blockscoutV2.d.ts +40 -0
  47. package/lib-es/adapters/blockscoutV2.d.ts.map +1 -0
  48. package/lib-es/adapters/blockscoutV2.js +168 -0
  49. package/lib-es/adapters/blockscoutV2.js.map +1 -0
  50. package/lib-es/adapters/index.d.ts +1 -0
  51. package/lib-es/adapters/index.d.ts.map +1 -1
  52. package/lib-es/adapters/index.js +1 -0
  53. package/lib-es/adapters/index.js.map +1 -1
  54. package/lib-es/chains.d.ts +1 -1
  55. package/lib-es/chains.js +1 -1
  56. package/lib-es/chains.js.map +1 -1
  57. package/lib-es/config.d.ts +24 -23
  58. package/lib-es/config.d.ts.map +1 -1
  59. package/lib-es/config.js +0 -16
  60. package/lib-es/config.js.map +1 -1
  61. package/lib-es/logic/internalTransactionsFetcher.d.ts.map +1 -1
  62. package/lib-es/logic/internalTransactionsFetcher.js +14 -3
  63. package/lib-es/logic/internalTransactionsFetcher.js.map +1 -1
  64. package/lib-es/network/blockscoutV2.d.ts +35 -0
  65. package/lib-es/network/blockscoutV2.d.ts.map +1 -0
  66. package/lib-es/network/blockscoutV2.js +271 -0
  67. package/lib-es/network/blockscoutV2.js.map +1 -0
  68. package/lib-es/network/explorer/etherscan.d.ts.map +1 -1
  69. package/lib-es/network/explorer/etherscan.js +2 -19
  70. package/lib-es/network/explorer/etherscan.js.map +1 -1
  71. package/lib-es/network/explorer/index.d.ts.map +1 -1
  72. package/lib-es/network/explorer/index.js +5 -0
  73. package/lib-es/network/explorer/index.js.map +1 -1
  74. package/lib-es/network/explorer/types.d.ts +6 -0
  75. package/lib-es/network/explorer/types.d.ts.map +1 -1
  76. package/lib-es/network/explorer/types.js +6 -0
  77. package/lib-es/network/explorer/types.js.map +1 -1
  78. package/lib-es/network/rewards.d.ts +11 -0
  79. package/lib-es/network/rewards.d.ts.map +1 -0
  80. package/lib-es/network/rewards.js +29 -0
  81. package/lib-es/network/rewards.js.map +1 -0
  82. package/lib-es/types/blockscoutV2.d.ts +80 -0
  83. package/lib-es/types/blockscoutV2.d.ts.map +1 -0
  84. package/lib-es/types/blockscoutV2.js +4 -0
  85. package/lib-es/types/blockscoutV2.js.map +1 -0
  86. package/lib-es/types/index.d.ts +1 -0
  87. package/lib-es/types/index.d.ts.map +1 -1
  88. package/lib-es/types/index.js +1 -0
  89. package/lib-es/types/index.js.map +1 -1
  90. package/package.json +1 -1
  91. package/src/adapters/blockscoutV2.test.ts +296 -0
  92. package/src/adapters/blockscoutV2.ts +244 -0
  93. package/src/adapters/index.ts +1 -0
  94. package/src/api/tests/index.alpen_testnet.integ.test.ts +73 -0
  95. package/src/api/tests/index.integ.test.ts +4 -2
  96. package/src/api/tests/index.scroll.integ.test.ts +168 -0
  97. package/src/api/tests/index.shape.integ.test.ts +34 -0
  98. package/src/chains.ts +1 -1
  99. package/src/config.ts +25 -16
  100. package/src/logic/internalTransactionsFetcher.ts +20 -3
  101. package/src/logic/tests/internalTransactionsFetcher.test.ts +51 -0
  102. package/src/network/blockscoutV2.ts +460 -0
  103. package/src/network/explorer/etherscan.ts +2 -29
  104. package/src/network/explorer/index.ts +5 -0
  105. package/src/network/explorer/types.ts +9 -0
  106. package/src/network/rewards.ts +47 -0
  107. package/src/network/tests/blockscoutV2.test.ts +565 -0
  108. package/src/network/tests/explorer/index.test.ts +21 -0
  109. package/src/types/blockscoutV2.ts +85 -0
  110. package/src/types/index.ts +1 -0
@@ -0,0 +1,296 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type {
5
+ BlockscoutV2InternalTransaction,
6
+ BlockscoutV2TokenTransfer,
7
+ BlockscoutV2Transaction,
8
+ } from '../types'
9
+ import {
10
+ blockscoutV2ERC1155TransfersToOperations,
11
+ blockscoutV2ERC20TransfersToOperations,
12
+ blockscoutV2ERC721TransfersToOperations,
13
+ blockscoutV2InternalTransactionsToOperations,
14
+ blockscoutV2TransactionToOperations,
15
+ } from '../adapters'
16
+ import { CHAIN_ID } from '../config'
17
+ import { InvalidExplorerResponse } from '../errors'
18
+ import { testLogger } from '../fixtures/context.fixtures'
19
+
20
+ const address = '0x0459a84952C85c6E9345e5d22d9B110790a5e1Ba'
21
+ const other = '0x9425E212647518853a92ae7D2C1Ab09D343e70b6'
22
+ const contract = '0x5300000000000000000000000000000000000004'
23
+ const txHash = '0x43e90e6c63094c216268f02fea72f798ecbea691f208c4143d2aac322f6690ff'
24
+ const blockHash = '0x7595540cbd81adedc1137fa3cd8d64adcf18d0c2d45ee85ca7dddb89611c750c'
25
+ const date = new Date('2024-12-23T11:24:16.000Z')
26
+
27
+ const transaction: BlockscoutV2Transaction = {
28
+ hash: txHash,
29
+ block_number: 10582834,
30
+ position: 3,
31
+ timestamp: '2024-12-23T11:24:16.000000Z',
32
+ nonce: 12,
33
+ from: { hash: address.toLowerCase() },
34
+ to: { hash: other },
35
+ created_contract: null,
36
+ value: '1000',
37
+ fee: { type: 'actual', value: '6863830062607' },
38
+ gas_used: '21000',
39
+ gas_price: '40000000',
40
+ gas_limit: '21000',
41
+ status: 'ok',
42
+ raw_input: '0x',
43
+ method: null,
44
+ }
45
+
46
+ const tokenTransfer: BlockscoutV2TokenTransfer = {
47
+ transaction_hash: txHash,
48
+ block_hash: blockHash,
49
+ block_number: 10582834,
50
+ log_index: 4,
51
+ timestamp: '2024-12-23T11:24:16.000000Z',
52
+ from: { hash: other },
53
+ to: { hash: address },
54
+ token: {
55
+ address_hash: contract,
56
+ name: 'Wrapped Ether',
57
+ symbol: 'WETH',
58
+ decimals: '18',
59
+ type: 'ERC-20',
60
+ },
61
+ token_type: 'ERC-20',
62
+ total: { value: '7' },
63
+ }
64
+
65
+ const internalTransaction: BlockscoutV2InternalTransaction = {
66
+ transaction_hash: txHash,
67
+ block_number: 10582834,
68
+ index: 2,
69
+ timestamp: '2024-12-23T11:24:16.000000Z',
70
+ from: { hash: other },
71
+ to: { hash: address },
72
+ created_contract: null,
73
+ value: '9',
74
+ type: 'call',
75
+ success: true,
76
+ error: null,
77
+ gas_limit: '2300',
78
+ }
79
+
80
+ describe('blockscoutV2 adapters', () => {
81
+ describe('blockscoutV2TransactionToOperations', () => {
82
+ it('converts a native transfer, with the fee Blockscout reports', () => {
83
+ expect(
84
+ blockscoutV2TransactionToOperations(
85
+ address,
86
+ CHAIN_ID.SCROLL,
87
+ transaction,
88
+ blockHash,
89
+ testLogger
90
+ )
91
+ ).toEqual([
92
+ {
93
+ id: `${address}-${txHash}-OUT`,
94
+ type: 'OUT',
95
+ senders: [address],
96
+ recipients: [other],
97
+ value: 1000n,
98
+ asset: { type: 'native' },
99
+ tx: {
100
+ hash: txHash,
101
+ block: { height: 10582834, hash: blockHash, time: date },
102
+ fees: 6863830062607n,
103
+ date,
104
+ failed: false,
105
+ feesPayer: address,
106
+ },
107
+ details: { sequence: '12', gasPrice: '40000000', gasLimit: '21000' },
108
+ },
109
+ ])
110
+ })
111
+
112
+ it('converts a failed contract call into a FEES operation', () => {
113
+ const [operation] = blockscoutV2TransactionToOperations(
114
+ address,
115
+ CHAIN_ID.SCROLL,
116
+ { ...transaction, status: 'error', raw_input: '0xa9059cbb0000', value: '0' },
117
+ blockHash,
118
+ testLogger
119
+ )
120
+
121
+ expect(operation.type).toEqual('FEES')
122
+ expect(operation.tx.failed).toBe(true)
123
+ expect(operation.tx.fees).toEqual(6863830062607n)
124
+ })
125
+
126
+ it('reports the created contract as the recipient of a contract creation', () => {
127
+ const [operation] = blockscoutV2TransactionToOperations(
128
+ address,
129
+ CHAIN_ID.SCROLL,
130
+ { ...transaction, to: null, created_contract: { hash: contract } },
131
+ blockHash,
132
+ testLogger
133
+ )
134
+
135
+ expect(operation.recipients).toEqual([contract])
136
+ })
137
+
138
+ it.each<[string, BlockscoutV2Transaction['fee']]>([
139
+ ['empty', { type: 'actual', value: '' }],
140
+ ['non-numeric', { type: 'actual', value: '1e3' }],
141
+ ['pending', { type: 'maximum', value: '1000' }],
142
+ ])('rejects a %s fee', (_, fee) => {
143
+ expect(() =>
144
+ blockscoutV2TransactionToOperations(
145
+ address,
146
+ CHAIN_ID.SCROLL,
147
+ { ...transaction, fee },
148
+ blockHash,
149
+ testLogger
150
+ )
151
+ ).toThrow(InvalidExplorerResponse)
152
+ })
153
+
154
+ it('converts a self-send into an IN and an OUT operation', () => {
155
+ const operations = blockscoutV2TransactionToOperations(
156
+ address,
157
+ CHAIN_ID.SCROLL,
158
+ { ...transaction, to: { hash: address } },
159
+ blockHash,
160
+ testLogger
161
+ )
162
+
163
+ expect(operations.map((op) => op.type)).toEqual(['IN', 'OUT'])
164
+ })
165
+ })
166
+
167
+ describe('blockscoutV2ERC20TransfersToOperations', () => {
168
+ it('converts transfers, indexed within their transaction and without a nonce', () => {
169
+ const operations = blockscoutV2ERC20TransfersToOperations(
170
+ address,
171
+ [tokenTransfer, { ...tokenTransfer, log_index: 5, total: { value: '8' } }],
172
+ testLogger
173
+ )
174
+
175
+ expect(operations.map((op) => [op.id, op.value])).toEqual([
176
+ [`${txHash}-erc20-0-IN`, 7n],
177
+ [`${txHash}-erc20-1-IN`, 8n],
178
+ ])
179
+ expect(operations[0]).toMatchObject({
180
+ type: 'IN',
181
+ senders: [other],
182
+ recipients: [address],
183
+ asset: { type: 'erc20', assetReference: contract, assetOwner: address },
184
+ tx: { block: { height: 10582834, hash: blockHash, time: date }, fees: 0n },
185
+ })
186
+ expect(operations[0].details).not.toHaveProperty('sequence')
187
+ })
188
+ })
189
+
190
+ describe('blockscoutV2ERC721TransfersToOperations', () => {
191
+ it('converts a transfer', () => {
192
+ const [operation] = blockscoutV2ERC721TransfersToOperations(
193
+ address,
194
+ [{ ...tokenTransfer, token_type: 'ERC-721', total: { token_id: '42' } }],
195
+ testLogger
196
+ )
197
+
198
+ expect(operation).toMatchObject({
199
+ id: `${txHash}-erc721-0-NFT_IN`,
200
+ type: 'NFT_IN',
201
+ value: 1n,
202
+ asset: { type: 'erc721', assetReference: contract },
203
+ details: { tokenId: '42' },
204
+ })
205
+ })
206
+ })
207
+
208
+ describe('blockscoutV2ERC1155TransfersToOperations', () => {
209
+ it('converts a transfer', () => {
210
+ const [operation] = blockscoutV2ERC1155TransfersToOperations(
211
+ address,
212
+ [{ ...tokenTransfer, token_type: 'ERC-1155', total: { token_id: '42', value: '3' } }],
213
+ testLogger
214
+ )
215
+
216
+ expect(operation).toMatchObject({
217
+ id: `${txHash}-erc1155-0-NFT_IN`,
218
+ type: 'NFT_IN',
219
+ value: 3n,
220
+ asset: { type: 'erc1155', assetReference: contract },
221
+ details: { tokenId: '42' },
222
+ })
223
+ })
224
+ })
225
+
226
+ describe('blockscoutV2InternalTransactionsToOperations', () => {
227
+ const blockHashes = new Map([[10582834, blockHash]])
228
+
229
+ it('converts an internal transaction, with the hash of its block', () => {
230
+ expect(
231
+ blockscoutV2InternalTransactionsToOperations(
232
+ address,
233
+ [internalTransaction],
234
+ blockHashes,
235
+ testLogger
236
+ )
237
+ ).toEqual([
238
+ {
239
+ id: `${txHash}-internal-0-IN`,
240
+ type: 'IN',
241
+ senders: [other],
242
+ recipients: [address],
243
+ value: 9n,
244
+ asset: { type: 'native' },
245
+ tx: {
246
+ hash: txHash,
247
+ block: { height: 10582834, hash: blockHash, time: date },
248
+ fees: 0n,
249
+ date,
250
+ failed: false,
251
+ },
252
+ details: { internal: true },
253
+ },
254
+ ])
255
+ })
256
+
257
+ it('credits the contract an internal contract creation funds', () => {
258
+ const creation: BlockscoutV2InternalTransaction = {
259
+ ...internalTransaction,
260
+ from: { hash: other },
261
+ to: null,
262
+ created_contract: { hash: address },
263
+ type: 'create',
264
+ }
265
+
266
+ expect(
267
+ blockscoutV2InternalTransactionsToOperations(address, [creation], blockHashes, testLogger)
268
+ ).toMatchObject([{ type: 'IN', senders: [other], recipients: [address], value: 9n }])
269
+ expect(
270
+ blockscoutV2InternalTransactionsToOperations(other, [creation], blockHashes, testLogger)
271
+ ).toMatchObject([{ type: 'OUT', senders: [other], recipients: [address], value: 9n }])
272
+ })
273
+
274
+ it('drops the calls that cannot move native value', () => {
275
+ expect(
276
+ blockscoutV2InternalTransactionsToOperations(
277
+ address,
278
+ [{ ...internalTransaction, type: 'delegatecall' }],
279
+ blockHashes,
280
+ testLogger
281
+ )
282
+ ).toEqual([])
283
+ })
284
+
285
+ it('marks a failed internal transaction', () => {
286
+ const [operation] = blockscoutV2InternalTransactionsToOperations(
287
+ address,
288
+ [{ ...internalTransaction, success: false, error: 'Reverted' }],
289
+ blockHashes,
290
+ testLogger
291
+ )
292
+
293
+ expect(operation.tx.failed).toBe(true)
294
+ })
295
+ })
296
+ })
@@ -0,0 +1,244 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { ChainId } from '../config'
5
+ import type {
6
+ BlockscoutV2InternalTransaction,
7
+ BlockscoutV2TokenTransfer,
8
+ BlockscoutV2Transaction,
9
+ EtherscanERC1155Event,
10
+ EtherscanERC20Event,
11
+ EtherscanERC721Event,
12
+ EtherscanInternalTransaction,
13
+ } from '../types'
14
+ import type { MemoNotSupported, Operation } from '@ledgerhq/coin-module-framework/api/types'
15
+ import type { Logger } from '@ledgerhq/coin-module-framework/config'
16
+ import { InvalidExplorerResponse } from '../errors'
17
+ import {
18
+ etherscanERC1155EventToOperations,
19
+ etherscanERC20EventToOperations,
20
+ etherscanERC721EventToOperations,
21
+ etherscanInternalTransactionToOperations,
22
+ etherscanOperationToOperations,
23
+ } from './etherscan'
24
+
25
+ // Blockscout v2 items are mapped onto the Etherscan-like records, so that both explorers produce the
26
+ // same operations (ids, types, details) from the same on-chain data.
27
+
28
+ /** Unix timestamp in seconds, as Etherscan-like explorers report it. Omitted when unknown. */
29
+ function timeStamp(timestamp: string | null): { timeStamp?: string } {
30
+ const ms = timestamp === null ? Number.NaN : Date.parse(timestamp)
31
+ return Number.isNaN(ms) ? {} : { timeStamp: String(Math.floor(ms / 1000)) }
32
+ }
33
+
34
+ /**
35
+ * Token transfers carry no nonce: drop the empty `sequence` the Etherscan-like adapters set, so
36
+ * `listOperations` falls back to the parent transaction's.
37
+ */
38
+ function withoutEmptySequence(op: Operation<MemoNotSupported>): Operation<MemoNotSupported> {
39
+ if (op.details?.sequence !== '') return op
40
+ const { sequence: _, ...details } = op.details
41
+ return { ...op, details }
42
+ }
43
+
44
+ /**
45
+ * Index of each item among the items of the same transaction, which makes the operation ids unique
46
+ * when a transaction emits several transfers (same scheme as the Etherscan-like explorer).
47
+ */
48
+ function indexByTransaction<T extends { transaction_hash: string }>(
49
+ items: T[]
50
+ ): Array<[T, number]> {
51
+ const counts = new Map<string, number>()
52
+ return items.map((item) => {
53
+ const index = counts.get(item.transaction_hash) ?? 0
54
+ counts.set(item.transaction_hash, index + 1)
55
+ return [item, index]
56
+ })
57
+ }
58
+
59
+ /**
60
+ * Adapter to convert a Blockscout v2 transaction into Ledger Live Operations.
61
+ * It can return more than one operation in case of self-send.
62
+ *
63
+ * The fee is the one Blockscout reports as paid, which includes the L1 data fee of rollups.
64
+ *
65
+ * @param blockHash hash of the transaction's block, which the v2 transaction list does not report
66
+ */
67
+ export const blockscoutV2TransactionToOperations = (
68
+ address: string,
69
+ chainId: ChainId,
70
+ tx: BlockscoutV2Transaction,
71
+ blockHash: string,
72
+ logger: Logger
73
+ ): Array<Operation<MemoNotSupported>> => {
74
+ const input = tx.raw_input ?? ''
75
+ const failed = tx.status === 'error'
76
+ const operations = etherscanOperationToOperations(
77
+ address,
78
+ chainId,
79
+ {
80
+ blockNumber: String(tx.block_number ?? 0),
81
+ ...timeStamp(tx.timestamp),
82
+ hash: tx.hash,
83
+ nonce: String(tx.nonce),
84
+ blockHash,
85
+ transactionIndex: String(tx.position ?? ''),
86
+ from: tx.from.hash,
87
+ to: tx.to?.hash ?? '',
88
+ value: tx.value,
89
+ gas: tx.gas_limit,
90
+ gasPrice: tx.gas_price ?? '',
91
+ isError: failed ? '1' : '0',
92
+ txreceipt_status: failed ? '0' : '1',
93
+ input,
94
+ contractAddress: tx.created_contract?.hash ?? '',
95
+ cumulativeGasUsed: '',
96
+ gasUsed: tx.gas_used ?? '',
97
+ confirmations: '',
98
+ methodId: input.length >= 10 ? input.slice(0, 10) : '',
99
+ functionName: tx.method ?? '',
100
+ },
101
+ logger
102
+ )
103
+ // A missing fee must fail rather than silently become 0 (`BigInt('')` is `0n`)
104
+ if (tx.fee.type !== 'actual' || !/^\d+$/.test(tx.fee.value)) {
105
+ throw new InvalidExplorerResponse(`Missing or invalid fee in Blockscout v2 transaction`, {
106
+ hash: tx.hash,
107
+ fee: JSON.stringify(tx.fee),
108
+ })
109
+ }
110
+ const fees = BigInt(tx.fee.value)
111
+ return operations.map((op) => ({ ...op, tx: { ...op.tx, fees } }))
112
+ }
113
+
114
+ /** Fields shared by the Etherscan-like ERC-20, ERC-721 and ERC-1155 events */
115
+ type TokenEventBase = Omit<EtherscanERC20Event, 'value' | 'tokenDecimal'>
116
+
117
+ function tokenTransferBase(transfer: BlockscoutV2TokenTransfer): TokenEventBase {
118
+ return {
119
+ blockNumber: String(transfer.block_number),
120
+ ...timeStamp(transfer.timestamp),
121
+ hash: transfer.transaction_hash,
122
+ nonce: '',
123
+ blockHash: transfer.block_hash,
124
+ from: transfer.from.hash,
125
+ contractAddress: transfer.token.address_hash,
126
+ to: transfer.to.hash,
127
+ tokenName: transfer.token.name ?? '',
128
+ tokenSymbol: transfer.token.symbol ?? '',
129
+ transactionIndex: '',
130
+ gas: '',
131
+ gasPrice: '',
132
+ gasUsed: '',
133
+ cumulativeGasUsed: '',
134
+ input: '',
135
+ confirmations: '',
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Adapter to convert Blockscout v2 ERC-20 transfers into Ledger Live Operations.
141
+ * A transfer can produce up to 2 operations in case of self-send or airdrop.
142
+ *
143
+ * The fees are left to 0: `listOperations` takes them from the parent transaction.
144
+ */
145
+ export const blockscoutV2ERC20TransfersToOperations = (
146
+ address: string,
147
+ transfers: BlockscoutV2TokenTransfer[],
148
+ logger: Logger
149
+ ): Array<Operation<MemoNotSupported>> =>
150
+ indexByTransaction(transfers).flatMap(([transfer, index]) => {
151
+ const event: EtherscanERC20Event = {
152
+ ...tokenTransferBase(transfer),
153
+ value: transfer.total?.value ?? '0',
154
+ tokenDecimal: transfer.token.decimals ?? '',
155
+ }
156
+ return etherscanERC20EventToOperations(address, event, index, logger).map(withoutEmptySequence)
157
+ })
158
+
159
+ /**
160
+ * Adapter to convert Blockscout v2 ERC-721 transfers into Ledger Live Operations.
161
+ */
162
+ export const blockscoutV2ERC721TransfersToOperations = (
163
+ address: string,
164
+ transfers: BlockscoutV2TokenTransfer[],
165
+ logger: Logger
166
+ ): Array<Operation<MemoNotSupported>> =>
167
+ indexByTransaction(transfers).flatMap(([transfer, index]) => {
168
+ const event: EtherscanERC721Event = {
169
+ ...tokenTransferBase(transfer),
170
+ tokenID: transfer.total?.token_id ?? '',
171
+ tokenDecimal: transfer.token.decimals ?? '',
172
+ }
173
+ return etherscanERC721EventToOperations(address, event, index, logger).map(withoutEmptySequence)
174
+ })
175
+
176
+ /**
177
+ * Adapter to convert Blockscout v2 ERC-1155 transfers into Ledger Live Operations.
178
+ */
179
+ export const blockscoutV2ERC1155TransfersToOperations = (
180
+ address: string,
181
+ transfers: BlockscoutV2TokenTransfer[],
182
+ logger: Logger
183
+ ): Array<Operation<MemoNotSupported>> =>
184
+ indexByTransaction(transfers).flatMap(([transfer, index]) => {
185
+ const event: EtherscanERC1155Event = {
186
+ ...tokenTransferBase(transfer),
187
+ tokenID: transfer.total?.token_id ?? '',
188
+ tokenValue: transfer.total?.value ?? '0',
189
+ }
190
+ return etherscanERC1155EventToOperations(address, event, index, logger).map(
191
+ withoutEmptySequence
192
+ )
193
+ })
194
+
195
+ /**
196
+ * Map a Blockscout v2 internal transaction onto the Etherscan-like record, so that both explorers
197
+ * share the same internal transaction adapters (`listOperations` and `getBlock` paths).
198
+ */
199
+ export const blockscoutV2InternalTransactionToEtherscan = (
200
+ internalTx: BlockscoutV2InternalTransaction
201
+ ): EtherscanInternalTransaction => ({
202
+ blockNumber: String(internalTx.block_number),
203
+ ...timeStamp(internalTx.timestamp),
204
+ hash: internalTx.transaction_hash,
205
+ from: internalTx.from.hash,
206
+ // A contract creation has no `to`: its recipient is the created contract
207
+ to: internalTx.to?.hash ?? internalTx.created_contract?.hash ?? '',
208
+ value: internalTx.value,
209
+ contractAddress: internalTx.created_contract?.hash ?? '',
210
+ input: '',
211
+ type: internalTx.type,
212
+ callType: internalTx.type,
213
+ gas: internalTx.gas_limit ?? '',
214
+ gasUsed: '',
215
+ traceId: String(internalTx.index),
216
+ isError: internalTx.success ? '0' : '1',
217
+ errCode: internalTx.error ?? '',
218
+ })
219
+
220
+ /**
221
+ * Adapter to convert Blockscout v2 internal transactions into Ledger Live Operations.
222
+ *
223
+ * @param blockHashes hash of each block, which the v2 internal transaction list does not report
224
+ */
225
+ export const blockscoutV2InternalTransactionsToOperations = (
226
+ address: string,
227
+ internalTransactions: BlockscoutV2InternalTransaction[],
228
+ blockHashes: ReadonlyMap<number, string>,
229
+ logger: Logger
230
+ ): Array<Operation<MemoNotSupported>> =>
231
+ indexByTransaction(internalTransactions).flatMap(([internalTx, index]) =>
232
+ etherscanInternalTransactionToOperations(
233
+ address,
234
+ blockscoutV2InternalTransactionToEtherscan(internalTx),
235
+ index,
236
+ logger
237
+ ).map((op) => ({
238
+ ...op,
239
+ tx: {
240
+ ...op.tx,
241
+ block: { ...op.tx.block, hash: blockHashes.get(internalTx.block_number) ?? '' },
242
+ },
243
+ }))
244
+ )
@@ -5,4 +5,5 @@
5
5
 
6
6
  export * from './ledger'
7
7
  export * from './etherscan'
8
+ export * from './blockscoutV2'
8
9
  export * from './blockOperations'
@@ -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)