Create2.sol 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (utils/Create2.sol)
  3. pragma solidity ^0.8.0;
  4. /**
  5. * @dev Helper to make usage of the `CREATE2` EVM opcode easier and safer.
  6. * `CREATE2` can be used to compute in advance the address where a smart
  7. * contract will be deployed, which allows for interesting new mechanisms known
  8. * as 'counterfactual interactions'.
  9. *
  10. * See the https://eips.ethereum.org/EIPS/eip-1014#motivation[EIP] for more
  11. * information.
  12. */
  13. library Create2 {
  14. /**
  15. * @dev Deploys a contract using `CREATE2`. The address where the contract
  16. * will be deployed can be known in advance via {computeAddress}.
  17. *
  18. * The bytecode for a contract can be obtained from Solidity with
  19. * `type(contractName).creationCode`.
  20. *
  21. * Requirements:
  22. *
  23. * - `bytecode` must not be empty.
  24. * - `salt` must have not been used for `bytecode` already.
  25. * - the factory must have a balance of at least `amount`.
  26. * - if `amount` is non-zero, `bytecode` must have a `payable` constructor.
  27. */
  28. function deploy(uint256 amount, bytes32 salt, bytes memory bytecode) internal returns (address addr) {
  29. require(address(this).balance >= amount, "Create2: insufficient balance");
  30. require(bytecode.length != 0, "Create2: bytecode length is zero");
  31. /// @solidity memory-safe-assembly
  32. assembly {
  33. addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
  34. }
  35. require(addr != address(0), "Create2: Failed on deploy");
  36. }
  37. /**
  38. * @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the
  39. * `bytecodeHash` or `salt` will result in a new destination address.
  40. */
  41. function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) {
  42. return computeAddress(salt, bytecodeHash, address(this));
  43. }
  44. /**
  45. * @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at
  46. * `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}.
  47. */
  48. function computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) internal pure returns (address addr) {
  49. /// @solidity memory-safe-assembly
  50. assembly {
  51. let ptr := mload(0x40) // Get free memory pointer
  52. // | | ↓ ptr ... ↓ ptr + 0x0B (start) ... ↓ ptr + 0x20 ... ↓ ptr + 0x40 ... |
  53. // |-------------------|---------------------------------------------------------------------------|
  54. // | bytecodeHash | CCCCCCCCCCCCC...CC |
  55. // | salt | BBBBBBBBBBBBB...BB |
  56. // | deployer | 000000...0000AAAAAAAAAAAAAAAAAAA...AA |
  57. // | 0xFF | FF |
  58. // |-------------------|---------------------------------------------------------------------------|
  59. // | memory | 000000...00FFAAAAAAAAAAAAAAAAAAA...AABBBBBBBBBBBBB...BBCCCCCCCCCCCCC...CC |
  60. // | keccak(start, 85) | ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ |
  61. mstore(add(ptr, 0x40), bytecodeHash)
  62. mstore(add(ptr, 0x20), salt)
  63. mstore(ptr, deployer) // Right-aligned with 12 preceding garbage bytes
  64. let start := add(ptr, 0x0b) // The hashed data starts at the final garbage byte which we will set to 0xff
  65. mstore8(start, 0xff)
  66. addr := keccak256(start, 85)
  67. }
  68. }
  69. }