@ledgerhq/coin-evm 17.0.0 → 17.1.1

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/logic/cip64Fees.d.ts +40 -9
  3. package/lib/logic/cip64Fees.d.ts.map +1 -1
  4. package/lib/logic/cip64Fees.js +86 -34
  5. package/lib/logic/cip64Fees.js.map +1 -1
  6. package/lib/logic/getBlock.d.ts.map +1 -1
  7. package/lib/logic/getBlock.js +48 -22
  8. package/lib/logic/getBlock.js.map +1 -1
  9. package/lib/logic/listOperations.d.ts.map +1 -1
  10. package/lib/logic/listOperations.js +21 -3
  11. package/lib/logic/listOperations.js.map +1 -1
  12. package/lib/logic/tokenFeeOperations.d.ts +61 -0
  13. package/lib/logic/tokenFeeOperations.d.ts.map +1 -0
  14. package/lib/logic/tokenFeeOperations.js +128 -0
  15. package/lib/logic/tokenFeeOperations.js.map +1 -0
  16. package/lib/network/node/rpc.common.d.ts.map +1 -1
  17. package/lib/network/node/rpc.common.js +18 -0
  18. package/lib/network/node/rpc.common.js.map +1 -1
  19. package/lib/network/node/types.d.ts +2 -0
  20. package/lib/network/node/types.d.ts.map +1 -1
  21. package/lib/network/node/types.js.map +1 -1
  22. package/lib/network/node/withNodeSources.d.ts.map +1 -1
  23. package/lib/network/node/withNodeSources.js +2 -0
  24. package/lib/network/node/withNodeSources.js.map +1 -1
  25. package/lib-es/logic/cip64Fees.d.ts +40 -9
  26. package/lib-es/logic/cip64Fees.d.ts.map +1 -1
  27. package/lib-es/logic/cip64Fees.js +85 -34
  28. package/lib-es/logic/cip64Fees.js.map +1 -1
  29. package/lib-es/logic/getBlock.d.ts.map +1 -1
  30. package/lib-es/logic/getBlock.js +48 -22
  31. package/lib-es/logic/getBlock.js.map +1 -1
  32. package/lib-es/logic/listOperations.d.ts.map +1 -1
  33. package/lib-es/logic/listOperations.js +21 -3
  34. package/lib-es/logic/listOperations.js.map +1 -1
  35. package/lib-es/logic/tokenFeeOperations.d.ts +61 -0
  36. package/lib-es/logic/tokenFeeOperations.d.ts.map +1 -0
  37. package/lib-es/logic/tokenFeeOperations.js +122 -0
  38. package/lib-es/logic/tokenFeeOperations.js.map +1 -0
  39. package/lib-es/network/node/rpc.common.d.ts.map +1 -1
  40. package/lib-es/network/node/rpc.common.js +18 -0
  41. package/lib-es/network/node/rpc.common.js.map +1 -1
  42. package/lib-es/network/node/types.d.ts +2 -0
  43. package/lib-es/network/node/types.d.ts.map +1 -1
  44. package/lib-es/network/node/types.js.map +1 -1
  45. package/lib-es/network/node/withNodeSources.d.ts.map +1 -1
  46. package/lib-es/network/node/withNodeSources.js +2 -0
  47. package/lib-es/network/node/withNodeSources.js.map +1 -1
  48. package/package.json +1 -1
  49. package/src/api/tests/index.celo.integ.test.ts +143 -0
  50. package/src/logic/cip64Fees.ts +123 -38
  51. package/src/logic/getBlock.ts +74 -34
  52. package/src/logic/listOperations.ts +32 -2
  53. package/src/logic/tests/cip64Fees.test.ts +149 -14
  54. package/src/logic/tests/getBlock.test.ts +45 -5
  55. package/src/logic/tests/listOperations.test.ts +469 -1
  56. package/src/logic/tokenFeeOperations.ts +169 -0
  57. package/src/network/node/rpc.common.ts +19 -0
  58. package/src/network/node/types.ts +2 -0
  59. package/src/network/node/withNodeSources.ts +3 -0
  60. package/src/network/tests/node/rpc.test.ts +36 -0
  61. package/src/network/tests/node/withNodeSources.test.ts +17 -0
