|
| 1 | +import hre from "hardhat"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import { AddressZero } from "@ethersproject/constants"; |
| 4 | +import { hexConcat } from "ethers/lib/utils"; |
| 5 | +import { getFactoryContract, getSafeSingletonContract } from "../utils/setup"; |
| 6 | +import { calculateProxyAddress } from "../../src/utils/proxies"; |
| 7 | + |
| 8 | +const ERC4337_TEST_ENV_VARIABLES_DEFINED = |
| 9 | + typeof process.env.ERC4337_TEST_BUNDLER_URL !== "undefined" && |
| 10 | + typeof process.env.ERC4337_TEST_NODE_URL !== "undefined" && |
| 11 | + typeof process.env.ERC4337_TEST_SAFE_FACTORY_ADDRESS !== "undefined" && |
| 12 | + typeof process.env.ERC4337_TEST_SINGLETON_ADDRESS !== "undefined" && |
| 13 | + typeof process.env.MNEMONIC !== "undefined"; |
| 14 | + |
| 15 | +const itif = ERC4337_TEST_ENV_VARIABLES_DEFINED ? it : it.skip; |
| 16 | +const SAFE_FACTORY_ADDRESS = process.env.ERC4337_TEST_SAFE_FACTORY_ADDRESS; |
| 17 | +const SINGLETON_ADDRESS = process.env.ERC4337_TEST_SINGLETON_ADDRESS; |
| 18 | +const BUNDLER_URL = process.env.ERC4337_TEST_BUNDLER_URL; |
| 19 | +const NODE_URL = process.env.ERC4337_TEST_NODE_URL; |
| 20 | +const MNEMONIC = process.env.MNEMONIC; |
| 21 | + |
| 22 | +type UserOperation = { |
| 23 | + sender: string; |
| 24 | + nonce: string; |
| 25 | + initCode: string; |
| 26 | + callData: string; |
| 27 | + callGasLimit: string; |
| 28 | + verificationGasLimit: string; |
| 29 | + preVerificationGas: string; |
| 30 | + maxFeePerGas: string; |
| 31 | + maxPriorityFeePerGas: string; |
| 32 | + paymasterAndData: string; |
| 33 | + signature: string; |
| 34 | +}; |
| 35 | + |
| 36 | +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 37 | + |
| 38 | +describe("Safe.ERC4337", () => { |
| 39 | + const setupTests = async () => { |
| 40 | + const factory = await getFactoryContract(); |
| 41 | + const singleton = await getSafeSingletonContract(); |
| 42 | + const bundlerProvider = new hre.ethers.providers.JsonRpcProvider(BUNDLER_URL); |
| 43 | + const provider = new hre.ethers.providers.JsonRpcProvider(NODE_URL); |
| 44 | + const userWallet = hre.ethers.Wallet.fromMnemonic(MNEMONIC as string).connect(provider); |
| 45 | + |
| 46 | + const entryPoints = await bundlerProvider.send("eth_supportedEntryPoints", []); |
| 47 | + if (entryPoints.length === 0) { |
| 48 | + throw new Error("No entry points found"); |
| 49 | + } |
| 50 | + |
| 51 | + return { |
| 52 | + factory: factory.attach(SAFE_FACTORY_ADDRESS).connect(userWallet), |
| 53 | + singleton: singleton.attach(SINGLETON_ADDRESS).connect(provider), |
| 54 | + bundlerProvider, |
| 55 | + provider, |
| 56 | + userWallet, |
| 57 | + entryPoints, |
| 58 | + }; |
| 59 | + }; |
| 60 | + |
| 61 | + /** |
| 62 | + * This test verifies the ERC4337 based on gas estimation for a user operation |
| 63 | + * The user operation deploys a Safe with the ERC4337 module and a handler |
| 64 | + * and executes a transaction, thus verifying two things: |
| 65 | + * 1. Deployment of the Safe with the ERC4337 module and handler is possible |
| 66 | + * 2. Executing a transaction is possible |
| 67 | + */ |
| 68 | + itif("should pass the ERC4337 validation", async () => { |
| 69 | + const { singleton, factory, provider, bundlerProvider, userWallet, entryPoints } = await setupTests(); |
| 70 | + const ENTRYPOINT_ADDRESS = entryPoints[0]; |
| 71 | + |
| 72 | + const erc4337ModuleAndHandlerFactory = (await hre.ethers.getContractFactory("Test4337ModuleAndHandler")).connect(userWallet); |
| 73 | + const erc4337ModuleAndHandler = await erc4337ModuleAndHandlerFactory.deploy(ENTRYPOINT_ADDRESS); |
| 74 | + // The bundler uses a different node, so we need to allow it sometime to sync |
| 75 | + await sleep(10000); |
| 76 | + |
| 77 | + const feeData = await provider.getFeeData(); |
| 78 | + const maxFeePerGas = feeData.maxFeePerGas.toHexString(); |
| 79 | + |
| 80 | + const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas.toHexString(); |
| 81 | + |
| 82 | + const moduleInitializer = erc4337ModuleAndHandler.interface.encodeFunctionData("enableMyself", []); |
| 83 | + const encodedInitializer = singleton.interface.encodeFunctionData("setup", [ |
| 84 | + [userWallet.address], |
| 85 | + 1, |
| 86 | + erc4337ModuleAndHandler.address, |
| 87 | + moduleInitializer, |
| 88 | + erc4337ModuleAndHandler.address, |
| 89 | + AddressZero, |
| 90 | + 0, |
| 91 | + AddressZero, |
| 92 | + ]); |
| 93 | + const deployedAddress = await calculateProxyAddress(factory, singleton.address, encodedInitializer, 73); |
| 94 | + |
| 95 | + // The initCode contains 20 bytes of the factory address and the rest is the calldata to be forwarded |
| 96 | + const initCode = hexConcat([ |
| 97 | + factory.address, |
| 98 | + factory.interface.encodeFunctionData("createProxyWithNonce", [singleton.address, encodedInitializer, 73]), |
| 99 | + ]); |
| 100 | + const userOpCallData = erc4337ModuleAndHandler.interface.encodeFunctionData("execTransaction", [userWallet.address, 0, 0]); |
| 101 | + |
| 102 | + // Native tokens for the pre-fund 💸 |
| 103 | + await userWallet.sendTransaction({ to: deployedAddress, value: hre.ethers.utils.parseEther("0.001") }); |
| 104 | + // The bundler uses a different node, so we need to allow it sometime to sync |
| 105 | + await sleep(10000); |
| 106 | + |
| 107 | + const userOperation: UserOperation = { |
| 108 | + sender: deployedAddress, |
| 109 | + nonce: "0x0", |
| 110 | + initCode, |
| 111 | + callData: userOpCallData, |
| 112 | + callGasLimit: "0x7A120", |
| 113 | + verificationGasLimit: "0x7A120", |
| 114 | + preVerificationGas: "0x186A0", |
| 115 | + maxFeePerGas, |
| 116 | + maxPriorityFeePerGas, |
| 117 | + paymasterAndData: "0x", |
| 118 | + signature: "0x", |
| 119 | + }; |
| 120 | + |
| 121 | + const DEBUG_MESSAGE = ` |
| 122 | + Using entry point: ${ENTRYPOINT_ADDRESS} |
| 123 | + Deployed Safe address: ${deployedAddress} |
| 124 | + Module/Handler address: ${erc4337ModuleAndHandler.address} |
| 125 | + User operation: |
| 126 | + ${JSON.stringify(userOperation, null, 2)} |
| 127 | + `; |
| 128 | + console.log(DEBUG_MESSAGE); |
| 129 | + |
| 130 | + const estimatedGas = await bundlerProvider.send("eth_estimateUserOperationGas", [userOperation, ENTRYPOINT_ADDRESS]); |
| 131 | + expect(estimatedGas).to.not.be.undefined; |
| 132 | + }); |
| 133 | +}); |
0 commit comments