@ledgerhq/coin-evm 16.4.2 → 16.5.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 (37) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/adapters/etherscan.d.ts.map +1 -1
  3. package/lib/adapters/etherscan.js +4 -1
  4. package/lib/adapters/etherscan.js.map +1 -1
  5. package/lib/config.d.ts +9 -0
  6. package/lib/config.d.ts.map +1 -1
  7. package/lib/config.js +4 -0
  8. package/lib/config.js.map +1 -1
  9. package/lib/logic/getBlock.d.ts.map +1 -1
  10. package/lib/logic/getBlock.js +76 -7
  11. package/lib/logic/getBlock.js.map +1 -1
  12. package/lib/logic/internalTransactionsFetcher.d.ts +20 -0
  13. package/lib/logic/internalTransactionsFetcher.d.ts.map +1 -1
  14. package/lib/logic/internalTransactionsFetcher.js +57 -0
  15. package/lib/logic/internalTransactionsFetcher.js.map +1 -1
  16. package/lib-es/adapters/etherscan.d.ts.map +1 -1
  17. package/lib-es/adapters/etherscan.js +4 -1
  18. package/lib-es/adapters/etherscan.js.map +1 -1
  19. package/lib-es/config.d.ts +9 -0
  20. package/lib-es/config.d.ts.map +1 -1
  21. package/lib-es/config.js +4 -0
  22. package/lib-es/config.js.map +1 -1
  23. package/lib-es/logic/getBlock.d.ts.map +1 -1
  24. package/lib-es/logic/getBlock.js +77 -8
  25. package/lib-es/logic/getBlock.js.map +1 -1
  26. package/lib-es/logic/internalTransactionsFetcher.d.ts +20 -0
  27. package/lib-es/logic/internalTransactionsFetcher.d.ts.map +1 -1
  28. package/lib-es/logic/internalTransactionsFetcher.js +55 -0
  29. package/lib-es/logic/internalTransactionsFetcher.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/adapters/etherscan.test.ts +16 -0
  32. package/src/adapters/etherscan.ts +4 -1
  33. package/src/config.ts +11 -0
  34. package/src/logic/getBlock.ts +117 -7
  35. package/src/logic/internalTransactionsFetcher.ts +72 -0
  36. package/src/logic/tests/getBlock.test.ts +418 -2
  37. package/src/logic/tests/internalTransactionsFetcher.test.ts +141 -1
@@ -1,7 +1,7 @@
1
1
  // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import type { ChainId, EvmContext } from '../../config'
4
+ import type { ChainId, EvmConfigInfo, EvmContext } from '../../config'
5
5
  import type {
6
6
  BlockByHeightResult,
7
7
  BlockReceiptInfo,
@@ -13,7 +13,8 @@ import type {
13
13
  } from '../../network/node/types'
14
14
  import type { EtherscanInternalTransaction } from '../../types'
15
15
  import type { BlockOperation } from '@ledgerhq/coin-module-framework/api/index'
16
- import { CHAIN_ID, internalTxSourcesFromList } from '../../config'
16
+ import { ethers } from 'ethers'
17
+ import { CHAIN_ID, evmDefaults, internalTxSourcesFromList } from '../../config'
17
18
  import { UnsupportedRpcMethodError } from '../../errors'
18
19
  import {
19
20
  createMockEvmConfigInfo,
@@ -575,6 +576,129 @@ describe('getBlock', () => {
575
576
  })
576
577
  })
577
578
 
