pragma solidity 0.5.10; import "./Proxy.sol"; /// @title Proxy Factory - Allows to create new proxy contract and execute a message call to the new proxy within one transaction. /// @author Stefan George - contract ProxyFactory { event ProxyCreation(Proxy proxy); /// @dev Allows to create new proxy contact and execute a message call to the new proxy within one transaction. /// @param masterCopy Address of master copy. /// @param data Payload for message call sent to new proxy contract. function createProxy(address masterCopy, bytes memory data) public returns (Proxy proxy) { proxy = new Proxy(masterCopy); if (data.length > 0) // solium-disable-next-line security/no-inline-assembly assembly { if eq(call(gas, proxy, 0, add(data, 0x20), mload(data), 0, 0), 0) { revert(0, 0) } } emit ProxyCreation(proxy); } /// @dev Allows to retrieve the runtime code of a deployed Proxy. This can be used to check that the expected Proxy was deployed. function proxyRuntimeCode() public pure returns (bytes memory) { return type(Proxy).runtimeCode; } /// @dev Allows to retrieve the creation code used for the Proxy deployment. With this it is easily possible to calculate predicted address. function proxyCreationCode() public pure returns (bytes memory) { return type(Proxy).creationCode; } /// @dev Allows to create new proxy contact and execute a message call to the new proxy within one transaction. /// @param _mastercopy Address of master copy. /// @param initializer Payload for message call sent to new proxy contract. /// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. function createProxyWithNonce( address _mastercopy, bytes memory initializer, uint256 saltNonce ) public returns (Proxy proxy) { // If the initializer changes, the proxy address should change too. Hashing the initializer data is cheaper than just concatinating it bytes32 salt = keccak256( abi.encodePacked(keccak256(initializer), saltNonce) ); bytes memory deploymentData = abi.encodePacked( type(Proxy).creationCode, uint256(_mastercopy) ); // solium-disable-next-line security/no-inline-assembly assembly { proxy := create2(0x0, add(0x20, deploymentData), mload(deploymentData), salt) } if (initializer.length > 0) // solium-disable-next-line security/no-inline-assembly assembly { if eq(call(gas, proxy, 0, add(initializer, 0x20), mload(initializer), 0, 0), 0) { revert(0,0) } } emit ProxyCreation(proxy); } }