@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.
- package/CHANGELOG.md +18 -0
- package/lib/adapters/blockscoutV2.d.ts +40 -0
- package/lib/adapters/blockscoutV2.d.ts.map +1 -0
- package/lib/adapters/blockscoutV2.js +177 -0
- package/lib/adapters/blockscoutV2.js.map +1 -0
- package/lib/adapters/index.d.ts +1 -0
- package/lib/adapters/index.d.ts.map +1 -1
- package/lib/adapters/index.js +1 -0
- package/lib/adapters/index.js.map +1 -1
- package/lib/chains.d.ts +1 -1
- package/lib/chains.js +1 -1
- package/lib/chains.js.map +1 -1
- package/lib/config.d.ts +24 -23
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +0 -16
- package/lib/config.js.map +1 -1
- package/lib/logic/internalTransactionsFetcher.d.ts.map +1 -1
- package/lib/logic/internalTransactionsFetcher.js +13 -2
- package/lib/logic/internalTransactionsFetcher.js.map +1 -1
- package/lib/network/blockscoutV2.d.ts +35 -0
- package/lib/network/blockscoutV2.d.ts.map +1 -0
- package/lib/network/blockscoutV2.js +311 -0
- package/lib/network/blockscoutV2.js.map +1 -0
- package/lib/network/explorer/etherscan.d.ts.map +1 -1
- package/lib/network/explorer/etherscan.js +11 -28
- package/lib/network/explorer/etherscan.js.map +1 -1
- package/lib/network/explorer/index.d.ts.map +1 -1
- package/lib/network/explorer/index.js +5 -0
- package/lib/network/explorer/index.js.map +1 -1
- package/lib/network/explorer/types.d.ts +6 -0
- package/lib/network/explorer/types.d.ts.map +1 -1
- package/lib/network/explorer/types.js +8 -1
- package/lib/network/explorer/types.js.map +1 -1
- package/lib/network/rewards.d.ts +11 -0
- package/lib/network/rewards.d.ts.map +1 -0
- package/lib/network/rewards.js +32 -0
- package/lib/network/rewards.js.map +1 -0
- package/lib/types/blockscoutV2.d.ts +80 -0
- package/lib/types/blockscoutV2.d.ts.map +1 -0
- package/lib/types/blockscoutV2.js +5 -0
- package/lib/types/blockscoutV2.js.map +1 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.js +1 -0
- package/lib/types/index.js.map +1 -1
- package/lib-es/adapters/blockscoutV2.d.ts +40 -0
- package/lib-es/adapters/blockscoutV2.d.ts.map +1 -0
- package/lib-es/adapters/blockscoutV2.js +168 -0
- package/lib-es/adapters/blockscoutV2.js.map +1 -0
- package/lib-es/adapters/index.d.ts +1 -0
- package/lib-es/adapters/index.d.ts.map +1 -1
- package/lib-es/adapters/index.js +1 -0
- package/lib-es/adapters/index.js.map +1 -1
- package/lib-es/chains.d.ts +1 -1
- package/lib-es/chains.js +1 -1
- package/lib-es/chains.js.map +1 -1
- package/lib-es/config.d.ts +24 -23
- package/lib-es/config.d.ts.map +1 -1
- package/lib-es/config.js +0 -16
- package/lib-es/config.js.map +1 -1
- package/lib-es/logic/internalTransactionsFetcher.d.ts.map +1 -1
- package/lib-es/logic/internalTransactionsFetcher.js +14 -3
- package/lib-es/logic/internalTransactionsFetcher.js.map +1 -1
- package/lib-es/network/blockscoutV2.d.ts +35 -0
- package/lib-es/network/blockscoutV2.d.ts.map +1 -0
- package/lib-es/network/blockscoutV2.js +271 -0
- package/lib-es/network/blockscoutV2.js.map +1 -0
- package/lib-es/network/explorer/etherscan.d.ts.map +1 -1
- package/lib-es/network/explorer/etherscan.js +2 -19
- package/lib-es/network/explorer/etherscan.js.map +1 -1
- package/lib-es/network/explorer/index.d.ts.map +1 -1
- package/lib-es/network/explorer/index.js +5 -0
- package/lib-es/network/explorer/index.js.map +1 -1
- package/lib-es/network/explorer/types.d.ts +6 -0
- package/lib-es/network/explorer/types.d.ts.map +1 -1
- package/lib-es/network/explorer/types.js +6 -0
- package/lib-es/network/explorer/types.js.map +1 -1
- package/lib-es/network/rewards.d.ts +11 -0
- package/lib-es/network/rewards.d.ts.map +1 -0
- package/lib-es/network/rewards.js +29 -0
- package/lib-es/network/rewards.js.map +1 -0
- package/lib-es/types/blockscoutV2.d.ts +80 -0
- package/lib-es/types/blockscoutV2.d.ts.map +1 -0
- package/lib-es/types/blockscoutV2.js +4 -0
- package/lib-es/types/blockscoutV2.js.map +1 -0
- package/lib-es/types/index.d.ts +1 -0
- package/lib-es/types/index.d.ts.map +1 -1
- package/lib-es/types/index.js +1 -0
- package/lib-es/types/index.js.map +1 -1
- package/package.json +1 -1
- package/src/adapters/blockscoutV2.test.ts +296 -0
- package/src/adapters/blockscoutV2.ts +244 -0
- package/src/adapters/index.ts +1 -0
- package/src/api/tests/index.alpen_testnet.integ.test.ts +73 -0
- package/src/api/tests/index.integ.test.ts +4 -2
- package/src/api/tests/index.scroll.integ.test.ts +168 -0
- package/src/api/tests/index.shape.integ.test.ts +34 -0
- package/src/chains.ts +1 -1
- package/src/config.ts +25 -16
- package/src/logic/internalTransactionsFetcher.ts +20 -3
- package/src/logic/tests/internalTransactionsFetcher.test.ts +51 -0
- package/src/network/blockscoutV2.ts +460 -0
- package/src/network/explorer/etherscan.ts +2 -29
- package/src/network/explorer/index.ts +5 -0
- package/src/network/explorer/types.ts +9 -0
- package/src/network/rewards.ts +47 -0
- package/src/network/tests/blockscoutV2.test.ts +565 -0
- package/src/network/tests/explorer/index.test.ts +21 -0
- package/src/types/blockscoutV2.ts +85 -0
- package/src/types/index.ts +1 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { EvmConfigInfo } from '../config'
|
|
5
|
+
import type {
|
|
6
|
+
BlockscoutV2InternalTransaction,
|
|
7
|
+
EtherscanInternalTransaction,
|
|
8
|
+
BlockscoutV2Page,
|
|
9
|
+
BlockscoutV2TokenTransfer,
|
|
10
|
+
BlockscoutV2Transaction,
|
|
11
|
+
} from '../types'
|
|
12
|
+
import type { ExplorerApi } from './explorer/types'
|
|
13
|
+
import type { AssetInfo } from '@ledgerhq/coin-module-framework/api/types'
|
|
14
|
+
import type { Logger } from '@ledgerhq/coin-module-framework/config'
|
|
15
|
+
import { InvalidParameterError } from '@ledgerhq/coin-module-framework/errors'
|
|
16
|
+
import { delay, promiseAllBatched } from '@ledgerhq/coin-module-framework/promises'
|
|
17
|
+
import { makeLRUCache } from '@ledgerhq/live-network/cache'
|
|
18
|
+
import axios, { isAxiosError } from 'axios'
|
|
19
|
+
import eip55 from 'eip55'
|
|
20
|
+
import {
|
|
21
|
+
blockscoutV2ERC1155TransfersToOperations,
|
|
22
|
+
blockscoutV2ERC20TransfersToOperations,
|
|
23
|
+
blockscoutV2ERC721TransfersToOperations,
|
|
24
|
+
blockscoutV2InternalTransactionToEtherscan,
|
|
25
|
+
blockscoutV2InternalTransactionsToOperations,
|
|
26
|
+
blockscoutV2TransactionToOperations,
|
|
27
|
+
} from '../adapters'
|
|
28
|
+
import { InvalidExplorerResponse, RateLimitError } from '../errors'
|
|
29
|
+
import { isBlockscoutV2ExplorerConfig, NO_TOKEN } from './explorer/types'
|
|
30
|
+
import { getNodeApi } from './node'
|
|
31
|
+
import { enrichRewardOperations } from './rewards'
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Explorer backed by the Blockscout v2 REST API (`/api/v2`).
|
|
35
|
+
*
|
|
36
|
+
* The address transactions, token transfers and internal transactions are three keyset-paginated
|
|
37
|
+
* lists, sorted by descending block. A request can start at any block by seeding the cursor with
|
|
38
|
+
* that block, which is how pages are resumed. The lists cannot be read in ascending order, so this
|
|
39
|
+
* explorer only serves descending pages.
|
|
40
|
+
*
|
|
41
|
+
* Unlike the Etherscan-compatible v1 API, the v2 API reports the fee actually paid by each
|
|
42
|
+
* transaction, including the L1 data fee of rollups.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/** Wait between 2 attempts of a failed request, and lifetime of the cached responses */
|
|
46
|
+
const BLOCKSCOUT_V2_TIMEOUT = 5000
|
|
47
|
+
|
|
48
|
+
/** Retries of a failed request, unless the explorer config sets `retries` */
|
|
49
|
+
const DEFAULT_RETRIES_API = 8
|
|
50
|
+
|
|
51
|
+
/** Node requests in flight at once to resolve block hashes (ethers sends them as one JSON-RPC batch) */
|
|
52
|
+
const BLOCK_HASH_BATCH_SIZE = 100
|
|
53
|
+
|
|
54
|
+
type PageParams = Record<string, string | number | null>
|
|
55
|
+
|
|
56
|
+
function isObject(data: unknown): data is object {
|
|
57
|
+
return typeof data === 'object' && data !== null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isPage(data: unknown): data is BlockscoutV2Page<unknown> {
|
|
61
|
+
return isObject(data) && 'items' in data && Array.isArray(data.items)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function fetchPage<T>(
|
|
65
|
+
url: string,
|
|
66
|
+
params: PageParams,
|
|
67
|
+
retries: number
|
|
68
|
+
): Promise<BlockscoutV2Page<T>> {
|
|
69
|
+
try {
|
|
70
|
+
const { data } = await axios.request<unknown>({ method: 'GET', url, params })
|
|
71
|
+
if (!isPage(data)) {
|
|
72
|
+
throw new InvalidExplorerResponse(`Unexpected Blockscout v2 API response from ${url}`)
|
|
73
|
+
}
|
|
74
|
+
// oxlint-disable-next-line consistent-type-assertions -- the item shape is the documented Blockscout v2 one, only the envelope is checked
|
|
75
|
+
return data as BlockscoutV2Page<T>
|
|
76
|
+
} catch (e) {
|
|
77
|
+
if (isAxiosError(e) && e.response?.status === 429) {
|
|
78
|
+
throw new RateLimitError()
|
|
79
|
+
}
|
|
80
|
+
if (retries) {
|
|
81
|
+
// wait the API timeout before trying again
|
|
82
|
+
await delay(BLOCKSCOUT_V2_TIMEOUT)
|
|
83
|
+
return fetchPage<T>(url, params, retries - 1)
|
|
84
|
+
}
|
|
85
|
+
throw e
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type StreamItem = { block_number: number | null }
|
|
90
|
+
|
|
91
|
+
type StreamRead<T> = {
|
|
92
|
+
/** Items in descending block order, all at or above the floor */
|
|
93
|
+
items: T[]
|
|
94
|
+
/**
|
|
95
|
+
* Block of the last item when the read stopped because `limit` was reached: more items may exist
|
|
96
|
+
* below it. The block itself is always complete. `undefined` when the list was read down to the
|
|
97
|
+
* floor.
|
|
98
|
+
*/
|
|
99
|
+
boundBlock: number | undefined
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
type Stream = {
|
|
103
|
+
/** List endpoint, relative to the explorer URI */
|
|
104
|
+
path: string
|
|
105
|
+
/** Fixed query params (filters) */
|
|
106
|
+
params: PageParams
|
|
107
|
+
/** Cursor that starts the list right below `block` */
|
|
108
|
+
seed: (block: number) => PageParams
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Visit each item of a list from `ceiling` (inclusive, or the chain head) down to `floor`
|
|
113
|
+
* (inclusive), one page at a time, until `visit` returns `false`. Pending items are skipped.
|
|
114
|
+
*/
|
|
115
|
+
async function walkStream<T extends StreamItem>(
|
|
116
|
+
uri: string,
|
|
117
|
+
stream: Stream,
|
|
118
|
+
floor: number,
|
|
119
|
+
ceiling: number | undefined,
|
|
120
|
+
retries: number,
|
|
121
|
+
visit: (item: T, block: number) => boolean
|
|
122
|
+
): Promise<void> {
|
|
123
|
+
let params: PageParams | null = {
|
|
124
|
+
...stream.params,
|
|
125
|
+
...(ceiling !== undefined && stream.seed(ceiling + 1)),
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
while (params) {
|
|
129
|
+
const page: BlockscoutV2Page<T> = await fetchPage<T>(`${uri}${stream.path}`, params, retries)
|
|
130
|
+
for (const item of page.items) {
|
|
131
|
+
// Pending transactions have no block yet
|
|
132
|
+
if (item.block_number === null) continue
|
|
133
|
+
if (item.block_number < floor || !visit(item, item.block_number)) return
|
|
134
|
+
}
|
|
135
|
+
// The next page params do not repeat the filters
|
|
136
|
+
params = page.next_page_params && { ...stream.params, ...page.next_page_params }
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Read a list from `ceiling` (inclusive, or the chain head) down to `floor` (inclusive).
|
|
142
|
+
*
|
|
143
|
+
* With a `limit`, stop once `limit` items are read and the block of the last one is complete, so
|
|
144
|
+
* that the next page can start right below it without splitting a block.
|
|
145
|
+
*/
|
|
146
|
+
async function readStream<T extends StreamItem>(
|
|
147
|
+
uri: string,
|
|
148
|
+
stream: Stream,
|
|
149
|
+
floor: number,
|
|
150
|
+
ceiling: number | undefined,
|
|
151
|
+
limit: number | undefined,
|
|
152
|
+
retries: number
|
|
153
|
+
): Promise<StreamRead<T>> {
|
|
154
|
+
const items: T[] = []
|
|
155
|
+
let lastBlock: number | undefined
|
|
156
|
+
let boundBlock: number | undefined
|
|
157
|
+
|
|
158
|
+
await walkStream<T>(uri, stream, floor, ceiling, retries, (item, block) => {
|
|
159
|
+
if (
|
|
160
|
+
limit !== undefined &&
|
|
161
|
+
items.length >= limit &&
|
|
162
|
+
lastBlock !== undefined &&
|
|
163
|
+
block < lastBlock
|
|
164
|
+
) {
|
|
165
|
+
boundBlock = lastBlock
|
|
166
|
+
return false
|
|
167
|
+
}
|
|
168
|
+
items.push(item)
|
|
169
|
+
lastBlock = block
|
|
170
|
+
return true
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
return { items, boundBlock }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const TRANSACTIONS = (address: string): Stream => ({
|
|
177
|
+
path: `/addresses/${address}/transactions`,
|
|
178
|
+
params: {},
|
|
179
|
+
seed: (block): PageParams => ({ block_number: block, index: 0 }),
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
const TOKEN_TRANSFERS = (address: string, types: string[]): Stream => ({
|
|
183
|
+
path: `/addresses/${address}/token-transfers`,
|
|
184
|
+
params: { type: types.join(',') },
|
|
185
|
+
seed: (block): PageParams => ({ block_number: block, index: 0 }),
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
const INTERNAL_TRANSACTIONS = (address: string): Stream => ({
|
|
189
|
+
path: `/addresses/${address}/internal-transactions`,
|
|
190
|
+
params: {},
|
|
191
|
+
seed: (block): PageParams => ({ block_number: block, index: 0, transaction_index: 0 }),
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Get all the internal transactions of a block, for the `explorer` internal transaction source of
|
|
196
|
+
* `getBlock`. Not retried: `getBlock` falls back to the next source on failure.
|
|
197
|
+
*/
|
|
198
|
+
export async function getInternalTransactionsByBlock(
|
|
199
|
+
config: EvmConfigInfo,
|
|
200
|
+
blockHeight: number
|
|
201
|
+
): Promise<EtherscanInternalTransaction[]> {
|
|
202
|
+
const { explorer } = config
|
|
203
|
+
if (!isBlockscoutV2ExplorerConfig(explorer)) return []
|
|
204
|
+
|
|
205
|
+
const internalTransactions: EtherscanInternalTransaction[] = []
|
|
206
|
+
await walkStream<BlockscoutV2InternalTransaction>(
|
|
207
|
+
explorer.uri,
|
|
208
|
+
{
|
|
209
|
+
path: `/blocks/${blockHeight}/internal-transactions`,
|
|
210
|
+
params: {},
|
|
211
|
+
// Only ever read from the first page
|
|
212
|
+
seed: (): PageParams => ({}),
|
|
213
|
+
},
|
|
214
|
+
blockHeight,
|
|
215
|
+
undefined,
|
|
216
|
+
0,
|
|
217
|
+
(internalTx) => {
|
|
218
|
+
internalTransactions.push(blockscoutV2InternalTransactionToEtherscan(internalTx))
|
|
219
|
+
return true
|
|
220
|
+
}
|
|
221
|
+
)
|
|
222
|
+
return internalTransactions
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function tokenTypes(config: EvmConfigInfo): string[] {
|
|
226
|
+
return [
|
|
227
|
+
'ERC-20',
|
|
228
|
+
...(config.supportedTokens?.includes('erc721') ? ['ERC-721'] : []),
|
|
229
|
+
...(config.supportedTokens?.includes('erc1155') ? ['ERC-1155'] : []),
|
|
230
|
+
]
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Resolve the hash of each block: the v2 transaction and internal transaction lists only report the
|
|
235
|
+
* block number. Hashes already known from token transfers are reused, the others are read from the
|
|
236
|
+
* node.
|
|
237
|
+
*/
|
|
238
|
+
async function getBlockHashes(
|
|
239
|
+
config: EvmConfigInfo,
|
|
240
|
+
logger: Logger,
|
|
241
|
+
heights: Set<number>,
|
|
242
|
+
known: Map<number, string>
|
|
243
|
+
): Promise<Map<number, string>> {
|
|
244
|
+
const missing = [...heights].filter((height) => !known.has(height))
|
|
245
|
+
if (missing.length === 0) return known
|
|
246
|
+
|
|
247
|
+
const nodeApi = getNodeApi(config, logger)
|
|
248
|
+
const blocks = await promiseAllBatched(BLOCK_HASH_BATCH_SIZE, missing, (height) =>
|
|
249
|
+
nodeApi.getBlockByHeight(height)
|
|
250
|
+
)
|
|
251
|
+
return new Map([
|
|
252
|
+
...known,
|
|
253
|
+
...blocks.map((block, index): [number, string] => [missing[index], block.hash]),
|
|
254
|
+
])
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** Parse a cursor: the highest block (inclusive) of the next page. */
|
|
258
|
+
function parseCursor(cursor: string | undefined): number | undefined {
|
|
259
|
+
if (cursor === undefined || cursor === NO_TOKEN) return undefined
|
|
260
|
+
const block = Number(cursor)
|
|
261
|
+
if (!Number.isSafeInteger(block) || block < 0) {
|
|
262
|
+
throw new InvalidParameterError(`Invalid Blockscout v2 explorer cursor: ${cursor}`)
|
|
263
|
+
}
|
|
264
|
+
return block
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** The lowest of two optional block bounds, or `undefined` when both are unbounded. */
|
|
268
|
+
function lowestBound(a: number | undefined, b: number | undefined): number | undefined {
|
|
269
|
+
if (a === undefined) return b
|
|
270
|
+
if (b === undefined) return a
|
|
271
|
+
return Math.min(a, b)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Returns all operation types from an address, in descending block order.
|
|
276
|
+
*
|
|
277
|
+
* Each list is read from the cursor (or `toBlock`, or the chain head) down to `fromBlock`. With a
|
|
278
|
+
* `limit`, each list stops after `limit` items and a complete block. The page then ends at the
|
|
279
|
+
* highest of those last blocks, the lowest block that every list has fully read, and the cursor
|
|
280
|
+
* resumes right below it. So a page holds at most about 3 × `limit` items and never splits a block.
|
|
281
|
+
*/
|
|
282
|
+
export const getOperations = makeLRUCache<
|
|
283
|
+
[
|
|
284
|
+
config: EvmConfigInfo,
|
|
285
|
+
logger: Logger,
|
|
286
|
+
address: string,
|
|
287
|
+
fromBlock: number,
|
|
288
|
+
toBlock: number | undefined,
|
|
289
|
+
pagingToken: string | undefined,
|
|
290
|
+
limit: number | undefined,
|
|
291
|
+
order: 'asc' | 'desc' | undefined,
|
|
292
|
+
],
|
|
293
|
+
Awaited<ReturnType<ExplorerApi['getOperations']>>
|
|
294
|
+
>(
|
|
295
|
+
async (config, logger, address, fromBlock, toBlock, pagingToken, limit, order = 'desc') => {
|
|
296
|
+
const { explorer } = config
|
|
297
|
+
if (!isBlockscoutV2ExplorerConfig(explorer)) {
|
|
298
|
+
throw new InvalidParameterError(
|
|
299
|
+
`Blockscout v2 explorer used incorrectly with currency: ${config.name}`
|
|
300
|
+
)
|
|
301
|
+
}
|
|
302
|
+
if (order === 'asc' && limit !== undefined) {
|
|
303
|
+
throw new InvalidParameterError(
|
|
304
|
+
`Ascending order is not supported by the Blockscout v2 explorer of ${config.name}`
|
|
305
|
+
)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
const retries = explorer.retries ?? DEFAULT_RETRIES_API
|
|
310
|
+
const cursor = parseCursor(pagingToken)
|
|
311
|
+
const ceiling = lowestBound(cursor, toBlock)
|
|
312
|
+
|
|
313
|
+
// ⚠ Sequential on purpose, parallel requests would hit the rate limits
|
|
314
|
+
const read = <T extends StreamItem>(stream: Stream): Promise<StreamRead<T>> =>
|
|
315
|
+
readStream<T>(explorer.uri, stream, fromBlock, ceiling, limit, retries)
|
|
316
|
+
const transactions = await read<BlockscoutV2Transaction>(TRANSACTIONS(address))
|
|
317
|
+
const tokenTransfers = await read<BlockscoutV2TokenTransfer>(
|
|
318
|
+
TOKEN_TRANSFERS(address, tokenTypes(config))
|
|
319
|
+
)
|
|
320
|
+
const internalTransactions = await read<BlockscoutV2InternalTransaction>(
|
|
321
|
+
INTERNAL_TRANSACTIONS(address)
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
// A list that stopped early may hold items below the blocks another list has fully read
|
|
325
|
+
const boundBlocks = [transactions, tokenTransfers, internalTransactions].flatMap((r) =>
|
|
326
|
+
r.boundBlock === undefined ? [] : [r.boundBlock]
|
|
327
|
+
)
|
|
328
|
+
const boundBlock = boundBlocks.length > 0 ? Math.max(...boundBlocks) : undefined
|
|
329
|
+
const inPage = (item: { block_number: number | null }): boolean =>
|
|
330
|
+
boundBlock === undefined || (item.block_number ?? 0) >= boundBlock
|
|
331
|
+
|
|
332
|
+
const pageTransactions = transactions.items.filter(inPage)
|
|
333
|
+
const pageTokenTransfers = tokenTransfers.items.filter(inPage)
|
|
334
|
+
const pageInternalTransactions = internalTransactions.items.filter(inPage)
|
|
335
|
+
|
|
336
|
+
const blockHashes = await getBlockHashes(
|
|
337
|
+
config,
|
|
338
|
+
logger,
|
|
339
|
+
new Set(
|
|
340
|
+
[...pageTransactions, ...pageInternalTransactions].map((item) => item.block_number ?? 0)
|
|
341
|
+
),
|
|
342
|
+
new Map(pageTokenTransfers.map((transfer) => [transfer.block_number, transfer.block_hash]))
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
const lastCoinOperations = pageTransactions.flatMap((tx) =>
|
|
346
|
+
blockscoutV2TransactionToOperations(
|
|
347
|
+
address,
|
|
348
|
+
config.chainId,
|
|
349
|
+
tx,
|
|
350
|
+
blockHashes.get(tx.block_number ?? 0) ?? '',
|
|
351
|
+
logger
|
|
352
|
+
)
|
|
353
|
+
)
|
|
354
|
+
await enrichRewardOperations(config, lastCoinOperations, logger)
|
|
355
|
+
|
|
356
|
+
// Drop ERC20 Transfer events on contracts that mirror the native asset
|
|
357
|
+
const nativeContracts = new Set(
|
|
358
|
+
(config.nativeContracts ?? []).map((contract) => contract.toLowerCase())
|
|
359
|
+
)
|
|
360
|
+
const byType = (type: string): BlockscoutV2TokenTransfer[] =>
|
|
361
|
+
pageTokenTransfers.filter(
|
|
362
|
+
(transfer) => (transfer.token_type ?? transfer.token.type) === type
|
|
363
|
+
)
|
|
364
|
+
const lastTokenOperations = blockscoutV2ERC20TransfersToOperations(
|
|
365
|
+
address,
|
|
366
|
+
byType('ERC-20').filter(
|
|
367
|
+
(transfer) => !nativeContracts.has(transfer.token.address_hash.toLowerCase())
|
|
368
|
+
),
|
|
369
|
+
logger
|
|
370
|
+
)
|
|
371
|
+
const lastNftOperations = [
|
|
372
|
+
...blockscoutV2ERC721TransfersToOperations(address, byType('ERC-721'), logger),
|
|
373
|
+
...blockscoutV2ERC1155TransfersToOperations(address, byType('ERC-1155'), logger),
|
|
374
|
+
].sort((a, b) => b.tx.block.height - a.tx.block.height)
|
|
375
|
+
const lastInternalOperations = blockscoutV2InternalTransactionsToOperations(
|
|
376
|
+
address,
|
|
377
|
+
pageInternalTransactions,
|
|
378
|
+
blockHashes,
|
|
379
|
+
logger
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
const nextPagingToken =
|
|
383
|
+
boundBlock === undefined || boundBlock - 1 < fromBlock ? NO_TOKEN : String(boundBlock - 1)
|
|
384
|
+
|
|
385
|
+
return {
|
|
386
|
+
lastCoinOperations,
|
|
387
|
+
lastTokenOperations,
|
|
388
|
+
lastNftOperations,
|
|
389
|
+
lastInternalOperations,
|
|
390
|
+
nextPagingToken,
|
|
391
|
+
}
|
|
392
|
+
} catch (err) {
|
|
393
|
+
if (err instanceof RateLimitError || err instanceof InvalidParameterError) throw err
|
|
394
|
+
logger('EVM getOperations', 'Error while fetching data from Blockscout v2 API', err)
|
|
395
|
+
const message = err instanceof Error ? `${err.name} - ${err.message}` : String(err)
|
|
396
|
+
throw new InvalidExplorerResponse(
|
|
397
|
+
`${config.name} - ${message}`,
|
|
398
|
+
{ currencyName: config.name },
|
|
399
|
+
{ cause: err }
|
|
400
|
+
)
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
(config, _logger, address, fromBlock, toBlock, pagingToken, limit, order) =>
|
|
404
|
+
`${config.chainId}:${address}:${fromBlock}:${toBlock ?? ''}:${pagingToken ?? ''}:${limit ?? ''}:${order ?? 'desc'}`,
|
|
405
|
+
{ ttl: BLOCKSCOUT_V2_TIMEOUT }
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Discover which tokens an address holds by walking its ERC-20 transfers down to `fromBlock`.
|
|
410
|
+
*/
|
|
411
|
+
export const discoverTokenAssets = makeLRUCache<
|
|
412
|
+
[config: EvmConfigInfo, logger: Logger, address: string, fromBlock?: number],
|
|
413
|
+
AssetInfo[]
|
|
414
|
+
>(
|
|
415
|
+
async (config, _logger, address, fromBlock = 0) => {
|
|
416
|
+
const { explorer } = config
|
|
417
|
+
if (!isBlockscoutV2ExplorerConfig(explorer)) {
|
|
418
|
+
throw new InvalidParameterError(
|
|
419
|
+
`Blockscout v2 explorer used incorrectly with currency: ${config.name}`
|
|
420
|
+
)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Skip the contracts that mirror the native asset, as `getOperations` does
|
|
424
|
+
const nativeContracts = new Set(
|
|
425
|
+
(config.nativeContracts ?? []).map((contract) => contract.toLowerCase())
|
|
426
|
+
)
|
|
427
|
+
// Fold each transfer into the asset map as it is read: memory stays bounded by the number of
|
|
428
|
+
// distinct contracts, not by the length of the history
|
|
429
|
+
const assets = new Map<string, AssetInfo>()
|
|
430
|
+
await walkStream<BlockscoutV2TokenTransfer>(
|
|
431
|
+
explorer.uri,
|
|
432
|
+
TOKEN_TRANSFERS(address, ['ERC-20']),
|
|
433
|
+
fromBlock,
|
|
434
|
+
undefined,
|
|
435
|
+
explorer.retries ?? DEFAULT_RETRIES_API,
|
|
436
|
+
(transfer) => {
|
|
437
|
+
if (!nativeContracts.has(transfer.token.address_hash.toLowerCase())) {
|
|
438
|
+
const contract = eip55.encode(transfer.token.address_hash)
|
|
439
|
+
assets.set(contract, { type: 'erc20', assetReference: contract, assetOwner: address })
|
|
440
|
+
}
|
|
441
|
+
return true
|
|
442
|
+
}
|
|
443
|
+
)
|
|
444
|
+
return Array.from(assets.values())
|
|
445
|
+
},
|
|
446
|
+
(config, _logger, address, fromBlock) => `${config.chainId}:${address}:${fromBlock ?? 0}`,
|
|
447
|
+
{ ttl: BLOCKSCOUT_V2_TIMEOUT }
|
|
448
|
+
)
|
|
449
|
+
|
|
450
|
+
const explorerApi: ExplorerApi = {
|
|
451
|
+
getOperations,
|
|
452
|
+
discoverTokenAssets,
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const explorerApiNoCache: ExplorerApi = {
|
|
456
|
+
getOperations: getOperations.force,
|
|
457
|
+
discoverTokenAssets: discoverTokenAssets.force,
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export default { explorerApi, explorerApiNoCache }
|
|
@@ -35,9 +35,7 @@ import {
|
|
|
35
35
|
RateLimitError,
|
|
36
36
|
InvalidExplorerResponse,
|
|
37
37
|
} from '../../errors'
|
|
38
|
-
import {
|
|
39
|
-
import { withApi } from '../node/rpc.common'
|
|
40
|
-
import { isExternalNodeConfig } from '../node/types'
|
|
38
|
+
import { enrichRewardOperations } from '../rewards'
|
|
41
39
|
import { isEtherscanLikeExplorerConfig } from './types'
|
|
42
40
|
|
|
43
41
|
export const ETHERSCAN_TIMEOUT = 5000 // 5 seconds between 2 calls
|
|
@@ -327,32 +325,7 @@ export const getCoinOperations = async (params: FetchOperationsParams): Promise<
|
|
|
327
325
|
etherscanOperationToOperations(params.address, params.config.chainId, tx, params.logger)
|
|
328
326
|
)
|
|
329
327
|
|
|
330
|
-
|
|
331
|
-
// value, so the amount would otherwise show as 0 in history and operation details.
|
|
332
|
-
const node = params.config.node
|
|
333
|
-
if (
|
|
334
|
-
operations.some((op) => op.type === 'REWARD' && op.value === 0n) &&
|
|
335
|
-
isExternalNodeConfig(node)
|
|
336
|
-
) {
|
|
337
|
-
try {
|
|
338
|
-
await withApi(
|
|
339
|
-
params.config,
|
|
340
|
-
(api) =>
|
|
341
|
-
enrichRewardOperationsValue(
|
|
342
|
-
params.config.chainId,
|
|
343
|
-
operations,
|
|
344
|
-
(hash) => api.getTransactionReceipt(hash),
|
|
345
|
-
params.logger
|
|
346
|
-
),
|
|
347
|
-
node
|
|
348
|
-
)
|
|
349
|
-
} catch (e) {
|
|
350
|
-
// Enrichment is best-effort, but a rate limit must not be downgraded into a successful
|
|
351
|
-
// response reporting 0-value rewards.
|
|
352
|
-
if (e instanceof RateLimitError) throw e
|
|
353
|
-
params.logger('EVM getOperations', 'reward amount enrichment failed', e)
|
|
354
|
-
}
|
|
355
|
-
}
|
|
328
|
+
await enrichRewardOperations(params.config, operations, params.logger)
|
|
356
329
|
|
|
357
330
|
const maxBlock = boundBlockFromOperations(operations)
|
|
358
331
|
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import type { EvmConfigInfo } from '../../config'
|
|
5
5
|
import type { ExplorerApi } from './types'
|
|
6
6
|
import { UnknownExplorer } from '../../errors'
|
|
7
|
+
import blockscoutV2Api from '../blockscoutV2'
|
|
7
8
|
import etherscanLikeApi from './etherscan'
|
|
8
9
|
import ledgerExplorerApi from './ledger'
|
|
9
10
|
import noExplorerAPI from './none'
|
|
@@ -22,6 +23,10 @@ export const getExplorerApi = (config: EvmConfigInfo): ExplorerApi => {
|
|
|
22
23
|
return config.explorer.noCache
|
|
23
24
|
? etherscanLikeApi.explorerApiNoCache
|
|
24
25
|
: etherscanLikeApi.explorerApi
|
|
26
|
+
case 'blockscout-v2':
|
|
27
|
+
return config.explorer.noCache
|
|
28
|
+
? blockscoutV2Api.explorerApiNoCache
|
|
29
|
+
: blockscoutV2Api.explorerApi
|
|
25
30
|
case 'ledger':
|
|
26
31
|
return ledgerExplorerApi
|
|
27
32
|
case 'none':
|
|
@@ -61,6 +61,15 @@ export const isLedgerExplorerConfig = (
|
|
|
61
61
|
return explorerConfig?.type === 'ledger'
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Type guard
|
|
66
|
+
*/
|
|
67
|
+
export const isBlockscoutV2ExplorerConfig = (
|
|
68
|
+
explorerConfig: ExplorerConfig
|
|
69
|
+
): explorerConfig is ExplorerConfig & { type: 'blockscout-v2' } => {
|
|
70
|
+
return explorerConfig?.type === 'blockscout-v2'
|
|
71
|
+
}
|
|
72
|
+
|
|
64
73
|
const ETHERSCAN_LIKE_EXPLORER_TYPES = new Set<string>([
|
|
65
74
|
'etherscan',
|
|
66
75
|
'blockscout',
|
|
@@ -0,0 +1,47 @@
|
|
|
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 type { Logger } from '@ledgerhq/coin-module-framework/config'
|
|
7
|
+
import { RateLimitError } from '../errors'
|
|
8
|
+
import { enrichRewardOperationsValue } from '../staking/rewardsFromReceipt'
|
|
9
|
+
import { withApi } from './node/rpc.common'
|
|
10
|
+
import { isExternalNodeConfig } from './node/types'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Recover REWARD op amounts (claim/compound) from receipt logs — these txs send 0 native
|
|
14
|
+
* value, so the amount would otherwise show as 0 in history and operation details.
|
|
15
|
+
*
|
|
16
|
+
* Mutates `operations` in place.
|
|
17
|
+
*/
|
|
18
|
+
export async function enrichRewardOperations(
|
|
19
|
+
config: EvmConfigInfo,
|
|
20
|
+
operations: Array<Operation<MemoNotSupported>>,
|
|
21
|
+
logger: Logger
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
const node = config.node
|
|
24
|
+
if (
|
|
25
|
+
operations.some((op) => op.type === 'REWARD' && op.value === 0n) &&
|
|
26
|
+
isExternalNodeConfig(node)
|
|
27
|
+
) {
|
|
28
|
+
try {
|
|
29
|
+
await withApi(
|
|
30
|
+
config,
|
|
31
|
+
(api) =>
|
|
32
|
+
enrichRewardOperationsValue(
|
|
33
|
+
config.chainId,
|
|
34
|
+
operations,
|
|
35
|
+
(hash) => api.getTransactionReceipt(hash),
|
|
36
|
+
logger
|
|
37
|
+
),
|
|
38
|
+
node
|
|
39
|
+
)
|
|
40
|
+
} catch (e) {
|
|
41
|
+
// Enrichment is best-effort, but a rate limit must not be downgraded into a successful
|
|
42
|
+
// response reporting 0-value rewards.
|
|
43
|
+
if (e instanceof RateLimitError) throw e
|
|
44
|
+
logger('EVM getOperations', 'reward amount enrichment failed', e)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|