@ledgerhq/coin-evm 16.8.1 → 17.1.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.
Files changed (101) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/lib/adapters/etherscan.d.ts.map +1 -1
  3. package/lib/adapters/etherscan.js +5 -0
  4. package/lib/adapters/etherscan.js.map +1 -1
  5. package/lib/adapters/ledger.d.ts.map +1 -1
  6. package/lib/adapters/ledger.js +1 -0
  7. package/lib/adapters/ledger.js.map +1 -1
  8. package/lib/api/index.d.ts +2 -5
  9. package/lib/api/index.d.ts.map +1 -1
  10. package/lib/api/index.js +1 -2
  11. package/lib/api/index.js.map +1 -1
  12. package/lib/logic/cip64Fees.d.ts +25 -3
  13. package/lib/logic/cip64Fees.d.ts.map +1 -1
  14. package/lib/logic/cip64Fees.js +78 -24
  15. package/lib/logic/cip64Fees.js.map +1 -1
  16. package/lib/logic/feeCollectorOperations.d.ts +3 -3
  17. package/lib/logic/feeCollectorOperations.js +3 -3
  18. package/lib/logic/getBlock.d.ts +2 -2
  19. package/lib/logic/getBlock.d.ts.map +1 -1
  20. package/lib/logic/getBlock.js +5 -11
  21. package/lib/logic/getBlock.js.map +1 -1
  22. package/lib/logic/listOperations.d.ts.map +1 -1
  23. package/lib/logic/listOperations.js +21 -3
  24. package/lib/logic/listOperations.js.map +1 -1
  25. package/lib/logic/tokenFeeOperations.d.ts +61 -0
  26. package/lib/logic/tokenFeeOperations.d.ts.map +1 -0
  27. package/lib/logic/tokenFeeOperations.js +128 -0
  28. package/lib/logic/tokenFeeOperations.js.map +1 -0
  29. package/lib/network/node/rpc.common.d.ts.map +1 -1
  30. package/lib/network/node/rpc.common.js +18 -0
  31. package/lib/network/node/rpc.common.js.map +1 -1
  32. package/lib/network/node/types.d.ts +2 -0
  33. package/lib/network/node/types.d.ts.map +1 -1
  34. package/lib/network/node/types.js.map +1 -1
  35. package/lib/network/node/withNodeSources.d.ts.map +1 -1
  36. package/lib/network/node/withNodeSources.js +2 -0
  37. package/lib/network/node/withNodeSources.js.map +1 -1
  38. package/lib-es/adapters/etherscan.d.ts.map +1 -1
  39. package/lib-es/adapters/etherscan.js +5 -0
  40. package/lib-es/adapters/etherscan.js.map +1 -1
  41. package/lib-es/adapters/ledger.d.ts.map +1 -1
  42. package/lib-es/adapters/ledger.js +1 -0
  43. package/lib-es/adapters/ledger.js.map +1 -1
  44. package/lib-es/api/index.d.ts +2 -5
  45. package/lib-es/api/index.d.ts.map +1 -1
  46. package/lib-es/api/index.js +1 -2
  47. package/lib-es/api/index.js.map +1 -1
  48. package/lib-es/logic/cip64Fees.d.ts +25 -3
  49. package/lib-es/logic/cip64Fees.d.ts.map +1 -1
  50. package/lib-es/logic/cip64Fees.js +77 -24
  51. package/lib-es/logic/cip64Fees.js.map +1 -1
  52. package/lib-es/logic/feeCollectorOperations.d.ts +3 -3
  53. package/lib-es/logic/feeCollectorOperations.js +3 -3
  54. package/lib-es/logic/getBlock.d.ts +2 -2
  55. package/lib-es/logic/getBlock.d.ts.map +1 -1
  56. package/lib-es/logic/getBlock.js +5 -11
  57. package/lib-es/logic/getBlock.js.map +1 -1
  58. package/lib-es/logic/listOperations.d.ts.map +1 -1
  59. package/lib-es/logic/listOperations.js +21 -3
  60. package/lib-es/logic/listOperations.js.map +1 -1
  61. package/lib-es/logic/tokenFeeOperations.d.ts +61 -0
  62. package/lib-es/logic/tokenFeeOperations.d.ts.map +1 -0
  63. package/lib-es/logic/tokenFeeOperations.js +122 -0
  64. package/lib-es/logic/tokenFeeOperations.js.map +1 -0
  65. package/lib-es/network/node/rpc.common.d.ts.map +1 -1
  66. package/lib-es/network/node/rpc.common.js +18 -0
  67. package/lib-es/network/node/rpc.common.js.map +1 -1
  68. package/lib-es/network/node/types.d.ts +2 -0
  69. package/lib-es/network/node/types.d.ts.map +1 -1
  70. package/lib-es/network/node/types.js.map +1 -1
  71. package/lib-es/network/node/withNodeSources.d.ts.map +1 -1
  72. package/lib-es/network/node/withNodeSources.js +2 -0
  73. package/lib-es/network/node/withNodeSources.js.map +1 -1
  74. package/package.json +2 -2
  75. package/src/adapters/blockscoutV2.test.ts +2 -0
  76. package/src/adapters/etherscan.test.ts +21 -0
  77. package/src/adapters/etherscan.ts +5 -0
  78. package/src/adapters/ledger.test.ts +8 -0
  79. package/src/adapters/ledger.ts +1 -0
  80. package/src/api/index.ts +1 -4
  81. package/src/api/tests/index.celo.integ.test.ts +144 -3
  82. package/src/api/tests/index.optimism.integ.test.ts +1 -0
  83. package/src/api/tests/index.test.ts +0 -9
  84. package/src/logic/cip64Fees.ts +99 -26
  85. package/src/logic/feeCollectorOperations.ts +3 -3
  86. package/src/logic/getBlock.ts +6 -17
  87. package/src/logic/listOperations.ts +32 -2
  88. package/src/logic/tests/cip64Fees.test.ts +128 -4
  89. package/src/logic/tests/getBlock.test.ts +10 -23
  90. package/src/logic/tests/listOperations.test.ts +522 -2
  91. package/src/logic/tests/validateIntent.test.ts +4 -0
  92. package/src/logic/tokenFeeOperations.ts +169 -0
  93. package/src/network/node/rpc.common.ts +19 -0
  94. package/src/network/node/types.ts +2 -0
  95. package/src/network/node/withNodeSources.ts +3 -0
  96. package/src/network/tests/explorer/etherscan.test.ts +1 -0
  97. package/src/network/tests/explorer/ledger.test.ts +4 -0
  98. package/src/network/tests/node/rpc.test.ts +36 -0
  99. package/src/network/tests/node/withNodeSources.test.ts +17 -0
  100. package/src/staking/rewardsFromReceipt.test.ts +1 -0
  101. package/src/staking/validators/monad.test.ts +1 -0
