Skip to content

Commit

Permalink
Deploy with CREATE2 - First draft
Browse files Browse the repository at this point in the history
  • Loading branch information
rya-sge committed Jun 19, 2024
1 parent 412b645 commit 2c65317
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 23 deletions.
115 changes: 93 additions & 22 deletions contracts/deployment/CMTAT_TP_FACTORY.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
settings: {
optimizer: {
enabled: true,
runs: 200,
runs: 200
},
evmVersion: 'shanghai'
}
Expand Down

0 comments on commit 2c65317

Please sign in to comment.