1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- pragma solidity ^0.5.0;
- /**
- * @dev Helper to make 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'.
- *
- * See the https://eips.ethereum.org/EIPS/eip-1014#motivation[EIP] for more
- * information.
- */
- library Create2 {
- /**
- * @dev Deploys a contract using `CREATE2`. The address where the contract
- * will be deployed can be known in advance via {computeAddress}. Note that
- * a contract cannot be deployed twice using the same salt.
- */
- function deploy(bytes32 salt, bytes memory bytecode) internal returns (address) {
- address addr;
- // solhint-disable-next-line no-inline-assembly
- assembly {
- addr := create2(0, add(bytecode, 0x20), mload(bytecode), salt)
- }
- require(addr != address(0), "Create2: Failed on deploy");
- return addr;
- }
- /**
- * @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the `bytecode`
- * or `salt` will result in a new destination address.
- */
- function computeAddress(bytes32 salt, bytes memory bytecode) internal view returns (address) {
- return computeAddress(salt, bytecode, address(this));
- }
- /**
- * @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 computeAddress(bytes32 salt, bytes memory bytecodeHash, address deployer) internal pure returns (address) {
- bytes32 bytecodeHashHash = keccak256(bytecodeHash);
- bytes32 _data = keccak256(
- abi.encodePacked(bytes1(0xff), deployer, salt, bytecodeHashHash)
- );
- return address(bytes20(_data << 96));
- }
- }
|