@@ -116,6 +116,7 @@ export const etherscanOperationToOperations = (
116
116
  time: date,
117
117
  },
118
118
  fees,
119
+ feesAsset: { type: 'native' },
119
120
  date,
120
121
  failed,
121
122
  ...(feesPayer ? { feesPayer } : {}),
@@ -171,6 +172,7 @@ export const etherscanERC20EventToOperations = (
171
172
  time: date,
172
173
  },
173
174
  fees,
175
+ feesAsset: { type: 'native' },
174
176
  date,
175
177
  failed: false,
176
178
  },
@@ -227,6 +229,7 @@ export const etherscanERC721EventToOperations = (
227
229
  time: date,
228
230
  },
229
231
  fees,
232
+ feesAsset: { type: 'native' },
230
233
  date,
231
234
  failed: false,
232
235
  },
@@ -283,6 +286,7 @@ export const etherscanERC1155EventToOperations = (
283
286
  time: date,
284
287
  },
285
288
  fees,
289
+ feesAsset: { type: 'native' },
286
290
  date,
287
291
  failed: false,
288
292
  },
@@ -365,6 +369,7 @@ export const etherscanInternalTransactionToOperations = (
365
369
  time: date,
366
370
  },
367
371
  fees: 0n, // unecessary as it's already contained in the fees of the main op
