@ledgerhq/coin-evm 5.3.4 → 5.3.6
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 +12 -0
- package/lib/config.d.ts +4 -0
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js.map +1 -1
- package/lib/errors.d.ts +6 -0
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +12 -1
- package/lib/errors.js.map +1 -1
- package/lib/network/explorer/etherscan.d.ts.map +1 -1
- package/lib/network/explorer/etherscan.js +15 -19
- package/lib/network/explorer/etherscan.js.map +1 -1
- package/lib/network/explorer/ledger.d.ts.map +1 -1
- package/lib/network/explorer/ledger.js +6 -4
- package/lib/network/explorer/ledger.js.map +1 -1
- package/lib/network/withRetries.d.ts.map +1 -1
- package/lib/network/withRetries.js +13 -0
- package/lib/network/withRetries.js.map +1 -1
- package/lib/utils.d.ts +0 -2
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +0 -30
- package/lib/utils.js.map +1 -1
- package/lib-es/config.d.ts +4 -0
- package/lib-es/config.d.ts.map +1 -1
- package/lib-es/config.js.map +1 -1
- package/lib-es/errors.d.ts +6 -0
- package/lib-es/errors.d.ts.map +1 -1
- package/lib-es/errors.js +10 -0
- package/lib-es/errors.js.map +1 -1
- package/lib-es/network/explorer/etherscan.d.ts.map +1 -1
- package/lib-es/network/explorer/etherscan.js +16 -20
- package/lib-es/network/explorer/etherscan.js.map +1 -1
- package/lib-es/network/explorer/ledger.d.ts.map +1 -1
- package/lib-es/network/explorer/ledger.js +7 -5
- package/lib-es/network/explorer/ledger.js.map +1 -1
- package/lib-es/network/withRetries.d.ts.map +1 -1
- package/lib-es/network/withRetries.js +10 -0
- package/lib-es/network/withRetries.js.map +1 -1
- package/lib-es/utils.d.ts +0 -2
- package/lib-es/utils.d.ts.map +1 -1
- package/lib-es/utils.js +0 -28
- package/lib-es/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/api/index.integ.test.ts +0 -8
- package/src/api/index.zksync.integ.test.ts +38 -0
- package/src/config.ts +4 -0
- package/src/errors.ts +10 -0
- package/src/network/explorer/etherscan.test.ts +68 -1
- package/src/network/explorer/etherscan.ts +37 -27
- package/src/network/explorer/ledger.test.ts +24 -13
- package/src/network/explorer/ledger.ts +27 -22
- package/src/network/withRetries.test.ts +63 -0
- package/src/network/withRetries.ts +12 -0
- package/src/utils.test.ts +0 -86
- package/src/utils.ts +0 -41
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'
|
|
@@ -18,7 +18,11 @@ import {
|
|
|
18
18
|
serializePagingToken,
|
|
19
19
|
} from '../../adapters'
|
|
20
20
|
import { getCoinConfig } from '../../config'
|
|
21
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
EtherscanAPIError,
|
|
23
|
+
EtherscanLikeExplorerUsedIncorrectly,
|
|
24
|
+
RateLimitError,
|
|
25
|
+
} from '../../errors'
|
|
22
26
|
import { createMockEvmCoinConfig, mkExternalNode } from '../../fixtures/context.fixtures'
|
|
23
27
|
import {
|
|
24
28
|
etherscanCoinOperations,
|
|
@@ -141,6 +145,33 @@ describe('EVM Family', () => {
|
|
|
141
145
|
// it should fail 2 times and succeed on the next try
|
|
142
146
|
expect(spy).toHaveBeenCalledTimes(3)
|
|
143
147
|
})
|
|
148
|
+
|
|
149
|
+
it('should surface a 429 HTTP response as RateLimitError without retrying', async () => {
|
|
150
|
+
const rateLimitError = Object.assign(new Error('Request failed with status code 429'), {
|
|
151
|
+
isAxiosError: true,
|
|
152
|
+
response: { status: 429 },
|
|
153
|
+
})
|
|
154
|
+
// axios is fully mocked — restore isAxiosError to its real behaviour for this test
|
|
155
|
+
jest
|
|
156
|
+
.spyOn(axios, 'isAxiosError')
|
|
157
|
+
.mockImplementation(
|
|
158
|
+
(e: unknown): boolean => typeof e === 'object' && e !== null && 'isAxiosError' in e
|
|
159
|
+
)
|
|
160
|
+
const spy = jest.spyOn(axios, 'request').mockRejectedValue(rateLimitError)
|
|
161
|
+
|
|
162
|
+
await expect(
|
|
163
|
+
ETHERSCAN_API.fetchWithRetries({}, ETHERSCAN_API.DEFAULT_RETRIES_API)
|
|
164
|
+
).rejects.toBeInstanceOf(RateLimitError)
|
|
165
|
+
// must not retry — a rate-limit error is not transient
|
|
166
|
+
expect(spy).toHaveBeenCalledTimes(1)
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('should respect retries: 0 from explorer config and not retry on failure', async () => {
|
|
170
|
+
const spy = jest.spyOn(axios, 'request').mockRejectedValue(new Error('upstream error'))
|
|
171
|
+
|
|
172
|
+
await expect(ETHERSCAN_API.fetchWithRetries({}, 0)).rejects.toThrow('upstream error')
|
|
173
|
+
expect(spy).toHaveBeenCalledTimes(1)
|
|
174
|
+
})
|
|
144
175
|
})
|
|
145
176
|
|
|
146
177
|
describe.each([
|
|
@@ -1903,6 +1934,42 @@ describe('EVM Family', () => {
|
|
|
1903
1934
|
jest.clearAllMocks()
|
|
1904
1935
|
})
|
|
1905
1936
|
|
|
1937
|
+
// getOperations wraps everything its outer catch sees into InvalidExplorerResponse. A rate
|
|
1938
|
+
// limit must escape that wrapping, otherwise the caller loses the 429 status.
|
|
1939
|
+
it('propagates a RateLimitError instead of wrapping it in InvalidExplorerResponse', async () => {
|
|
1940
|
+
mockGetConfig.mockImplementation(() =>
|
|
1941
|
+
createMockEvmCoinConfig({
|
|
1942
|
+
chainId: 1,
|
|
1943
|
+
node: mkExternalNode({ uri: 'mock' }),
|
|
1944
|
+
explorer: { type: 'etherscan', uri: 'https://api.etherscan.io', retries: 0 },
|
|
1945
|
+
})
|
|
1946
|
+
)
|
|
1947
|
+
const rateLimitError = Object.assign(new Error('Request failed with status code 429'), {
|
|
1948
|
+
isAxiosError: true,
|
|
1949
|
+
response: { status: 429 },
|
|
1950
|
+
})
|
|
1951
|
+
// axios is fully mocked - restore isAxiosError to its real behaviour for this test
|
|
1952
|
+
jest
|
|
1953
|
+
.spyOn(axios, 'isAxiosError')
|
|
1954
|
+
.mockImplementation(
|
|
1955
|
+
(e: unknown): boolean => typeof e === 'object' && e !== null && 'isAxiosError' in e
|
|
1956
|
+
)
|
|
1957
|
+
jest.spyOn(axios, 'request').mockRejectedValue(rateLimitError)
|
|
1958
|
+
|
|
1959
|
+
await expect(
|
|
1960
|
+
ETHERSCAN_API.getOperations.force(
|
|
1961
|
+
getCoinConfig(currencyId).info,
|
|
1962
|
+
currencyId,
|
|
1963
|
+
address,
|
|
1964
|
+
0,
|
|
1965
|
+
undefined,
|
|
1966
|
+
undefined,
|
|
1967
|
+
1,
|
|
1968
|
+
'asc'
|
|
1969
|
+
)
|
|
1970
|
+
).rejects.toBeInstanceOf(RateLimitError)
|
|
1971
|
+
})
|
|
1972
|
+
|
|
1906
1973
|
// Helpers to create mock Etherscan responses with specific block heights for each operation type
|
|
1907
1974
|
const createMockCoinTxResponse = (blockNumbers: number[]) =>
|
|
1908
1975
|
blockNumbers.map((blockNumber, i) => ({
|
|
@@ -28,10 +28,10 @@ import {
|
|
|
28
28
|
import {
|
|
29
29
|
EtherscanAPIError,
|
|
30
30
|
EtherscanLikeExplorerUsedIncorrectly,
|
|
31
|
+
RateLimitError,
|
|
31
32
|
InvalidExplorerResponse,
|
|
32
33
|
} from '../../errors'
|
|
33
34
|
import { enrichRewardOperationsValue } from '../../staking/rewardsFromReceipt'
|
|
34
|
-
import { nftDisabled } from '../../utils'
|
|
35
35
|
import { withApi } from '../node/rpc.common'
|
|
36
36
|
import { isExternalNodeConfig } from '../node/types'
|
|
37
37
|
import { isEtherscanLikeExplorerConfig } from './types'
|
|
@@ -119,6 +119,9 @@ export async function fetchWithRetries<T>(
|
|
|
119
119
|
|
|
120
120
|
return data.result
|
|
121
121
|
} catch (e) {
|
|
122
|
+
if (axios.isAxiosError(e) && e.response?.status === 429) {
|
|
123
|
+
throw new RateLimitError()
|
|
124
|
+
}
|
|
122
125
|
if (retries) {
|
|
123
126
|
// wait the API timeout before trying again
|
|
124
127
|
await delay(ETHERSCAN_TIMEOUT)
|
|
@@ -277,11 +280,10 @@ export const getCoinOperations = async (params: FetchOperationsParams): Promise<
|
|
|
277
280
|
? `${explorer.uri}/accounts/list_of_txs_by_address/${params.address}`
|
|
278
281
|
: `${explorer.uri}?module=account&action=txlist&address=${params.address}`
|
|
279
282
|
|
|
280
|
-
const raw = await fetchWithRetries<EtherscanOperation[] | null | string>(
|
|
281
|
-
method: 'GET',
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
})
|
|
283
|
+
const raw = await fetchWithRetries<EtherscanOperation[] | null | string>(
|
|
284
|
+
{ method: 'GET', url, params: paginationParams(params) },
|
|
285
|
+
explorer.retries ?? DEFAULT_RETRIES_API
|
|
286
|
+
)
|
|
285
287
|
const ops = Array.isArray(raw) ? raw : []
|
|
286
288
|
|
|
287
289
|
const operations = ops.flatMap((tx) =>
|
|
@@ -306,6 +308,9 @@ export const getCoinOperations = async (params: FetchOperationsParams): Promise<
|
|
|
306
308
|
node
|
|
307
309
|
)
|
|
308
310
|
} catch (e) {
|
|
311
|
+
// Enrichment is best-effort, but a rate limit must not be downgraded into a successful
|
|
312
|
+
// response reporting 0-value rewards.
|
|
313
|
+
if (e instanceof RateLimitError) throw e
|
|
309
314
|
log('EVM getOperations', 'reward amount enrichment failed', e)
|
|
310
315
|
}
|
|
311
316
|
}
|
|
@@ -337,11 +342,10 @@ export const getTokenOperations = async (
|
|
|
337
342
|
? `${explorer.uri}/accounts/list_of_erc20_transfer_events_by_address/${params.address}`
|
|
338
343
|
: `${explorer.uri}?module=account&action=tokentx&address=${params.address}`
|
|
339
344
|
|
|
340
|
-
const raw = await fetchWithRetries<EtherscanERC20Event[] | null | string>(
|
|
341
|
-
method: 'GET',
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
})
|
|
345
|
+
const raw = await fetchWithRetries<EtherscanERC20Event[] | null | string>(
|
|
346
|
+
{ method: 'GET', url, params: paginationParams(params) },
|
|
347
|
+
explorer.retries ?? DEFAULT_RETRIES_API
|
|
348
|
+
)
|
|
345
349
|
const ops = Array.isArray(raw) ? raw : []
|
|
346
350
|
|
|
347
351
|
// Drop ERC20 Transfer events on contracts that mirror the native asset
|
|
@@ -392,11 +396,10 @@ export const getERC721Operations = async (
|
|
|
392
396
|
? `${explorer.uri}/accounts/list_of_erc721_transfer_events_by_address/${params.address}`
|
|
393
397
|
: `${explorer.uri}?module=account&action=tokennfttx&address=${params.address}`
|
|
394
398
|
|
|
395
|
-
const raw = await fetchWithRetries<EtherscanERC721Event[] | null | string>(
|
|
396
|
-
method: 'GET',
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
})
|
|
399
|
+
const raw = await fetchWithRetries<EtherscanERC721Event[] | null | string>(
|
|
400
|
+
{ method: 'GET', url, params: paginationParams(params) },
|
|
401
|
+
explorer.retries ?? DEFAULT_RETRIES_API
|
|
402
|
+
)
|
|
400
403
|
const ops = Array.isArray(raw) ? raw : []
|
|
401
404
|
|
|
402
405
|
// Why this thing ?
|
|
@@ -440,11 +443,14 @@ export const getERC1155Operations = async (
|
|
|
440
443
|
return EMPTY_RESULT
|
|
441
444
|
}
|
|
442
445
|
|
|
443
|
-
const raw = await fetchWithRetries<EtherscanERC1155Event[] | null | string>(
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
446
|
+
const raw = await fetchWithRetries<EtherscanERC1155Event[] | null | string>(
|
|
447
|
+
{
|
|
448
|
+
method: 'GET',
|
|
449
|
+
url: `${explorer.uri}?module=account&action=token1155tx&address=${params.address}`,
|
|
450
|
+
params: paginationParams(params),
|
|
451
|
+
},
|
|
452
|
+
explorer.retries ?? DEFAULT_RETRIES_API
|
|
453
|
+
)
|
|
448
454
|
const ops = Array.isArray(raw) ? raw : []
|
|
449
455
|
|
|
450
456
|
// Why this thing ?
|
|
@@ -529,11 +535,14 @@ export const getInternalOperations = async (
|
|
|
529
535
|
}
|
|
530
536
|
|
|
531
537
|
// Some explorers (e.g. Monad Testnet) return null instead of [] for empty results.
|
|
532
|
-
const ops = await fetchWithRetries<EtherscanInternalTransaction[] | null>(
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
538
|
+
const ops = await fetchWithRetries<EtherscanInternalTransaction[] | null>(
|
|
539
|
+
{
|
|
540
|
+
method: 'GET',
|
|
541
|
+
url: `${explorer.uri}?module=account&action=txlistinternal&address=${params.address}`,
|
|
542
|
+
params: paginationParams(params),
|
|
543
|
+
},
|
|
544
|
+
explorer.retries ?? DEFAULT_RETRIES_API
|
|
545
|
+
).then((ops) => (ops ?? []).map(fixTxHash))
|
|
537
546
|
|
|
538
547
|
// Why this thing ?
|
|
539
548
|
// Multiple internal transactions can be executed from
|
|
@@ -814,7 +823,7 @@ export const getOperations = makeLRUCache<
|
|
|
814
823
|
const nfts = await callEndpoint(
|
|
815
824
|
getNftOperations,
|
|
816
825
|
tokens.effectiveBoundBlock,
|
|
817
|
-
nftUnsupported ||
|
|
826
|
+
nftUnsupported || pagingState?.nftIsDone
|
|
818
827
|
)
|
|
819
828
|
|
|
820
829
|
const effectiveBoundBlock = nfts.effectiveBoundBlock
|
|
@@ -852,6 +861,7 @@ export const getOperations = makeLRUCache<
|
|
|
852
861
|
}),
|
|
853
862
|
}
|
|
854
863
|
} catch (err) {
|
|
864
|
+
if (err instanceof RateLimitError) throw err
|
|
855
865
|
log('EVM getOperations', 'Error while fetching data from Etherscan like API', err)
|
|
856
866
|
const message =
|
|
857
867
|
typeof err === 'string'
|
|
@@ -7,7 +7,7 @@ import { delay } from '@ledgerhq/coin-module-framework/promises'
|
|
|
7
7
|
import axios from 'axios'
|
|
8
8
|
import eip55 from 'eip55'
|
|
9
9
|
import { DEFAULT_LEDGER_EXPLORER_URI, getCoinConfig } from '../../config'
|
|
10
|
-
import { LedgerExplorerUsedIncorrectly } from '../../errors'
|
|
10
|
+
import { LedgerExplorerUsedIncorrectly, RateLimitError } from '../../errors'
|
|
11
11
|
import {
|
|
12
12
|
createMockEvmCoinConfig,
|
|
13
13
|
mkExternalNode,
|
|
@@ -94,6 +94,29 @@ describe('EVM Family', () => {
|
|
|
94
94
|
}
|
|
95
95
|
})
|
|
96
96
|
|
|
97
|
+
it('should surface a 429 as a RateLimitError without retrying', async () => {
|
|
98
|
+
const rateLimitError = Object.assign(new Error('Request failed with status code 429'), {
|
|
99
|
+
isAxiosError: true,
|
|
100
|
+
response: { status: 429 },
|
|
101
|
+
})
|
|
102
|
+
// axios is fully mocked - restore isAxiosError to its real behaviour for this test
|
|
103
|
+
jest
|
|
104
|
+
.spyOn(axios, 'isAxiosError')
|
|
105
|
+
.mockImplementation(
|
|
106
|
+
(e: unknown): boolean => typeof e === 'object' && e !== null && 'isAxiosError' in e
|
|
107
|
+
)
|
|
108
|
+
const spy = jest.spyOn(axios, 'request').mockRejectedValue(rateLimitError)
|
|
109
|
+
|
|
110
|
+
await expect(
|
|
111
|
+
LEDGER_API.fetchPaginatedOpsWithRetries(
|
|
112
|
+
{ explorerId: 'test', address: '0x', fromBlock: 0, batchSize: 10 },
|
|
113
|
+
null
|
|
114
|
+
)
|
|
115
|
+
).rejects.toBeInstanceOf(RateLimitError)
|
|
116
|
+
// a rate limit is not transient: it must not consume the retry budget
|
|
117
|
+
expect(spy).toHaveBeenCalledTimes(1)
|
|
118
|
+
})
|
|
119
|
+
|
|
97
120
|
it('should recursively fetch paginated data', async () => {
|
|
98
121
|
jest.spyOn(axios, 'request').mockImplementation(async ({ params }) => {
|
|
99
122
|
switch (params.token) {
|
|
@@ -176,10 +199,6 @@ describe('EVM Family', () => {
|
|
|
176
199
|
})
|
|
177
200
|
|
|
178
201
|
describe('getOperations', () => {
|
|
179
|
-
beforeEach(() => {
|
|
180
|
-
process.env.NFT_CURRENCIES = JSON.stringify([])
|
|
181
|
-
})
|
|
182
|
-
|
|
183
202
|
it('should throw if the explorer is misconfigured', async () => {
|
|
184
203
|
mockGetConfig.mockImplementationOnce(() =>
|
|
185
204
|
createMockEvmCoinConfig({ chainId: 1, node: mkExternalNode({ uri: 'anything' }) })
|
|
@@ -232,8 +251,6 @@ describe('EVM Family', () => {
|
|
|
232
251
|
})
|
|
233
252
|
|
|
234
253
|
it('should return the different operation types', async () => {
|
|
235
|
-
process.env.NFT_CURRENCIES = JSON.stringify(['ethereum'])
|
|
236
|
-
|
|
237
254
|
jest.spyOn(axios, 'request').mockImplementation(async () => ({
|
|
238
255
|
data: { data: [coinOperation1, coinOperation2, coinOperation3, coinOperation4] },
|
|
239
256
|
}))
|
|
@@ -474,12 +491,6 @@ describe('EVM Family', () => {
|
|
|
474
491
|
jest.spyOn(axios, 'request').mockImplementation(async () => ({
|
|
475
492
|
data: { data: [coinOperation1, coinOperation2, coinOperation3, coinOperation4] },
|
|
476
493
|
}))
|
|
477
|
-
|
|
478
|
-
process.env.NFT_CURRENCIES = JSON.stringify(['ethereum'])
|
|
479
|
-
})
|
|
480
|
-
|
|
481
|
-
afterAll(() => {
|
|
482
|
-
process.env.NFT_CURRENCIES = JSON.stringify([])
|
|
483
494
|
})
|
|
484
495
|
|
|
485
496
|
const mockConfigWithTokens = (supportedTokens: NftStandard[]) =>
|
|
@@ -14,8 +14,7 @@ import {
|
|
|
14
14
|
ledgerOperationToOperations,
|
|
15
15
|
} from '../../adapters/index'
|
|
16
16
|
import { DEFAULT_LEDGER_EXPLORER_URI } from '../../config'
|
|
17
|
-
import { LedgerExplorerUsedIncorrectly } from '../../errors'
|
|
18
|
-
import { nftEnabled } from '../../utils'
|
|
17
|
+
import { LedgerExplorerUsedIncorrectly, RateLimitError } from '../../errors'
|
|
19
18
|
import { isLedgerExplorerConfig, NO_TOKEN } from './types'
|
|
20
19
|
|
|
21
20
|
export const DEFAULT_BATCH_SIZE = 10_000
|
|
@@ -75,6 +74,9 @@ export async function fetchPaginatedOpsWithRetries(
|
|
|
75
74
|
(a, b) => new Date(b.block.time).getTime() - new Date(a.block.time).getTime()
|
|
76
75
|
)
|
|
77
76
|
} catch (e) {
|
|
77
|
+
if (axios.isAxiosError(e) && e.response?.status === 429) {
|
|
78
|
+
throw new RateLimitError()
|
|
79
|
+
}
|
|
78
80
|
if (retries) {
|
|
79
81
|
// wait the API timeout before trying again
|
|
80
82
|
await delay(LEDGER_TIMEOUT)
|
|
@@ -105,14 +107,19 @@ export const getOperations: ExplorerApi['getOperations'] = async (
|
|
|
105
107
|
)
|
|
106
108
|
}
|
|
107
109
|
|
|
108
|
-
const ledgerExplorerOps = await fetchPaginatedOpsWithRetries(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
110
|
+
const ledgerExplorerOps = await fetchPaginatedOpsWithRetries(
|
|
111
|
+
{
|
|
112
|
+
explorerUri: config.ledgerExplorerUri,
|
|
113
|
+
clientVersion: config.ledgerClientVersion,
|
|
114
|
+
explorerId: explorer.explorerId,
|
|
115
|
+
address,
|
|
116
|
+
fromBlock,
|
|
117
|
+
batchSize: explorer.batchSize ?? DEFAULT_BATCH_SIZE,
|
|
118
|
+
},
|
|
119
|
+
null,
|
|
120
|
+
[],
|
|
121
|
+
explorer.retries ?? DEFAULT_RETRIES_API
|
|
122
|
+
)
|
|
116
123
|
|
|
117
124
|
const lastCoinOperations: Array<Operation<MemoNotSupported>> = []
|
|
118
125
|
const lastTokenOperations: Array<Operation<MemoNotSupported>> = []
|
|
@@ -135,18 +142,16 @@ export const getOperations: ExplorerApi['getOperations'] = async (
|
|
|
135
142
|
const erc20Ops = erc20TransferEvents.flatMap((event, index) =>
|
|
136
143
|
ledgerERC20EventToOperations(address, coinOps[0], event, index)
|
|
137
144
|
)
|
|
138
|
-
const erc721Ops =
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
)
|
|
149
|
-
: []
|
|
145
|
+
const erc721Ops = config.supportedTokens?.includes('erc721')
|
|
146
|
+
? ledgerOp.erc721_transfer_events.flatMap((event, index) =>
|
|
147
|
+
ledgerERC721EventToOperations(address, coinOps[0], event, index)
|
|
148
|
+
)
|
|
149
|
+
: []
|
|
150
|
+
const erc1155Ops = config.supportedTokens?.includes('erc1155')
|
|
151
|
+
? ledgerOp.erc1155_transfer_events.flatMap((event, index) =>
|
|
152
|
+
ledgerERC1155EventToOperations(address, coinOps[0], event, index)
|
|
153
|
+
)
|
|
154
|
+
: []
|
|
150
155
|
const internalOps = ledgerOp.actions.flatMap((action, index) =>
|
|
151
156
|
ledgerInternalTransactionToOperations(address, coinOps[0], action, index)
|
|
152
157
|
)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { delay } from '@ledgerhq/coin-module-framework/promises'
|
|
5
|
+
import { RateLimitError } from '../errors'
|
|
6
|
+
import { withRetries } from './withRetries'
|
|
7
|
+
|
|
8
|
+
jest.mock('@ledgerhq/coin-module-framework/promises')
|
|
9
|
+
jest.mocked(delay).mockResolvedValue(undefined)
|
|
10
|
+
|
|
11
|
+
describe('EVM Family', () => {
|
|
12
|
+
describe('network/withRetries.ts', () => {
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
jest.clearAllMocks()
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should retry a transient error until it succeeds', async () => {
|
|
18
|
+
const fn = jest.fn().mockRejectedValueOnce(new Error('boom')).mockResolvedValueOnce('ok')
|
|
19
|
+
|
|
20
|
+
await expect(withRetries(fn, 2, 10)).resolves.toBe('ok')
|
|
21
|
+
expect(fn).toHaveBeenCalledTimes(2)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should surface an external node 429 as a RateLimitError without retrying', async () => {
|
|
25
|
+
// ethers escalates a 429 into a 599 SERVER_ERROR, keeping the original status in the
|
|
26
|
+
// statusMessage. This is the exact `${statusCode} ${statusMessage}` shape it produces.
|
|
27
|
+
const escalated = Object.assign(new Error('server response 599'), {
|
|
28
|
+
code: 'SERVER_ERROR',
|
|
29
|
+
info: {
|
|
30
|
+
responseStatus:
|
|
31
|
+
'599 CLIENT ESCALATED SERVER ERROR (429 Too Many Requests; exceeded maximum retry limit)',
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
const fn = jest.fn().mockRejectedValue(escalated)
|
|
35
|
+
|
|
36
|
+
await expect(withRetries(fn, 5, 10)).rejects.toBeInstanceOf(RateLimitError)
|
|
37
|
+
// a rate limit is not transient: it must not consume the retry budget
|
|
38
|
+
expect(fn).toHaveBeenCalledTimes(1)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should surface a Ledger node 429 as a RateLimitError without retrying', async () => {
|
|
42
|
+
const rateLimitError = Object.assign(new Error('Request failed with status code 429'), {
|
|
43
|
+
isAxiosError: true,
|
|
44
|
+
response: { status: 429 },
|
|
45
|
+
})
|
|
46
|
+
const fn = jest.fn().mockRejectedValue(rateLimitError)
|
|
47
|
+
|
|
48
|
+
await expect(withRetries(fn, 5, 10)).rejects.toBeInstanceOf(RateLimitError)
|
|
49
|
+
expect(fn).toHaveBeenCalledTimes(1)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('should keep retrying a non-429 SERVER_ERROR', async () => {
|
|
53
|
+
const serverError = Object.assign(new Error('server response 503'), {
|
|
54
|
+
code: 'SERVER_ERROR',
|
|
55
|
+
info: { responseStatus: '503 Service Unavailable' },
|
|
56
|
+
})
|
|
57
|
+
const fn = jest.fn().mockRejectedValue(serverError)
|
|
58
|
+
|
|
59
|
+
await expect(withRetries(fn, 2, 10)).rejects.toBe(serverError)
|
|
60
|
+
expect(fn).toHaveBeenCalledTimes(3)
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import { delay } from '@ledgerhq/coin-module-framework/promises'
|
|
5
|
+
import axios from 'axios'
|
|
6
|
+
import { isError } from 'ethers'
|
|
7
|
+
import { RateLimitError } from '../errors'
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
10
|
* Executes an async function and retries on failure.
|
|
@@ -18,6 +21,15 @@ export async function withRetries<T>(
|
|
|
18
21
|
try {
|
|
19
22
|
return await fn()
|
|
20
23
|
} catch (e) {
|
|
24
|
+
// External nodes go through ethers, which escalates a 429 into a 599 SERVER_ERROR whose
|
|
25
|
+
// responseStatus (`${statusCode} ${statusMessage}`) embeds the original status — hence the
|
|
26
|
+
// substring match rather than an equality check. Ledger nodes reject with plain axios errors.
|
|
27
|
+
if (
|
|
28
|
+
(isError(e, 'SERVER_ERROR') && String(e.info?.responseStatus ?? '').includes('429')) ||
|
|
29
|
+
(axios.isAxiosError(e) && e.response?.status === 429)
|
|
30
|
+
) {
|
|
31
|
+
throw new RateLimitError()
|
|
32
|
+
}
|
|
21
33
|
if (retries > 0) {
|
|
22
34
|
await delay(delayMs)
|
|
23
35
|
return withRetries(fn, retries - 1, delayMs)
|
package/src/utils.test.ts
CHANGED
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
import {
|
|
5
5
|
buildSmartContractDetails,
|
|
6
6
|
isSmartContractInput,
|
|
7
|
-
nftDisabled,
|
|
8
|
-
nftEnabled,
|
|
9
7
|
parseDecimalIntegerPart,
|
|
10
8
|
safeEncodeEIP55,
|
|
11
9
|
} from './utils'
|
|
@@ -88,87 +86,3 @@ describe('parseDecimalIntegerPart', () => {
|
|
|
88
86
|
}
|
|
89
87
|
)
|
|
90
88
|
})
|
|
91
|
-
|
|
92
|
-
describe('nftEnabled', () => {
|
|
93
|
-
beforeEach(() => {
|
|
94
|
-
process.env.NFT_CURRENCIES = JSON.stringify([])
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
it('should return false when currency is null or undefined', () => {
|
|
98
|
-
expect(nftEnabled(null)).toEqual(false)
|
|
99
|
-
expect(nftEnabled(undefined)).toEqual(false)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it('should return false when NFT_CURRENCIES env does not include currency', () => {
|
|
103
|
-
process.env.NFT_CURRENCIES = JSON.stringify(['base', 'polygon'])
|
|
104
|
-
expect(nftEnabled('ethereum')).toEqual(false)
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
it('should return true when NFT_CURRENCIES env include currency', () => {
|
|
108
|
-
process.env.NFT_CURRENCIES = JSON.stringify(['base', 'ethereum', 'polygon'])
|
|
109
|
-
expect(nftEnabled('ethereum')).toEqual(true)
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
it.each([
|
|
113
|
-
'',
|
|
114
|
-
'word',
|
|
115
|
-
'a simple sentence',
|
|
116
|
-
'0x66c4371aE8FFeD2ec1c2EBbbcCfb7E494181E1E3',
|
|
117
|
-
'[]}',
|
|
118
|
-
'{ [] }',
|
|
119
|
-
])('should return false when NFT_CURRENCIES is not a valid JSON (%s)', (value) => {
|
|
120
|
-
process.env.NFT_CURRENCIES = value
|
|
121
|
-
expect(nftEnabled('ethereum')).toEqual(false)
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
it('should return false when NFT_CURRENCIES is not an array', () => {
|
|
125
|
-
// A new config not supported for example (feature flag like)
|
|
126
|
-
process.env.NFT_CURRENCIES = JSON.stringify({
|
|
127
|
-
enabled: true,
|
|
128
|
-
params: { families: ['ethereum'] },
|
|
129
|
-
})
|
|
130
|
-
expect(nftEnabled('ethereum')).toBe(false)
|
|
131
|
-
})
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
describe('nftDisabled', () => {
|
|
135
|
-
beforeEach(() => {
|
|
136
|
-
process.env.NFT_CURRENCIES = JSON.stringify([])
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
it('should return false when currency is null or undefined', () => {
|
|
140
|
-
expect(nftDisabled(null)).toEqual(true)
|
|
141
|
-
expect(nftDisabled(undefined)).toEqual(true)
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
it('should return true when NFT_CURRENCIES env does not include currency', () => {
|
|
145
|
-
process.env.NFT_CURRENCIES = JSON.stringify(['base', 'polygon'])
|
|
146
|
-
expect(nftDisabled('ethereum')).toEqual(true)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
it('should return false when NFT_CURRENCIES env include currency', () => {
|
|
150
|
-
process.env.NFT_CURRENCIES = JSON.stringify(['base', 'ethereum', 'polygon'])
|
|
151
|
-
expect(nftDisabled('ethereum')).toEqual(false)
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
it.each([
|
|
155
|
-
'',
|
|
156
|
-
'word',
|
|
157
|
-
'a simple sentence',
|
|
158
|
-
'0x66c4371aE8FFeD2ec1c2EBbbcCfb7E494181E1E3',
|
|
159
|
-
'[]}',
|
|
160
|
-
'{ [] }',
|
|
161
|
-
])('should return true when NFT_CURRENCIES is not a valid JSON (%s)', (value) => {
|
|
162
|
-
process.env.NFT_CURRENCIES = value
|
|
163
|
-
expect(nftDisabled('ethereum')).toEqual(true)
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
it('should return true when NFT_CURRENCIES is not an array', () => {
|
|
167
|
-
// A new config not supported for example (feature flag like)
|
|
168
|
-
process.env.NFT_CURRENCIES = JSON.stringify({
|
|
169
|
-
enabled: true,
|
|
170
|
-
params: { families: ['ethereum'] },
|
|
171
|
-
})
|
|
172
|
-
expect(nftDisabled('ethereum')).toBe(true)
|
|
173
|
-
})
|
|
174
|
-
})
|
package/src/utils.ts
CHANGED
|
@@ -371,44 +371,3 @@ export type EvmStakingIntent = StakingTransactionIntent & {
|
|
|
371
371
|
export function isStakingIntent(intent: TransactionIntent): intent is EvmStakingIntent {
|
|
372
372
|
return intent.intentType === 'staking'
|
|
373
373
|
}
|
|
374
|
-
|
|
375
|
-
export function nftEnabled(currencyId: string | undefined | null): boolean {
|
|
376
|
-
let parsedNftCurrencies: unknown = undefined
|
|
377
|
-
if (process.env.NFT_CURRENCIES) {
|
|
378
|
-
try {
|
|
379
|
-
parsedNftCurrencies = JSON.parse(process.env.NFT_CURRENCIES)
|
|
380
|
-
} catch (error) {
|
|
381
|
-
log(
|
|
382
|
-
'EVM Family - utils.ts',
|
|
383
|
-
'Env variable NFT_CURRENCIES is malformed, expecting a string array',
|
|
384
|
-
{
|
|
385
|
-
envName: 'NFT_CURRENCIES',
|
|
386
|
-
envValue: process.env.NFT_CURRENCIES,
|
|
387
|
-
error,
|
|
388
|
-
}
|
|
389
|
-
)
|
|
390
|
-
|
|
391
|
-
return false
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
if (!Array.isArray(parsedNftCurrencies)) {
|
|
396
|
-
log(
|
|
397
|
-
'EVM Family - utils.ts',
|
|
398
|
-
'Env variable NFT_CURRENCIES is malformed, expecting a string array',
|
|
399
|
-
{
|
|
400
|
-
envName: 'NFT_CURRENCIES',
|
|
401
|
-
envValue: process.env.NFT_CURRENCIES,
|
|
402
|
-
}
|
|
403
|
-
)
|
|
404
|
-
|
|
405
|
-
return false
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
return Boolean(currencyId && parsedNftCurrencies.includes(currencyId))
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
// Simple utility function, to ease reading code
|
|
412
|
-
export function nftDisabled(currencyId: string | undefined | null): boolean {
|
|
413
|
-
return !nftEnabled(currencyId)
|
|
414
|
-
}
|