163 - OZ Create2
OpenZeppelin Create2: makes usage of the CREATE2
EVM opcode easier and safer.
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’.
-
deploy(uint256 amount, bytes32 salt, bytes bytecode)
→address
: Deploys a contract usingCREATE2
. The address where the contract will be deployed can be known in advance viacomputeAddress
. The bytecode for a contract can be obtained from Solidity withtype(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 amount.
- if amount is non-zero, bytecode must have a
payable
constructor.
-
computeAddress(bytes32 salt, bytes32 bytecodeHash)
→address
: Returns the address where a contract will be stored if deployed via deploy. Any change in thebytecodeHash
orsalt
will result in a new destination address. -
computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer)
→address
: Returns the address where a contract will be stored if deployed via deploy from a contract located at deployer. If the deployer is this contract’s address, it returns the same value ascomputeAddress
.
CREATE2
EVM Opcode- Easier & Safer
deploy(uint256 amount, bytes32 salt, bytes bytecode)
computeAddress(bytes32 salt, a bytecodeHash)
computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer)
-> address