|
| 1 | +import { AztecAddress } from '@aztec/foundation/aztec-address'; |
| 2 | +import { EthAddress } from '@aztec/foundation/eth-address'; |
| 3 | +import { Fr } from '@aztec/foundation/fields'; |
| 4 | +import { type Logger, createLogger } from '@aztec/foundation/log'; |
| 5 | +import { GovernanceProposerAbi } from '@aztec/l1-artifacts/GovernanceProposerAbi'; |
| 6 | +import { TestERC20Abi } from '@aztec/l1-artifacts/TestERC20Abi'; |
| 7 | +import { TestERC20Bytecode } from '@aztec/l1-artifacts/TestERC20Bytecode'; |
| 8 | + |
| 9 | +import { type Anvil } from '@viem/anvil'; |
| 10 | +import { |
| 11 | + type Chain, |
| 12 | + type GetContractReturnType, |
| 13 | + type HttpTransport, |
| 14 | + type PublicClient, |
| 15 | + encodeFunctionData, |
| 16 | + getContract, |
| 17 | +} from 'viem'; |
| 18 | +import { type PrivateKeyAccount, privateKeyToAccount } from 'viem/accounts'; |
| 19 | +import { foundry } from 'viem/chains'; |
| 20 | + |
| 21 | +import { DefaultL1ContractsConfig } from '../config.js'; |
| 22 | +import { type L1Clients, createL1Clients, deployL1Contract, deployL1Contracts } from '../deploy_l1_contracts.js'; |
| 23 | +import { L1TxUtils } from '../l1_tx_utils.js'; |
| 24 | +import { startAnvil } from '../test/start_anvil.js'; |
| 25 | +import { FormattedViemError } from '../utils.js'; |
| 26 | +import { ForwarderContract } from './forwarder.js'; |
| 27 | + |
| 28 | +describe('Forwarder', () => { |
| 29 | + let anvil: Anvil; |
| 30 | + let rpcUrl: string; |
| 31 | + let privateKey: PrivateKeyAccount; |
| 32 | + let logger: Logger; |
| 33 | + |
| 34 | + let vkTreeRoot: Fr; |
| 35 | + let protocolContractTreeRoot: Fr; |
| 36 | + // let initialValidators: EthAddress[]; |
| 37 | + let l2FeeJuiceAddress: AztecAddress; |
| 38 | + let walletClient: L1Clients['walletClient']; |
| 39 | + let publicClient: L1Clients['publicClient']; |
| 40 | + let forwarder: ForwarderContract; |
| 41 | + let l1TxUtils: L1TxUtils; |
| 42 | + // let rollupAddress: EthAddress; |
| 43 | + let govProposerAddress: EthAddress; |
| 44 | + let tokenAddress: EthAddress; |
| 45 | + let tokenContract: GetContractReturnType<typeof TestERC20Abi, PublicClient<HttpTransport, Chain>>; |
| 46 | + beforeAll(async () => { |
| 47 | + logger = createLogger('ethereum:test:forwarder'); |
| 48 | + // this is the 6th address that gets funded by the junk mnemonic |
| 49 | + privateKey = privateKeyToAccount('0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba'); |
| 50 | + vkTreeRoot = Fr.random(); |
| 51 | + protocolContractTreeRoot = Fr.random(); |
| 52 | + // initialValidators = times(3, EthAddress.random); |
| 53 | + l2FeeJuiceAddress = await AztecAddress.random(); |
| 54 | + |
| 55 | + ({ anvil, rpcUrl } = await startAnvil()); |
| 56 | + |
| 57 | + ({ walletClient, publicClient } = createL1Clients(rpcUrl, privateKey)); |
| 58 | + |
| 59 | + const deployed = await deployL1Contracts(rpcUrl, privateKey, foundry, logger, { |
| 60 | + ...DefaultL1ContractsConfig, |
| 61 | + salt: undefined, |
| 62 | + vkTreeRoot, |
| 63 | + protocolContractTreeRoot, |
| 64 | + l2FeeJuiceAddress, |
| 65 | + }); |
| 66 | + |
| 67 | + govProposerAddress = deployed.l1ContractAddresses.governanceProposerAddress; |
| 68 | + // rollupAddress = deployed.l1ContractAddresses.rollupAddress; |
| 69 | + |
| 70 | + forwarder = await ForwarderContract.create( |
| 71 | + privateKey.address, |
| 72 | + walletClient, |
| 73 | + publicClient, |
| 74 | + logger, |
| 75 | + deployed.l1ContractAddresses.rollupAddress.toString(), |
| 76 | + ); |
| 77 | + |
| 78 | + l1TxUtils = new L1TxUtils(publicClient, walletClient, logger); |
| 79 | + |
| 80 | + const { address: erc20Address, txHash: erc20TxHash } = await deployL1Contract( |
| 81 | + walletClient, |
| 82 | + publicClient, |
| 83 | + TestERC20Abi, |
| 84 | + TestERC20Bytecode, |
| 85 | + ['test', 'TST', privateKey.address], |
| 86 | + '0x42', |
| 87 | + undefined, |
| 88 | + logger, |
| 89 | + ); |
| 90 | + expect(erc20TxHash).toBeDefined(); |
| 91 | + await publicClient.waitForTransactionReceipt({ hash: erc20TxHash! }); |
| 92 | + tokenAddress = erc20Address; |
| 93 | + tokenContract = getContract({ |
| 94 | + address: tokenAddress.toString(), |
| 95 | + abi: TestERC20Abi, |
| 96 | + client: publicClient, |
| 97 | + }); |
| 98 | + |
| 99 | + const freeForAllHash = await tokenContract.write.setFreeForAll([true], { account: privateKey }); |
| 100 | + await publicClient.waitForTransactionReceipt({ hash: freeForAllHash }); |
| 101 | + |
| 102 | + logger.info(`Token address: ${tokenAddress}`); |
| 103 | + }); |
| 104 | + |
| 105 | + afterAll(async () => { |
| 106 | + await anvil.stop(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('gets good error messages', async () => { |
| 110 | + expect(forwarder).toBeDefined(); |
| 111 | + const initialBalance = await tokenContract.read.balanceOf([privateKey.address]); |
| 112 | + expect(initialBalance).toBe(0n); |
| 113 | + const err = await forwarder |
| 114 | + .forward( |
| 115 | + [ |
| 116 | + // This one passes |
| 117 | + { |
| 118 | + to: tokenAddress.toString(), |
| 119 | + data: encodeFunctionData({ |
| 120 | + abi: TestERC20Abi, |
| 121 | + functionName: 'mint', |
| 122 | + args: [privateKey.address, 100n], |
| 123 | + }), |
| 124 | + }, |
| 125 | + |
| 126 | + // This one fails |
| 127 | + { |
| 128 | + to: govProposerAddress.toString(), |
| 129 | + data: encodeFunctionData({ |
| 130 | + abi: GovernanceProposerAbi, |
| 131 | + functionName: 'vote', |
| 132 | + args: [EthAddress.random().toString()], |
| 133 | + }), |
| 134 | + }, |
| 135 | + ], |
| 136 | + l1TxUtils, |
| 137 | + undefined, |
| 138 | + undefined, |
| 139 | + logger, |
| 140 | + ) |
| 141 | + .catch(err => err); |
| 142 | + expect(err).toBeDefined(); |
| 143 | + expect(err).toBeInstanceOf(FormattedViemError); |
| 144 | + expect(err.message).toMatch(/GovernanceProposer__OnlyProposerCanVote/); |
| 145 | + }); |
| 146 | +}); |
0 commit comments