Created
March 17, 2024 17:43
-
-
Save mitche50/65c65998796c7db6d2dbb111d9c09110 to your computer and use it in GitHub Desktop.
Efficient Immutable Create2 Factory Foundry Test Base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0-or-later | |
pragma solidity 0.8.19; | |
import "forge-std/console.sol"; | |
import "forge-std/StdCheats.sol"; | |
import "forge-std/StdAssertions.sol"; | |
import "forge-std/StdUtils.sol"; | |
import {TestBase} from "forge-std/Base.sol"; | |
interface IImmutableCreate2Factory { | |
function safeCreate2(bytes32 salt, bytes calldata initializationCode) external payable returns (address deploymentAddress); | |
function findCreate2Address(bytes32 salt, bytes calldata initCode) external view returns (address deploymentAddress); | |
function findCreate2AddressViaHash(bytes32 salt, bytes32 initCodeHash) external view returns (address deploymentAddress); | |
function hasBeenDeployed(address deploymentAddress) external view returns (bool); | |
} | |
contract BaseTest is TestBase, StdAssertions, StdCheats, StdUtils { | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
IImmutableCreate2Factory create2Factory; | |
uint256 adminKey; | |
uint256 aliceKey; | |
uint256 bobKey; | |
uint256 carolKey; | |
address admin; | |
address alice; | |
address bob; | |
address carol; | |
address constant initialDeployer = 0x4c8D290a1B368ac4728d83a9e8321fC3af2b39b1; | |
address constant expectedIntermediaryAddress = 0xcfA3A7637547094fF06246817a35B8333C315196; | |
address constant expectedFinalFactoryAddress = 0x0000000000FFe8B47B3e2130213B802212439497; | |
function setUp() public virtual { | |
(admin, adminKey) = makeAddrAndKey("admin"); | |
(alice, aliceKey) = makeAddrAndKey("alice"); | |
(bob, bobKey) = makeAddrAndKey("bob"); | |
(carol, carolKey) = makeAddrAndKey("carol"); | |
// Warp to a more realistic timestamp | |
vm.warp(1703688340); | |
} | |
function changePrank(address msgSender) internal virtual override { | |
vm.stopPrank(); | |
vm.startPrank(msgSender); | |
} | |
function deployCreate2Factory() internal virtual { | |
bytes memory initialBytecode = hex"601f80600e600039806000f350fe60003681823780368234f58015156014578182fd5b80825250506014600cf3"; | |
bytes memory bytecode = hex"608060405234801561001057600080fd5b50610833806100206000396000f3fe60806040526004361061003f5760003560e01c806308508b8f1461004457806364e030871461009857806385cf97ab14610138578063a49a7c90146101bc575b600080fd5b34801561005057600080fd5b506100846004803603602081101561006757600080fd5b503573ffffffffffffffffffffffffffffffffffffffff166101ec565b604080519115158252519081900360200190f35b61010f600480360360408110156100ae57600080fd5b813591908101906040810160208201356401000000008111156100d057600080fd5b8201836020820111156100e257600080fd5b8035906020019184600183028401116401000000008311171561010457600080fd5b509092509050610217565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b34801561014457600080fd5b5061010f6004803603604081101561015b57600080fd5b8135919081019060408101602082013564010000000081111561017d57600080fd5b82018360208201111561018f57600080fd5b803590602001918460018302840111640100000000831117156101b157600080fd5b509092509050610592565b3480156101c857600080fd5b5061010f600480360360408110156101df57600080fd5b508035906020013561069e565b73ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090205460ff1690565b600083606081901c33148061024c57507fffffffffffffffffffffffffffffffffffffffff0000000000000000000000008116155b6102a1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260458152602001806107746045913960600191505060405180910390fd5b606084848080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920182905250604051855195965090943094508b93508692506020918201918291908401908083835b6020831061033557805182527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe090920191602091820191016102f8565b51815160209384036101000a7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff018019909216911617905260408051929094018281037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe00183528085528251928201929092207fff000000000000000000000000000000000000000000000000000000000000008383015260609890981b7fffffffffffffffffffffffffffffffffffffffff00000000000000000000000016602183015260358201969096526055808201979097528251808203909701875260750182525084519484019490942073ffffffffffffffffffffffffffffffffffffffff81166000908152938490529390922054929350505060ff16156104a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252603f815260200180610735603f913960400191505060405180910390fd5b81602001825188818334f5955050508073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161461053a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260468152602001806107b96046913960600191505060405180910390fd5b50505073ffffffffffffffffffffffffffffffffffffffff8116600090815260208190526040902080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660011790559392505050565b6000308484846040516020018083838082843760408051919093018181037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe001825280845281516020928301207fff000000000000000000000000000000000000000000000000000000000000008383015260609990991b7fffffffffffffffffffffffffffffffffffffffff000000000000000000000000166021820152603581019790975260558088019890985282518088039098018852607590960182525085519585019590952073ffffffffffffffffffffffffffffffffffffffff81166000908152948590529490932054939450505060ff909116159050610697575060005b9392505050565b604080517fff000000000000000000000000000000000000000000000000000000000000006020808301919091523060601b6021830152603582018590526055808301859052835180840390910181526075909201835281519181019190912073ffffffffffffffffffffffffffffffffffffffff81166000908152918290529190205460ff161561072e575060005b9291505056fe496e76616c696420636f6e7472616374206372656174696f6e202d20636f6e74726163742068617320616c7265616479206265656e206465706c6f7965642e496e76616c69642073616c74202d206669727374203230206279746573206f66207468652073616c74206d757374206d617463682063616c6c696e6720616464726573732e4661696c656420746f206465706c6f7920636f6e7472616374207573696e672070726f76696465642073616c7420616e6420696e697469616c697a6174696f6e20636f64652ea265627a7a723058202bdc55310d97c4088f18acf04253db593f0914059f0c781a9df3624dcef0d1cf64736f6c634300050a0032"; | |
address inefficientFactory; | |
uint256 length = initialBytecode.length; | |
vm.deal(initialDeployer, 0.01 ether); | |
changePrank(initialDeployer); | |
assembly{ | |
mstore(0x0, initialBytecode) | |
inefficientFactory := create(0, 0xa0, length) | |
} | |
changePrank(msg.sender); | |
(bool success,) = address(inefficientFactory).call(bytecode); | |
create2Factory = IImmutableCreate2Factory(IImmutableCreate2Factory(expectedIntermediaryAddress).safeCreate2(0x0000000000000000000000000000000000000000f4b0218f13a6440a6f020000, bytecode)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment