@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
@@ -8,7 +8,7 @@ import {
8
8
  UnsupportedFeeCurrencyError,
9
9
  } from '../../errors'
10
10
  import { mockNodeApi } from '../../network/tests/node/node.fixtures'
11
- import { createCeloTokenFeeReader, splitTokenFee } from '../cip64Fees'
11
+ import { createCeloTokenFeeReader, readCeloTransactionFee, splitTokenFee } from '../cip64Fees'
12
12
 
13
13
  const USDT = '0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e'
14
14
  const USDT_ADAPTER = '0x0E2A3e05bc9A16F5292A6170456A710cb89C6f72'
@@ -17,6 +17,9 @@ const USDC = '0xcebA9300f2b948710d2653dD7B07f33A8B32118C'
17
17
  const USDC_ADAPTER = '0x2F25deB3848C207fc8E0c34035B3Ba7fC157602B'
18
18
  const CUSD = '0x765DE816845861e75A25fCA122bb6898B8B1282a'
19
19
  const CEUR = '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73'
20
+ const KESM = '0x456a3D042C0DbD3db53D5489e98dFb038553B0d0'
21
+ const USAT = '0xD2ab3C9A02DBBAB236BfEC45D1d755DF4267F771'
22
+ const USAT_ADAPTER = '0x0357EE22278c922e1D36cFe6b899269b161880C4'
20
23
  const ZERO = '0x0000000000000000000000000000000000000000'
21
24
  const SEQUENCER_VAULT = '0x4200000000000000000000000000000000000011'
22
25
  const FEE_HANDLER = '0xcD437749E43A154C07F3553504c68fBfD56B8778'
@@ -58,6 +61,7 @@ describe('splitTokenFee', () => {
58
61
  expect(splitTokenFee(USDT_ADAPTER, PAYER, [usdtDebit, userTransfer, ...usdtCredits])).toEqual({
59
62
  fees: 3176n,
60
63
  feesAsset: { type: 'erc20', assetReference: USDT },
64
+ feeTransfers: [usdtDebit, ...usdtCredits],
61
65
  transfers: [userTransfer],
62
66
  })
63
67
  })
@@ -66,6 +70,7 @@ describe('splitTokenFee', () => {
66
70
  expect(splitTokenFee(USDC_ADAPTER, PAYER, [usdcDebit, userTransfer, ...usdcCredits])).toEqual({
67
71
  fees: 4072n,
68
72
  feesAsset: { type: 'erc20', assetReference: USDC },
73
+ feeTransfers: [usdcDebit, ...usdcCredits],
69
74
  transfers: [userTransfer],
70
75
  })
71
76
  })
@@ -74,6 +79,7 @@ describe('splitTokenFee', () => {
74
79
  expect(splitTokenFee(CUSD, PAYER, [cusdDebit, ...cusdCredits])).toEqual({
75
80
  fees: 12404402010n + 2255345820000000n,
76
81
  feesAsset: { type: 'erc20', assetReference: CUSD },
82
+ feeTransfers: [cusdDebit, ...cusdCredits],
77
83
  transfers: [],
78
84
  })
79
85
  })
@@ -90,6 +96,7 @@ describe('splitTokenFee', () => {
90
96
  expect(splitTokenFee(CEUR, PAYER, transfers)).toEqual({
91
97
  fees: 2623801530470940n,
92
98
  feesAsset: { type: 'erc20', assetReference: CEUR },
99
+ feeTransfers: transfers,
93
100
  transfers: [],
94
101
  })
95
102
  })
@@ -106,6 +113,7 @@ describe('splitTokenFee', () => {
106
113
  expect(splitTokenFee(USDT_ADAPTER, PAYER, transfers)).toEqual({
107
114
  fees: 2816n,
108
115
  feesAsset: { type: 'erc20', assetReference: USDT },
116
+ feeTransfers: transfers,
109
117
  transfers: [],
110
118
  })
111
119
  })
@@ -154,9 +162,63 @@ describe('splitTokenFee', () => {
154
162
  ).toBe(3176n)
155
163
  })
156
164
 
