From 4b3b7f99bbf318e88173399e3e6f51450a805d25 Mon Sep 17 00:00:00 2001 From: kyriediculous Date: Fri, 12 Apr 2024 15:56:39 +0200 Subject: [PATCH] feat: create2 --- script/Factory_Deploy.s.sol | 8 +++++--- src/Factory.sol | 15 +++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/script/Factory_Deploy.s.sol b/script/Factory_Deploy.s.sol index 2bfedcb..ff3ed15 100644 --- a/script/Factory_Deploy.s.sol +++ b/script/Factory_Deploy.s.sol @@ -7,19 +7,21 @@ import { ERC1967Proxy } from "openzeppelin-contracts/proxy/ERC1967/ERC1967Proxy. address constant FACTORY = address(0); +uint256 constant VERSION = 1; + contract Swap_Deploy is Script { // Contracts are deployed deterministically. // e.g. `foo = new Foo{salt: salt}(constructorArgs)` // The presence of the salt argument tells forge to use https://github.com/Arachnid/deterministic-deployment-proxy - bytes32 private constant salt = 0x0; + bytes32 constant SALT = bytes32(VERSION); // Start broadcasting with private key from `.env` file uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); function run() public { vm.startBroadcast(deployerPrivateKey); - address fac = address(new SwapFactory()); - address proxy = address(new ERC1967Proxy(fac, abi.encodeWithSelector(SwapFactory.initialize.selector))); + address fac = address(new SwapFactory{ salt: SALT }()); + address proxy = address(new ERC1967Proxy{ salt: SALT }(fac, abi.encodeWithSelector(SwapFactory.initialize.selector))); console2.log("SwapFactory deployed at: ", proxy); console2.log("Implementation deployed at: ", fac); vm.stopBroadcast(); diff --git a/src/Factory.sol b/src/Factory.sol index e9ff489..be50192 100644 --- a/src/Factory.sol +++ b/src/Factory.sol @@ -25,6 +25,8 @@ contract SwapFactory is Initializable, UUPSUpgradeable, OwnableUpgradeable { event SwapDeployed(address underlying, address swap, address implementation); event SwapUpgraded(address underlying, address swap, address implementation); + mapping(address pool => uint256 v) public version; + function initialize() public initializer { __Ownable_init(); __UUPSUpgradeable_init(); @@ -35,13 +37,16 @@ contract SwapFactory is Initializable, UUPSUpgradeable, OwnableUpgradeable { } function deploy(ConstructorConfig memory cfg) external onlyOwner returns (address proxy, address implementation) { + uint256 v = 1; // Deploy the implementation - implementation = address(new TenderSwap(cfg)); + implementation = address(new TenderSwap{ salt: bytes32(v) }(cfg)); // deploy the contract - proxy = address(new ERC1967Proxy(implementation, abi.encodeWithSelector(TenderSwap.initialize.selector))); + proxy = address( + new ERC1967Proxy{ salt: bytes32("tenderswap") }(implementation, abi.encodeWithSelector(TenderSwap.initialize.selector)) + ); TenderSwap(proxy).transferOwnership(owner()); - + version[proxy] = v; emit SwapDeployed(address(cfg.UNDERLYING), proxy, implementation); } @@ -50,7 +55,9 @@ contract SwapFactory is Initializable, UUPSUpgradeable, OwnableUpgradeable { revert("SwapFactory: UNDERLYING_MISMATCH"); } - implementation = address(new TenderSwap(cfg)); + uint256 v = ++version[swapProxy]; + + implementation = address(new TenderSwap{ salt: bytes32(v) }(cfg)); TenderSwap(swapProxy).upgradeTo(implementation); }