-
Notifications
You must be signed in to change notification settings - Fork 42
/
Create2Deployer.sol
85 lines (77 loc) · 3.12 KB
/
Create2Deployer.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: MIT
// Further information: https://eips.ethereum.org/EIPS/eip-1014
pragma solidity ^0.8.9;
import {Create2} from "@openzeppelin/contracts/utils/Create2.sol";
import {ERC1820Implementer} from "@openzeppelin/contracts/utils/introspection/ERC1820Implementer.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Pausable} from "@openzeppelin/contracts/security/Pausable.sol";
/**
* @title CREATE2 Deployer Smart Contract
* @author Pascal Marco Caversaccio, [email protected]
* @dev Helper smart contract to make easier and safer usage of the
* `CREATE2` EVM opcode. `CREATE2` can be used to compute in advance
* the address where a smart contract will be deployed, which allows
* for interesting new mechanisms known as 'counterfactual interactions'.
*/
contract Create2Deployer is Ownable, Pausable {
/**
* @dev Deploys a contract using `CREATE2`. The address where the
* contract will be deployed can be known in advance via {computeAddress}.
*
* The bytecode for a contract can be obtained from Solidity with
* `type(contractName).creationCode`.
*
* Requirements:
* - `bytecode` must not be empty.
* - `salt` must have not been used for `bytecode` already.
* - the factory must have a balance of at least `value`.
* - if `value` is non-zero, `bytecode` must have a `payable` constructor.
*/
function deploy(uint256 value, bytes32 salt, bytes memory code) public whenNotPaused {
Create2.deploy(value, salt, code);
}
/**
* @dev Deployment of the {ERC1820Implementer}.
* Further information: https://eips.ethereum.org/EIPS/eip-1820
*/
function deployERC1820Implementer(uint256 value, bytes32 salt) public whenNotPaused {
Create2.deploy(value, salt, type(ERC1820Implementer).creationCode);
}
/**
* @dev Returns the address where a contract will be stored if deployed via {deploy}.
* Any change in the `bytecodeHash` or `salt` will result in a new destination address.
*/
function computeAddress(bytes32 salt, bytes32 codeHash) public view returns (address) {
return Create2.computeAddress(salt, codeHash);
}
/**
* @dev Returns the address where a contract will be stored if deployed via {deploy} from a
* contract located at `deployer`. If `deployer` is this contract's address, returns the
* same value as {computeAddress}.
*/
function computeAddressWithDeployer(
bytes32 salt,
bytes32 codeHash,
address deployer
) public pure returns (address) {
return Create2.computeAddress(salt, codeHash, deployer);
}
/**
* @dev The contract can receive ether to enable `payable` constructor calls if needed.
*/
receive() external payable {}
/**
* @dev Triggers stopped state.
* Requirements: The contract must not be paused.
*/
function pause() public onlyOwner {
_pause();
}
/**
* @dev Returns to normal state.
* Requirements: The contract must be paused.
*/
function unpause() public onlyOwner {
_unpause();
}
}