157
- it('rejects an unsupported fee currency', () => {
158
- expect(() => splitTokenFee(OTHER, PAYER, [usdtDebit, ...usdtCredits])).toThrow(
159
- UnsupportedFeeCurrencyError
165
+ it.each([
166
+ {
167
+ // Fee logs of tx 0x437cfcbf162770f8586be4e263383653e0ccac33949d4fe1fc04e628b208ef80
168
+ name: 'USAT, through a holder',
169
+ feeCurrency: USAT_ADAPTER,
170
+ token: USAT,
171
+ transfers: [
172
+ transfer(USAT, PAYER, USDT_HOLDER, 2032n),
173
+ transfer(USAT, USDT_HOLDER, SEQUENCER_VAULT, 0n),
174
+ transfer(USAT, USDT_HOLDER, FEE_HANDLER, 1601n),
175
+ transfer(USAT, USDT_HOLDER, PAYER, 431n),
176
+ ],
177
+ fees: 1601n,
178
+ },
179
+ {
180
+ // Fee logs of tx 0x0cc6b0258288a498b7836808f107af9591b27c8b05f28ded56c2960a1e254074
181
+ name: 'KESm, from the burn, the re-mint and the payments',
182
+ feeCurrency: KESM,
183
+ token: KESM,
184
+ transfers: [
185
+ transfer(KESM, PAYER, ZERO, 157008136848681234n),
186
+ transfer(KESM, ZERO, PAYER, 157008136848681234n),
187
+ transfer(KESM, PAYER, SEQUENCER_VAULT, 742929512920n),
188
+ transfer(KESM, PAYER, FEE_HANDLER, 148585907422703224n),
189
+ ],
190
+ fees: 148586650352216144n,
191
+ },
192
+ {
193
+ // Fee logs of tx 0x870ee46734ffd61acb692f6b2c338c791178aa3ea82e0334136840ec4696e49d
194
+ name: 'cUSD, from before cUSD minted its credits',
195
+ feeCurrency: CUSD,
196
+ token: CUSD,
197
+ transfers: [
198
+ transfer(CUSD, PAYER, ZERO, 775085251046848n),
199
+ transfer(CUSD, ZERO, PAYER, 775085251046848n),
200
+ transfer(CUSD, PAYER, SEQUENCER_VAULT, 26611431540n),
201
+ transfer(CUSD, PAYER, FEE_HANDLER, 604807385242500n),
202
+ ],
203
+ fees: 604833996674040n,
204
+ },
205
+ ])('reads a $name fee', ({ feeCurrency, token, transfers, fees }) => {
206
+ expect(splitTokenFee(feeCurrency, PAYER, transfers)).toEqual({
207
+ fees,
208
+ feesAsset: { type: 'erc20', assetReference: token },
209
+ feeTransfers: transfers,
210
+ transfers: [],
211
+ })
212
+ })
213
+
214
+ it('rejects an unsupported fee currency, with the transaction in the error', () => {
215
+ expect(() => splitTokenFee(OTHER, PAYER, [usdtDebit, ...usdtCredits], '0xabc')).toThrow(
216
+ expect.objectContaining({
217
+ name: UnsupportedFeeCurrencyError.name,
218
+ feeCurrency: OTHER,
219
+ payer: PAYER,
220
+ hash: '0xabc',
221
+ })
160
222
  )
161
223
  })
162
224
 
@@ -221,19 +283,25 @@ describe('createCeloTokenFeeReader', () => {
221
283
  erc20Transfers: [usdtDebit, ...usdtCredits],
222
284
  }
223
285
 
224
- it('reads nothing for a transaction that is not CIP-64', async () => {
286
+ it('reads a native fee for a transaction that is not CIP-64', async () => {
225
287
  const nodeApi = nodeWithFeeCurrencies(new Map())
226
288
 
227
- await expect(
228
- createCeloTokenFeeReader(nodeApi, 1)({ ...cip64Tx, type: 2 })
229
- ).resolves.toBeUndefined()
289
+ await expect(createCeloTokenFeeReader(nodeApi, 1)({ ...cip64Tx, type: 2 })).resolves.toEqual({
290
+ kind: 'native',
291
+ })
230
292
  expect(nodeApi.getFeeCurrencies).not.toHaveBeenCalled()
231
293
  })
232
294
 
233
- it('reads nothing for a CIP-64 transaction that pays in the native coin', async () => {
295
+ it('reads a native fee for a CIP-64 transaction that pays in the native coin', async () => {
234
296
  const read = createCeloTokenFeeReader(nodeWithFeeCurrencies(new Map([['0xabc', null]])), 1)
235
297
 
236
- await expect(read(cip64Tx)).resolves.toBeUndefined()
298
+ await expect(read(cip64Tx)).resolves.toEqual({ kind: 'native' })
299
+ })
300
+
301
+ it('reports a fee currency whose fee logs are not known', async () => {
302
+ const read = createCeloTokenFeeReader(nodeWithFeeCurrencies(new Map([['0xabc', OTHER]])), 1)
303
+
304
+ await expect(read(cip64Tx)).resolves.toEqual({ kind: 'unsupported', feeCurrency: OTHER })
237
305
  })
238
306
 
239
307
  it('reads the fee currency of a transaction whose receipt has no type', async () => {
@@ -247,8 +315,13 @@ describe('createCeloTokenFeeReader', () => {
247
315
  1
248
316
  )
249
317
 
250
- await expect(read({ ...cip64Tx, type: undefined })).resolves.toMatchObject({ fees: 3176n })
251
- await expect(read({ ...cip64Tx, hash: '0xDEF', type: undefined })).resolves.toBeUndefined()
318
+ await expect(read({ ...cip64Tx, type: undefined })).resolves.toMatchObject({
319
+ kind: 'token',
320
+ tokenFee: { fees: 3176n },
321
+ })
322
+ await expect(read({ ...cip64Tx, hash: '0xDEF', type: undefined })).resolves.toEqual({
323
+ kind: 'native',
324
+ })
252
325
  })
253
326
 
254
327
  it('reads the token fee, fetching the fee currencies of the block once', async () => {
@@ -257,8 +330,8 @@ describe('createCeloTokenFeeReader', () => {
257
330
 
258
331
  const [first, second] = await Promise.all([read(cip64Tx), read(cip64Tx)])
259
332
 
260
- expect(first?.fees).toBe(3176n)
261
- expect(second?.fees).toBe(3176n)
333
+ expect(first).toMatchObject({ kind: 'token', tokenFee: { fees: 3176n } })
334
+ expect(second).toMatchObject({ kind: 'token', tokenFee: { fees: 3176n } })
262
335
  expect(nodeApi.getFeeCurrencies).toHaveBeenCalledTimes(1)
263
336
  expect(nodeApi.getFeeCurrencies).toHaveBeenCalledWith(80494727)
264
337
  })
@@ -275,3 +348,65 @@ describe('createCeloTokenFeeReader', () => {
275
348
  )
276
349
  })
277
350
  })
351
+
352
+ function nodeWithFeeCurrency(feeCurrency: string | null): NodeApi {
353
+ return mockNodeApi({ getFeeCurrency: jest.fn().mockResolvedValue(feeCurrency) })
354
+ }
355
+
356
+ describe('readCeloTransactionFee', () => {
357
+ const cip64Tx = {
358
+ hash: '0xabc',
359
+ from: PAYER,
360
+ type: 0x7b,
361
+ erc20Transfers: [usdtDebit, ...usdtCredits],
362
+ }
363
+
364
+ it('reads a native fee for a transaction that is not CIP-64', async () => {
365
+ const nodeApi = nodeWithFeeCurrency(USDT_ADAPTER)
366
+
367
+ await expect(readCeloTransactionFee(nodeApi, { ...cip64Tx, type: 2 })).resolves.toEqual({
368
+ kind: 'native',
369
+ })
370
+ expect(nodeApi.getFeeCurrency).not.toHaveBeenCalled()
371
+ })
372
+
373
+ it('reads a native fee for a CIP-64 transaction that pays in the native coin', async () => {
374
+ await expect(readCeloTransactionFee(nodeWithFeeCurrency(null), cip64Tx)).resolves.toEqual({
375
+ kind: 'native',
376
+ })
377
+ })
378
+
379
+ it('reads the token fee of a transaction, also when its receipt has no type', async () => {
380
+ const nodeApi = nodeWithFeeCurrency(USDT_ADAPTER)
381
+
382
+ await expect(
383
+ readCeloTransactionFee(nodeApi, { ...cip64Tx, type: undefined })
384
+ ).resolves.toMatchObject({
385
+ kind: 'token',
386
+ tokenFee: { fees: 3176n, feesAsset: { type: 'erc20', assetReference: USDT } },
387
+ })
388
+ expect(nodeApi.getFeeCurrency).toHaveBeenCalledWith('0xabc')
389
+ })
390
+
391
+ it('reports a fee currency whose fee logs are not known', async () => {
392
+ await expect(readCeloTransactionFee(nodeWithFeeCurrency(OTHER), cip64Tx)).resolves.toEqual({
393
+ kind: 'unsupported',
394
+ feeCurrency: OTHER,
395
+ })
396
+ })
397
+
398
+ it('rejects fee logs that do not match a supported fee currency', async () => {
399
+ await expect(
400
+ readCeloTransactionFee(nodeWithFeeCurrency(USDT_ADAPTER), {
401
+ ...cip64Tx,
402
+ erc20Transfers: usdtCredits,
403
+ })
404
+ ).rejects.toThrow(expect.objectContaining({ name: UnexpectedFeeLogsError.name, hash: '0xabc' }))
405
+ })
406
+
407
+ it('rejects a CIP-64 transaction when the node cannot report its fee currency', async () => {
408
+ await expect(readCeloTransactionFee(mockNodeApi(), cip64Tx)).rejects.toThrow(
409
+ FeeCurrencyUnavailableError
410
+ )
411
+ })
412
+ })
@@ -15,7 +15,7 @@ import type { EtherscanInternalTransaction } from '../../types'
15
15
  import type { BlockOperation } from '@ledgerhq/coin-module-framework/api/index'
16
16
  import { ethers } from 'ethers'
17
17
  import { CHAIN_ID, evmDefaults, internalTxSourcesFromList } from '../../config'
18
- import { UnsupportedFeeCurrencyError, UnsupportedRpcMethodError } from '../../errors'
18
+ import { UnexpectedFeeLogsError, UnsupportedRpcMethodError } from '../../errors'
19
19
  import {
20
20
  createMockEvmConfigInfo,
21
21
  createMockEvmContext,
@@ -2346,11 +2346,15 @@ describe('getBlock', () => {
2346
2346
  feeCurrency = USDT_ADAPTER,
2347
2347
  prefetched = true,
2348
2348
  receiptType = 0x7b,
2349
+ status = 1,
2350
+ erc20Transfers = transfers,
2349
2351
  }: {
2350
2352
  chainId?: ChainId
2351
2353
  feeCurrency?: string | null
2352
2354
  prefetched?: boolean
2353
2355
  receiptType?: number | null
2356
+ status?: number
2357
+ erc20Transfers?: ERC20Transfer[]
2354
2358
  } = {}): { context: EvmContext; nodeApi: ReturnType<typeof mockNodeApi> } {
2355
2359
  const context = createMockEvmContext(
2356
2360
  createMockEvmConfigInfo({
@@ -2363,8 +2367,9 @@ describe('getBlock', () => {
2363
2367
  hash: '0xtx1',
2364
2368
  gasUsed: GAS_USED,
2365
2369
  gasPrice: GAS_PRICE,
2370
+ status,
2366
2371
  ...(receiptType !== null ? { type: receiptType } : {}),
2367
- erc20Transfers: transfers,
2372
+ erc20Transfers,
2368
2373
  }
2369
2374
  const nodeApi = mockNodeApi({
2370
2375
  getBlockByHeight: jest.fn().mockResolvedValueOnce(
@@ -2423,10 +2428,45 @@ describe('getBlock', () => {
2423
2428
  expect(nodeApi.getFeeCurrencies).not.toHaveBeenCalled()
2424
2429
  })
2425
2430
 
2426
- it('rejects a block with a transaction paid in an unsupported fee currency', async () => {
2427
- const { context } = mockCeloBlock({ feeCurrency: address2 })
2431
+ // A failed transaction keeps only its fee logs: the reverted call drops its own
2432
+ const feeLogs = transfers.filter((transfer) => transfer !== userTransfer)
2428
2433
 
2429
- await expect(getBlock(context, 12345)).rejects.toThrow(UnsupportedFeeCurrencyError)
2434
+ it.each([
2435
+ ['prefetched with block receipts', true, 1, transfers],
2436
+ ['fetched one by one', false, 1, transfers],
2437
+ ['that failed', true, 0, feeLogs],
2438
+ ])(
2439
+ 'reports no fee and keeps the fee transfers of a transaction %s, paid in an unsupported fee currency',
2440
+ async (_, prefetched, status, erc20Transfers) => {
2441
+ const { context } = mockCeloBlock({
2442
+ feeCurrency: address2,
2443
+ prefetched,
2444
+ status,
2445
+ erc20Transfers,
2446
+ })
2447
+
2448
+ const [tx] = (await getBlock(context, 12345)).transactions
2449
+
2450
+ expect(tx).toMatchObject({
2451
+ failed: status === 0,
2452
+ fees: 0n,
2453
+ feesAsset: { type: 'native' },
2454
+ feesPayer: PAYER,
2455
+ details: expect.objectContaining({ unsupportedFeeCurrency: address2 }),
2456
+ })
2457
+ expect(tx.operations).toHaveLength(erc20Transfers.length * 2)
2458
+ expect(context.logger).toHaveBeenCalledWith(
2459
+ 'warn',
2460
+ expect.any(String),
2461
+ expect.objectContaining({ hash: '0xtx1', feeCurrency: address2 })
2462
+ )
2463
+ }
2464
+ )
2465
+
2466
+ it('rejects a block with fee logs that do not match a supported fee currency', async () => {
2467
+ const { context } = mockCeloBlock({ erc20Transfers: [userTransfer] })
2468
+
2469
+ await expect(getBlock(context, 12345)).rejects.toThrow(UnexpectedFeeLogsError)
2430
2470
  })
2431
2471
  })
2432
2472
  })