579
+ describe('staking operation type', () => {
580
+ const seiStakingPrecompile = '0x0000000000000000000000000000000000001005'
581
+ const delegateCalldata = `${ethers.id('delegate(string)').slice(0, 10)}00000000`
582
+
583
+ it('sets ledgerOpType on a prefetched staking call', async () => {
584
+ const context = createMockEvmContext(
585
+ createMockEvmConfigInfo({ chainId: CHAIN_ID.SEI_EVM, node: mkExternalNode() })
586
+ )
587
+
588
+ mockGetBlockNodeApi(
589
+ mockNodeApi({
590
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
591
+ makeNodeBlock({
592
+ transactions: [
593
+ makeNodeBlockTx({
594
+ hash: '0xtx1',
595
+ to: seiStakingPrecompile,
596
+ input: delegateCalldata,
597
+ }),
598
+ ],
599
+ })
600
+ ),
601
+ getBlockReceipts: jest
602
+ .fn()
603
+ .mockResolvedValueOnce([makeNodeBlockReceipt({ hash: '0xtx1' })]),
604
+ getTransaction: jest.fn(),
605
+ })
606
+ )
607
+
608
+ const result = await getBlock(context, 12345)
609
+
610
+ expect(result.transactions[0].details).toMatchObject({
611
+ ledgerOpType: 'DELEGATE',
612
+ contractInteraction: 'SmartContractInteraction',
613
+ })
614
+ })
615
+
616
+ it('sets ledgerOpType on a staking call when falling back to getTransaction', async () => {
617
+ const context = createMockEvmContext(
618
+ createMockEvmConfigInfo({ chainId: CHAIN_ID.SEI_EVM, node: mkExternalNode() })
619
+ )
620
+
621
+ mockGetBlockNodeApi(
622
+ mockNodeApi({
623
+ getBlockByHeight: jest
624
+ .fn()
625
+ .mockResolvedValueOnce(makeNodeBlock({ transactionHashes: ['0xtx1'] })),
626
+ getBlockReceipts: jest.fn(),
627
+ getTransaction: jest.fn().mockResolvedValueOnce(
628
+ makeNodeTxInfo({
629
+ to: seiStakingPrecompile,
630
+ input: delegateCalldata,
631
+ })
632
+ ),
633
+ })
634
+ )
635
+
636
+ const result = await getBlock(context, 12345)
637
+
638
+ expect(result.transactions[0].details).toMatchObject({ ledgerOpType: 'DELEGATE' })
639
+ })
640
+
641
+ it('omits ledgerOpType when the staking selector targets another contract', async () => {
642
+ const context = createMockEvmContext(
643
+ createMockEvmConfigInfo({ chainId: CHAIN_ID.SEI_EVM, node: mkExternalNode() })
644
+ )
645
+
646
+ mockGetBlockNodeApi(
647
+ mockNodeApi({
648
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
649
+ makeNodeBlock({
650
+ transactions: [
651
+ makeNodeBlockTx({
652
+ hash: '0xtx1',
653
+ to: address2,
654
+ input: delegateCalldata,
655
+ }),
656
+ ],
657
+ })
658
+ ),
659
+ getBlockReceipts: jest
660
+ .fn()
661
+ .mockResolvedValueOnce([makeNodeBlockReceipt({ hash: '0xtx1' })]),
662
+ getTransaction: jest.fn(),
663
+ })
664
+ )
665
+
666
+ const result = await getBlock(context, 12345)
667
+
668
+ expect(result.transactions[0].details).not.toHaveProperty('ledgerOpType')
669
+ })
670
+
671
+ it('omits ledgerOpType on a chain without staking contracts', async () => {
672
+ const context = createMockEvmContext(
673
+ createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
674
+ )
675
+
676
+ mockGetBlockNodeApi(
677
+ mockNodeApi({
678
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
679
+ makeNodeBlock({
680
+ transactions: [
681
+ makeNodeBlockTx({
682
+ hash: '0xtx1',
683
+ to: seiStakingPrecompile,
684
+ input: delegateCalldata,
685
+ }),
686
+ ],
687
+ })
688
+ ),
689
+ getBlockReceipts: jest
690
+ .fn()
691
+ .mockResolvedValueOnce([makeNodeBlockReceipt({ hash: '0xtx1' })]),
692
+ getTransaction: jest.fn(),
693
+ })
694
+ )
695
+
696
+ const result = await getBlock(context, 12345)
697
+
698
+ expect(result.transactions[0].details).not.toHaveProperty('ledgerOpType')
699
+ })
700
+ })
701
+
578
702
  it('falls back to per-transaction calls when bulk receipts are unavailable', async () => {
579
703
  const context = createMockEvmContext(
580
704
  createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
@@ -854,6 +978,7 @@ describe('getBlock', () => {
854
978
  (op) => op.type === 'transfer' && op.asset.type === 'native'
855
979
  )
856
980
  expect(nativeOps).toHaveLength(2)
981
+ expect(result.transactions[0].details).not.toHaveProperty('internalTransfers')
857
982
  })
858
983
 
859
984
  it("keeps a nested internal transaction that does not match the coin tx's native transfer", async () => {
@@ -926,6 +1051,288 @@ describe('getBlock', () => {
926
1051
  }),
927
1052
  ])
928
1053
  )
1054
+ expect(result.transactions[0].details).toEqual({
1055
+ gasPrice: '20000000000',
1056
+ internalTransfers: [
1057
+ {
1058
+ from: safeEncodeEIP55(address2, testLogger),
1059
+ to: safeEncodeEIP55(nestedRecipient, testLogger),
1060
+ value: nestedAmount.toString(),
1061
+ },
1062
+ ],
1063
+ })
1064
+ })
1065
+
1066
+ describe('zkSync bootloader fee prepayment and refund', () => {
1067
+ // Values of tx 0x13600459c61346e7195861973c04158d1b8b86adde05028066ae16ec852ac00f (zkSync Era,
1068
+ // height 4419528): gasLimit 758351, gasUsed 407022, gasPrice 0.25 gwei. The sender prepays
1069
+ // gasLimit × gasPrice to the bootloader, which refunds the unused gas.
1070
+ const USER = '0xbEB30f27e61eFFF46Aa27dcE18E23ee8D5A7c6a4'
1071
+ const BOOTLOADER = '0x0000000000000000000000000000000000008001'
1072
+ const L2_BASE_TOKEN = '0x000000000000000000000000000000000000800A'
1073
+ const GAS_PRICE = '250000000'
1074
+ const GAS_USED = '407022'
1075
+ const PREPAYMENT = 189587750000000n
1076
+ const REFUND = 87832250000000n
1077
+ const FEE = 101755500000000n
1078
+
1079
+ function userNativeDelta(operations: BlockOperation[]): bigint {
1080
+ return operations.reduce(
1081
+ (sum, op) =>
1082
+ op.type === 'transfer' && op.asset.type === 'native' && op.address === USER
1083
+ ? sum + op.amount
1084
+ : sum,
1085
+ 0n
1086
+ )
1087
+ }
1088
+
1089
+ function bootloaderNativeOps(operations: BlockOperation[]): BlockOperation[] {
1090
+ return operations.filter(
1091
+ (op) =>
1092
+ op.type === 'transfer' &&
1093
+ op.asset.type === 'native' &&
1094
+ (op.address.toLowerCase() === BOOTLOADER || op.peer?.toLowerCase() === BOOTLOADER)
1095
+ )
1096
+ }
1097
+
1098
+ function mockBulkBlock(
1099
+ network: Pick<EvmConfigInfo, 'chainId' | 'feeCollectorContracts'>
1100
+ ): EvmContext {
1101
+ const context = createMockEvmContext(
1102
+ createMockEvmConfigInfo({
1103
+ ...network,
1104
+ node: mkExternalNode(),
1105
+ explorer: mkEtherscanExplorer(),
1106
+ })
1107
+ )
1108
+ mockGetBlockNodeApi(
1109
+ mockNodeApi({
1110
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
1111
+ makeNodeBlock({
1112
+ transactions: [
1113
+ makeNodeBlockTx({ from: USER, to: address2, value: '0', gasPrice: GAS_PRICE }),
1114
+ ],
1115
+ })
1116
+ ),
1117
+ getBlockReceipts: jest.fn().mockResolvedValueOnce([
1118
+ makeNodeBlockReceipt({
1119
+ gasUsed: GAS_USED,
1120
+ gasPrice: GAS_PRICE,
1121
+ erc20Transfers: [
1122
+ makeNodeErc20Transfer({
1123
+ asset: { type: 'erc20', assetReference: L2_BASE_TOKEN },
1124
+ from: USER,
1125
+ to: BOOTLOADER,
1126
+ value: PREPAYMENT.toString(),
1127
+ }),
1128
+ makeNodeErc20Transfer({
1129
+ asset: { type: 'erc20', assetReference: L2_BASE_TOKEN },
1130
+ from: BOOTLOADER,
1131
+ to: USER,
1132
+ value: REFUND.toString(),
1133
+ }),
1134
+ ],
1135
+ }),
1136
+ ]),
1137
+ getTransaction: jest.fn(),
1138
+ })
1139
+ )
1140
+ return context
1141
+ }
1142
+
1143
+ it.each([
1144
+ ['ZKsync', evmDefaults.zksync],
1145
+ ['ZKsync Sepolia', evmDefaults.zksync_sepolia],
1146
+ ])(
1147
+ 'on %s, drops the native prepayment leg so the sender pays the fee once',
1148
+ async (_, network) => {
1149
+ const context = mockBulkBlock({
1150
+ chainId: network.chainId,
1151
+ feeCollectorContracts: network.feeCollectorContracts,
1152
+ })
1153
+ jest.mocked(getInternalTransactionsByBlock).mockResolvedValueOnce([
1154
+ makeExplorerInternalTransaction({
1155
+ from: USER,
1156
+ to: BOOTLOADER,
1157
+ value: PREPAYMENT.toString(),
1158
+ }),
1159
+ ])
1160
+
1161
+ const [tx] = (await getBlock(context, 12345)).transactions
1162
+
1163
+ expect(bootloaderNativeOps(tx.operations)).toEqual([])
1164
+ expect(userNativeDelta(tx.operations)).toBe(0n)
1165
+ expect(tx.fees).toBe(FEE)
1166
+ expect(tx.feesPayer).toBe(USER)
1167
+ }
1168
+ )
1169
+
1170
+ it('drops a native refund leg too, and keeps the other internal transfers and the base-token legs', async () => {
1171
+ const context = mockBulkBlock({
1172
+ chainId: CHAIN_ID.ZKSYNC,
1173
+ feeCollectorContracts: [BOOTLOADER],
1174
+ })
1175
+ jest.mocked(getInternalTransactionsByBlock).mockResolvedValueOnce([
1176
+ makeExplorerInternalTransaction({
1177
+ from: USER,
1178
+ to: BOOTLOADER,
1179
+ value: PREPAYMENT.toString(),
1180
+ }),
1181
+ makeExplorerInternalTransaction({ from: BOOTLOADER, to: USER, value: REFUND.toString() }),
1182
+ makeExplorerInternalTransaction({ from: address2, to: USER, value: '1000' }),
1183
+ ])
1184
+
1185
+ const [tx] = (await getBlock(context, 12345)).transactions
1186
+
1187
+ expect(bootloaderNativeOps(tx.operations)).toEqual([])
1188
+ expect(userNativeDelta(tx.operations)).toBe(1000n)
1189
+ expect(tx.operations).toEqual(
1190
+ expect.arrayContaining([
1191
+ expect.objectContaining({
1192
+ address: USER,
1193
+ asset: { type: 'erc20', assetReference: L2_BASE_TOKEN },
1194
+ amount: -PREPAYMENT,
1195
+ }),
1196
+ expect.objectContaining({
1197
+ address: USER,
1198
+ asset: { type: 'erc20', assetReference: L2_BASE_TOKEN },
1199
+ amount: REFUND,
1200
+ }),
1201
+ ])
1202
+ )
1203
+ })
1204
+
1205
+ it('charges the fee to a paymaster that prepays it, not to the sender', async () => {
1206
+ // Values of tx 0x23f32c7199a6568f2c551cd0f1e29a10780a811128037efa7691cfc32cf4615d (zkSync
1207
+ // Era, height 72812813): the paymaster prepays gasLimit 1100000 × 0.04525 gwei to the
1208
+ // bootloader and gets the unused gas back, and the sender moves no ETH. Blockscout only
1209
+ // reports the prepayment as an internal transaction.
1210
+ const SENDER = '0x1B4a3d0A5712362FA06CDD4930b9ca720Ed6db18'
1211
+ const PAYMASTER = '0xA2Aac7bC9725c36ad9B12D2407dF8de6B2B68359'
1212
+ const PAYMASTER_PREPAYMENT = 49775000000000n
1213
+ const PAYMASTER_REFUND = 42357348500000n
1214
+ const PAYMASTER_FEE = 7417651500000n
1215
+ const context = createMockEvmContext(
1216
+ createMockEvmConfigInfo({
1217
+ chainId: CHAIN_ID.ZKSYNC,
1218
+ feeCollectorContracts: [BOOTLOADER],
1219
+ node: mkExternalNode(),
1220
+ explorer: mkEtherscanExplorer(),
1221
+ })
1222
+ )
1223
+ mockGetBlockNodeApi(
1224
+ mockNodeApi({
1225
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
1226
+ makeNodeBlock({
1227
+ transactions: [
1228
+ makeNodeBlockTx({ from: SENDER, to: address2, value: '0', gasPrice: '45250000' }),
1229
+ ],
1230
+ })
1231
+ ),
1232
+ getBlockReceipts: jest.fn().mockResolvedValueOnce([
1233
+ makeNodeBlockReceipt({
1234
+ gasUsed: '163926',
1235
+ gasPrice: '45250000',
1236
+ erc20Transfers: [
1237
+ makeNodeErc20Transfer({
1238
+ asset: { type: 'erc20', assetReference: L2_BASE_TOKEN },
1239
+ from: PAYMASTER,
1240
+ to: BOOTLOADER,
1241
+ value: PAYMASTER_PREPAYMENT.toString(),
1242
+ }),
1243
+ makeNodeErc20Transfer({
1244
+ asset: { type: 'erc20', assetReference: L2_BASE_TOKEN },
1245
+ from: BOOTLOADER,
1246
+ to: PAYMASTER,
1247
+ value: PAYMASTER_REFUND.toString(),
1248
+ }),
1249
+ ],
1250
+ }),
1251
+ ]),
1252
+ getTransaction: jest.fn(),
1253
+ })
1254
+ )
1255
+ jest.mocked(getInternalTransactionsByBlock).mockResolvedValueOnce([
1256
+ makeExplorerInternalTransaction({
1257
+ from: PAYMASTER.toLowerCase(),
1258
+ to: BOOTLOADER,
1259
+ value: PAYMASTER_PREPAYMENT.toString(),
1260
+ }),
1261
+ ])
1262
+
1263
+ const [tx] = (await getBlock(context, 12345)).transactions
1264
+
1265
+ const nativeDelta = (account: string): bigint =>
1266
+ tx.operations.reduce(
1267
+ (sum, op) =>
1268
+ op.type === 'transfer' && op.asset.type === 'native' && op.address === account
1269
+ ? sum + op.amount
1270
+ : sum,
1271
+ 0n
1272
+ )
1273
+ expect(tx.feesPayer).toBe(PAYMASTER)
1274
+ expect(tx.fees).toBe(PAYMASTER_FEE)
1275
+ expect(bootloaderNativeOps(tx.operations)).toEqual([])
1276
+ expect(nativeDelta(SENDER)).toBe(0n)
1277
+ expect(nativeDelta(PAYMASTER)).toBe(0n)
1278
+ })
1279
+
1280
+ it('drops the native prepayment leg when transactions are fetched one by one', async () => {
1281
+ const context = createMockEvmContext(
1282
+ createMockEvmConfigInfo({
1283
+ chainId: CHAIN_ID.ZKSYNC,
1284
+ feeCollectorContracts: [BOOTLOADER],
1285
+ node: mkExternalNode(),
1286
+ explorer: mkEtherscanExplorer(),
1287
+ })
1288
+ )
1289
+ mockGetBlockNodeApi(
1290
+ mockNodeApi({
1291
+ getBlockByHeight: jest
1292
+ .fn()
1293
+ .mockResolvedValueOnce(makeNodeBlock({ transactionHashes: ['0xtx1'] })),
1294
+ getBlockReceipts: jest.fn(),
1295
+ getTransaction: jest.fn().mockResolvedValueOnce(
1296
+ makeNodeTxInfo({
1297
+ from: USER,
1298
+ to: address2,
1299
+ value: '0',
1300
+ gasUsed: GAS_USED,
1301
+ gasPrice: GAS_PRICE,
1302
+ })
1303
+ ),
1304
+ })
1305
+ )
1306
+ jest.mocked(getInternalTransactionsByBlock).mockResolvedValueOnce([
1307
+ makeExplorerInternalTransaction({
1308
+ from: USER,
1309
+ to: BOOTLOADER,
1310
+ value: PREPAYMENT.toString(),
1311
+ }),
1312
+ ])
1313
+
1314
+ const [tx] = (await getBlock(context, 12345)).transactions
1315
+
1316
+ expect(bootloaderNativeOps(tx.operations)).toEqual([])
1317
+ expect(userNativeDelta(tx.operations)).toBe(0n)
1318
+ expect(tx.fees).toBe(FEE)
1319
+ })
1320
+
1321
+ it('keeps native transfers to the bootloader address when the network lists no fee collector', async () => {
1322
+ const context = mockBulkBlock({ chainId: CHAIN_ID.ETHEREUM })
1323
+ jest.mocked(getInternalTransactionsByBlock).mockResolvedValueOnce([
1324
+ makeExplorerInternalTransaction({
1325
+ from: USER,
1326
+ to: BOOTLOADER,
1327
+ value: PREPAYMENT.toString(),
1328
+ }),
1329
+ ])
1330
+
1331
+ const [tx] = (await getBlock(context, 12345)).transactions
1332
+
1333
+ expect(bootloaderNativeOps(tx.operations)).toHaveLength(2)
1334
+ expect(userNativeDelta(tx.operations)).toBe(-PREPAYMENT)
1335
+ })
929
1336
  })
