From 2c653174ac5f1647bb86850d4b86302379399e3f Mon Sep 17 00:00:00 2001 From: Ryan Sauge Date: Wed, 19 Jun 2024 16:55:45 +0200 Subject: [PATCH] Deploy with CREATE2 - First draft --- contracts/deployment/CMTAT_TP_FACTORY.sol | 115 +++++++++++++++++----- hardhat.config.js | 2 +- 2 files changed, 94 insertions(+), 23 deletions(-) diff --git a/contracts/deployment/CMTAT_TP_FACTORY.sol b/contracts/deployment/CMTAT_TP_FACTORY.sol index 785747c2..c27b120e 100644 --- a/contracts/deployment/CMTAT_TP_FACTORY.sol +++ b/contracts/deployment/CMTAT_TP_FACTORY.sol @@ -37,7 +37,6 @@ contract CMTAT_TP_FACTORY is AccessControl { _grantRole(DEFAULT_ADMIN_ROLE, factoryAdmin); _grantRole(CMTAT_DEPLOYER_ROLE, factoryAdmin); } - function deployCMTAT( address proxyAdminOwner, // CMTAT function initialize @@ -52,29 +51,101 @@ contract CMTAT_TP_FACTORY is AccessControl { string memory information_, uint256 flag_ ) public onlyRole(CMTAT_DEPLOYER_ROLE) returns(TransparentUpgradeableProxy cmtat) { - cmtat = new TransparentUpgradeableProxy( - logic, - proxyAdminOwner, - abi.encodeWithSelector( - CMTAT_PROXY(address(0)).initialize.selector, - admin, - authorizationEngineIrrevocable, - nameIrrevocable, - symbolIrrevocable, - decimalsIrrevocable, - tokenId_, - terms_, - ruleEngine_, - information_, - flag_ - ) - ); - cmtats[cmtatID] = address(cmtat); - emit CMTAT(address(cmtat), cmtatID); - cmtatID++; - cmtatsList.push(address(cmtat)); + bytes memory bytecode = _getBytecode(proxyAdminOwner, + // CMTAT function initialize + admin, + authorizationEngineIrrevocable, + nameIrrevocable, + symbolIrrevocable, + decimalsIrrevocable, + tokenId_, + terms_, + ruleEngine_, + information_, + flag_); + cmtat = _deployBytecode(bytecode); return cmtat; } + // get the computed address before the contract DeployWithCreate2 deployed using Bytecode of contract DeployWithCreate2 and salt specified by the sender + + function getProxyAddress( address proxyAdminOwner, + // CMTAT function initialize + address admin, + IAuthorizationEngine authorizationEngineIrrevocable, + string memory nameIrrevocable, + string memory symbolIrrevocable, + uint8 decimalsIrrevocable, + string memory tokenId_, + string memory terms_, + IRuleEngine ruleEngine_, + string memory information_, + uint256 flag_, bytes32 salt) public view returns (address) { + bytes memory bytecode = _getBytecode(proxyAdminOwner, + // CMTAT function initialize + admin, + authorizationEngineIrrevocable, + nameIrrevocable, + symbolIrrevocable, + decimalsIrrevocable, + tokenId_, + terms_, + ruleEngine_, + information_, + flag_); + bytes32 hash = keccak256( + abi.encodePacked( + bytes1(0xff), address(this), salt, keccak256(bytecode) + ) + ); + return address (uint160(uint(hash))); + } + + function _deployBytecode(bytes memory bytecode) internal returns (TransparentUpgradeableProxy cmtat) { + bytes32 saltBytes = bytes32(keccak256(abi.encodePacked(cmtatID))); + cmtat = TransparentUpgradeableProxy(payable(_deploy(bytecode, saltBytes))); + cmtats[cmtatID] = address(cmtat); + emit CMTAT(address(cmtat), cmtatID); + cmtatID++; + cmtatsList.push(address(cmtat)); + return cmtat; + } + + function _getBytecode( address proxyAdminOwner, + // CMTAT function initialize + address admin, + IAuthorizationEngine authorizationEngineIrrevocable, + string memory nameIrrevocable, + string memory symbolIrrevocable, + uint8 decimalsIrrevocable, + string memory tokenId_, + string memory terms_, + IRuleEngine ruleEngine_, + string memory information_, + uint256 flag_) internal view returns(bytes memory bytecode) { + bytes memory implementation = abi.encodeWithSelector( + CMTAT_PROXY(address(0)).initialize.selector, + admin, + authorizationEngineIrrevocable, + nameIrrevocable, + symbolIrrevocable, + decimalsIrrevocable, + tokenId_, + terms_, + ruleEngine_, + information_, + flag_ + ); + bytecode = abi.encodePacked(type(TransparentUpgradeableProxy).creationCode, abi.encode(logic, proxyAdminOwner, implementation)); + } + + function _deploy(bytes memory bytecode, bytes32 _salt) internal returns (address contractAddress) { + assembly { + contractAddress := create2(0, add(bytecode, 0x20), mload(bytecode), _salt) + if iszero(extcodesize(contractAddress)) { + revert(0, 0) + } + } + } /** * @notice get CMTAT proxy address diff --git a/hardhat.config.js b/hardhat.config.js index 58de15f8..2e10ccd5 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -11,7 +11,7 @@ module.exports = { settings: { optimizer: { enabled: true, - runs: 200, + runs: 200 }, evmVersion: 'shanghai' }