Skip to content

Commit

Permalink
Merge pull request #5 from gnosis/feat/module-factory-uses-create2
Browse files Browse the repository at this point in the history
Feat: Create2 implemented & tests improved
  • Loading branch information
auryn-macmillan committed Jul 16, 2021
2 parents 4fd02a2 + eed75ae commit e24d093
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 1,034 deletions.
82 changes: 32 additions & 50 deletions contracts/ModuleProxyFactory.sol
@@ -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);
}
}
23 changes: 0 additions & 23 deletions contracts/interfaces/Executor.sol

This file was deleted.

19 changes: 0 additions & 19 deletions contracts/interfaces/Realitio.sol

This file was deleted.

107 changes: 0 additions & 107 deletions contracts/lib/AMBModule.sol

This file was deleted.

0 comments on commit e24d093

Please sign in to comment.