Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gnosis/feat/module-factory-uses-create2
Feat: Create2 implemented & tests improved
- Loading branch information
Showing
11 changed files
with
189 additions
and
1,034 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,44 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity >=0.8.0; | ||
|
||
// @TODO: Cesar's opinion: This is the way we should import external modules | ||
// currently it's not working because there are redundant declarations | ||
// i.e: Enum or Executor - Making the compile script to fail | ||
// Hence, forcing us to create the lib & interfaces folders | ||
|
||
// import "@gnosis/dao-module/contracts/DaoModule.sol"; | ||
// import "@gnosis/AMBModule/contracts/AMBModule.sol"; | ||
// import "@gnosis/SafeDelay/contracts/DelayModule.sol"; | ||
|
||
contract ModuleProxyFactory { | ||
event ModuleProxyCreation(address proxy); | ||
|
||
uint32 public nonce = 1; | ||
event ModuleProxyCreation( | ||
address indexed proxy, | ||
address indexed masterCopy | ||
); | ||
|
||
function createClone(address target) internal returns (address result) { | ||
bytes20 targetBytes = bytes20(target); | ||
function createProxy(address target, bytes32 salt) | ||
internal | ||
returns (address result) | ||
{ | ||
require( | ||
address(target) != address(0), | ||
"createProxy: address can not be zero" | ||
); | ||
bytes memory deployment = abi.encodePacked( | ||
hex"3d602d80600a3d3981f3363d3d373d3d3d363d73", | ||
target, | ||
hex"5af43d82803e903d91602b57fd5bf3" | ||
); | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
let clone := mload(0x40) | ||
mstore( | ||
clone, | ||
0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000 | ||
) | ||
mstore(add(clone, 0x14), targetBytes) | ||
mstore( | ||
add(clone, 0x28), | ||
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000 | ||
) | ||
result := create(0, clone, 0x37) | ||
result := create2(0, add(deployment, 0x20), mload(deployment), salt) | ||
} | ||
|
||
emit ModuleProxyCreation(result); | ||
require(result != address(0), "createProxy: address already taken"); | ||
} | ||
|
||
function deployModule(address singleton, bytes memory initializer) | ||
public | ||
returns (address clone) | ||
{ | ||
clone = createClone(singleton); | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
if eq( | ||
call( | ||
gas(), | ||
clone, | ||
0, | ||
add(initializer, 0x20), | ||
mload(initializer), | ||
0, | ||
0 | ||
), | ||
0 | ||
) { | ||
revert(0, 0) | ||
} | ||
} | ||
nonce++; | ||
function deployModule( | ||
address masterCopy, | ||
bytes memory initializer, | ||
uint256 saltNonce | ||
) public returns (address proxy) { | ||
proxy = createProxy( | ||
masterCopy, | ||
keccak256(abi.encodePacked(keccak256(initializer), saltNonce)) | ||
); | ||
(bool success, ) = proxy.call(initializer); | ||
require(success, "deployModule: initialization failed"); | ||
|
||
emit ModuleProxyCreation(proxy, masterCopy); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.