12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.0.0) (utils/Create2.sol)
- pragma solidity ^0.8.20;
- import {Errors} from "./Errors.sol";
- /**
- * @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 There's no code to deploy.
- */
- error Create2EmptyBytecode();
- /**
- * @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 `amount`.
- * - if `amount` is non-zero, `bytecode` must have a `payable` constructor.
- */
- function deploy(uint256 amount, bytes32 salt, bytes memory bytecode) internal returns (address addr) {
- if (address(this).balance < amount) {
- revert Errors.InsufficientBalance(address(this).balance, amount);
- }
- if (bytecode.length == 0) {
- revert Create2EmptyBytecode();
- }
- /// @solidity memory-safe-assembly
- assembly {
- addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
- // if no address was created, and returndata is not empty, bubble revert
- if and(iszero(addr), not(iszero(returndatasize()))) {
- let p := mload(0x40)
- returndatacopy(p, 0, returndatasize())
- revert(p, returndatasize())
- }
- }
- if (addr == address(0)) {
- revert Errors.FailedDeployment();
- }
- }
- /**
- * @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 bytecodeHash) internal view returns (address) {
- return computeAddress(salt, bytecodeHash, 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, bytes32 bytecodeHash, address deployer) internal pure returns (address addr) {
- /// @solidity memory-safe-assembly
- assembly {
- let ptr := mload(0x40) // Get free memory pointer
- // | | ↓ ptr ... ↓ ptr + 0x0B (start) ... ↓ ptr + 0x20 ... ↓ ptr + 0x40 ... |
- // |-------------------|---------------------------------------------------------------------------|
- // | bytecodeHash | CCCCCCCCCCCCC...CC |
- // | salt | BBBBBBBBBBBBB...BB |
- // | deployer | 000000...0000AAAAAAAAAAAAAAAAAAA...AA |
- // | 0xFF | FF |
- // |-------------------|---------------------------------------------------------------------------|
- // | memory | 000000...00FFAAAAAAAAAAAAAAAAAAA...AABBBBBBBBBBBBB...BBCCCCCCCCCCCCC...CC |
- // | keccak(start, 85) | ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ |
- mstore(add(ptr, 0x40), bytecodeHash)
- mstore(add(ptr, 0x20), salt)
- mstore(ptr, deployer) // Right-aligned with 12 preceding garbage bytes
- let start := add(ptr, 0x0b) // The hashed data starts at the final garbage byte which we will set to 0xff
- mstore8(start, 0xff)
- addr := and(keccak256(start, 85), 0xffffffffffffffffffffffffffffffffffffffff)
- }
- }
- }
|