Skip to content

Instantly share code, notes, and snippets.

@viraj124
Created November 12, 2020 09:01
Show Gist options
  • Save viraj124/d5f63b4dc644ccd8af6b3b6e0121fc7e to your computer and use it in GitHub Desktop.
Save viraj124/d5f63b4dc644ccd8af6b3b6e0121fc7e to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-contracts/master/contracts/utils/Create2.sol";
contract MinimalProxyFactory {
event MinimalProxyCreated(address minimalProxy);
function computeAddress(uint256 salt, address implementation)
public
view
returns (address)
{
return
Create2.computeAddress(
keccak256(abi.encodePacked(salt)),
keccak256(getContractCreationCode(implementation)),
address(this)
);
}
function deploy(
uint256 salt,
address implementation,
bytes memory _data
) public {
address minimalProxy = Create2.deploy(
0,
keccak256(abi.encodePacked(salt)),
getContractCreationCode(implementation)
);
if(_data.length > 0) {
(bool success,) = minimalProxy.call(_data);
require(success);
}
emit MinimalProxyCreated(minimalProxy);
}
function getContractCreationCode(address logic)
internal
pure
returns (bytes memory)
{
bytes10 creation = 0x3d602d80600a3d3981f3;
bytes10 prefix = 0x363d3d373d3d3d363d73;
bytes20 targetBytes = bytes20(logic);
bytes15 suffix = 0x5af43d82803e903d91602b57fd5bf3;
return abi.encodePacked(creation, prefix, targetBytes, suffix);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment