Create2.sol 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (utils/Create2.sol)
  3. pragma solidity ^0.8.20;
  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 Not enough balance for performing a CREATE2 deploy.
  16. */
  17. error Create2InsufficientBalance(uint256 balance, uint256 needed);
  18. /**
  19. * @dev There's no code to deploy.
  20. */
  21. error Create2EmptyBytecode();
  22. /**
  23. * @dev The deployment failed.
  24. */
  25. error Create2FailedDeployment();
  26. /**
  27. * @dev Deploys a contract using `CREATE2`. The address where the contract
  28. * will be deployed can be known in advance via {computeAddress}.
  29. *
  30. * The bytecode for a contract can be obtained from Solidity with
  31. * `type(contractName).creationCode`.
  32. *
  33. * Requirements:
  34. *
  35. * - `bytecode` must not be empty.
  36. * - `salt` must have not been used for `bytecode` already.
  37. * - the factory must have a balance of at least `amount`.
  38. * - if `amount` is non-zero, `bytecode` must have a `payable` constructor.
  39. */
  40. function deploy(uint256 amount, bytes32 salt, bytes memory bytecode) internal returns (address addr) {
  41. if (address(this).balance < amount) {
  42. revert Create2InsufficientBalance(address(this).balance, amount);
  43. }
  44. if (bytecode.length == 0) {
  45. revert Create2EmptyBytecode();
  46. }
  47. /// @solidity memory-safe-assembly
  48. assembly {
  49. addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
  50. }
  51. if (addr == address(0)) {
  52. revert Create2FailedDeployment();
  53. }
  54. }
  55. /**
  56. * @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the
  57. * `bytecodeHash` or `salt` will result in a new destination address.
  58. */
  59. function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) {
  60. return computeAddress(salt, bytecodeHash, address(this));
  61. }
  62. /**
  63. * @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at
  64. * `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}.
  65. */
  66. function computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) internal pure returns (address addr) {
  67. /// @solidity memory-safe-assembly
  68. assembly {
  69. let ptr := mload(0x40) // Get free memory pointer
  70. // | | ↓ ptr ... ↓ ptr + 0x0B (start) ... ↓ ptr + 0x20 ... ↓ ptr + 0x40 ... |
  71. // |-------------------|---------------------------------------------------------------------------|
  72. // | bytecodeHash | CCCCCCCCCCCCC...CC |
  73. // | salt | BBBBBBBBBBBBB...BB |
  74. // | deployer | 000000...0000AAAAAAAAAAAAAAAAAAA...AA |
  75. // | 0xFF | FF |
  76. // |-------------------|---------------------------------------------------------------------------|
  77. // | memory | 000000...00FFAAAAAAAAAAAAAAAAAAA...AABBBBBBBBBBBBB...BBCCCCCCCCCCCCC...CC |
  78. // | keccak(start, 85) | ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ |
  79. mstore(add(ptr, 0x40), bytecodeHash)
  80. mstore(add(ptr, 0x20), salt)
  81. mstore(ptr, deployer) // Right-aligned with 12 preceding garbage bytes
  82. let start := add(ptr, 0x0b) // The hashed data starts at the final garbage byte which we will set to 0xff
  83. mstore8(start, 0xff)
  84. addr := keccak256(start, 85)
  85. }
  86. }
  87. }