Create2.sol 4.1 KB

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