372
+ feesAsset: { type: 'native' },
368
373
  date,
369
374
  failed: hasFailed,
370
375
  },
@@ -36,6 +36,7 @@ const coinOperation: Operation<MemoNotSupported> = {
36
36
  time: new Date('2023-01-24T17:11:45Z'),
37
37
  },
38
38
  fees: 4254163264389158n,
39
+ feesAsset: { type: 'native' },
39
40
  date: new Date('2023-01-24T17:11:45Z'),
40
41
  failed: false,
41
42
  },
@@ -102,6 +103,7 @@ describe('EVM Family', () => {
102
103
  time: new Date('2023-01-24T17:11:45Z'),
103
104
  },
104
105
  fees: 4254163264389158n,
106
+ feesAsset: { type: 'native' },
105
107
  date: new Date('2023-01-24T17:11:45Z'),
106
108
  failed: false,
107
109
  feesPayer: '0x6cBCD73CD8e8a42844662f0A0e76D7F79Afd933d',
@@ -172,6 +174,7 @@ describe('EVM Family', () => {
172
174
  time: new Date('2023-01-24T17:11:45Z'),
173
175
  },
174
176
  fees: 4254163264389158n,
177
+ feesAsset: { type: 'native' },
175
178
  date: new Date('2023-01-24T17:11:45Z'),
176
179
  failed: false,
177
180
  feesPayer: '0x6cBCD73CD8e8a42844662f0A0e76D7F79Afd933d',
@@ -237,6 +240,7 @@ describe('EVM Family', () => {
237
240
  time: new Date('2023-01-24T17:11:45Z'),
238
241
  },
239
242
  fees: 4254163264389158n,
243
+ feesAsset: { type: 'native' },
240
244
  date: new Date('2023-01-24T17:11:45Z'),
241
245
  failed: false,
242
246
  feesPayer: '0x6cBCD73CD8e8a42844662f0A0e76D7F79Afd933d',
@@ -350,6 +354,7 @@ describe('EVM Family', () => {
350
354
  time: new Date('2023-01-24T17:11:45Z'),
351
355
  },
352
356
  fees: 4254163264389158n,
357
+ feesAsset: { type: 'native' },
353
358
  date: new Date('2023-01-24T17:11:45Z'),
354
359
  failed: false,
355
360
  feesPayer: '0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619',
@@ -415,6 +420,7 @@ describe('EVM Family', () => {
415
420
  time: new Date('2023-01-24T17:11:45Z'),
416
421
  },
417
422
  fees: 4254163264389158n,
423
+ feesAsset: { type: 'native' },
418
424
  date: new Date('2023-01-24T17:11:45Z'),
419
425
  failed: false,
420
426
  feesPayer: '0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619',
@@ -485,6 +491,7 @@ describe('EVM Family', () => {
485
491
  time: new Date('2022-12-13T21:41:37Z'),
486
492
  },
487
493
  fees: 905335872155003804n,
494
+ feesAsset: { type: 'native' },
488
495
  date: new Date('2022-12-13T21:41:37Z'),
489
496
  failed: false,
490
497
  feesPayer: '0x787aCF62fFC81Bb171d1f46fB8b3Fc6503D503e8',
@@ -543,6 +550,7 @@ describe('EVM Family', () => {
543
550
  time: new Date('2023-01-24T17:11:45Z'),
544
551
  },
545
552
  fees: 4254163264389158n,
553
+ feesAsset: { type: 'native' },
546
554
  date: new Date('2023-01-24T17:11:45Z'),
547
555
  failed: false,
548
556
  feesPayer: '0x6cBCD73CD8e8a42844662f0A0e76D7F79Afd933d',
@@ -67,6 +67,7 @@ export const ledgerOperationToOperations = (
67
67
  time: date,
68
68
  },
69
69
  fees: safeBigInt(ledgerOp.gas_used) * safeBigInt(ledgerOp.gas_price),
70
+ feesAsset: { type: 'native' },
70
71
  date,
71
72
  failed,
72
73
  ...(feesPayer ? { feesPayer } : {}),
package/src/api/index.ts CHANGED
@@ -14,7 +14,6 @@ import type {
14
14
  CraftedTransaction,
15
15
  Cursor,
16
16
  FeeEstimation,
17
- GetBlockOptions,
18
17
  ListOperationsOptions,
19
18
  MemoNotSupported,
20
19
  Operation,
@@ -113,8 +112,7 @@ export function createApi() {
113
112
  address: string,
114
113
  options: ListOperationsOptions
115
114
  ): Promise<Page<Operation<MemoNotSupported>>> => listOperations(context, address, options),
116
- getBlock: (context: EvmContext, height: number, options?: GetBlockOptions): Promise<Block> =>
117
- getBlock(context, height, options),
115
+ getBlock: (context: EvmContext, height: number): Promise<Block> => getBlock(context, height),
118
116
  getBlockInfo: (context: EvmContext, height: number): Promise<BlockInfo> =>
119
117
  getBlockInfo(context, height),
120
118
  getValidators: async (
@@ -138,7 +136,6 @@ export function createApi() {
138
136
  _context: EvmContext,
139
137
  intent: TransactionIntent<MemoNotSupported, BufferTxData>
140
138
  ): BufferTxData => craftTransactionData(intent),
141
- readOptions: { getBlock: ['feesAsset'] },
142
139
  } satisfies CoinModuleImpl<EvmConfigInfo, MemoNotSupported, BufferTxData>
143
140
  }
144
141
 
@@ -11,7 +11,19 @@ const USDT = '0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e'
11
11
  const USDC = '0xcebA9300f2b948710d2653dD7B07f33A8B32118C'
12
12
  const CUSD = '0x765DE816845861e75A25fCA122bb6898B8B1282a'
13
13
  const CEUR = '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73'
14
+ const USAT = '0xD2ab3C9A02DBBAB236BfEC45D1d755DF4267F771'
15
+ const KESM = '0x456a3D042C0DbD3db53D5489e98dFb038553B0d0'
14
16
  const USDT_SENDER = '0xfb4863FC2FE52A8131696e4a3a86676cff40A806'
17
+ const USAT_FEE_ONLY = {
18
+ height: 83106787,
19
+ hash: '0x437cfcbf162770f8586be4e263383653e0ccac33949d4fe1fc04e628b208ef80',
20
+ payer: '0xF30C0240a65a21b89B32F1bd0CaDfA6924cADA4E',
21
+ }
22
+ const KESM_FEE_ONLY = {
23
+ height: 83055835,
24
+ hash: '0x0cc6b0258288a498b7836808f107af9591b27c8b05f28ded56c2960a1e254074',
25
+ payer: '0xf0B48e98d0d8089cAa7D03478178482cAb75E0a3',
26
+ }
15
27
 
16
28
  function usdtTransfer(from: string, to: string, amount: bigint): BlockOperation[] {
17
29
  const asset = { type: 'erc20' as const, assetReference: USDT }
@@ -71,12 +83,37 @@ describe('EVM Celo Network', () => {
71
83
  feesAsset: CEUR,
72
84
  operations: [],
73
85
  },
86
+ {
87
+ fee: 'cUSD (before cUSD minted its credits, fee-only transaction)',
88
+ height: 32000002,
89
+ hash: '0x870ee46734ffd61acb692f6b2c338c791178aa3ea82e0334136840ec4696e49d',
90
+ payer: '0xe17ad09b4f3ee94c9616c548cbba5a8c1fc89ceb',
91
+ fees: 604833996674040n,
92
+ feesAsset: CUSD,
93
+ operations: [],
94
+ },
95
+ {
96
+ fee: 'USAT (adapter, fee-only transaction)',
97
+ height: USAT_FEE_ONLY.height,
98
+ hash: USAT_FEE_ONLY.hash,
99
+ payer: USAT_FEE_ONLY.payer,
100
+ fees: 1601n,
101
+ feesAsset: USAT,
102
+ operations: [],
103
+ },
104
+ {
105
+ fee: 'KESm (fee-only transaction)',
106
+ height: KESM_FEE_ONLY.height,
107
+ hash: KESM_FEE_ONLY.hash,
108
+ payer: KESM_FEE_ONLY.payer,
109
+ fees: 148586650352216144n,
110
+ feesAsset: KESM,
111
+ operations: [],
112
+ },
74
113
  ])(
75
114
  'reports a fee paid in $fee in token units, without its fee logs',
76
115
  async ({ height, hash, payer, fees, feesAsset, operations }) => {
77
- const block = await module.getBlock(createMockEvmContext(config), height, {
78
- feesAsset: true,
79
- })
116
+ const block = await module.getBlock(createMockEvmContext(config), height)
80
117
 
81
118
  const tx = block.transactions.find((transaction) => transaction.hash === hash)
82
119
  expect(tx).toMatchObject({
@@ -90,4 +127,108 @@ describe('EVM Celo Network', () => {
90
127
  }
91
128
  )
92
129
  })
130
+
131
+ describe('listOperations with the fee asset', () => {
132
+ const historyContext = createMockEvmContext({
133
+ ...config,
134
+ // forno load-balances backends whose receipt index differs, so a receipt can be missing
135
+ node: { type: 'external', uri: 'https://celo.coin.ledger.com/archive' },
136
+ explorer: {
137
+ type: 'etherscan',
138
+ uri: 'https://proxyetherscan.api.live.ledger.com/v2/api/42220',
139
+ },
140
+ })
141
+ const feeCarrier = (payer: string, contract: string) => ({
142
+ type: 'OUT',
143
+ value: 0n,
144
+ asset: { type: 'native' },
145
+ senders: [payer],
146
+ recipients: [contract],
147
+ })
148
+ const usdtIn = (from: string, to: string, value: bigint) => ({
149
+ type: 'IN',
150
+ value,
151
+ asset: { type: 'erc20', assetReference: USDT },
152
+ senders: [from],
153
+ recipients: [to],
154
+ })
155
+
156
+ it.each([
157
+ {
158
+ fee: 'USD₮ (adapter)',
159
+ address: '0x5362C723476b46a1e65D54E667C00e46b46F6f12',
160
+ height: 80494727,
161
+ hash: '0x45e41cf20fca9b079f8b23510bb9637d14df647cb6d25d85157924cd27df09d7',
162
+ fees: 3176n,
163
+ feesAsset: USDT,
164
+ operations: [usdtIn(USDT_SENDER, '0x5362C723476b46a1e65D54E667C00e46b46F6f12', 7000n)],
165
+ },
166
+ {
167
+ fee: 'USDC (adapter)',
168
+ address: '0x3EE17b4D2eC4E33339eE34f6F2a16429c4C67738',
169
+ height: 80502070,
170
+ hash: '0xf43732d1ca9d43dd6515708669e6083010672aed8a61de4c8d07164c7e3d400d',
171
+ fees: 4072n,
172
+ feesAsset: USDC,
173
+ operations: [usdtIn(USDT_SENDER, '0x3EE17b4D2eC4E33339eE34f6F2a16429c4C67738', 9000n)],
174
+ },
175
+ {
176
+ fee: 'cUSD',
177
+ address: '0xfb6f89fd218A62E39c9bb4D3bb0bc029ec0C6F73',
178
+ height: 82654841,
179
+ hash: '0xa92de9a2b85e285d2bf1c1317386e012034ddfbbd84739e5b5f46fe6c252e748',
180
+ fees: 2255358224402010n,
181
+ feesAsset: CUSD,
182
+ operations: [usdtIn(USDT_SENDER, '0xfb6f89fd218A62E39c9bb4D3bb0bc029ec0C6F73', 5800n)],
183
+ },
184
+ {
185
+ fee: 'USD₮ (fee-only transaction)',
186
+ address: '0x540d7DCfF6dE998Ec5e14AE5e6CDb063eA2eC88B',
187
+ height: 83099643,
188
+ hash: '0x670feaaac5150943eb5090b82f9bc51c06ada222e7c00fad86ad44a37f55ea6f',
189
+ fees: 1601n,
190
+ feesAsset: USDT,
191
+ operations: [feeCarrier('0x540d7DCfF6dE998Ec5e14AE5e6CDb063eA2eC88B', USDT)],
192
+ },
193
+ {
194
+ fee: 'USAT (fee-only transaction)',
195
+ address: USAT_FEE_ONLY.payer,
196
+ height: USAT_FEE_ONLY.height,
197
+ hash: USAT_FEE_ONLY.hash,
198
+ fees: 1601n,
199
+ feesAsset: USAT,
200
+ operations: [feeCarrier(USAT_FEE_ONLY.payer, USAT)],
201
+ },
202
+ {
203
+ fee: 'KESm (fee-only transaction)',
204
+ address: KESM_FEE_ONLY.payer,
205
+ height: KESM_FEE_ONLY.height,
206
+ hash: KESM_FEE_ONLY.hash,
207
+ fees: 148586650352216144n,
208
+ feesAsset: KESM,
209
+ operations: [feeCarrier(KESM_FEE_ONLY.payer, KESM)],
210
+ },
211
+ ])(
212
+ 'reports a fee paid in $fee on every operation, without its fee transfers',
213
+ async ({ address, height, hash, fees, feesAsset, operations }) => {
214
+ const { items } = await module.listOperations(historyContext, address, {
215
+ minHeight: height,
216
+ order: 'asc',
217
+ limit: 10,
218
+ })
219
+
220
+ const txOperations = items.filter((op) => op.tx.hash === hash)
221
+ expect(txOperations).toMatchObject(
222
+ operations.map((operation) => ({
223
+ ...operation,
224
+ tx: {
225
+ fees,
226
+ feesAsset: { type: 'erc20', assetReference: feesAsset },
227
+ feesPayer: address,
228
+ },
229
+ }))
230
+ )
231
+ }
232
+ )
233
+ })
93
234
  })
@@ -48,6 +48,7 @@ describe('EVM Optimism Network', () => {
48
48
  failed: expect.any(Boolean),
49
49
  operations: expect.any(Array),
50
50
  fees: expect.any(BigInt),
51
+ feesAsset: { type: 'native' },
51
52
  feesPayer: expect.any(String),
52
53
  }),
53
54
  ]),
@@ -1,7 +1,6 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { withDefaults } from '@ledgerhq/coin-module-framework/api/index'
5
4
  import { mockEvmContext } from '../../fixtures/context.fixtures'
6
5
  import { getValidatorsPage } from '../../staking/validators'
7
6
  import { createApi, parseCallParams } from '../index'
@@ -30,7 +29,6 @@ describe.each([
30
29
  validateAddress: expect.any(Function),
31
30
  validateIntent: expect.any(Function),
32
31
  craftTransactionData: expect.any(Function),
33
- readOptions: { getBlock: ['feesAsset'] },
34
32
  },
35
33
  ],
36
34
  [
@@ -52,7 +50,6 @@ describe.each([
52
50
  validateAddress: expect.any(Function),
53
51
  validateIntent: expect.any(Function),
54
52
  craftTransactionData: expect.any(Function),
55
- readOptions: { getBlock: ['feesAsset'] },
56
53
  },
57
54
  ],
58
55
  ])('coin-framework methods %s', (_s, _config, methods) => {
@@ -66,12 +63,6 @@ describe.each([
66
63
  expect(impl).not.toHaveProperty(method)
67
64
  }
68
65
  })
69
-
70
- it('honours the fee asset read option on getBlock only', () => {
71
- const api = withDefaults(createApi())
72
- expect(api.supportsReadOption('getBlock', 'feesAsset')).toBe(true)
73
- expect(api.supportsReadOption('listOperations', 'feesAsset')).toBe(false)
74
- })
75
66
  })
76
67
 
77
68
  describe('parseCallParams', () => {
@@ -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
+
35
+ /**
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.
39
+ */
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
+
33
47
  /**
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.
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.
37
53
  */
38
- const CELO_FEE_CURRENCIES: ReadonlyMap<string, { token: string; pattern: FeePattern }> = new Map([
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,14 +181,13 @@ 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
  }
@@ -188,6 +225,42 @@ export function createCeloTokenFeeReader(nodeApi: NodeApi, blockHeight: number):
188
225
  hash,
189
226
  })
190
227
  }
191
- return feeCurrency === null ? undefined : splitTokenFee(feeCurrency, from, erc20Transfers)
228
+ return feeCurrency === null ? undefined : splitTokenFee(feeCurrency, from, erc20Transfers, hash)
229
+ }
230
+ }
231
+
232
+ /** How a Celo transaction paid its fee. */
233
+ export type CeloTransactionFee =
234
+ | { kind: 'native' }
235
+ | { kind: 'token'; tokenFee: TokenFee }
236
+ | { kind: 'unsupported'; feeCurrency: string }
237
+
238
+ /**
239
+ * Reads how a Celo transaction paid its fee, fetching its fee currency alone, for transactions
240
+ * that are not read block by block. A fee currency whose fee logs are not known is reported, not
241
+ * rejected, so that the caller decides whether the fee matters to it.
242
+ *
243
+ * @param nodeApi node of the network
244
+ * @param tx the transaction, with the token transfers of its receipt
245
+ * @throws UnexpectedFeeLogsError when the fee logs do not match a supported fee currency
246
+ */
247
+ export async function readCeloTransactionFee(
248
+ nodeApi: NodeApi,
249
+ tx: Parameters<ReadTokenFee>[0]
250
+ ): Promise<CeloTransactionFee> {
251
+ if (tx.type !== undefined && tx.type !== CIP64_TX_TYPE) return { kind: 'native' }
252
+ if (nodeApi.getFeeCurrency === undefined) {
253
+ throw new FeeCurrencyUnavailableError(
254
+ 'The node cannot report the fee currency of a transaction',
255
+ { hash: tx.hash }
256
+ )
257
+ }
258
+ const feeCurrency = await nodeApi.getFeeCurrency(tx.hash)
259
+ if (feeCurrency === null) return { kind: 'native' }
260
+ if (!CELO_FEE_CURRENCIES.has(feeCurrency.toLowerCase()))
261
+ return { kind: 'unsupported', feeCurrency }
262
+ return {
263
+ kind: 'token',
264
+ tokenFee: splitTokenFee(feeCurrency, tx.from, tx.erc20Transfers, tx.hash),
192
265
  }
193
266
  }
@@ -192,9 +192,9 @@ export async function findTransactionsPaidByOthers(
192
192
  * operation of the transaction for the account, a value-0 `FEES` operation carries the fee
193
193
  * instead, as ADR-016 allows.
194
194
  *
195
- * The fee also moves as base-token transfers (zkSync `0x800A`), which stay as token operations,
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.
195
+ * The fee also moves as base-token transfers (zkSync `0x800A`), which stay as token operations
196
+ * (BACK-11036). ADR-068 removes fee movements in every asset, as `getBlock` does; this function
197
+ * 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
@@ -11,7 +11,6 @@ import type {
11
11
  BlockInfo,
12
12
  BlockOperation,
13
13
  BlockTransaction,
14
- GetBlockOptions,
15
14
  TransferBlockOperation,
16
15
  } from '@ledgerhq/coin-module-framework/api/index'
17
16
  import type { Logger } from '@ledgerhq/coin-module-framework/config'
@@ -30,11 +29,7 @@ import {
30
29
  } from './internalTransactionsFetcher'
31
30
  import { dropRootTraceDuplicates } from './rootTraceDedup'
32
31
 
33
- export async function getBlock(
34
- context: EvmContext,
35
- height: number,
36
- options?: GetBlockOptions
37
- ): Promise<Block> {
32
+ export async function getBlock(context: EvmContext, height: number): Promise<Block> {
38
33
  // Note: to use RPC calls efficiently, the strategy here is:
39
34
  // - fetch the block + transactions in one call (using prefetchTxs=true)
40
35
  // - fetch transaction receipts in one call using eth_getBlockReceipts
@@ -72,9 +67,7 @@ export async function getBlock(
72
67
  result.transactionHashes || result.transactions?.map((tx) => tx.hash) || []
73
68
 
74
69
  const readTokenFee =
75
- options?.feesAsset && config.chainId === CHAIN_ID.CELO
76
- ? createCeloTokenFeeReader(nodeApi, result.height)
77
- : undefined
70
+ config.chainId === CHAIN_ID.CELO ? createCeloTokenFeeReader(nodeApi, result.height) : undefined
78
71
 
79
72
  // Internal transactions only exist inside transactions, so a block with none has nothing to
80
73
  // merge. Skipping the fetch avoids a request for a guaranteed-empty answer — on the `explorer`
@@ -110,17 +103,13 @@ export async function getBlock(
110
103
  const feeCollectorContracts = config.feeCollectorContracts ?? []
111
104
  const feeCollectorTokens = config.feeCollectorTokens ?? []
112
105
  const [paidTransactions, unpaidInternalTxs] = attributeFeeCollectorTransfers(
113
- options?.feesAsset
114
- ? attributeFeeCollectorTokenPayers(transactions, feeCollectorContracts, feeCollectorTokens)
115
- : transactions,
106
+ attributeFeeCollectorTokenPayers(transactions, feeCollectorContracts, feeCollectorTokens),
116
107
  internalTxs,
117
108
  feeCollectorContracts,
118
109
  context.logger
119
110
  )
120
111
  const mergedTransactions = mergeInternalTransactions(
121
- options?.feesAsset
122
- ? dropFeeCollectorTokenTransfers(paidTransactions, feeCollectorContracts, feeCollectorTokens)
123
- : paidTransactions,
112
+ dropFeeCollectorTokenTransfers(paidTransactions, feeCollectorContracts, feeCollectorTokens),
124
113
  unpaidInternalTxs
125
114
  )
126
115
 
@@ -478,9 +467,9 @@ function prefetchedTransactionToBlockTransaction(
478
467
  function withTokenFee(
479
468
  nativeFees: bigint,
480
469
  tokenFee: TokenFee | undefined
481
- ): Pick<BlockTransaction, 'fees' | 'feesAsset'> {
470
+ ): Required<Pick<BlockTransaction, 'fees' | 'feesAsset'>> {
482
471
  return tokenFee === undefined
483
- ? { fees: nativeFees }
472
+ ? { fees: nativeFees, feesAsset: { type: 'native' } }
484
473
  : { fees: tokenFee.fees, feesAsset: tokenFee.feesAsset }
485
474
  }
486
475