@ledgerhq/coin-evm 5.4.1 → 7.0.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 +191 -0
- package/docs/evm-family-integration-process/README.md +1 -1
- package/knip.json +2 -3
- package/lib/adapters/blockOperations.js +6 -6
- package/lib/adapters/blockOperations.js.map +1 -1
- package/lib/adapters/etherscan.js +23 -23
- package/lib/adapters/etherscan.js.map +1 -1
- package/lib/adapters/ledger.js +17 -17
- package/lib/adapters/ledger.js.map +1 -1
- package/lib/api/index.js +4 -4
- package/lib/api/index.js.map +1 -1
- package/lib/config.d.ts +21 -15
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +5 -25
- package/lib/config.js.map +1 -1
- package/lib/errors.d.ts +9 -0
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +15 -1
- package/lib/errors.js.map +1 -1
- package/lib/logic/broadcast.js +1 -1
- package/lib/logic/broadcast.js.map +1 -1
- package/lib/logic/common.d.ts +2 -1
- package/lib/logic/common.d.ts.map +1 -1
- package/lib/logic/common.js +4 -4
- package/lib/logic/common.js.map +1 -1
- package/lib/logic/craftTransaction.d.ts.map +1 -1
- package/lib/logic/craftTransaction.js +2 -2
- package/lib/logic/craftTransaction.js.map +1 -1
- package/lib/logic/estimateFees.d.ts.map +1 -1
- package/lib/logic/estimateFees.js +9 -9
- package/lib/logic/estimateFees.js.map +1 -1
- package/lib/logic/getBalance.d.ts.map +1 -1
- package/lib/logic/getBalance.js +50 -16
- package/lib/logic/getBalance.js.map +1 -1
- package/lib/logic/getBlock.d.ts.map +1 -1
- package/lib/logic/getBlock.js +19 -11
- package/lib/logic/getBlock.js.map +1 -1
- package/lib/logic/getBlockInfo.js +1 -1
- package/lib/logic/getBlockInfo.js.map +1 -1
- package/lib/logic/getNextSequence.d.ts.map +1 -1
- package/lib/logic/getNextSequence.js +1 -1
- package/lib/logic/getNextSequence.js.map +1 -1
- package/lib/logic/index.d.ts +2 -0
- package/lib/logic/index.d.ts.map +1 -1
- package/lib/logic/index.js +4 -0
- package/lib/logic/index.js.map +1 -1
- package/lib/logic/lastBlock.js +1 -1
- package/lib/logic/lastBlock.js.map +1 -1
- package/lib/logic/listOperations.d.ts.map +1 -1
- package/lib/logic/listOperations.js +10 -1
- package/lib/logic/listOperations.js.map +1 -1
- package/lib/logic/validateIntent.d.ts.map +1 -1
- package/lib/logic/validateIntent.js +12 -12
- package/lib/logic/validateIntent.js.map +1 -1
- package/lib/logic-public/index.d.ts +3 -0
- package/lib/logic-public/index.d.ts.map +1 -0
- package/lib/logic-public/index.js +19 -0
- package/lib/logic-public/index.js.map +1 -0
- package/lib/{utils.d.ts → logic-public/utils.d.ts} +0 -44
- package/lib/logic-public/utils.d.ts.map +1 -0
- package/lib/logic-public/utils.js +166 -0
- package/lib/logic-public/utils.js.map +1 -0
- package/lib/logic.d.ts +2 -1
- package/lib/logic.d.ts.map +1 -1
- package/lib/logic.js +3 -3
- package/lib/logic.js.map +1 -1
- package/lib/network/explorer/etherscan.d.ts +16 -3
- package/lib/network/explorer/etherscan.d.ts.map +1 -1
- package/lib/network/explorer/etherscan.js +61 -6
- package/lib/network/explorer/etherscan.js.map +1 -1
- package/lib/network/explorer/ledger.d.ts +50 -4
- package/lib/network/explorer/ledger.d.ts.map +1 -1
- package/lib/network/explorer/ledger.js +286 -37
- package/lib/network/explorer/ledger.js.map +1 -1
- package/lib/network/explorer/none.d.ts +1 -0
- package/lib/network/explorer/none.d.ts.map +1 -1
- package/lib/network/explorer/none.js +4 -1
- package/lib/network/explorer/none.js.map +1 -1
- package/lib/network/explorer/types.d.ts +15 -2
- package/lib/network/explorer/types.d.ts.map +1 -1
- package/lib/network/explorer/types.js.map +1 -1
- package/lib/network/index.d.ts +5 -0
- package/lib/network/index.d.ts.map +1 -0
- package/lib/network/index.js +23 -0
- package/lib/network/index.js.map +1 -0
- package/lib/network/node/explorerProxy.d.ts +27 -0
- package/lib/network/node/explorerProxy.d.ts.map +1 -0
- package/lib/network/node/explorerProxy.js +262 -0
- package/lib/network/node/explorerProxy.js.map +1 -0
- package/lib/network/node/index.d.ts +2 -1
- package/lib/network/node/index.d.ts.map +1 -1
- package/lib/network/node/index.js +11 -10
- package/lib/network/node/index.js.map +1 -1
- package/lib/network/node/ledger.js +7 -7
- package/lib/network/node/ledger.js.map +1 -1
- package/lib/network/node/rpc.common.d.ts +2 -2
- package/lib/network/node/rpc.common.js +19 -19
- package/lib/network/node/rpc.common.js.map +1 -1
- package/lib/network/node/withNodeSources.d.ts +21 -0
- package/lib/network/node/withNodeSources.d.ts.map +1 -0
- package/lib/network/node/withNodeSources.js +78 -0
- package/lib/network/node/withNodeSources.js.map +1 -0
- package/lib/nodeSources.d.ts +15 -0
- package/lib/nodeSources.d.ts.map +1 -0
- package/lib/nodeSources.js +33 -0
- package/lib/nodeSources.js.map +1 -0
- package/lib/staking/contracts.js +1 -1
- package/lib/staking/contracts.js.map +1 -1
- package/lib/staking/fetchers.js +1 -1
- package/lib/staking/fetchers.js.map +1 -1
- package/lib/staking/operations/sei.js +1 -1
- package/lib/staking/operations/sei.js.map +1 -1
- package/lib/staking/prepareIntents.js +3 -3
- package/lib/staking/prepareIntents.js.map +1 -1
- package/lib/staking/rewards.js +1 -1
- package/lib/staking/rewards.js.map +1 -1
- package/lib/staking/transactionData.js +2 -2
- package/lib/staking/transactionData.js.map +1 -1
- package/lib/staking/utils.d.ts +45 -0
- package/lib/staking/utils.d.ts.map +1 -0
- package/lib/{utils.js → staking/utils.js} +5 -164
- package/lib/staking/utils.js.map +1 -0
- package/lib/staking/validators/sei.js +1 -1
- package/lib/staking/validators/sei.js.map +1 -1
- package/lib/types/staking.d.ts +2 -2
- package/lib-es/adapters/blockOperations.js +1 -1
- package/lib-es/adapters/blockOperations.js.map +1 -1
- package/lib-es/adapters/etherscan.js +1 -1
- package/lib-es/adapters/etherscan.js.map +1 -1
- package/lib-es/adapters/ledger.js +1 -1
- package/lib-es/adapters/ledger.js.map +1 -1
- package/lib-es/api/index.js +2 -2
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/config.d.ts +21 -15
- package/lib-es/config.d.ts.map +1 -1
- package/lib-es/config.js +1 -21
- package/lib-es/config.js.map +1 -1
- package/lib-es/errors.d.ts +9 -0
- package/lib-es/errors.d.ts.map +1 -1
- package/lib-es/errors.js +13 -0
- package/lib-es/errors.js.map +1 -1
- package/lib-es/logic/broadcast.js +1 -1
- package/lib-es/logic/broadcast.js.map +1 -1
- package/lib-es/logic/common.d.ts +2 -1
- package/lib-es/logic/common.d.ts.map +1 -1
- package/lib-es/logic/common.js +3 -3
- package/lib-es/logic/common.js.map +1 -1
- package/lib-es/logic/craftTransaction.d.ts.map +1 -1
- package/lib-es/logic/craftTransaction.js +2 -2
- package/lib-es/logic/craftTransaction.js.map +1 -1
- package/lib-es/logic/estimateFees.d.ts.map +1 -1
- package/lib-es/logic/estimateFees.js +6 -6
- package/lib-es/logic/estimateFees.js.map +1 -1
- package/lib-es/logic/getBalance.d.ts.map +1 -1
- package/lib-es/logic/getBalance.js +50 -16
- package/lib-es/logic/getBalance.js.map +1 -1
- package/lib-es/logic/getBlock.d.ts.map +1 -1
- package/lib-es/logic/getBlock.js +16 -8
- package/lib-es/logic/getBlock.js.map +1 -1
- package/lib-es/logic/getBlockInfo.js +1 -1
- package/lib-es/logic/getBlockInfo.js.map +1 -1
- package/lib-es/logic/getNextSequence.d.ts.map +1 -1
- package/lib-es/logic/getNextSequence.js +1 -1
- package/lib-es/logic/getNextSequence.js.map +1 -1
- package/lib-es/logic/index.d.ts +2 -0
- package/lib-es/logic/index.d.ts.map +1 -1
- package/lib-es/logic/index.js +4 -0
- package/lib-es/logic/index.js.map +1 -1
- package/lib-es/logic/lastBlock.js +1 -1
- package/lib-es/logic/lastBlock.js.map +1 -1
- package/lib-es/logic/listOperations.d.ts.map +1 -1
- package/lib-es/logic/listOperations.js +10 -1
- package/lib-es/logic/listOperations.js.map +1 -1
- package/lib-es/logic/validateIntent.d.ts.map +1 -1
- package/lib-es/logic/validateIntent.js +4 -4
- package/lib-es/logic/validateIntent.js.map +1 -1
- package/lib-es/logic-public/index.d.ts +3 -0
- package/lib-es/logic-public/index.d.ts.map +1 -0
- package/lib-es/logic-public/index.js +4 -0
- package/lib-es/logic-public/index.js.map +1 -0
- package/lib-es/{utils.d.ts → logic-public/utils.d.ts} +0 -44
- package/lib-es/logic-public/utils.d.ts.map +1 -0
- package/lib-es/logic-public/utils.js +150 -0
- package/lib-es/logic-public/utils.js.map +1 -0
- package/lib-es/logic.d.ts +2 -1
- package/lib-es/logic.d.ts.map +1 -1
- package/lib-es/logic.js +3 -3
- package/lib-es/logic.js.map +1 -1
- package/lib-es/network/explorer/etherscan.d.ts +16 -3
- package/lib-es/network/explorer/etherscan.d.ts.map +1 -1
- package/lib-es/network/explorer/etherscan.js +60 -5
- package/lib-es/network/explorer/etherscan.js.map +1 -1
- package/lib-es/network/explorer/ledger.d.ts +50 -4
- package/lib-es/network/explorer/ledger.d.ts.map +1 -1
- package/lib-es/network/explorer/ledger.js +283 -37
- package/lib-es/network/explorer/ledger.js.map +1 -1
- package/lib-es/network/explorer/none.d.ts +1 -0
- package/lib-es/network/explorer/none.d.ts.map +1 -1
- package/lib-es/network/explorer/none.js +2 -0
- package/lib-es/network/explorer/none.js.map +1 -1
- package/lib-es/network/explorer/types.d.ts +15 -2
- package/lib-es/network/explorer/types.d.ts.map +1 -1
- package/lib-es/network/explorer/types.js.map +1 -1
- package/lib-es/network/index.d.ts +5 -0
- package/lib-es/network/index.d.ts.map +1 -0
- package/lib-es/network/index.js +7 -0
- package/lib-es/network/index.js.map +1 -0
- package/lib-es/network/node/explorerProxy.d.ts +27 -0
- package/lib-es/network/node/explorerProxy.d.ts.map +1 -0
- package/lib-es/network/node/explorerProxy.js +254 -0
- package/lib-es/network/node/explorerProxy.js.map +1 -0
- package/lib-es/network/node/index.d.ts +2 -1
- package/lib-es/network/node/index.d.ts.map +1 -1
- package/lib-es/network/node/index.js +11 -10
- package/lib-es/network/node/index.js.map +1 -1
- package/lib-es/network/node/ledger.js +1 -1
- package/lib-es/network/node/ledger.js.map +1 -1
- package/lib-es/network/node/rpc.common.d.ts +2 -2
- package/lib-es/network/node/rpc.common.js +3 -3
- package/lib-es/network/node/rpc.common.js.map +1 -1
- package/lib-es/network/node/withNodeSources.d.ts +21 -0
- package/lib-es/network/node/withNodeSources.d.ts.map +1 -0
- package/lib-es/network/node/withNodeSources.js +74 -0
- package/lib-es/network/node/withNodeSources.js.map +1 -0
- package/lib-es/nodeSources.d.ts +15 -0
- package/lib-es/nodeSources.d.ts.map +1 -0
- package/lib-es/nodeSources.js +28 -0
- package/lib-es/nodeSources.js.map +1 -0
- package/lib-es/staking/contracts.js +1 -1
- package/lib-es/staking/contracts.js.map +1 -1
- package/lib-es/staking/fetchers.js +1 -1
- package/lib-es/staking/fetchers.js.map +1 -1
- package/lib-es/staking/operations/sei.js +1 -1
- package/lib-es/staking/operations/sei.js.map +1 -1
- package/lib-es/staking/prepareIntents.js +1 -1
- package/lib-es/staking/prepareIntents.js.map +1 -1
- package/lib-es/staking/rewards.js +1 -1
- package/lib-es/staking/rewards.js.map +1 -1
- package/lib-es/staking/transactionData.js +1 -1
- package/lib-es/staking/transactionData.js.map +1 -1
- package/lib-es/staking/utils.d.ts +45 -0
- package/lib-es/staking/utils.d.ts.map +1 -0
- package/lib-es/{utils.js → staking/utils.js} +1 -147
- package/lib-es/staking/utils.js.map +1 -0
- package/lib-es/staking/validators/sei.js +1 -1
- package/lib-es/staking/validators/sei.js.map +1 -1
- package/lib-es/types/staking.d.ts +2 -2
- package/package.json +72 -7
- package/src/adapters/blockOperations.ts +1 -1
- package/src/adapters/etherscan.ts +1 -1
- package/src/adapters/ledger.ts +1 -1
- package/src/api/index.monad_testnet.integ.test.ts +95 -4
- package/src/api/index.ts +2 -2
- package/src/config.ts +22 -34
- package/src/errors.ts +13 -0
- package/src/fixtures/context.fixtures.ts +6 -1
- package/src/logic/broadcast.ts +1 -1
- package/src/logic/common.ts +4 -2
- package/src/logic/craftTransaction.ts +2 -1
- package/src/logic/estimateFees.ts +20 -6
- package/src/logic/getBalance.ts +72 -26
- package/src/logic/getBlock.ts +26 -14
- package/src/logic/getBlockInfo.ts +1 -1
- package/src/logic/getNextSequence.ts +4 -1
- package/src/logic/index.ts +4 -0
- package/src/logic/lastBlock.ts +1 -1
- package/src/logic/listOperations.ts +14 -1
- package/src/logic/tests/broadcast.integ.test.ts +53 -0
- package/src/logic/{broadcast.test.ts → tests/broadcast.test.ts} +7 -7
- package/src/logic/{broadcastSource.test.ts → tests/broadcastSource.test.ts} +6 -6
- package/src/logic/{combine.test.ts → tests/combine.test.ts} +1 -1
- package/src/logic/{common.test.ts → tests/common.test.ts} +45 -21
- package/src/logic/{computeGasLimit.test.ts → tests/computeGasLimit.test.ts} +2 -2
- package/src/logic/{craftTransaction.test.ts → tests/craftTransaction.test.ts} +7 -7
- package/src/logic/{craftTransactionData.test.ts → tests/craftTransactionData.test.ts} +4 -4
- package/src/logic/{estimateFees.test.ts → tests/estimateFees.test.ts} +12 -12
- package/src/logic/tests/getBalance.test.ts +602 -0
- package/src/logic/{getBlock.test.ts → tests/getBlock.test.ts} +182 -89
- package/src/logic/{getBlockInfo.test.ts → tests/getBlockInfo.test.ts} +8 -8
- package/src/logic/{getErc20Data.test.ts → tests/getErc20Data.test.ts} +1 -1
- package/src/logic/{getNextSequence.test.ts → tests/getNextSequence.test.ts} +7 -7
- package/src/logic/{getStakes.test.ts → tests/getStakes.test.ts} +13 -13
- package/src/logic/{internalTransactionsFetcher.test.ts → tests/internalTransactionsFetcher.test.ts} +8 -8
- package/src/logic/{lastBlock.test.ts → tests/lastBlock.test.ts} +7 -7
- package/src/logic/{listOperations.test.ts → tests/listOperations.test.ts} +125 -6
- package/src/logic/{validateAddress.test.ts → tests/validateAddress.test.ts} +1 -1
- package/src/logic/{validateIntent.test.ts → tests/validateIntent.test.ts} +80 -78
- package/src/logic/validateIntent.ts +7 -4
- package/src/logic-public/index.ts +18 -0
- package/src/{utils.test.ts → logic-public/tests/utils.test.ts} +1 -27
- package/src/logic-public/utils.ts +184 -0
- package/src/logic.test.ts +5 -59
- package/src/logic.ts +5 -3
- package/src/network/explorer/etherscan.test.ts +403 -195
- package/src/network/explorer/etherscan.ts +99 -10
- package/src/network/explorer/index.test.ts +27 -0
- package/src/network/explorer/ledger.test.ts +791 -62
- package/src/network/explorer/ledger.ts +426 -54
- package/src/network/explorer/none.ts +3 -0
- package/src/network/explorer/types.ts +29 -5
- package/src/network/index.ts +7 -0
- package/src/network/node/explorerProxy.test.ts +673 -0
- package/src/network/node/explorerProxy.ts +308 -0
- package/src/network/node/index.memoization.test.ts +22 -10
- package/src/network/node/index.test.ts +6 -3
- package/src/network/node/index.ts +11 -7
- package/src/network/node/ledger.ts +1 -1
- package/src/network/node/rpc.common.ts +3 -3
- package/src/network/node/rpc.test.ts +66 -105
- package/src/network/node/withNodeSources.test.ts +240 -0
- package/src/network/node/withNodeSources.ts +95 -0
- package/src/nodeSources.ts +38 -0
- package/src/staking/contracts.ts +1 -1
- package/src/staking/encoder.test.ts +1 -1
- package/src/staking/fetchers.ts +1 -1
- package/src/staking/operations/sei.ts +1 -1
- package/src/staking/prepareIntents.ts +1 -1
- package/src/staking/redelegations.test.ts +1 -1
- package/src/staking/rewards.test.ts +1 -1
- package/src/staking/rewards.ts +1 -1
- package/src/staking/transactionData.ts +1 -1
- package/src/staking/unbondings.test.ts +1 -1
- package/src/staking/utils.test.ts +25 -0
- package/src/{utils.ts → staking/utils.ts} +2 -182
- package/src/staking/validators/sei.ts +1 -1
- package/src/types/staking.ts +2 -2
- package/lib/constants.d.ts +0 -2
- package/lib/constants.d.ts.map +0 -1
- package/lib/constants.js +0 -11
- package/lib/constants.js.map +0 -1
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js.map +0 -1
- package/lib-es/constants.d.ts +0 -2
- package/lib-es/constants.d.ts.map +0 -1
- package/lib-es/constants.js +0 -7
- package/lib-es/constants.js.map +0 -1
- package/lib-es/utils.d.ts.map +0 -1
- package/lib-es/utils.js.map +0 -1
- package/src/constants.ts +0 -9
- package/src/logic/broadcast.integ.test.ts +0 -46
- package/src/logic/getBalance.test.ts +0 -362
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
+
import type { EvmConfigInfo } from '../../config'
|
|
4
5
|
import type { LedgerExplorerOperation } from '../../types'
|
|
5
6
|
import type { ExplorerApi } from './types'
|
|
6
|
-
import type {
|
|
7
|
+
import type {
|
|
8
|
+
AssetInfo,
|
|
9
|
+
MemoNotSupported,
|
|
10
|
+
Operation,
|
|
11
|
+
} from '@ledgerhq/coin-module-framework/api/types'
|
|
7
12
|
import { delay } from '@ledgerhq/coin-module-framework/promises'
|
|
8
13
|
import axios from 'axios'
|
|
14
|
+
import eip55 from 'eip55'
|
|
9
15
|
import {
|
|
10
16
|
ledgerERC1155EventToOperations,
|
|
11
17
|
ledgerERC20EventToOperations,
|
|
@@ -14,13 +20,33 @@ import {
|
|
|
14
20
|
ledgerOperationToOperations,
|
|
15
21
|
} from '../../adapters/index'
|
|
16
22
|
import { DEFAULT_LEDGER_EXPLORER_URI } from '../../config'
|
|
17
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
LedgerExplorerUsedIncorrectly,
|
|
25
|
+
RateLimitError,
|
|
26
|
+
UnpageableLedgerExplorerBlock,
|
|
27
|
+
} from '../../errors'
|
|
28
|
+
import { safeEncodeEIP55 } from '../../logic-public'
|
|
18
29
|
import { isLedgerExplorerConfig, NO_TOKEN } from './types'
|
|
19
30
|
|
|
20
31
|
export const DEFAULT_BATCH_SIZE = 10_000
|
|
21
32
|
export const LEDGER_TIMEOUT = 200 // 200ms between 2 calls
|
|
22
33
|
export const DEFAULT_RETRIES_API = 2
|
|
23
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Page size for the discovery walk (see `discoverTokenAssets` below). Once discovery no longer
|
|
37
|
+
* accumulates pages (see `fetchPaginatedOpsWithRetries`), its peak memory is one *parsed* page,
|
|
38
|
+
* so this constant is a memory bound, not a throughput knob — it does not affect `getOperations`.
|
|
39
|
+
*/
|
|
40
|
+
export const DISCOVERY_BATCH_SIZE = 100
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The explorer serves at most this many records per request regardless of a larger `batch_size`
|
|
44
|
+
* — measured against `https://explorers.api.live.ledger.com`: a requested 10 000 is served as
|
|
45
|
+
* 500. A raw page at or above this ceiling can never be trusted as proof there is nothing more,
|
|
46
|
+
* no matter how large `batch_size` was asked to be — see `fetchLedgerOpsPage`.
|
|
47
|
+
*/
|
|
48
|
+
export const EXPLORER_MAX_PAGE_SIZE = 500
|
|
49
|
+
|
|
24
50
|
type OperationsRequestParams = {
|
|
25
51
|
explorerId: string
|
|
26
52
|
address: string
|
|
@@ -28,12 +54,65 @@ type OperationsRequestParams = {
|
|
|
28
54
|
batchSize: number
|
|
29
55
|
}
|
|
30
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Layered onto `OperationsRequestParams` only by the paged fetch below. Kept as a separate,
|
|
59
|
+
* fully-optional type so the legacy exhaustive callers' object literals (`fetchPaginatedOpsWithRetries`,
|
|
60
|
+
* `discoverTokenAssets`) do not need to change: they keep requesting ascending with no ceiling.
|
|
61
|
+
*/
|
|
62
|
+
type OrderedRequestParams = {
|
|
63
|
+
toBlock?: number | undefined
|
|
64
|
+
order?: 'asc' | 'desc' | undefined
|
|
65
|
+
}
|
|
66
|
+
|
|
31
67
|
/** Both default to what the env used to supply, so the previous call shape keeps working. */
|
|
32
68
|
type LedgerExplorerSettings = {
|
|
33
69
|
explorerUri?: string | undefined
|
|
34
70
|
clientVersion?: string | undefined
|
|
35
71
|
}
|
|
36
72
|
|
|
73
|
+
type OpsPage = { operations: LedgerExplorerOperation[]; token: string }
|
|
74
|
+
|
|
75
|
+
function mapExplorerOrder(order: 'asc' | 'desc' = 'asc'): 'ascending' | 'descending' {
|
|
76
|
+
return order === 'desc' ? 'descending' : 'ascending'
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Bare HTTP call for a single page: no retry, no accumulation.
|
|
81
|
+
*
|
|
82
|
+
* `paginationToken`: `undefined` omits the `token` parameter entirely (the paged fetch resumes
|
|
83
|
+
* through `from_height`/`to_height`, never through the explorer's own token); `null` or a string
|
|
84
|
+
* requests continuation through the explorer's token, as the exhaustive walks below do.
|
|
85
|
+
*/
|
|
86
|
+
async function requestOpsPage(
|
|
87
|
+
params: Required<OperationsRequestParams> & LedgerExplorerSettings & OrderedRequestParams,
|
|
88
|
+
paginationToken: string | null | undefined
|
|
89
|
+
): Promise<OpsPage> {
|
|
90
|
+
const {
|
|
91
|
+
data: { data: operations, token },
|
|
92
|
+
} = await axios.request<{
|
|
93
|
+
data: LedgerExplorerOperation[]
|
|
94
|
+
token: string
|
|
95
|
+
}>({
|
|
96
|
+
...(params.clientVersion
|
|
97
|
+
? { headers: { 'X-Ledger-Client-Version': params.clientVersion } }
|
|
98
|
+
: {}),
|
|
99
|
+
method: 'GET',
|
|
100
|
+
url: `${params.explorerUri ?? DEFAULT_LEDGER_EXPLORER_URI}/blockchain/v4/${params.explorerId}/address/${params.address}/txs`,
|
|
101
|
+
params: {
|
|
102
|
+
filtering: true,
|
|
103
|
+
from_height: params.fromBlock ?? 0,
|
|
104
|
+
// Needed to make sure we get transactions after the block height and not before, when no
|
|
105
|
+
// explicit order is requested. Order is still descending in the end for that legacy walk.
|
|
106
|
+
order: mapExplorerOrder(params.order),
|
|
107
|
+
batch_size: params.batchSize,
|
|
108
|
+
...(params.toBlock !== undefined ? { to_height: params.toBlock } : {}),
|
|
109
|
+
...(paginationToken !== undefined ? { token: paginationToken } : {}),
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
return { operations, token }
|
|
114
|
+
}
|
|
115
|
+
|
|
37
116
|
/**
|
|
38
117
|
* Request fetching all operations from an address
|
|
39
118
|
* and supporting pagination through tokens.
|
|
@@ -45,25 +124,7 @@ export async function fetchPaginatedOpsWithRetries(
|
|
|
45
124
|
retries = DEFAULT_RETRIES_API
|
|
46
125
|
): Promise<LedgerExplorerOperation[]> {
|
|
47
126
|
try {
|
|
48
|
-
const {
|
|
49
|
-
data: { data: operationsBatch, token },
|
|
50
|
-
} = await axios.request<{
|
|
51
|
-
data: LedgerExplorerOperation[]
|
|
52
|
-
token: string
|
|
53
|
-
}>({
|
|
54
|
-
...(params.clientVersion
|
|
55
|
-
? { headers: { 'X-Ledger-Client-Version': params.clientVersion } }
|
|
56
|
-
: {}),
|
|
57
|
-
method: 'GET',
|
|
58
|
-
url: `${params.explorerUri ?? DEFAULT_LEDGER_EXPLORER_URI}/blockchain/v4/${params.explorerId}/address/${params.address}/txs`,
|
|
59
|
-
params: {
|
|
60
|
-
filtering: true,
|
|
61
|
-
from_height: params.fromBlock ?? 0,
|
|
62
|
-
order: 'ascending', // Needed to make sure we get transactions after the block height and not before. Order is still descending in the end
|
|
63
|
-
batch_size: params.batchSize,
|
|
64
|
-
token: paginationToken,
|
|
65
|
-
},
|
|
66
|
-
})
|
|
127
|
+
const { operations: operationsBatch, token } = await requestOpsPage(params, paginationToken)
|
|
67
128
|
|
|
68
129
|
previousOperations.push(...operationsBatch)
|
|
69
130
|
|
|
@@ -88,39 +149,210 @@ export async function fetchPaginatedOpsWithRetries(
|
|
|
88
149
|
}
|
|
89
150
|
|
|
90
151
|
/**
|
|
91
|
-
*
|
|
152
|
+
* Fetch a single page, retrying up to `retries` times on failure. Shared by the discovery walk
|
|
153
|
+
* and the paged fetch below. Unlike `fetchPaginatedOpsWithRetries`, the retry budget is per page,
|
|
154
|
+
* not per walk: a page that keeps failing after its own retries propagates instead of being
|
|
155
|
+
* silently skipped — a hole in the middle of a walk is worse than a failure.
|
|
92
156
|
*
|
|
93
|
-
*
|
|
94
|
-
* so
|
|
95
|
-
* Pagination may be supported in the future.
|
|
157
|
+
* A rate limit is the one error that is never retried, exactly as the exhaustive walk treats it:
|
|
158
|
+
* it is not transient, so spending the budget on it only walks the caller into a timeout.
|
|
96
159
|
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
160
|
+
async function requestOpsPageWithRetries(
|
|
161
|
+
params: Required<OperationsRequestParams> & LedgerExplorerSettings & OrderedRequestParams,
|
|
162
|
+
paginationToken: string | null | undefined,
|
|
163
|
+
retries = DEFAULT_RETRIES_API
|
|
164
|
+
): Promise<OpsPage> {
|
|
165
|
+
try {
|
|
166
|
+
return await requestOpsPage(params, paginationToken)
|
|
167
|
+
} catch (e) {
|
|
168
|
+
if (axios.isAxiosError(e) && e.response?.status === 429) {
|
|
169
|
+
throw new RateLimitError()
|
|
170
|
+
}
|
|
171
|
+
if (retries) {
|
|
172
|
+
await delay(LEDGER_TIMEOUT)
|
|
173
|
+
return requestOpsPageWithRetries(params, paginationToken, retries - 1)
|
|
174
|
+
}
|
|
175
|
+
throw e
|
|
108
176
|
}
|
|
177
|
+
}
|
|
109
178
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
179
|
+
/**
|
|
180
|
+
* A ledger-explorer pagination cursor: the height and hash of the last record a page delivered.
|
|
181
|
+
*
|
|
182
|
+
* Deliberately not the explorer's own continuation token — the framework's cursor contract
|
|
183
|
+
* requires a cursor that "can be used long after the last request and still provide consistent
|
|
184
|
+
* results" (`@ledgerhq/coin-module-framework/api/types`), which a server-side token with an
|
|
185
|
+
* undocumented lifetime does not guarantee by construction. Height-plus-hash needs no server
|
|
186
|
+
* state and doubles as the request's ceiling (descending) or floor (ascending) on the next call.
|
|
187
|
+
*/
|
|
188
|
+
type LedgerCursor = { height: number; hash: string }
|
|
189
|
+
|
|
190
|
+
/** Serialises a cursor, or `NO_TOKEN` when there is no record to resume from. */
|
|
191
|
+
export function serializeLedgerCursor(record?: LedgerCursor): string {
|
|
192
|
+
return record ? `${record.height}:${record.hash}` : NO_TOKEN
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Parses a cursor produced by `serializeLedgerCursor`. Returns `undefined` for an absent cursor
|
|
197
|
+
* or `NO_TOKEN` (first page); throws on anything else that does not match the `height:hash` shape
|
|
198
|
+
* — mirroring `deserializePagingToken`'s contract for the etherscan-like arm.
|
|
199
|
+
*/
|
|
200
|
+
export function parseLedgerCursor(cursor: string | undefined): LedgerCursor | undefined {
|
|
201
|
+
if (cursor === undefined || cursor === NO_TOKEN) return undefined
|
|
202
|
+
|
|
203
|
+
const separatorIndex = cursor.indexOf(':')
|
|
204
|
+
const heightPart = cursor.slice(0, separatorIndex)
|
|
205
|
+
const hash = cursor.slice(separatorIndex + 1)
|
|
206
|
+
const height = Number.parseInt(heightPart, 10)
|
|
207
|
+
|
|
208
|
+
if (separatorIndex <= 0 || hash.length === 0 || Number.isNaN(height)) {
|
|
209
|
+
throw new Error(`Invalid ledger explorer cursor: ${cursor}`)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return { height, hash }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Resolves the block window for one paged request: the cursor caps a descending walk (it is a
|
|
217
|
+
* ceiling, combined with the caller's `toBlock` as the more restrictive of the two) and floors an
|
|
218
|
+
* ascending one (combined with the caller's `fromBlock` the same way). `from_height` is always a
|
|
219
|
+
* floor on this explorer, never a resume point — resuming a descending walk therefore goes
|
|
220
|
+
* through `to_height`, not `from_height`.
|
|
221
|
+
*/
|
|
222
|
+
function resolveWindow(
|
|
223
|
+
order: 'asc' | 'desc',
|
|
224
|
+
fromBlock: number,
|
|
225
|
+
toBlock: number | undefined,
|
|
226
|
+
cursor: LedgerCursor | undefined
|
|
227
|
+
): { fromBlock: number; toBlock: number | undefined } {
|
|
228
|
+
if (order === 'desc') {
|
|
229
|
+
const ceiling =
|
|
230
|
+
cursor === undefined
|
|
231
|
+
? toBlock
|
|
232
|
+
: toBlock === undefined
|
|
233
|
+
? cursor.height
|
|
234
|
+
: Math.min(cursor.height, toBlock)
|
|
235
|
+
return { fromBlock, toBlock: ceiling }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const floor = cursor === undefined ? fromBlock : Math.max(cursor.height, fromBlock)
|
|
239
|
+
return { fromBlock: floor, toBlock }
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
type PagedFetchParams = {
|
|
243
|
+
explorerId: string
|
|
244
|
+
address: string
|
|
245
|
+
fromBlock: number
|
|
246
|
+
toBlock?: number | undefined
|
|
247
|
+
cursor?: string | undefined
|
|
248
|
+
limit: number
|
|
249
|
+
order: 'asc' | 'desc'
|
|
250
|
+
/** Per-page retry budget; falls back to `DEFAULT_RETRIES_API` when the explorer sets none. */
|
|
251
|
+
retries?: number | undefined
|
|
252
|
+
} & LedgerExplorerSettings
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Fetch exactly one page for a caller that supplied `limit`: one explorer request in the common
|
|
256
|
+
* case, more only to work around boundary exhaustion (see below). Never accumulates further
|
|
257
|
+
* pages — a caller that stops after this page causes no further explorer request.
|
|
258
|
+
*/
|
|
259
|
+
async function fetchLedgerOpsPage(
|
|
260
|
+
params: PagedFetchParams
|
|
261
|
+
): Promise<{ operations: LedgerExplorerOperation[]; nextPagingToken: string }> {
|
|
262
|
+
const cursor = parseLedgerCursor(params.cursor)
|
|
263
|
+
const { fromBlock, toBlock } = resolveWindow(
|
|
264
|
+
params.order,
|
|
265
|
+
params.fromBlock,
|
|
266
|
+
params.toBlock,
|
|
267
|
+
cursor
|
|
122
268
|
)
|
|
123
269
|
|
|
270
|
+
let batchSize = params.limit
|
|
271
|
+
let records: LedgerExplorerOperation[] = []
|
|
272
|
+
let rawPageIsFull = false
|
|
273
|
+
|
|
274
|
+
for (;;) {
|
|
275
|
+
const { operations: rawRecords } = await requestOpsPageWithRetries(
|
|
276
|
+
{
|
|
277
|
+
explorerUri: params.explorerUri,
|
|
278
|
+
clientVersion: params.clientVersion,
|
|
279
|
+
explorerId: params.explorerId,
|
|
280
|
+
address: params.address,
|
|
281
|
+
fromBlock,
|
|
282
|
+
toBlock,
|
|
283
|
+
batchSize,
|
|
284
|
+
order: params.order,
|
|
285
|
+
},
|
|
286
|
+
undefined, // resumed through from_height/to_height, never through the explorer's token
|
|
287
|
+
params.retries
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
// A raw page at or beyond the explorer's own page-size ceiling can never prove the window is
|
|
291
|
+
// exhausted, no matter how large `batchSize` was asked to be (measured: requests above ~500
|
|
292
|
+
// are served as 500) — "full" is judged against whichever of the two is smaller, so doubling
|
|
293
|
+
// past that ceiling cannot be mistaken for the walk having reached the end of the window.
|
|
294
|
+
rawPageIsFull = rawRecords.length >= Math.min(batchSize, EXPLORER_MAX_PAGE_SIZE)
|
|
295
|
+
|
|
296
|
+
const cursorIndex = cursor ? rawRecords.findIndex((record) => record.hash === cursor.hash) : -1
|
|
297
|
+
// No cursor: the whole page is new. A cursor supplied but not located in this page is never
|
|
298
|
+
// treated as "nothing to skip" — that would risk re-serving already-delivered records if a
|
|
299
|
+
// caller changed `limit` between two calls — it is treated the same as an exhausted boundary
|
|
300
|
+
// block below, so the loop can only end once every already-delivered record is accounted for.
|
|
301
|
+
records = !cursor ? rawRecords : cursorIndex === -1 ? [] : rawRecords.slice(cursorIndex + 1)
|
|
302
|
+
|
|
303
|
+
// Boundary exhaustion: everything the cursor should skip is still in this page, and the raw
|
|
304
|
+
// page was full, so the block holding the cursor's record may hold more records than the
|
|
305
|
+
// requested page size. Enlarge the page and re-request the same window. Bounded by that
|
|
306
|
+
// block's record count: the loop ends once a new record surfaces or the explorer confirms
|
|
307
|
+
// there is nothing left (a raw page that is not full).
|
|
308
|
+
if (records.length > 0 || !rawPageIsFull) break
|
|
309
|
+
|
|
310
|
+
if (batchSize >= EXPLORER_MAX_PAGE_SIZE) {
|
|
311
|
+
// Even a request at the explorer's own ceiling cannot get past this block: there is no
|
|
312
|
+
// per-block offset in this API, so no `batch_size` ever will either. Paging through it is
|
|
313
|
+
// impossible, not merely slow — fail loudly rather than loop forever or silently report the
|
|
314
|
+
// end of the history (which would be indistinguishable from a genuinely complete one).
|
|
315
|
+
// This branch is only reachable with a cursor: without one, `records` always equals
|
|
316
|
+
// `rawRecords`, so `records.length === 0` (the other condition to reach here) requires an
|
|
317
|
+
// empty raw page, which is never "full" and breaks the loop before this check.
|
|
318
|
+
const boundaryHeight = cursor!.height
|
|
319
|
+
throw new UnpageableLedgerExplorerBlock(
|
|
320
|
+
`Ledger explorer block ${boundaryHeight} holds more records than a single page can ` +
|
|
321
|
+
`serve (explorer page-size ceiling: ${EXPLORER_MAX_PAGE_SIZE})`,
|
|
322
|
+
{ blockHeight: boundaryHeight, explorerMaxPageSize: EXPLORER_MAX_PAGE_SIZE }
|
|
323
|
+
)
|
|
324
|
+
}
|
|
325
|
+
batchSize *= 2
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// `rawPageIsFull` is only ever true when the loop above broke on `records.length > 0`, so
|
|
329
|
+
// `lastRecord` is always defined on that branch.
|
|
330
|
+
const lastRecord = records[records.length - 1]
|
|
331
|
+
const nextPagingToken = rawPageIsFull
|
|
332
|
+
? serializeLedgerCursor({ height: lastRecord.block.height, hash: lastRecord.hash })
|
|
333
|
+
: NO_TOKEN
|
|
334
|
+
|
|
335
|
+
return { operations: records, nextPagingToken }
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
type AdaptedLedgerOperations = {
|
|
339
|
+
lastCoinOperations: Array<Operation<MemoNotSupported>>
|
|
340
|
+
lastTokenOperations: Array<Operation<MemoNotSupported>>
|
|
341
|
+
lastNftOperations: Array<Operation<MemoNotSupported>>
|
|
342
|
+
lastInternalOperations: Array<Operation<MemoNotSupported>>
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Turns explorer records into the four operation buckets `getOperations` returns. Shared by both
|
|
347
|
+
* the legacy exhaustive path and the paged path: the same input records must produce the same
|
|
348
|
+
* operations regardless of which path fetched them.
|
|
349
|
+
*/
|
|
350
|
+
function adaptLedgerRecords(
|
|
351
|
+
config: EvmConfigInfo,
|
|
352
|
+
currencyId: string,
|
|
353
|
+
address: string,
|
|
354
|
+
records: LedgerExplorerOperation[]
|
|
355
|
+
): AdaptedLedgerOperations {
|
|
124
356
|
const lastCoinOperations: Array<Operation<MemoNotSupported>> = []
|
|
125
357
|
const lastTokenOperations: Array<Operation<MemoNotSupported>> = []
|
|
126
358
|
const lastNftOperations: Array<Operation<MemoNotSupported>> = []
|
|
@@ -131,7 +363,7 @@ export const getOperations: ExplorerApi['getOperations'] = async (
|
|
|
131
363
|
const isNativeContract = (contract: string): boolean =>
|
|
132
364
|
nativeContractsSet.has(contract.toLowerCase())
|
|
133
365
|
|
|
134
|
-
for (const ledgerOp of
|
|
366
|
+
for (const ledgerOp of records) {
|
|
135
367
|
const coinOps = ledgerOperationToOperations(address, currencyId, ledgerOp)
|
|
136
368
|
|
|
137
369
|
const erc20TransferEvents =
|
|
@@ -163,17 +395,157 @@ export const getOperations: ExplorerApi['getOperations'] = async (
|
|
|
163
395
|
lastInternalOperations.push(...internalOps)
|
|
164
396
|
}
|
|
165
397
|
|
|
398
|
+
return { lastCoinOperations, lastTokenOperations, lastNftOperations, lastInternalOperations }
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Returns all operation types from an address.
|
|
403
|
+
*
|
|
404
|
+
* With no `limit`, Ledger explorer fetches all pages recursively internally, so `nextPagingToken`
|
|
405
|
+
* is always empty — this is the legacy, unchanged shape. With a `limit`, exactly one page is
|
|
406
|
+
* fetched and `nextPagingToken` carries a cursor the caller can resume from (see
|
|
407
|
+
* `serializeLedgerCursor`).
|
|
408
|
+
*/
|
|
409
|
+
export const getOperations: ExplorerApi['getOperations'] = async (
|
|
410
|
+
config,
|
|
411
|
+
currencyId,
|
|
412
|
+
address,
|
|
413
|
+
fromBlock,
|
|
414
|
+
toBlock,
|
|
415
|
+
pagingToken,
|
|
416
|
+
limit,
|
|
417
|
+
order
|
|
418
|
+
) => {
|
|
419
|
+
const { explorer } = config || /* istanbul ignore next */ {}
|
|
420
|
+
if (!isLedgerExplorerConfig(explorer)) {
|
|
421
|
+
throw new LedgerExplorerUsedIncorrectly(
|
|
422
|
+
`Ledger explorer used incorrectly with currency: ${currencyId}`
|
|
423
|
+
)
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const settings = {
|
|
427
|
+
explorerUri: config.ledgerExplorerUri,
|
|
428
|
+
clientVersion: config.ledgerClientVersion,
|
|
429
|
+
explorerId: explorer.explorerId,
|
|
430
|
+
address,
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
let records: LedgerExplorerOperation[]
|
|
434
|
+
let nextPagingToken: string
|
|
435
|
+
|
|
436
|
+
if (limit === undefined) {
|
|
437
|
+
records = await fetchPaginatedOpsWithRetries(
|
|
438
|
+
{
|
|
439
|
+
...settings,
|
|
440
|
+
fromBlock,
|
|
441
|
+
batchSize: explorer.batchSize ?? DEFAULT_BATCH_SIZE,
|
|
442
|
+
},
|
|
443
|
+
null,
|
|
444
|
+
[],
|
|
445
|
+
explorer.retries ?? DEFAULT_RETRIES_API
|
|
446
|
+
)
|
|
447
|
+
nextPagingToken = NO_TOKEN // Ledger explorer fetches all pages internally
|
|
448
|
+
} else {
|
|
449
|
+
const page = await fetchLedgerOpsPage({
|
|
450
|
+
...settings,
|
|
451
|
+
fromBlock,
|
|
452
|
+
toBlock,
|
|
453
|
+
cursor: pagingToken,
|
|
454
|
+
limit,
|
|
455
|
+
order: order ?? 'desc',
|
|
456
|
+
retries: explorer.retries ?? DEFAULT_RETRIES_API,
|
|
457
|
+
})
|
|
458
|
+
records = page.operations
|
|
459
|
+
nextPagingToken = page.nextPagingToken
|
|
460
|
+
}
|
|
461
|
+
|
|
166
462
|
return {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
lastNftOperations,
|
|
170
|
-
lastInternalOperations,
|
|
171
|
-
nextPagingToken: NO_TOKEN, // Ledger explorer fetches all pages internally
|
|
463
|
+
...adaptLedgerRecords(config, currencyId, address, records),
|
|
464
|
+
nextPagingToken,
|
|
172
465
|
}
|
|
173
466
|
}
|
|
174
467
|
|
|
468
|
+
/**
|
|
469
|
+
* Discover which tokens an address holds by walking its ERC20 transfer events, one page at a
|
|
470
|
+
* time, without ever holding more than the current page in memory.
|
|
471
|
+
*
|
|
472
|
+
* Unlike `getOperations`, this walk loops instead of recursing over pages and drops each page
|
|
473
|
+
* once its events are folded into the running `Map<assetReference, AssetInfo>`, so its peak
|
|
474
|
+
* memory is O(distinct contracts), not O(history length). It never caps or truncates: it keeps
|
|
475
|
+
* walking until the explorer says there is no more, and a page that cannot be fetched after its
|
|
476
|
+
* own retry budget propagates rather than being skipped.
|
|
477
|
+
*/
|
|
478
|
+
export const discoverTokenAssets: ExplorerApi['discoverTokenAssets'] = async (
|
|
479
|
+
config,
|
|
480
|
+
currencyId,
|
|
481
|
+
address,
|
|
482
|
+
fromBlock
|
|
483
|
+
) => {
|
|
484
|
+
const { explorer } = config || /* istanbul ignore next */ {}
|
|
485
|
+
if (!isLedgerExplorerConfig(explorer)) {
|
|
486
|
+
throw new LedgerExplorerUsedIncorrectly(
|
|
487
|
+
`Ledger explorer used incorrectly with currency: ${currencyId}`
|
|
488
|
+
)
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// A smaller operator-configured batch size is honoured (it only shrinks the page); a larger
|
|
492
|
+
// one cannot defeat the memory bound the discovery walk relies on. A non-positive one is not
|
|
493
|
+
// honoured at all: it would make every page empty, end the walk immediately, and report an
|
|
494
|
+
// account as holding no token -- which the framework turns into a zero balance on every token
|
|
495
|
+
// it does hold.
|
|
496
|
+
const configuredBatchSize = explorer.batchSize
|
|
497
|
+
const batchSize =
|
|
498
|
+
configuredBatchSize !== undefined && configuredBatchSize > 0
|
|
499
|
+
? Math.min(configuredBatchSize, DISCOVERY_BATCH_SIZE)
|
|
500
|
+
: DISCOVERY_BATCH_SIZE
|
|
501
|
+
const params = {
|
|
502
|
+
explorerUri: config.ledgerExplorerUri,
|
|
503
|
+
clientVersion: config.ledgerClientVersion,
|
|
504
|
+
explorerId: explorer.explorerId,
|
|
505
|
+
address,
|
|
506
|
+
fromBlock: fromBlock ?? 0,
|
|
507
|
+
batchSize,
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const checksummedAddress = eip55.encode(address)
|
|
511
|
+
const assets = new Map<string, AssetInfo>()
|
|
512
|
+
let token: string | null = null
|
|
513
|
+
|
|
514
|
+
for (;;) {
|
|
515
|
+
const page = await requestOpsPageWithRetries(
|
|
516
|
+
params,
|
|
517
|
+
token,
|
|
518
|
+
explorer.retries ?? DEFAULT_RETRIES_API
|
|
519
|
+
)
|
|
520
|
+
|
|
521
|
+
for (const record of page.operations) {
|
|
522
|
+
for (const event of record.transfer_events) {
|
|
523
|
+
// Same party filter as the ERC20 adapter: an event contributes an asset only if the
|
|
524
|
+
// requested address is its `from` or `to`, never a third party the address merely
|
|
525
|
+
// co-occurred with in the same transaction.
|
|
526
|
+
const isParty =
|
|
527
|
+
safeEncodeEIP55(event.from) === checksummedAddress ||
|
|
528
|
+
safeEncodeEIP55(event.to) === checksummedAddress
|
|
529
|
+
if (!isParty) continue
|
|
530
|
+
|
|
531
|
+
const contract = eip55.encode(event.contract)
|
|
532
|
+
assets.set(contract, { type: 'erc20', assetReference: contract, assetOwner: address })
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// Stop on: a falsy token (normal end of history), an empty page that still carries a token
|
|
537
|
+
// (defends against a misbehaving explorer), or a token identical to the one just used (a
|
|
538
|
+
// cycling-cursor guard a plain recursion could not express).
|
|
539
|
+
if (!page.token || page.operations.length === 0 || page.token === token) break
|
|
540
|
+
token = page.token
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
return Array.from(assets.values())
|
|
544
|
+
}
|
|
545
|
+
|
|
175
546
|
const ledgerExplorerAPI: ExplorerApi = {
|
|
176
547
|
getOperations,
|
|
548
|
+
discoverTokenAssets,
|
|
177
549
|
}
|
|
178
550
|
|
|
179
551
|
export default ledgerExplorerAPI
|
|
@@ -18,8 +18,11 @@ export const getOperations: ExplorerApi['getOperations'] = async () => {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
export const discoverTokenAssets: ExplorerApi['discoverTokenAssets'] = async () => []
|
|
22
|
+
|
|
21
23
|
const noExplorerAPI: ExplorerApi = {
|
|
22
24
|
getOperations,
|
|
25
|
+
discoverTokenAssets,
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
export default noExplorerAPI
|
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import type { EvmConfigInfo } from '../../config'
|
|
5
|
-
import type {
|
|
5
|
+
import type {
|
|
6
|
+
AssetInfo,
|
|
7
|
+
MemoNotSupported,
|
|
8
|
+
Operation,
|
|
9
|
+
} from '@ledgerhq/coin-module-framework/api/types'
|
|
10
|
+
import type { Logger } from '@ledgerhq/coin-module-framework/config'
|
|
6
11
|
|
|
7
12
|
/** Constant representing no pagination token (end of pagination or first page) */
|
|
8
13
|
export const NO_TOKEN = ''
|
|
@@ -13,10 +18,11 @@ export type ExplorerApi = {
|
|
|
13
18
|
currencyId: string,
|
|
14
19
|
address: string,
|
|
15
20
|
fromBlock: number,
|
|
16
|
-
toBlock
|
|
17
|
-
pagingToken
|
|
18
|
-
limit
|
|
19
|
-
order
|
|
21
|
+
toBlock: number | undefined,
|
|
22
|
+
pagingToken: string | undefined,
|
|
23
|
+
limit: number | undefined,
|
|
24
|
+
order: 'asc' | 'desc' | undefined,
|
|
25
|
+
logger: Logger
|
|
20
26
|
) => Promise<{
|
|
21
27
|
lastCoinOperations: Array<Operation<MemoNotSupported>>
|
|
22
28
|
lastTokenOperations: Array<Operation<MemoNotSupported>>
|
|
@@ -24,6 +30,24 @@ export type ExplorerApi = {
|
|
|
24
30
|
lastInternalOperations: Array<Operation<MemoNotSupported>>
|
|
25
31
|
nextPagingToken: string
|
|
26
32
|
}>
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Discover the tokens an address holds, without accumulating its operation history.
|
|
36
|
+
*
|
|
37
|
+
* Returns the deduplicated set of assets the address has ever transferred, as of the current
|
|
38
|
+
* chain head (or resumed from `fromBlock` when the arm supports it) — never operations, never
|
|
39
|
+
* raw explorer records. This is the only input `getBalance` needs to know which contracts to
|
|
40
|
+
* read a balance for.
|
|
41
|
+
*
|
|
42
|
+
* @param fromBlock resume the walk from this height instead of from genesis; an arm that does
|
|
43
|
+
* not support resuming ignores it and walks the full history.
|
|
44
|
+
*/
|
|
45
|
+
discoverTokenAssets: (
|
|
46
|
+
config: EvmConfigInfo,
|
|
47
|
+
currencyId: string,
|
|
48
|
+
address: string,
|
|
49
|
+
fromBlock?: number
|
|
50
|
+
) => Promise<AssetInfo[]>
|
|
27
51
|
}
|
|
28
52
|
|
|
29
53
|
type ExplorerConfig = EvmConfigInfo['explorer']
|