@ledgerhq/coin-evm 16.5.4 → 16.6.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 +6 -0
- package/lib/api/index.d.ts +5 -2
- package/lib/api/index.d.ts.map +1 -1
- package/lib/api/index.js +2 -1
- package/lib/api/index.js.map +1 -1
- package/lib/errors.d.ts +17 -0
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +26 -1
- package/lib/errors.js.map +1 -1
- package/lib/logic/cip64Fees.d.ts +43 -0
- package/lib/logic/cip64Fees.d.ts.map +1 -0
- package/lib/logic/cip64Fees.js +139 -0
- package/lib/logic/cip64Fees.js.map +1 -0
- package/lib/logic/feeCollectorOperations.d.ts +2 -2
- package/lib/logic/feeCollectorOperations.js +2 -2
- package/lib/logic/getBlock.d.ts +2 -2
- package/lib/logic/getBlock.d.ts.map +1 -1
- package/lib/logic/getBlock.js +99 -19
- package/lib/logic/getBlock.js.map +1 -1
- package/lib/network/node/rpc.common.d.ts.map +1 -1
- package/lib/network/node/rpc.common.js +20 -0
- package/lib/network/node/rpc.common.js.map +1 -1
- package/lib/network/node/types.d.ts +6 -0
- package/lib/network/node/types.d.ts.map +1 -1
- package/lib/network/node/types.js.map +1 -1
- package/lib/network/node/withNodeSources.d.ts.map +1 -1
- package/lib/network/node/withNodeSources.js +2 -0
- package/lib/network/node/withNodeSources.js.map +1 -1
- package/lib-es/api/index.d.ts +5 -2
- package/lib-es/api/index.d.ts.map +1 -1
- package/lib-es/api/index.js +2 -1
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/errors.d.ts +17 -0
- package/lib-es/errors.d.ts.map +1 -1
- package/lib-es/errors.js +21 -0
- package/lib-es/errors.js.map +1 -1
- package/lib-es/logic/cip64Fees.d.ts +43 -0
- package/lib-es/logic/cip64Fees.d.ts.map +1 -0
- package/lib-es/logic/cip64Fees.js +134 -0
- package/lib-es/logic/cip64Fees.js.map +1 -0
- package/lib-es/logic/feeCollectorOperations.d.ts +2 -2
- package/lib-es/logic/feeCollectorOperations.js +2 -2
- package/lib-es/logic/getBlock.d.ts +2 -2
- package/lib-es/logic/getBlock.d.ts.map +1 -1
- package/lib-es/logic/getBlock.js +99 -19
- package/lib-es/logic/getBlock.js.map +1 -1
- package/lib-es/network/node/rpc.common.d.ts.map +1 -1
- package/lib-es/network/node/rpc.common.js +20 -0
- package/lib-es/network/node/rpc.common.js.map +1 -1
- package/lib-es/network/node/types.d.ts +6 -0
- package/lib-es/network/node/types.d.ts.map +1 -1
- package/lib-es/network/node/types.js.map +1 -1
- package/lib-es/network/node/withNodeSources.d.ts.map +1 -1
- package/lib-es/network/node/withNodeSources.js +2 -0
- package/lib-es/network/node/withNodeSources.js.map +1 -1
- package/package.json +2 -2
- package/src/api/index.ts +4 -1
- package/src/api/tests/index.celo.integ.test.ts +93 -0
- package/src/api/tests/index.flare.integ.test.ts +2 -1
- package/src/api/tests/index.test.ts +9 -0
- package/src/errors.ts +24 -0
- package/src/logic/cip64Fees.ts +193 -0
- package/src/logic/feeCollectorOperations.ts +2 -2
- package/src/logic/getBlock.ts +170 -30
- package/src/logic/tests/cip64Fees.test.ts +277 -0
- package/src/logic/tests/getBlock.test.ts +249 -2
- package/src/network/node/rpc.common.ts +29 -0
- package/src/network/node/types.ts +6 -0
- package/src/network/node/withNodeSources.ts +3 -0
- package/src/network/tests/node/rpc.test.ts +39 -0
- package/src/network/tests/node/withNodeSources.test.ts +18 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ERC20Transfer, NodeApi } from '../network/node/types'
|
|
5
|
+
import type { AssetInfo } from '@ledgerhq/coin-module-framework/api/index'
|
|
6
|
+
import {
|
|
7
|
+
FeeCurrencyUnavailableError,
|
|
8
|
+
UnexpectedFeeLogsError,
|
|
9
|
+
UnsupportedFeeCurrencyError,
|
|
10
|
+
} from '../errors'
|
|
11
|
+
|
|
12
|
+
/** EIP-2718 type of a Celo CIP-64 transaction, which can pay its fee in a token. */
|
|
13
|
+
export const CIP64_TX_TYPE = 0x7b
|
|
14
|
+
|
|
15
|
+
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* How a fee currency moves the fee, as token transfers around the transaction's own logs. Celo's
|
|
19
|
+
* op-geth debits the fee before the transaction runs and credits it after, so a receipt is
|
|
20
|
+
* `[debit][transaction][credits]`, also when the transaction fails. The credits are, in an order
|
|
21
|
+
* that depends on the fee currency, the refund to the payer, the tip with the L1 fee, and the
|
|
22
|
+
* base fee.
|
|
23
|
+
*
|
|
24
|
+
* - `holder`: the payer sends the fee to a holder address, which sends each credit.
|
|
25
|
+
* - `burnRemint`: the payer burns the fee, gets it minted back, then sends the tip and base fee.
|
|
26
|
+
* - `burnMint`: the payer burns the fee, and each credit is minted.
|
|
27
|
+
*/
|
|
28
|
+
type FeePattern = 'holder' | 'burnRemint' | 'burnMint'
|
|
29
|
+
|
|
30
|
+
/** Credits of every supported fee currency: refund, tip and base fee, each emitted also when 0. */
|
|
31
|
+
const CREDIT_COUNT = 3
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Fee currencies whose fee logs were checked on mainnet receipts, by lower-case fee currency
|
|
35
|
+
* address, with the lower-case token they move. Any other fee currency is rejected rather than
|
|
36
|
+
* guessed.
|
|
37
|
+
*/
|
|
38
|
+
const CELO_FEE_CURRENCIES: ReadonlyMap<string, { token: string; pattern: FeePattern }> = new Map([
|
|
39
|
+
// USD₮ through its adapter
|
|
40
|
+
[
|
|
41
|
+
'0x0e2a3e05bc9a16f5292a6170456a710cb89c6f72',
|
|
42
|
+
{ token: '0x48065fbbe25f71c9282ddf5e1cd6d6a887483d5e', pattern: 'holder' },
|
|
43
|
+
],
|
|
44
|
+
// USDC through its adapter
|
|
45
|
+
[
|
|
46
|
+
'0x2f25deb3848c207fc8e0c34035b3ba7fc157602b',
|
|
47
|
+
{ token: '0xceba9300f2b948710d2653dd7b07f33a8b32118c', pattern: 'burnRemint' },
|
|
48
|
+
],
|
|
49
|
+
// cUSD
|
|
50
|
+
[
|
|
51
|
+
'0x765de816845861e75a25fca122bb6898b8b1282a',
|
|
52
|
+
{ token: '0x765de816845861e75a25fca122bb6898b8b1282a', pattern: 'burnMint' },
|
|
53
|
+
],
|
|
54
|
+
// cEUR
|
|
55
|
+
[
|
|
56
|
+
'0xd8763cba276a3738e6de85b4b3bf5fded6d6ca73',
|
|
57
|
+
{ token: '0xd8763cba276a3738e6de85b4b3bf5fded6d6ca73', pattern: 'burnMint' },
|
|
58
|
+
],
|
|
59
|
+
])
|
|
60
|
+
|
|
61
|
+
/** The fee of a token-paid transaction, and the transfers of the transaction itself. */
|
|
62
|
+
export type TokenFee = {
|
|
63
|
+
fees: bigint
|
|
64
|
+
feesAsset: AssetInfo
|
|
65
|
+
transfers: ERC20Transfer[]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const same = (a: string, b: string): boolean => a.toLowerCase() === b.toLowerCase()
|
|
69
|
+
|
|
70
|
+
function matchesPattern(
|
|
71
|
+
pattern: FeePattern,
|
|
72
|
+
payer: string,
|
|
73
|
+
debit: ERC20Transfer,
|
|
74
|
+
credits: readonly ERC20Transfer[]
|
|
75
|
+
): boolean {
|
|
76
|
+
if (!same(debit.from, payer)) return false
|
|
77
|
+
switch (pattern) {
|
|
78
|
+
case 'holder':
|
|
79
|
+
return credits.every((credit) => same(credit.from, debit.to))
|
|
80
|
+
case 'burnRemint': {
|
|
81
|
+
const [remint, ...payments] = credits
|
|
82
|
+
return (
|
|
83
|
+
same(debit.to, ZERO_ADDRESS) &&
|
|
84
|
+
remint !== undefined &&
|
|
85
|
+
same(remint.from, ZERO_ADDRESS) &&
|
|
86
|
+
same(remint.to, payer) &&
|
|
87
|
+
remint.value === debit.value &&
|
|
88
|
+
payments.every((payment) => same(payment.from, payer))
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
case 'burnMint':
|
|
92
|
+
return (
|
|
93
|
+
same(debit.to, ZERO_ADDRESS) && credits.every((credit) => same(credit.from, ZERO_ADDRESS))
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Splits the token transfers of a CIP-64 transaction into its fee and its own transfers.
|
|
100
|
+
*
|
|
101
|
+
* The fee logs are found by position: the first transfer is the debit and the last
|
|
102
|
+
* {@link CREDIT_COUNT} are the credits, each checked against the pattern of the fee currency.
|
|
103
|
+
* Matching on position, not on recipients, keeps a transfer of the transaction to the fee
|
|
104
|
+
* recipients. The fee is what the payer sent minus what it got back, in the token's own units:
|
|
105
|
+
* a value converted from the adapter's 18 decimals would not be the amount debited.
|
|
106
|
+
*
|
|
107
|
+
* @param feeCurrency `feeCurrency` of the transaction
|
|
108
|
+
* @param payer sender of the transaction
|
|
109
|
+
* @param transfers the token transfers of the receipt, in log order
|
|
110
|
+
* @throws UnsupportedFeeCurrencyError when the fee currency is not supported
|
|
111
|
+
* @throws UnexpectedFeeLogsError when the fee logs do not match the fee currency
|
|
112
|
+
*/
|
|
113
|
+
export function splitTokenFee(
|
|
114
|
+
feeCurrency: string,
|
|
115
|
+
payer: string,
|
|
116
|
+
transfers: readonly ERC20Transfer[]
|
|
117
|
+
): TokenFee {
|
|
118
|
+
const currency = CELO_FEE_CURRENCIES.get(feeCurrency.toLowerCase())
|
|
119
|
+
if (currency === undefined) {
|
|
120
|
+
throw new UnsupportedFeeCurrencyError(`Unsupported Celo fee currency ${feeCurrency}`, {
|
|
121
|
+
feeCurrency,
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const creditStart = transfers.length - CREDIT_COUNT
|
|
126
|
+
const [debit] = transfers
|
|
127
|
+
const credits = transfers.slice(creditStart)
|
|
128
|
+
if (
|
|
129
|
+
debit === undefined ||
|
|
130
|
+
creditStart < 1 ||
|
|
131
|
+
![debit, ...credits].every((transfer) => same(transfer.asset.assetReference, currency.token)) ||
|
|
132
|
+
!matchesPattern(currency.pattern, payer, debit, credits)
|
|
133
|
+
) {
|
|
134
|
+
throw new UnexpectedFeeLogsError(`Unexpected fee logs for Celo fee currency ${feeCurrency}`, {
|
|
135
|
+
feeCurrency,
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let fees = 0n
|
|
140
|
+
for (const transfer of [debit, ...credits]) {
|
|
141
|
+
const value = BigInt(transfer.value)
|
|
142
|
+
if (same(transfer.from, payer)) fees += value
|
|
143
|
+
if (same(transfer.to, payer)) fees -= value
|
|
144
|
+
}
|
|
145
|
+
if (fees < 0n) {
|
|
146
|
+
throw new UnexpectedFeeLogsError(`Negative fee for Celo fee currency ${feeCurrency}`, {
|
|
147
|
+
feeCurrency,
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
fees,
|
|
153
|
+
feesAsset: { type: 'erc20', assetReference: debit.asset.assetReference },
|
|
154
|
+
transfers: transfers.slice(1, creditStart),
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Reads the token fee of a transaction, or `undefined` when it pays in the native coin. */
|
|
159
|
+
export type ReadTokenFee = (tx: {
|
|
160
|
+
hash: string
|
|
161
|
+
from: string
|
|
162
|
+
type?: number | undefined
|
|
163
|
+
erc20Transfers: readonly ERC20Transfer[]
|
|
164
|
+
}) => Promise<TokenFee | undefined>
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Reads the token fee of the CIP-64 transactions of a Celo block. The fee currencies of the block
|
|
168
|
+
* are fetched once, at the first transaction that is or may be CIP-64: a receipt without a type
|
|
169
|
+
* does not prove the transaction pays in the native coin.
|
|
170
|
+
*
|
|
171
|
+
* @param nodeApi node of the network
|
|
172
|
+
* @param blockHeight height of the block
|
|
173
|
+
*/
|
|
174
|
+
export function createCeloTokenFeeReader(nodeApi: NodeApi, blockHeight: number): ReadTokenFee {
|
|
175
|
+
let feeCurrencies: Promise<Map<string, string | null>> | undefined
|
|
176
|
+
return async ({ hash, from, type, erc20Transfers }) => {
|
|
177
|
+
if (type !== undefined && type !== CIP64_TX_TYPE) return undefined
|
|
178
|
+
if (nodeApi.getFeeCurrencies === undefined) {
|
|
179
|
+
throw new FeeCurrencyUnavailableError(
|
|
180
|
+
'The node cannot report the fee currency of a transaction',
|
|
181
|
+
{ hash }
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
feeCurrencies ??= nodeApi.getFeeCurrencies(blockHeight)
|
|
185
|
+
const feeCurrency = (await feeCurrencies).get(hash.toLowerCase())
|
|
186
|
+
if (feeCurrency === undefined) {
|
|
187
|
+
throw new FeeCurrencyUnavailableError(`The node block has no transaction ${hash}`, {
|
|
188
|
+
hash,
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
return feeCurrency === null ? undefined : splitTokenFee(feeCurrency, from, erc20Transfers)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -193,8 +193,8 @@ export async function findTransactionsPaidByOthers(
|
|
|
193
193
|
* instead, as ADR-016 allows.
|
|
194
194
|
*
|
|
195
195
|
* The fee also moves as base-token transfers (zkSync `0x800A`), which stay as token operations,
|
|
196
|
-
* as `getBlock` keeps them (BACK-11036). ADR-068
|
|
197
|
-
* asset
|
|
196
|
+
* as `getBlock` keeps them without its `feesAsset` option (BACK-11036). ADR-068 removes fee
|
|
197
|
+
* movements in every asset behind that option; this function does not follow it yet.
|
|
198
198
|
*
|
|
199
199
|
* @param operations operations of the account, as listOperations returns them
|
|
200
200
|
* @param address the account
|
package/src/logic/getBlock.ts
CHANGED
|
@@ -4,12 +4,14 @@
|
|
|
4
4
|
import type { ChainId, EvmContext } from '../config'
|
|
5
5
|
import type { BlockReceiptInfo, PrefetchedBlockTransaction } from '../network/node/types'
|
|
6
6
|
import type { EvmStakingOperationType } from '../types/staking'
|
|
7
|
+
import type { ReadTokenFee, TokenFee } from './cip64Fees'
|
|
7
8
|
import type { InternalTxsByHash } from './internalTransactionsFetcher'
|
|
8
9
|
import type {
|
|
9
10
|
Block,
|
|
10
11
|
BlockInfo,
|
|
11
12
|
BlockOperation,
|
|
12
13
|
BlockTransaction,
|
|
14
|
+
GetBlockOptions,
|
|
13
15
|
TransferBlockOperation,
|
|
14
16
|
} from '@ledgerhq/coin-module-framework/api/index'
|
|
15
17
|
import type { Logger } from '@ledgerhq/coin-module-framework/config'
|
|
@@ -20,6 +22,7 @@ import { UnsupportedRpcMethodError } from '../errors'
|
|
|
20
22
|
import { buildSmartContractDetails, safeEncodeEIP55 } from '../logic-public'
|
|
21
23
|
import { getNodeApi } from '../network/node'
|
|
22
24
|
import { detectEvmStakingOperationType } from '../staking/detectOperationType'
|
|
25
|
+
import { createCeloTokenFeeReader } from './cip64Fees'
|
|
23
26
|
import {
|
|
24
27
|
createInternalTransactionsFetcher,
|
|
25
28
|
dropFeeCollectorTransfers,
|
|
@@ -27,7 +30,11 @@ import {
|
|
|
27
30
|
} from './internalTransactionsFetcher'
|
|
28
31
|
import { dropRootTraceDuplicates } from './rootTraceDedup'
|
|
29
32
|
|
|
30
|
-
export async function getBlock(
|
|
33
|
+
export async function getBlock(
|
|
34
|
+
context: EvmContext,
|
|
35
|
+
height: number,
|
|
36
|
+
options?: GetBlockOptions
|
|
37
|
+
): Promise<Block> {
|
|
31
38
|
// Note: to use RPC calls efficiently, the strategy here is:
|
|
32
39
|
// - fetch the block + transactions in one call (using prefetchTxs=true)
|
|
33
40
|
// - fetch transaction receipts in one call using eth_getBlockReceipts
|
|
@@ -64,6 +71,11 @@ export async function getBlock(context: EvmContext, height: number): Promise<Blo
|
|
|
64
71
|
const transactionHashes =
|
|
65
72
|
result.transactionHashes || result.transactions?.map((tx) => tx.hash) || []
|
|
66
73
|
|
|
74
|
+
const readTokenFee =
|
|
75
|
+
options?.feesAsset && config.chainId === CHAIN_ID.CELO
|
|
76
|
+
? createCeloTokenFeeReader(nodeApi, result.height)
|
|
77
|
+
: undefined
|
|
78
|
+
|
|
67
79
|
// Internal transactions only exist inside transactions, so a block with none has nothing to
|
|
68
80
|
// merge. Skipping the fetch avoids a request for a guaranteed-empty answer — on the `explorer`
|
|
69
81
|
// source that is one metered `txlistinternal` call per empty block.
|
|
@@ -75,7 +87,7 @@ export async function getBlock(context: EvmContext, height: number): Promise<Blo
|
|
|
75
87
|
nodeApi,
|
|
76
88
|
result.height,
|
|
77
89
|
context.logger,
|
|
78
|
-
result.transactions
|
|
90
|
+
{ prefetchedTransactions: result.transactions, readTokenFee }
|
|
79
91
|
),
|
|
80
92
|
transactionHashes.length === 0
|
|
81
93
|
? Promise.resolve<InternalTxsByHash>(new Map())
|
|
@@ -95,13 +107,21 @@ export async function getBlock(context: EvmContext, height: number): Promise<Blo
|
|
|
95
107
|
const transactions = transactionsResult.value
|
|
96
108
|
const internalTxs = internalTxsResult.value
|
|
97
109
|
|
|
110
|
+
const feeCollectorContracts = config.feeCollectorContracts ?? []
|
|
111
|
+
const feeCollectorTokens = config.feeCollectorTokens ?? []
|
|
112
|
+
const [paidTransactions, unpaidInternalTxs] = attributeFeeCollectorTransfers(
|
|
113
|
+
options?.feesAsset
|
|
114
|
+
? attributeFeeCollectorTokenPayers(transactions, feeCollectorContracts, feeCollectorTokens)
|
|
115
|
+
: transactions,
|
|
116
|
+
internalTxs,
|
|
117
|
+
feeCollectorContracts,
|
|
118
|
+
context.logger
|
|
119
|
+
)
|
|
98
120
|
const mergedTransactions = mergeInternalTransactions(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
context.logger
|
|
104
|
-
)
|
|
121
|
+
options?.feesAsset
|
|
122
|
+
? dropFeeCollectorTokenTransfers(paidTransactions, feeCollectorContracts, feeCollectorTokens)
|
|
123
|
+
: paidTransactions,
|
|
124
|
+
unpaidInternalTxs
|
|
105
125
|
)
|
|
106
126
|
|
|
107
127
|
return {
|
|
@@ -145,6 +165,86 @@ function attributeFeeCollectorTransfers(
|
|
|
145
165
|
]
|
|
146
166
|
}
|
|
147
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Matches the transfers of one of the network's `feeCollectorTokens` (the zkSync base token),
|
|
170
|
+
* which carry the native coin to and from its `feeCollectorContracts`.
|
|
171
|
+
*/
|
|
172
|
+
function feeCollectorTokenMatchers(
|
|
173
|
+
feeCollectorContracts: readonly string[],
|
|
174
|
+
feeCollectorTokens: readonly string[]
|
|
175
|
+
): {
|
|
176
|
+
isCollector: (address: string) => boolean
|
|
177
|
+
isTokenTransfer: (op: BlockOperation) => op is TransferBlockOperation & { peer: string }
|
|
178
|
+
} {
|
|
179
|
+
const collectors = new Set(feeCollectorContracts.map((contract) => contract.toLowerCase()))
|
|
180
|
+
const tokens = new Set(feeCollectorTokens.map((token) => token.toLowerCase()))
|
|
181
|
+
return {
|
|
182
|
+
isCollector: (address) => collectors.has(address.toLowerCase()),
|
|
183
|
+
isTokenTransfer: (op): op is TransferBlockOperation & { peer: string } =>
|
|
184
|
+
op.type === 'transfer' &&
|
|
185
|
+
op.peer !== undefined &&
|
|
186
|
+
'assetReference' in op.asset &&
|
|
187
|
+
op.asset.assetReference !== undefined &&
|
|
188
|
+
tokens.has(op.asset.assetReference.toLowerCase()),
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Gives each transaction the one account that prepays a fee collector in a `feeCollectorTokens`
|
|
194
|
+
* transfer as its fee payer. The internal transfers that name a zkSync paymaster can be missing or
|
|
195
|
+
* hold only its refund, so the payer is read from the receipt before any fee transfer is dropped.
|
|
196
|
+
*/
|
|
197
|
+
function attributeFeeCollectorTokenPayers(
|
|
198
|
+
transactions: BlockTransaction[],
|
|
199
|
+
feeCollectorContracts: readonly string[],
|
|
200
|
+
feeCollectorTokens: readonly string[]
|
|
201
|
+
): BlockTransaction[] {
|
|
202
|
+
if (feeCollectorContracts.length === 0 || feeCollectorTokens.length === 0) return transactions
|
|
203
|
+
|
|
204
|
+
const { isCollector, isTokenTransfer } = feeCollectorTokenMatchers(
|
|
205
|
+
feeCollectorContracts,
|
|
206
|
+
feeCollectorTokens
|
|
207
|
+
)
|
|
208
|
+
return transactions.map((tx) => {
|
|
209
|
+
const prepayers = new Map(
|
|
210
|
+
tx.operations
|
|
211
|
+
.filter(isTokenTransfer)
|
|
212
|
+
.filter((op) => op.amount < 0n && isCollector(op.peer) && !isCollector(op.address))
|
|
213
|
+
.map((op) => [op.address.toLowerCase(), op.address])
|
|
214
|
+
)
|
|
215
|
+
return prepayers.size === 1 ? { ...tx, feesPayer: [...prepayers.values()][0] } : tx
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Drops the `feeCollectorTokens` transfers between each transaction's fee payer and the network's
|
|
221
|
+
* `feeCollectorContracts`: the transaction `fees` already holds them, and the operations of a
|
|
222
|
+
* transaction never include its fee movement when the caller asks for the fee asset. Transfers to
|
|
223
|
+
* or from other accounts stay.
|
|
224
|
+
*/
|
|
225
|
+
function dropFeeCollectorTokenTransfers(
|
|
226
|
+
transactions: BlockTransaction[],
|
|
227
|
+
feeCollectorContracts: readonly string[],
|
|
228
|
+
feeCollectorTokens: readonly string[]
|
|
229
|
+
): BlockTransaction[] {
|
|
230
|
+
if (feeCollectorContracts.length === 0 || feeCollectorTokens.length === 0) return transactions
|
|
231
|
+
|
|
232
|
+
const { isCollector, isTokenTransfer } = feeCollectorTokenMatchers(
|
|
233
|
+
feeCollectorContracts,
|
|
234
|
+
feeCollectorTokens
|
|
235
|
+
)
|
|
236
|
+
return transactions.map((tx) => {
|
|
237
|
+
const payer = tx.feesPayer?.toLowerCase()
|
|
238
|
+
if (payer === undefined) return tx
|
|
239
|
+
const isFeeTransfer = (op: BlockOperation): boolean =>
|
|
240
|
+
isTokenTransfer(op) &&
|
|
241
|
+
((op.address.toLowerCase() === payer && isCollector(op.peer)) ||
|
|
242
|
+
(isCollector(op.address) && op.peer.toLowerCase() === payer))
|
|
243
|
+
const operations = tx.operations.filter((op) => !isFeeTransfer(op))
|
|
244
|
+
return operations.length === tx.operations.length ? tx : { ...tx, operations }
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
148
248
|
/**
|
|
149
249
|
* Merges internal transaction operations into block transactions by matching tx hash.
|
|
150
250
|
*
|
|
@@ -232,7 +332,13 @@ async function getTransactionsFromNode(
|
|
|
232
332
|
nodeApi: ReturnType<typeof getNodeApi>,
|
|
233
333
|
blockHeight: number,
|
|
234
334
|
logger: Logger,
|
|
235
|
-
|
|
335
|
+
{
|
|
336
|
+
prefetchedTransactions,
|
|
337
|
+
readTokenFee,
|
|
338
|
+
}: {
|
|
339
|
+
prefetchedTransactions?: PrefetchedBlockTransaction[] | undefined
|
|
340
|
+
readTokenFee?: ReadTokenFee | undefined
|
|
341
|
+
} = {}
|
|
236
342
|
): Promise<BlockTransaction[]> {
|
|
237
343
|
if (transactionHashes.length === 0) return []
|
|
238
344
|
|
|
@@ -242,7 +348,8 @@ async function getTransactionsFromNode(
|
|
|
242
348
|
blockHeight,
|
|
243
349
|
prefetchedTransactions,
|
|
244
350
|
nodeApi,
|
|
245
|
-
logger
|
|
351
|
+
logger,
|
|
352
|
+
readTokenFee
|
|
246
353
|
)
|
|
247
354
|
if (bulkTransactions) return bulkTransactions
|
|
248
355
|
|
|
@@ -257,7 +364,7 @@ async function getTransactionsFromNode(
|
|
|
257
364
|
const transactionResults = await promiseAllBatched(
|
|
258
365
|
MAX_CONCURRENCY,
|
|
259
366
|
transactionHashes,
|
|
260
|
-
(txHash: string) => getTransactionFromHash(chainId, txHash, nodeApi, logger)
|
|
367
|
+
(txHash: string) => getTransactionFromHash(chainId, txHash, nodeApi, logger, readTokenFee)
|
|
261
368
|
)
|
|
262
369
|
|
|
263
370
|
return transactionResults.filter((tx): tx is BlockTransaction => tx !== null)
|
|
@@ -269,7 +376,8 @@ async function getTransactionsFromPrefetchedData(
|
|
|
269
376
|
blockHeight: number,
|
|
270
377
|
prefetchedTransactions: PrefetchedBlockTransaction[] | undefined,
|
|
271
378
|
nodeApi: ReturnType<typeof getNodeApi>,
|
|
272
|
-
logger: Logger
|
|
379
|
+
logger: Logger,
|
|
380
|
+
readTokenFee?: ReadTokenFee
|
|
273
381
|
): Promise<BlockTransaction[] | null> {
|
|
274
382
|
if (!prefetchedTransactions || prefetchedTransactions.length === 0 || !nodeApi.getBlockReceipts)
|
|
275
383
|
return null
|
|
@@ -278,23 +386,39 @@ async function getTransactionsFromPrefetchedData(
|
|
|
278
386
|
const receipts = await nodeApi.getBlockReceipts(blockHeight)
|
|
279
387
|
const receiptsByHash = new Map(receipts.map((receipt) => [receipt.hash, receipt]))
|
|
280
388
|
|
|
281
|
-
const
|
|
389
|
+
const withReceipts: { tx: PrefetchedBlockTransaction; receipt: BlockReceiptInfo }[] = []
|
|
282
390
|
for (const tx of prefetchedTransactions) {
|
|
283
391
|
const receipt = receiptsByHash.get(tx.hash)
|
|
284
392
|
if (!receipt) return null
|
|
285
|
-
|
|
286
|
-
transactions.push({
|
|
287
|
-
...blockTx,
|
|
288
|
-
operations: applyChainSpecificCorrections(
|
|
289
|
-
chainId,
|
|
290
|
-
blockTx.operations,
|
|
291
|
-
receipt.type,
|
|
292
|
-
logger
|
|
293
|
-
),
|
|
294
|
-
})
|
|
393
|
+
withReceipts.push({ tx, receipt })
|
|
295
394
|
}
|
|
296
395
|
|
|
297
|
-
return
|
|
396
|
+
return await Promise.all(
|
|
397
|
+
withReceipts.map(async ({ tx, receipt }) => {
|
|
398
|
+
const tokenFee = await readTokenFee?.({
|
|
399
|
+
hash: tx.hash,
|
|
400
|
+
from: tx.from,
|
|
401
|
+
type: receipt.type,
|
|
402
|
+
erc20Transfers: receipt.erc20Transfers,
|
|
403
|
+
})
|
|
404
|
+
const blockTx = prefetchedTransactionToBlockTransaction(
|
|
405
|
+
chainId,
|
|
406
|
+
tx,
|
|
407
|
+
receipt,
|
|
408
|
+
logger,
|
|
409
|
+
tokenFee
|
|
410
|
+
)
|
|
411
|
+
return {
|
|
412
|
+
...blockTx,
|
|
413
|
+
operations: applyChainSpecificCorrections(
|
|
414
|
+
chainId,
|
|
415
|
+
blockTx.operations,
|
|
416
|
+
receipt.type,
|
|
417
|
+
logger
|
|
418
|
+
),
|
|
419
|
+
}
|
|
420
|
+
})
|
|
421
|
+
)
|
|
298
422
|
} catch (error) {
|
|
299
423
|
if (!(error instanceof UnsupportedRpcMethodError) || error.method !== 'eth_getBlockReceipts')
|
|
300
424
|
throw error
|
|
@@ -312,7 +436,8 @@ function prefetchedTransactionToBlockTransaction(
|
|
|
312
436
|
chainId: ChainId,
|
|
313
437
|
tx: PrefetchedBlockTransaction,
|
|
314
438
|
receipt: BlockReceiptInfo,
|
|
315
|
-
logger: Logger
|
|
439
|
+
logger: Logger,
|
|
440
|
+
tokenFee: TokenFee | undefined
|
|
316
441
|
): BlockTransaction {
|
|
317
442
|
const failed = receipt.status === 0
|
|
318
443
|
// Some EVM chains (e.g. Cronos) omit `gasPrice` from transaction
|
|
@@ -326,7 +451,7 @@ function prefetchedTransactionToBlockTransaction(
|
|
|
326
451
|
from: tx.from,
|
|
327
452
|
to: tx.to,
|
|
328
453
|
value: tx.value,
|
|
329
|
-
erc20Transfers: receipt.erc20Transfers,
|
|
454
|
+
erc20Transfers: tokenFee?.transfers ?? receipt.erc20Transfers,
|
|
330
455
|
},
|
|
331
456
|
logger
|
|
332
457
|
)
|
|
@@ -343,25 +468,40 @@ function prefetchedTransactionToBlockTransaction(
|
|
|
343
468
|
hash: tx.hash,
|
|
344
469
|
failed,
|
|
345
470
|
operations,
|
|
346
|
-
fees,
|
|
471
|
+
...withTokenFee(fees, tokenFee),
|
|
347
472
|
feesPayer: tx.from,
|
|
348
473
|
details,
|
|
349
474
|
}
|
|
350
475
|
}
|
|
351
476
|
|
|
477
|
+
/** The fee fields of a transaction: its token fee when it has one, else its native fee. */
|
|
478
|
+
function withTokenFee(
|
|
479
|
+
nativeFees: bigint,
|
|
480
|
+
tokenFee: TokenFee | undefined
|
|
481
|
+
): Pick<BlockTransaction, 'fees' | 'feesAsset'> {
|
|
482
|
+
return tokenFee === undefined
|
|
483
|
+
? { fees: nativeFees }
|
|
484
|
+
: { fees: tokenFee.fees, feesAsset: tokenFee.feesAsset }
|
|
485
|
+
}
|
|
486
|
+
|
|
352
487
|
async function getTransactionFromHash(
|
|
353
488
|
chainId: ChainId,
|
|
354
489
|
txHash: string,
|
|
355
490
|
nodeApi: ReturnType<typeof getNodeApi>,
|
|
356
|
-
logger: Logger
|
|
491
|
+
logger: Logger,
|
|
492
|
+
readTokenFee: ReadTokenFee | undefined
|
|
357
493
|
): Promise<BlockTransaction | null> {
|
|
358
494
|
const txInfo = await nodeApi.getTransaction(txHash)
|
|
495
|
+
const tokenFee = await readTokenFee?.(txInfo)
|
|
359
496
|
|
|
360
497
|
const failed = txInfo.status === 0
|
|
361
498
|
const fees =
|
|
362
499
|
BigInt(txInfo.gasUsed) * BigInt(txInfo.gasPrice) + (txInfo.l1Fee ? BigInt(txInfo.l1Fee) : 0n)
|
|
363
500
|
|
|
364
|
-
const rawOperations = rpcTransactionToBlockOperations(
|
|
501
|
+
const rawOperations = rpcTransactionToBlockOperations(
|
|
502
|
+
tokenFee === undefined ? txInfo : { ...txInfo, erc20Transfers: tokenFee.transfers },
|
|
503
|
+
logger
|
|
504
|
+
)
|
|
365
505
|
const operations = applyChainSpecificCorrections(chainId, rawOperations, txInfo.type, logger)
|
|
366
506
|
|
|
367
507
|
const details = buildTransactionDetails(
|
|
@@ -376,7 +516,7 @@ async function getTransactionFromHash(
|
|
|
376
516
|
hash: txHash,
|
|
377
517
|
failed,
|
|
378
518
|
operations,
|
|
379
|
-
fees,
|
|
519
|
+
...withTokenFee(fees, tokenFee),
|
|
380
520
|
feesPayer: txInfo.from,
|
|
381
521
|
details,
|
|
382
522
|
}
|