-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathModuleProxyFactory.sol
51 lines (43 loc) · 1.46 KB
/
ModuleProxyFactory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.8.0;
contract ModuleProxyFactory {
event ModuleProxyCreation(address indexed proxy, address indexed masterCopy);
/// `target` can not be zero.
error ZeroAddress(address target);
/// `target` has no code deployed.
error TargetHasNoCode(address target);
/// `address_` is already taken.
error TakenAddress(address address_);
/// @notice Initialization failed.
error FailedInitialization();
function createProxy(
address target,
bytes32 salt
) internal returns (address result) {
if (address(target) == address(0)) revert ZeroAddress(target);
if (address(target).code.length == 0) revert TargetHasNoCode(target);
bytes memory deployment = abi.encodePacked(
hex"602d8060093d393df3363d3d373d3d3d363d73",
target,
hex"5af43d82803e903d91602b57fd5bf3"
);
// solhint-disable-next-line no-inline-assembly
assembly {
result := create2(0, add(deployment, 0x20), mload(deployment), salt)
}
if (result == address(0)) revert TakenAddress(result);
}
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);
if (!success) revert FailedInitialization();
emit ModuleProxyCreation(proxy, masterCopy);
}
}