@ledgerhq/coin-evm 16.5.4 → 16.8.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 +22 -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/staking/contracts.d.ts.map +1 -1
- package/lib/staking/contracts.js +7 -0
- package/lib/staking/contracts.js.map +1 -1
- package/lib/staking/validators/index.d.ts.map +1 -1
- package/lib/staking/validators/index.js +36 -1
- package/lib/staking/validators/index.js.map +1 -1
- package/lib/staking/validators/monad.d.ts +1 -0
- package/lib/staking/validators/monad.d.ts.map +1 -1
- package/lib/staking/validators/monad.js +103 -6
- package/lib/staking/validators/monad.js.map +1 -1
- package/lib/staking/validators/sei.d.ts.map +1 -1
- package/lib/staking/validators/sei.js +46 -14
- package/lib/staking/validators/sei.js.map +1 -1
- package/lib/staking/validators/somnia.d.ts.map +1 -1
- package/lib/staking/validators/somnia.js +31 -2
- package/lib/staking/validators/somnia.js.map +1 -1
- package/lib/staking/validators/zero_gravity.d.ts.map +1 -1
- package/lib/staking/validators/zero_gravity.js +14 -2
- package/lib/staking/validators/zero_gravity.js.map +1 -1
- package/lib/types/staking.d.ts +10 -0
- package/lib/types/staking.d.ts.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/lib-es/staking/contracts.d.ts.map +1 -1
- package/lib-es/staking/contracts.js +7 -0
- package/lib-es/staking/contracts.js.map +1 -1
- package/lib-es/staking/validators/index.d.ts.map +1 -1
- package/lib-es/staking/validators/index.js +3 -1
- package/lib-es/staking/validators/index.js.map +1 -1
- package/lib-es/staking/validators/monad.d.ts +1 -0
- package/lib-es/staking/validators/monad.d.ts.map +1 -1
- package/lib-es/staking/validators/monad.js +101 -5
- package/lib-es/staking/validators/monad.js.map +1 -1
- package/lib-es/staking/validators/sei.d.ts.map +1 -1
- package/lib-es/staking/validators/sei.js +46 -14
- package/lib-es/staking/validators/sei.js.map +1 -1
- package/lib-es/staking/validators/somnia.d.ts.map +1 -1
- package/lib-es/staking/validators/somnia.js +31 -2
- package/lib-es/staking/validators/somnia.js.map +1 -1
- package/lib-es/staking/validators/zero_gravity.d.ts.map +1 -1
- package/lib-es/staking/validators/zero_gravity.js +14 -2
- package/lib-es/staking/validators/zero_gravity.js.map +1 -1
- package/lib-es/types/staking.d.ts +10 -0
- package/lib-es/types/staking.d.ts.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
- package/src/staking/contracts.ts +7 -0
- package/src/staking/validators/index.ts +4 -1
- package/src/staking/validators/monad.test.ts +175 -44
- package/src/staking/validators/monad.ts +156 -4
- package/src/staking/validators/sei.test.ts +93 -0
- package/src/staking/validators/sei.ts +56 -14
- package/src/staking/validators/somnia.test.ts +44 -2
- package/src/staking/validators/somnia.ts +32 -2
- package/src/staking/validators/zero_gravity.test.ts +25 -0
- package/src/staking/validators/zero_gravity.ts +17 -2
- package/src/staking/validators.integ.test.ts +35 -4
- package/src/staking/validators.test.ts +34 -22
- package/src/types/staking.ts +10 -0
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
|
}
|
|
@@ -0,0 +1,277 @@
|
|
|
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 {
|
|
6
|
+
FeeCurrencyUnavailableError,
|
|
7
|
+
UnexpectedFeeLogsError,
|
|
8
|
+
UnsupportedFeeCurrencyError,
|
|
9
|
+
} from '../../errors'
|
|
10
|
+
import { mockNodeApi } from '../../network/tests/node/node.fixtures'
|
|
11
|
+
import { createCeloTokenFeeReader, splitTokenFee } from '../cip64Fees'
|
|
12
|
+
|
|
13
|
+
const USDT = '0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e'
|
|
14
|
+
const USDT_ADAPTER = '0x0E2A3e05bc9A16F5292A6170456A710cb89C6f72'
|
|
15
|
+
const USDT_HOLDER = '0x000000000000000000000000000000000Ce106A5'
|
|
16
|
+
const USDC = '0xcebA9300f2b948710d2653dD7B07f33A8B32118C'
|
|
17
|
+
const USDC_ADAPTER = '0x2F25deB3848C207fc8E0c34035B3Ba7fC157602B'
|
|
18
|
+
const CUSD = '0x765DE816845861e75A25fCA122bb6898B8B1282a'
|
|
19
|
+
const CEUR = '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73'
|
|
20
|
+
const ZERO = '0x0000000000000000000000000000000000000000'
|
|
21
|
+
const SEQUENCER_VAULT = '0x4200000000000000000000000000000000000011'
|
|
22
|
+
const FEE_HANDLER = '0xcD437749E43A154C07F3553504c68fBfD56B8778'
|
|
23
|
+
const PAYER = '0x5362C723476b46a1e65D54E667C00e46b46F6f12'
|
|
24
|
+
const OTHER = '0xfb4863FC2FE52A8131696e4a3a86676cff40A806'
|
|
25
|
+
|
|
26
|
+
function transfer(token: string, from: string, to: string, value: bigint): ERC20Transfer {
|
|
27
|
+
return { asset: { type: 'erc20', assetReference: token }, from, to, value: value.toString() }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Fee logs of tx 0x45e41cf20fca9b079f8b23510bb9637d14df647cb6d25d85157924cd27df09d7 (block 80494727)
|
|
31
|
+
const usdtDebit = transfer(USDT, PAYER, USDT_HOLDER, 4252n)
|
|
32
|
+
const usdtCredits = [
|
|
33
|
+
transfer(USDT, USDT_HOLDER, SEQUENCER_VAULT, 0n),
|
|
34
|
+
transfer(USDT, USDT_HOLDER, FEE_HANDLER, 3176n),
|
|
35
|
+
transfer(USDT, USDT_HOLDER, PAYER, 1076n),
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
// Fee logs of tx 0xf43732d1ca9d43dd6515708669e6083010672aed8a61de4c8d07164c7e3d400d
|
|
39
|
+
const usdcDebit = transfer(USDC, PAYER, ZERO, 5020n)
|
|
40
|
+
const usdcCredits = [
|
|
41
|
+
transfer(USDC, ZERO, PAYER, 5020n),
|
|
42
|
+
transfer(USDC, PAYER, SEQUENCER_VAULT, 0n),
|
|
43
|
+
transfer(USDC, PAYER, FEE_HANDLER, 4072n),
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
// Fee logs of tx 0xa92de9a2b85e285d2bf1c1317386e012034ddfbbd84739e5b5f46fe6c252e748
|
|
47
|
+
const cusdDebit = transfer(CUSD, PAYER, ZERO, 2831373668469528n)
|
|
48
|
+
const cusdCredits = [
|
|
49
|
+
transfer(CUSD, ZERO, PAYER, 576015444067518n),
|
|
50
|
+
transfer(CUSD, ZERO, SEQUENCER_VAULT, 12404402010n),
|
|
51
|
+
transfer(CUSD, ZERO, FEE_HANDLER, 2255345820000000n),
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
const userTransfer = transfer(USDT, OTHER, PAYER, 5800n)
|
|
55
|
+
|
|
56
|
+
describe('splitTokenFee', () => {
|
|
57
|
+
it('reads the net USD₮ debit and keeps the transfers of the transaction', () => {
|
|
58
|
+
expect(splitTokenFee(USDT_ADAPTER, PAYER, [usdtDebit, userTransfer, ...usdtCredits])).toEqual({
|
|
59
|
+
fees: 3176n,
|
|
60
|
+
feesAsset: { type: 'erc20', assetReference: USDT },
|
|
61
|
+
transfers: [userTransfer],
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('reads the USDC fee from the burn, the re-mint and the payments', () => {
|
|
66
|
+
expect(splitTokenFee(USDC_ADAPTER, PAYER, [usdcDebit, userTransfer, ...usdcCredits])).toEqual({
|
|
67
|
+
fees: 4072n,
|
|
68
|
+
feesAsset: { type: 'erc20', assetReference: USDC },
|
|
69
|
+
transfers: [userTransfer],
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('reads a Mento fee from the burn and the mints', () => {
|
|
74
|
+
expect(splitTokenFee(CUSD, PAYER, [cusdDebit, ...cusdCredits])).toEqual({
|
|
75
|
+
fees: 12404402010n + 2255345820000000n,
|
|
76
|
+
feesAsset: { type: 'erc20', assetReference: CUSD },
|
|
77
|
+
transfers: [],
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('reads a cEUR fee from a transaction whose only effect is its fee', () => {
|
|
82
|
+
// Fee logs of tx 0x6b455f0193606be12273669e67edebf8765de56e4472d7aef86b51157a889797
|
|
83
|
+
const transfers = [
|
|
84
|
+
transfer(CEUR, PAYER, ZERO, 4330420086600000n),
|
|
85
|
+
transfer(CEUR, ZERO, PAYER, 1706618556129060n),
|
|
86
|
+
transfer(CEUR, ZERO, SEQUENCER_VAULT, 454425000000000n),
|
|
87
|
+
transfer(CEUR, ZERO, FEE_HANDLER, 2169376530470940n),
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
expect(splitTokenFee(CEUR, PAYER, transfers)).toEqual({
|
|
91
|
+
fees: 2623801530470940n,
|
|
92
|
+
feesAsset: { type: 'erc20', assetReference: CEUR },
|
|
93
|
+
transfers: [],
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('reads the fee of a failed transaction, which keeps its fee logs', () => {
|
|
98
|
+
// Values of tx 0x799747034724d0099d7e692b0f9ca0809184a6379dc0c5bf63dbc96e866ea9b9 (status 0)
|
|
99
|
+
const transfers = [
|
|
100
|
+
transfer(USDT, PAYER, USDT_HOLDER, 4230n),
|
|
101
|
+
transfer(USDT, USDT_HOLDER, SEQUENCER_VAULT, 0n),
|
|
102
|
+
transfer(USDT, USDT_HOLDER, FEE_HANDLER, 2816n),
|
|
103
|
+
transfer(USDT, USDT_HOLDER, PAYER, 1414n),
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
expect(splitTokenFee(USDT_ADAPTER, PAYER, transfers)).toEqual({
|
|
107
|
+
fees: 2816n,
|
|
108
|
+
feesAsset: { type: 'erc20', assetReference: USDT },
|
|
109
|
+
transfers: [],
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('keeps a transfer of the transaction to the fee handler, in the fee token', () => {
|
|
114
|
+
const toFeeHandler = transfer(USDT, PAYER, FEE_HANDLER, 1000n)
|
|
115
|
+
|
|
116
|
+
const { fees, transfers } = splitTokenFee(USDT_ADAPTER, PAYER, [
|
|
117
|
+
usdtDebit,
|
|
118
|
+
toFeeHandler,
|
|
119
|
+
...usdtCredits,
|
|
120
|
+
])
|
|
121
|
+
|
|
122
|
+
expect(fees).toBe(3176n)
|
|
123
|
+
expect(transfers).toEqual([toFeeHandler])
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('keeps a USDC mint and payment of the transaction just before the credits', () => {
|
|
127
|
+
const mint = transfer(USDC, ZERO, PAYER, 100n)
|
|
128
|
+
const payment = transfer(USDC, PAYER, OTHER, 50n)
|
|
129
|
+
|
|
130
|
+
const { fees, transfers } = splitTokenFee(USDC_ADAPTER, PAYER, [
|
|
131
|
+
usdcDebit,
|
|
132
|
+
mint,
|
|
133
|
+
payment,
|
|
134
|
+
...usdcCredits,
|
|
135
|
+
])
|
|
136
|
+
|
|
137
|
+
expect(fees).toBe(4072n)
|
|
138
|
+
expect(transfers).toEqual([mint, payment])
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('keeps a Mento mint of the transaction just before the credits', () => {
|
|
142
|
+
const mint = transfer(CUSD, ZERO, PAYER, 100n)
|
|
143
|
+
|
|
144
|
+
const { fees, transfers } = splitTokenFee(CUSD, PAYER, [cusdDebit, mint, ...cusdCredits])
|
|
145
|
+
|
|
146
|
+
expect(fees).toBe(12404402010n + 2255345820000000n)
|
|
147
|
+
expect(transfers).toEqual([mint])
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('matches addresses whatever their case', () => {
|
|
151
|
+
expect(
|
|
152
|
+
splitTokenFee(USDT_ADAPTER.toLowerCase(), PAYER.toLowerCase(), [usdtDebit, ...usdtCredits])
|
|
153
|
+
.fees
|
|
154
|
+
).toBe(3176n)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('rejects an unsupported fee currency', () => {
|
|
158
|
+
expect(() => splitTokenFee(OTHER, PAYER, [usdtDebit, ...usdtCredits])).toThrow(
|
|
159
|
+
UnsupportedFeeCurrencyError
|
|
160
|
+
)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it.each([
|
|
164
|
+
['a receipt without fee logs', USDT_ADAPTER, [userTransfer]],
|
|
165
|
+
['a receipt with fewer than four token transfers', USDT_ADAPTER, usdtCredits],
|
|
166
|
+
[
|
|
167
|
+
'a debit sent by another account',
|
|
168
|
+
USDT_ADAPTER,
|
|
169
|
+
[{ ...usdtDebit, from: OTHER }, ...usdtCredits],
|
|
170
|
+
],
|
|
171
|
+
['fee logs of another token', USDT_ADAPTER, [usdcDebit, ...usdcCredits]],
|
|
172
|
+
[
|
|
173
|
+
'a credit not sent by the holder',
|
|
174
|
+
USDT_ADAPTER,
|
|
175
|
+
[usdtDebit, ...usdtCredits.slice(0, 2), transfer(USDT, OTHER, PAYER, 1076n)],
|
|
176
|
+
],
|
|
177
|
+
[
|
|
178
|
+
'a USDC re-mint of another amount',
|
|
179
|
+
USDC_ADAPTER,
|
|
180
|
+
[usdcDebit, transfer(USDC, ZERO, PAYER, 5019n), ...usdcCredits.slice(1)],
|
|
181
|
+
],
|
|
182
|
+
[
|
|
183
|
+
'a USDC payment not sent by the payer',
|
|
184
|
+
USDC_ADAPTER,
|
|
185
|
+
[usdcDebit, ...usdcCredits.slice(0, 2), transfer(USDC, OTHER, FEE_HANDLER, 4072n)],
|
|
186
|
+
],
|
|
187
|
+
[
|
|
188
|
+
'a Mento debit that is not a burn',
|
|
189
|
+
CUSD,
|
|
190
|
+
[transfer(CUSD, PAYER, OTHER, 2831373668469528n), ...cusdCredits],
|
|
191
|
+
],
|
|
192
|
+
[
|
|
193
|
+
'a Mento credit that is not a mint',
|
|
194
|
+
CEUR,
|
|
195
|
+
[
|
|
196
|
+
transfer(CEUR, PAYER, ZERO, 10n),
|
|
197
|
+
transfer(CEUR, ZERO, PAYER, 1n),
|
|
198
|
+
transfer(CEUR, ZERO, SEQUENCER_VAULT, 1n),
|
|
199
|
+
transfer(CEUR, PAYER, FEE_HANDLER, 8n),
|
|
200
|
+
],
|
|
201
|
+
],
|
|
202
|
+
[
|
|
203
|
+
'credits larger than the debit',
|
|
204
|
+
USDT_ADAPTER,
|
|
205
|
+
[transfer(USDT, PAYER, USDT_HOLDER, 1n), ...usdtCredits],
|
|
206
|
+
],
|
|
207
|
+
])('rejects %s', (_, feeCurrency, transfers) => {
|
|
208
|
+
expect(() => splitTokenFee(feeCurrency, PAYER, transfers)).toThrow(UnexpectedFeeLogsError)
|
|
209
|
+
})
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
function nodeWithFeeCurrencies(feeCurrencies: Map<string, string | null>): NodeApi {
|
|
213
|
+
return mockNodeApi({ getFeeCurrencies: jest.fn().mockResolvedValue(feeCurrencies) })
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
describe('createCeloTokenFeeReader', () => {
|
|
217
|
+
const cip64Tx = {
|
|
218
|
+
hash: '0xABC',
|
|
219
|
+
from: PAYER,
|
|
220
|
+
type: 0x7b,
|
|
221
|
+
erc20Transfers: [usdtDebit, ...usdtCredits],
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
it('reads nothing for a transaction that is not CIP-64', async () => {
|
|
225
|
+
const nodeApi = nodeWithFeeCurrencies(new Map())
|
|
226
|
+
|
|
227
|
+
await expect(
|
|
228
|
+
createCeloTokenFeeReader(nodeApi, 1)({ ...cip64Tx, type: 2 })
|
|
229
|
+
).resolves.toBeUndefined()
|
|
230
|
+
expect(nodeApi.getFeeCurrencies).not.toHaveBeenCalled()
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it('reads nothing for a CIP-64 transaction that pays in the native coin', async () => {
|
|
234
|
+
const read = createCeloTokenFeeReader(nodeWithFeeCurrencies(new Map([['0xabc', null]])), 1)
|
|
235
|
+
|
|
236
|
+
await expect(read(cip64Tx)).resolves.toBeUndefined()
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
it('reads the fee currency of a transaction whose receipt has no type', async () => {
|
|
240
|
+
const read = createCeloTokenFeeReader(
|
|
241
|
+
nodeWithFeeCurrencies(
|
|
242
|
+
new Map([
|
|
243
|
+
['0xabc', USDT_ADAPTER],
|
|
244
|
+
['0xdef', null],
|
|
245
|
+
])
|
|
246
|
+
),
|
|
247
|
+
1
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
await expect(read({ ...cip64Tx, type: undefined })).resolves.toMatchObject({ fees: 3176n })
|
|
251
|
+
await expect(read({ ...cip64Tx, hash: '0xDEF', type: undefined })).resolves.toBeUndefined()
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
it('reads the token fee, fetching the fee currencies of the block once', async () => {
|
|
255
|
+
const nodeApi = nodeWithFeeCurrencies(new Map([['0xabc', USDT_ADAPTER]]))
|
|
256
|
+
const read = createCeloTokenFeeReader(nodeApi, 80494727)
|
|
257
|
+
|
|
258
|
+
const [first, second] = await Promise.all([read(cip64Tx), read(cip64Tx)])
|
|
259
|
+
|
|
260
|
+
expect(first?.fees).toBe(3176n)
|
|
261
|
+
expect(second?.fees).toBe(3176n)
|
|
262
|
+
expect(nodeApi.getFeeCurrencies).toHaveBeenCalledTimes(1)
|
|
263
|
+
expect(nodeApi.getFeeCurrencies).toHaveBeenCalledWith(80494727)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
it('rejects a CIP-64 transaction whose fee currency the node does not report', async () => {
|
|
267
|
+
const read = createCeloTokenFeeReader(nodeWithFeeCurrencies(new Map()), 1)
|
|
268
|
+
|
|
269
|
+
await expect(read(cip64Tx)).rejects.toThrow(FeeCurrencyUnavailableError)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it('rejects a CIP-64 transaction when the node cannot report fee currencies', async () => {
|
|
273
|
+
await expect(createCeloTokenFeeReader(mockNodeApi(), 1)(cip64Tx)).rejects.toThrow(
|
|
274
|
+
FeeCurrencyUnavailableError
|
|
275
|
+
)
|
|
276
|
+
})
|
|
277
|
+
})
|