930
1337
 
931
1338
  it('when explorer is not etherscan like, falls back to node.traceBlockErigon', async () => {
@@ -964,6 +1371,15 @@ describe('getBlock', () => {
964
1371
 
965
1372
  const encodedFrom = safeEncodeEIP55(address1, testLogger)
966
1373
  const encodedTo = safeEncodeEIP55(address2, testLogger)
1374
+ expect(result.transactions[0].details).toMatchObject({
1375
+ internalTransfers: [
1376
+ {
1377
+ from: encodedFrom,
1378
+ to: encodedTo,
1379
+ value: amount.toString(),
1380
+ },
1381
+ ],
1382
+ })
967
1383
  expect(result.transactions[0].operations).toEqual(
968
1384
  expect.arrayContaining([
969
1385
  expect.objectContaining({
@@ -15,7 +15,12 @@ import {
15
15
  import { getInternalTransactionsByBlock as getBlockscoutV2InternalTransactionsByBlock } from '../../network/blockscoutV2'
16
16
  import { getInternalTransactionsByBlock } from '../../network/explorer/etherscan'
17
17
  import { mockNodeApi } from '../../network/tests/node/node.fixtures'
18
- import { composeInternalTxsFetcher, makeSourceFetchers } from '../internalTransactionsFetcher'
18
+ import {
19
+ composeInternalTxsFetcher,
20
+ dropFeeCollectorTransfers,
21
+ findFeeCollectorPayer,
22
+ makeSourceFetchers,
23
+ } from '../internalTransactionsFetcher'
19
24
 
20
25
  jest.mock('../../network/explorer/etherscan', () => ({
21
26
  getInternalTransactionsByBlock: jest.fn().mockResolvedValue([]),
@@ -322,3 +327,138 @@ describe('makeSourceFetchers', () => {
322
327
  await expect(fetchers.empty(1)).resolves.toEqual(new Map<string, BlockOperation[]>())
323
328
  })
324
329
  })
330
+
331
+ const BOOTLOADER = '0x0000000000000000000000000000000000008001'
332
+ const USER = '0xbEB30f27e61eFFF46Aa27dcE18E23ee8D5A7c6a4'
333
+ const OTHER = '0x0459a84952C85c6E9345e5d22d9B110790a5e1Ba'
334
+
335
+ function nativeTransfer(address: string, peer: string | undefined, amount: bigint): BlockOperation {
336
+ return {
337
+ type: 'transfer',
338
+ address,
339
+ ...(peer !== undefined ? { peer } : {}),
340
+ asset: { type: 'native' },
341
+ amount,
342
+ }
343
+ }
344
+
345
+ describe('dropFeeCollectorTransfers', () => {
346
+ it('drops the native transfers between a fee collector and the fee payer and keeps the other operations', () => {
347
+ const otherTransfer = nativeTransfer(USER, OTHER, -7n)
348
+ const unknownPeer = nativeTransfer(USER, undefined, -3n)
349
+ const baseTokenLeg: BlockOperation = {
350
+ type: 'transfer',
351
+ address: USER,
352
+ peer: BOOTLOADER,
353
+ asset: { type: 'erc20', assetReference: '0x000000000000000000000000000000000000800A' },
354
+ amount: -5n,
355
+ }
356
+ const otherOperation: BlockOperation = { type: 'other' }
357
+ const internalTxs = new Map<string, BlockOperation[]>([
358
+ [
359
+ '0x1',
360
+ [
361
+ nativeTransfer(USER, BOOTLOADER, -5n),
362
+ nativeTransfer(BOOTLOADER, USER, 5n),
363
+ otherTransfer,
364
+ unknownPeer,
365
+ baseTokenLeg,
366
+ otherOperation,
367
+ ],
368
+ ],
369
+ ['0x2', [nativeTransfer(BOOTLOADER, USER, 2n), nativeTransfer(USER, BOOTLOADER, -2n)]],
370
+ ])
371
+ const feesPayers = new Map([
372
+ ['0x1', USER],
373
+ ['0x2', USER],
374
+ ])
375
+
376
+ const result = dropFeeCollectorTransfers(internalTxs, [BOOTLOADER], feesPayers)
377
+
378
+ expect(result.get('0x1')).toEqual([otherTransfer, unknownPeer, baseTokenLeg, otherOperation])
379
+ expect(result.get('0x2')).toEqual([])
380
+ })
381
+
382
+ it('keeps the native transfers between a fee collector and an account that is not the fee payer', () => {
383
+ // A zkSync paymaster prepays the fee of USER's transaction and gets the unused gas back
384
+ const paymasterLegs = [
385
+ nativeTransfer(OTHER, BOOTLOADER, -5n),
386
+ nativeTransfer(BOOTLOADER, OTHER, 5n),
387
+ nativeTransfer(BOOTLOADER, OTHER, 2n),
388
+ nativeTransfer(OTHER, BOOTLOADER, -2n),
389
+ ]
390
+ const internalTxs = new Map<string, BlockOperation[]>([['0x1', paymasterLegs]])
391
+
392
+ const result = dropFeeCollectorTransfers(internalTxs, [BOOTLOADER], new Map([['0x1', USER]]))
393
+
394
+ expect(result.get('0x1')).toEqual(paymasterLegs)
395
+ })
396
+
397
+ it('keeps the operations of a transaction without a known fee payer', () => {
398
+ const operations = [nativeTransfer(USER, BOOTLOADER, -5n), nativeTransfer(BOOTLOADER, USER, 5n)]
399
+ const internalTxs = new Map<string, BlockOperation[]>([['0x1', operations]])
400
+
401
+ expect(dropFeeCollectorTransfers(internalTxs, [BOOTLOADER], new Map()).get('0x1')).toEqual(
402
+ operations
403
+ )
404
+ })
405
+
406
+ it('matches the fee collector and fee payer addresses whatever their case', () => {
407
+ const internalTxs = new Map<string, BlockOperation[]>([
408
+ ['0x1', [nativeTransfer(USER.toUpperCase().replace('0X', '0x'), OTHER.toLowerCase(), -5n)]],
409
+ ])
410
+
411
+ expect(
412
+ dropFeeCollectorTransfers(internalTxs, [OTHER], new Map([['0x1', USER.toLowerCase()]])).get(
413
+ '0x1'
414
+ )
415
+ ).toEqual([])
416
+ })
417
+ })
418
+
419
+ describe('findFeeCollectorPayer', () => {
420
+ const PAYMASTER = '0xA2Aac7bC9725c36ad9B12D2407dF8de6B2B68359'
421
+
422
+ it('finds a paymaster that prepays the fee to the fee collector', () => {
423
+ const operations = [
424
+ nativeTransfer(PAYMASTER, BOOTLOADER, -5n),
425
+ nativeTransfer(BOOTLOADER, PAYMASTER, 5n),
426
+ ]
427
+
428
+ expect(findFeeCollectorPayer(operations, [BOOTLOADER], USER)).toBe(PAYMASTER.toLowerCase())
429
+ })
430
+
431
+ it('finds no other payer when the sender prepays the fee', () => {
432
+ const operations = [
433
+ nativeTransfer(USER.toLowerCase(), BOOTLOADER, -5n),
434
+ nativeTransfer(BOOTLOADER, USER, 5n),
435
+ ]
436
+
437
+ expect(findFeeCollectorPayer(operations, [BOOTLOADER], USER)).toBeUndefined()
438
+ })
439
+
440
+ it('finds no payer without a native prepayment to the fee collector', () => {
441
+ const operations: BlockOperation[] = [
442
+ nativeTransfer(BOOTLOADER, PAYMASTER, 5n),
443
+ nativeTransfer(PAYMASTER, OTHER, -5n),
444
+ {
445
+ type: 'transfer',
446
+ address: PAYMASTER,
447
+ peer: BOOTLOADER,
448
+ asset: { type: 'erc20', assetReference: '0x000000000000000000000000000000000000800A' },
449
+ amount: -5n,
450
+ },
451
+ ]
452
+
453
+ expect(findFeeCollectorPayer(operations, [BOOTLOADER], USER)).toBeUndefined()
454
+ })
455
+
456
+ it('finds no payer when several accounts other than the sender prepay', () => {
457
+ const operations = [
458
+ nativeTransfer(PAYMASTER, BOOTLOADER, -5n),
459
+ nativeTransfer(OTHER, BOOTLOADER, -2n),
460
+ ]
461
+
462
+ expect(findFeeCollectorPayer(operations, [BOOTLOADER], USER)).toBeUndefined()
463
+ })
464
+ })