@@ -30,38 +30,74 @@ type FeePattern = 'holder' | 'burnRemint' | 'burnMint'
30
30
  /** Credits of every supported fee currency: refund, tip and base fee, each emitted also when 0. */
31
31
  const CREDIT_COUNT = 3
32
32
 
33
+ type FeeCurrency = { token: string; patterns: readonly FeePattern[] }
34
+
33
35
  /**
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.
36
+ * Mento stable tokens have moved fees both ways: cUSD used `burnRemint` until an upgrade between
37
+ * blocks 50M and 75M, and `burnMint` since. The two shapes cannot be mistaken for each other, and
38
+ * the fee is the payer's net movement in either, so every Mento token accepts both.
37
39
  */
38
- const CELO_FEE_CURRENCIES: ReadonlyMap<string, { token: string; pattern: FeePattern }> = new Map([
40
+ const MENTO_PATTERNS: readonly FeePattern[] = ['burnMint', 'burnRemint']
41
+
42
+ const mentoToken = (token: string): [string, FeeCurrency] => [
43
+ token,
44
+ { token, patterns: MENTO_PATTERNS },
45
+ ]
46
+
47
+ /**
48
+ * Fee currencies whose fee logs are known, by lower-case fee currency address, with the
49
+ * lower-case token they move. The patterns were checked on mainnet receipts, the oldest on Celo
50
+ * L1. A Mento token without receipts shares the proxy implementation of one with receipts, and
51
+ * WETH's verified source burns the debit and mints each credit. Any other fee currency is
52
+ * reported as unsupported rather than guessed.
53
+ */
54
+ const CELO_FEE_CURRENCIES: ReadonlyMap<string, FeeCurrency> = new Map([
39
55
  // USD₮ through its adapter
40
56
  [
41
57
  '0x0e2a3e05bc9a16f5292a6170456a710cb89c6f72',
42
- { token: '0x48065fbbe25f71c9282ddf5e1cd6d6a887483d5e', pattern: 'holder' },
58
+ { token: '0x48065fbbe25f71c9282ddf5e1cd6d6a887483d5e', patterns: ['holder'] },
43
59
  ],
44
60
  // USDC through its adapter
45
61
  [
46
62
  '0x2f25deb3848c207fc8e0c34035b3ba7fc157602b',
47
- { token: '0xceba9300f2b948710d2653dd7b07f33a8b32118c', pattern: 'burnRemint' },
63
+ { token: '0xceba9300f2b948710d2653dd7b07f33a8b32118c', patterns: ['burnRemint'] },
48
64
  ],
49
- // cUSD
65
+ // USAT through its adapter
50
66
  [
51
- '0x765de816845861e75a25fca122bb6898b8b1282a',
52
- { token: '0x765de816845861e75a25fca122bb6898b8b1282a', pattern: 'burnMint' },
67
+ '0x0357ee22278c922e1d36cfe6b899269b161880c4',
68
+ { token: '0xd2ab3c9a02dbbab236bfec45d1d755df4267f771', patterns: ['holder'] },
53
69
  ],
54
- // cEUR
70
+ // WETH, not upgradeable
55
71
  [
56
- '0xd8763cba276a3738e6de85b4b3bf5fded6d6ca73',
57
- { token: '0xd8763cba276a3738e6de85b4b3bf5fded6d6ca73', pattern: 'burnMint' },
72
+ '0xd221812de1bd094f35587ee8e174b07b6167d9af',
73
+ { token: '0xd221812de1bd094f35587ee8e174b07b6167d9af', patterns: ['burnMint'] },
58
74
  ],
75
+ // Mento USDm (cUSD), EURm (cEUR), GBPm, CHFm, JPYm, KESm, COPm, NGNm, GHSm, PHPm, ZARm, AUDm,
76
+ // XOFm, BRLm, CADm
77
+ ...[
78
+ '0x765de816845861e75a25fca122bb6898b8b1282a',
79
+ '0xd8763cba276a3738e6de85b4b3bf5fded6d6ca73',
80
+ '0xccf663b1ff11028f0b19058d0f7b674004a40746',
81
+ '0xb55a79f398e759e43c95b979163f30ec87ee131d',
82
+ '0xc45ecf20f3cd864b32d9794d6f76814ae8892e20',
83
+ '0x456a3d042c0dbd3db53d5489e98dfb038553b0d0',
84
+ '0x8a567e2ae79ca692bd748ab832081c45de4041ea',
85
+ '0xe2702bd97ee33c88c8f6f92da3b733608aa76f71',
86
+ '0xfaea5f3404bba20d3cc2f8c4b0a888f55a3c7313',
87
+ '0x105d4a9306d2e55a71d2eb95b81553ae1dc20d7b',
88
+ '0x4c35853a3b4e647fd266f4de678dcc8fec410bf6',
89
+ '0x7175504c455076f15c04a2f90a8e352281f492f9',
90
+ '0x73f93dcc49cb8a239e2032663e9475dd5ef29a08',
91
+ '0xe8537a3d056da446677b9e9d6c5db704eaab4787',
92
+ '0xff4ab19391af240c311c54200a492233052b6325',
93
+ ].map(mentoToken),
59
94
  ])
60
95
 
61
- /** The fee of a token-paid transaction, and the transfers of the transaction itself. */
96
+ /** The fee of a token-paid transaction, the transfers that move it, and the other transfers. */
62
97
  export type TokenFee = {
63
98
  fees: bigint
64
99
  feesAsset: AssetInfo
100
+ feeTransfers: ERC20Transfer[]
65
101
  transfers: ERC20Transfer[]
66
102
  }
67
103
 
@@ -99,7 +135,7 @@ function matchesPattern(
99
135
  * Splits the token transfers of a CIP-64 transaction into its fee and its own transfers.
100
136
  *
101
137
  * 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.
138
+ * {@link CREDIT_COUNT} are the credits, checked against the patterns of the fee currency.
103
139
  * Matching on position, not on recipients, keeps a transfer of the transaction to the fee
104
140
  * recipients. The fee is what the payer sent minus what it got back, in the token's own units:
105
141
  * a value converted from the adapter's 18 decimals would not be the amount debited.
@@ -107,19 +143,20 @@ function matchesPattern(
107
143
  * @param feeCurrency `feeCurrency` of the transaction
108
144
  * @param payer sender of the transaction
109
145
  * @param transfers the token transfers of the receipt, in log order
146
+ * @param hash hash of the transaction, for the error context
110
147
  * @throws UnsupportedFeeCurrencyError when the fee currency is not supported
111
148
  * @throws UnexpectedFeeLogsError when the fee logs do not match the fee currency
112
149
  */
113
150
  export function splitTokenFee(
114
151
  feeCurrency: string,
115
152
  payer: string,
116
- transfers: readonly ERC20Transfer[]
153
+ transfers: readonly ERC20Transfer[],
154
+ hash?: string
117
155
  ): TokenFee {
156
+ const context = { feeCurrency, payer, ...(hash !== undefined && { hash }) }
118
157
  const currency = CELO_FEE_CURRENCIES.get(feeCurrency.toLowerCase())
119
158
  if (currency === undefined) {
120
- throw new UnsupportedFeeCurrencyError(`Unsupported Celo fee currency ${feeCurrency}`, {
121
- feeCurrency,
122
- })
159
+ throw new UnsupportedFeeCurrencyError(`Unsupported Celo fee currency ${feeCurrency}`, context)
123
160
  }
124
161
 
125
162
  const creditStart = transfers.length - CREDIT_COUNT
@@ -129,11 +166,12 @@ export function splitTokenFee(
129
166
  debit === undefined ||
130
167
  creditStart < 1 ||
131
168
  ![debit, ...credits].every((transfer) => same(transfer.asset.assetReference, currency.token)) ||
132
- !matchesPattern(currency.pattern, payer, debit, credits)
169
+ !currency.patterns.some((pattern) => matchesPattern(pattern, payer, debit, credits))
133
170
  ) {
134
- throw new UnexpectedFeeLogsError(`Unexpected fee logs for Celo fee currency ${feeCurrency}`, {
135
- feeCurrency,
136
- })
171
+ throw new UnexpectedFeeLogsError(
172
+ `Unexpected fee logs for Celo fee currency ${feeCurrency}`,
173
+ context
174
+ )
137
175
  }
138
176
 
139
177
  let fees = 0n
@@ -143,51 +181,98 @@ export function splitTokenFee(
143
181
  if (same(transfer.to, payer)) fees -= value
144
182
  }
145
183
  if (fees < 0n) {
146
- throw new UnexpectedFeeLogsError(`Negative fee for Celo fee currency ${feeCurrency}`, {
147
- feeCurrency,
148
- })
184
+ throw new UnexpectedFeeLogsError(`Negative fee for Celo fee currency ${feeCurrency}`, context)
149
185
  }
150
186
 
151
187
  return {
152
188
  fees,
153
189
  feesAsset: { type: 'erc20', assetReference: debit.asset.assetReference },
190
+ feeTransfers: [debit, ...credits],
154
191
  transfers: transfers.slice(1, creditStart),
155
192
  }
156
193
  }
157
194
 
158
- /** Reads the token fee of a transaction, or `undefined` when it pays in the native coin. */
159
- export type ReadTokenFee = (tx: {
195
+ /**
196
+ * How a Celo transaction paid its fee. A fee currency whose fee logs are not known is reported,
197
+ * not rejected, so that the caller decides how to degrade.
198
+ */
199
+ export type CeloTransactionFee =
200
+ | { kind: 'native' }
201
+ | { kind: 'token'; tokenFee: TokenFee }
202
+ | { kind: 'unsupported'; feeCurrency: string }
203
+
204
+ /** A transaction, with the token transfers of its receipt, whose fee is to be read. */
205
+ type FeeTransaction = {
160
206
  hash: string
161
207
  from: string
162
208
  type?: number | undefined
163
209
  erc20Transfers: readonly ERC20Transfer[]
164
- }) => Promise<TokenFee | undefined>
210
+ }
165
211
 
166
212
  /**
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.
213
+ * Reads how a transaction paid its fee.
214
+ *
215
+ * @throws UnexpectedFeeLogsError when the fee logs do not match a supported fee currency
216
+ */
217
+ export type ReadTokenFee = (tx: FeeTransaction) => Promise<CeloTransactionFee>
218
+
219
+ function toCeloTransactionFee(
220
+ feeCurrency: string | null,
221
+ { hash, from, erc20Transfers }: FeeTransaction
222
+ ): CeloTransactionFee {
223
+ if (feeCurrency === null) return { kind: 'native' }
224
+ if (!CELO_FEE_CURRENCIES.has(feeCurrency.toLowerCase()))
225
+ return { kind: 'unsupported', feeCurrency }
226
+ return { kind: 'token', tokenFee: splitTokenFee(feeCurrency, from, erc20Transfers, hash) }
227
+ }
228
+
229
+ /**
230
+ * Reads the fee of the CIP-64 transactions of a Celo block. The fee currencies of the block are
231
+ * fetched once, at the first transaction that is or may be CIP-64: a receipt without a type does
232
+ * not prove the transaction pays in the native coin.
170
233
  *
171
234
  * @param nodeApi node of the network
172
235
  * @param blockHeight height of the block
173
236
  */
174
237
  export function createCeloTokenFeeReader(nodeApi: NodeApi, blockHeight: number): ReadTokenFee {
175
238
  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
239
+ return async (tx) => {
240
+ if (tx.type !== undefined && tx.type !== CIP64_TX_TYPE) return { kind: 'native' }
178
241
  if (nodeApi.getFeeCurrencies === undefined) {
179
242
  throw new FeeCurrencyUnavailableError(
180
243
  'The node cannot report the fee currency of a transaction',
181
- { hash }
244
+ { hash: tx.hash }
182
245
  )
183
246
  }
184
247
  feeCurrencies ??= nodeApi.getFeeCurrencies(blockHeight)
185
- const feeCurrency = (await feeCurrencies).get(hash.toLowerCase())
248
+ const feeCurrency = (await feeCurrencies).get(tx.hash.toLowerCase())
186
249
  if (feeCurrency === undefined) {
187
- throw new FeeCurrencyUnavailableError(`The node block has no transaction ${hash}`, {
188
- hash,
250
+ throw new FeeCurrencyUnavailableError(`The node block has no transaction ${tx.hash}`, {
251
+ hash: tx.hash,
189
252
  })
190
253
  }
191
- return feeCurrency === null ? undefined : splitTokenFee(feeCurrency, from, erc20Transfers)
254
+ return toCeloTransactionFee(feeCurrency, tx)
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Reads how a Celo transaction paid its fee, fetching its fee currency alone, for transactions
260
+ * that are not read block by block.
261
+ *
262
+ * @param nodeApi node of the network
263
+ * @param tx the transaction, with the token transfers of its receipt
264
+ * @throws UnexpectedFeeLogsError when the fee logs do not match a supported fee currency
265
+ */
266
+ export async function readCeloTransactionFee(
267
+ nodeApi: NodeApi,
268
+ tx: FeeTransaction
269
+ ): Promise<CeloTransactionFee> {
270
+ if (tx.type !== undefined && tx.type !== CIP64_TX_TYPE) return { kind: 'native' }
271
+ if (nodeApi.getFeeCurrency === undefined) {
272
+ throw new FeeCurrencyUnavailableError(
273
+ 'The node cannot report the fee currency of a transaction',
274
+ { hash: tx.hash }
275
+ )
192
276
  }
277
+ return toCeloTransactionFee(await nodeApi.getFeeCurrency(tx.hash), tx)
193
278
  }
@@ -2,9 +2,13 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import type { ChainId, EvmContext } from '../config'
5
- import type { BlockReceiptInfo, PrefetchedBlockTransaction } from '../network/node/types'
5
+ import type {
6
+ BlockReceiptInfo,
7
+ ERC20Transfer,
8
+ PrefetchedBlockTransaction,
9
+ } from '../network/node/types'
6
10
  import type { EvmStakingOperationType } from '../types/staking'
7
- import type { ReadTokenFee, TokenFee } from './cip64Fees'
11
+ import type { CeloTransactionFee, ReadTokenFee } from './cip64Fees'
8
12
  import type { InternalTxsByHash } from './internalTransactionsFetcher'
9
13
  import type {
10
14
  Block,
@@ -384,19 +388,13 @@ async function getTransactionsFromPrefetchedData(
384
388
 
385
389
  return await Promise.all(
386
390
  withReceipts.map(async ({ tx, receipt }) => {
387
- const tokenFee = await readTokenFee?.({
391
+ const fee = await readTokenFee?.({
388
392
  hash: tx.hash,
389
393
  from: tx.from,
390
394
  type: receipt.type,
391
395
  erc20Transfers: receipt.erc20Transfers,
392
396
  })
393
- const blockTx = prefetchedTransactionToBlockTransaction(
394
- chainId,
395
- tx,
396
- receipt,
397
- logger,
398
- tokenFee
399
- )
397
+ const blockTx = prefetchedTransactionToBlockTransaction(chainId, tx, receipt, logger, fee)
400
398
  return {
401
399
  ...blockTx,
402
400
  operations: applyChainSpecificCorrections(
@@ -426,7 +424,7 @@ function prefetchedTransactionToBlockTransaction(
426
424
  tx: PrefetchedBlockTransaction,
427
425
  receipt: BlockReceiptInfo,
428
426
  logger: Logger,
429
- tokenFee: TokenFee | undefined
427
+ fee: CeloTransactionFee | undefined
430
428
  ): BlockTransaction {
431
429
  const failed = receipt.status === 0
432
430
  // Some EVM chains (e.g. Cronos) omit `gasPrice` from transaction
@@ -434,14 +432,17 @@ function prefetchedTransactionToBlockTransaction(
434
432
  // (the node layer provides `0` when the raw `eth_getBlockByNumber(_, true)` payload omits it).
435
433
  const receiptGasPrice = BigInt(receipt.gasPrice)
436
434
  const gasPrice = receiptGasPrice > 0n ? receiptGasPrice : BigInt(tx.gasPrice)
437
- const fees = BigInt(receipt.gasUsed) * gasPrice + (receipt.l1Fee ? BigInt(receipt.l1Fee) : 0n)
435
+ const nativeFees =
436
+ BigInt(receipt.gasUsed) * gasPrice + (receipt.l1Fee ? BigInt(receipt.l1Fee) : 0n)
437
+ const { erc20Transfers, feeDetails, ...feeFields } = readFeeFields(
438
+ tx.hash,
439
+ nativeFees,
440
+ receipt.erc20Transfers,
441
+ fee,
442
+ logger
443
+ )
438
444
  const operations = rpcTransactionToBlockOperations(
439
- {
440
- from: tx.from,
441
- to: tx.to,
442
- value: tx.value,
443
- erc20Transfers: tokenFee?.transfers ?? receipt.erc20Transfers,
444
- },
445
+ { from: tx.from, to: tx.to, value: tx.value, erc20Transfers },
445
446
  logger
446
447
  )
447
448
 
@@ -457,20 +458,55 @@ function prefetchedTransactionToBlockTransaction(
457
458
  hash: tx.hash,
458
459
  failed,
459
460
  operations,
460
- ...withTokenFee(fees, tokenFee),
461
+ ...feeFields,
461
462
  feesPayer: tx.from,
462
- details,
463
+ details: { ...details, ...feeDetails },
463
464
  }
464
465
  }
465
466
 
466
- /** The fee fields of a transaction: its token fee when it has one, else its native fee. */
467
- function withTokenFee(
467
+ /**
468
+ * The fee fields of a transaction, and the token transfers left for its operations, from how it
469
+ * paid its fee: a token fee without its fee transfers, or else the native fee.
470
+ *
471
+ * A fee currency with unknown fee logs degrades instead of failing the block, which anyone could
472
+ * cause with one transaction. The fee is reported as 0 in the native coin, every token transfer
473
+ * of the receipt stays an operation, fee transfers included, and `unsupportedFeeCurrency` in the
474
+ * details names the fee currency. When the fee currency emits standard Transfer events, applying
475
+ * the operations still gives each account's balance change; a failed transaction loses its fee,
476
+ * because consumers ignore its operations.
477
+ */
478
+ function readFeeFields(
479
+ hash: string,
468
480
  nativeFees: bigint,
469
- tokenFee: TokenFee | undefined
470
- ): Required<Pick<BlockTransaction, 'fees' | 'feesAsset'>> {
471
- return tokenFee === undefined
472
- ? { fees: nativeFees, feesAsset: { type: 'native' } }
473
- : { fees: tokenFee.fees, feesAsset: tokenFee.feesAsset }
481
+ erc20Transfers: ERC20Transfer[],
482
+ fee: CeloTransactionFee | undefined,
483
+ logger: Logger
484
+ ): Required<Pick<BlockTransaction, 'fees' | 'feesAsset'>> & {
485
+ erc20Transfers: ERC20Transfer[]
486
+ feeDetails: Record<string, unknown>
487
+ } {
488
+ switch (fee?.kind) {
489
+ case 'token':
490
+ return {
491
+ fees: fee.tokenFee.fees,
492
+ feesAsset: fee.tokenFee.feesAsset,
493
+ erc20Transfers: fee.tokenFee.transfers,
494
+ feeDetails: {},
495
+ }
496
+ case 'unsupported':
497
+ logger('warn', 'Celo fee currency not supported, reporting no fee', {
498
+ hash,
499
+ feeCurrency: fee.feeCurrency,
500
+ })
501
+ return {
502
+ fees: 0n,
503
+ feesAsset: { type: 'native' },
504
+ erc20Transfers,
505
+ feeDetails: { unsupportedFeeCurrency: fee.feeCurrency },
506
+ }
507
+ default:
508
+ return { fees: nativeFees, feesAsset: { type: 'native' }, erc20Transfers, feeDetails: {} }
509
+ }
474
510
  }
475
511
 
476
512
  async function getTransactionFromHash(
@@ -481,16 +517,20 @@ async function getTransactionFromHash(
481
517
  readTokenFee: ReadTokenFee | undefined
482
518
  ): Promise<BlockTransaction | null> {
483
519
  const txInfo = await nodeApi.getTransaction(txHash)
484
- const tokenFee = await readTokenFee?.(txInfo)
520
+ const fee = await readTokenFee?.(txInfo)
485
521
 
486
522
  const failed = txInfo.status === 0
487
- const fees =
523
+ const nativeFees =
488
524
  BigInt(txInfo.gasUsed) * BigInt(txInfo.gasPrice) + (txInfo.l1Fee ? BigInt(txInfo.l1Fee) : 0n)
489
-
490
- const rawOperations = rpcTransactionToBlockOperations(
491
- tokenFee === undefined ? txInfo : { ...txInfo, erc20Transfers: tokenFee.transfers },
525
+ const { erc20Transfers, feeDetails, ...feeFields } = readFeeFields(
526
+ txHash,
527
+ nativeFees,
528
+ txInfo.erc20Transfers,
529
+ fee,
492
530
  logger
493
531
  )
532
+
533
+ const rawOperations = rpcTransactionToBlockOperations({ ...txInfo, erc20Transfers }, logger)
494
534
  const operations = applyChainSpecificCorrections(chainId, rawOperations, txInfo.type, logger)
495
535
 
496
536
  const details = buildTransactionDetails(
@@ -505,9 +545,9 @@ async function getTransactionFromHash(
505
545
  hash: txHash,
506
546
  failed,
507
547
  operations,
508
- ...withTokenFee(fees, tokenFee),
548
+ ...feeFields,
509
549
  feesPayer: txInfo.from,
510
- details,
550
+ details: { ...details, ...feeDetails },
511
551
  }
512
552
  }
513
553
 
@@ -2,6 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import type { EvmContext } from '../config'
5
+ import type { TransactionTokenFee } from './tokenFeeOperations'
5
6
  import type {
6
7
  Page,
7
8
  MemoNotSupported,
@@ -9,11 +10,13 @@ import type {
9
10
  ListOperationsOptions,
10
11
  } from '@ledgerhq/coin-module-framework/api/types'
11
12
  import { InvalidParameterError } from '@ledgerhq/coin-module-framework/errors'
13
+ import { CHAIN_ID } from '../config'
12
14
  import { getExplorerApi } from '../network/explorer'
13
15
  import {
14
16
  attributeFeeCollectorPayments,
15
17
  findTransactionsPaidByOthers,
16
18
  } from './feeCollectorOperations'
19
+ import { readCeloTokenFees, removeFeeTransfers, withTokenFee } from './tokenFeeOperations'
17
20
 
18
21
  // the sort parameter has a double meaning:
19
22
  // - legacy (for the bridge): it's used to sort the operations in the result list. Explorer always queried in "desc" order.
@@ -67,7 +70,7 @@ export async function listOperations(
67
70
  const explorerOrder = options.limit === undefined ? 'desc' : (options.order ?? 'desc')
68
71
  const {
69
72
  lastCoinOperations,
70
- lastTokenOperations,
73
+ lastTokenOperations: explorerTokenOperations,
71
74
  lastNftOperations,
72
75
  lastInternalOperations,
73
76
  nextPagingToken,
@@ -82,6 +85,31 @@ export async function listOperations(
82
85
  explorerOrder
83
86
  )
84
87
 
88
+ const addressLower = address.toLowerCase()
89
+
90
+ // A Celo CIP-64 transaction can pay its fee in a token. The fee transfers are removed before the
91
+ // parent suppression below, so that the native operation stays as the fee carrier.
92
+ const tokenFees =
93
+ config.chainId === CHAIN_ID.CELO
94
+ ? await readCeloTokenFees(
95
+ config,
96
+ context.logger,
97
+ address,
98
+ [
99
+ ...lastCoinOperations,
100
+ ...explorerTokenOperations,
101
+ ...lastNftOperations,
102
+ ...lastInternalOperations,
103
+ ].map((op) => op.tx.hash),
104
+ new Set(
105
+ lastCoinOperations
106
+ .filter((op) => op.senders.some((sender) => sender.toLowerCase() === addressLower))
107
+ .map((op) => op.tx.hash.toLowerCase())
108
+ )
109
+ )
110
+ : new Map<string, TransactionTokenFee>()
111
+ const lastTokenOperations = removeFeeTransfers(explorerTokenOperations, tokenFees)
112
+
85
113
  const tokenOrNftHashes = new Set(
86
114
  [...lastTokenOperations, ...lastNftOperations].map((op) => op.tx.hash)
87
115
  )
@@ -91,7 +119,6 @@ export async function listOperations(
91
119
  )
92
120
  )
93
121
 
94
- const addressLower = address.toLowerCase()
95
122
  const isAddressInOp = (op: Operation<MemoNotSupported>): boolean =>
96
123
  op.senders.some((s) => s.toLowerCase() === addressLower) ||
97
124
  op.recipients.some((r) => r.toLowerCase() === addressLower)
@@ -143,6 +170,7 @@ export async function listOperations(
143
170
  ...op.tx,
144
171
  block: { ...op.tx.block, hash: op.tx.block.hash || parent.tx.block.hash },
145
172
  fees: parent.tx.fees,
173
+ ...(parent.tx.feesAsset ? { feesAsset: parent.tx.feesAsset } : {}),
146
174
  failed: parent.tx.failed,
147
175
  ...(parent.tx.feesPayer ? { feesPayer: parent.tx.feesPayer } : {}),
148
176
  },
@@ -192,6 +220,7 @@ export async function listOperations(
192
220
  ...op.tx,
193
221
  block: { ...op.tx.block, hash: parent.tx.block.hash },
194
222
  fees: parent.tx.fees,
223
+ ...(parent.tx.feesAsset ? { feesAsset: parent.tx.feesAsset } : {}),
195
224
  ...(parent.tx.feesPayer ? { feesPayer: parent.tx.feesPayer } : {}),
196
225
  },
197
226
  }
@@ -286,6 +315,7 @@ export async function listOperations(
286
315
  .concat(internalOperations)
287
316
  .filter(hasValidType)
288
317
  .filter(isAddressInvolved)
318
+ .map((op) => withTokenFee(op, tokenFees))
289
319
  const feeEvidence = [...lastTokenOperations, ...lastNftOperations, ...lastInternalOperations]
290
320
  const operations = attributeFeeCollectorPayments(
291
321
  accountOperations,