Create2.sol 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. assembly ("memory-safe") {
  41. addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
  42. // if no address was created, and returndata is not empty, bubble revert
  43. if and(iszero(addr), not(iszero(returndatasize()))) {
  44. let p := mload(0x40)
  45. returndatacopy(p, 0, returndatasize())
  46. revert(p, returndatasize())
  47. }
  48. }
  49. if (addr == address(0)) {
  50. revert Errors.FailedDeployment();
  51. }
  52. }
  53. /**
  54. * @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the
  55. * `bytecodeHash` or `salt` will result in a new destination address.
  56. */
  57. function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) {
  58. return computeAddress(salt, bytecodeHash, address(this));
  59. }
  60. /**
  61. * @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at
  62. * `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}.
  63. */
  64. function computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) internal pure returns (address addr) {
  65. assembly ("memory-safe") {
  66. let ptr := mload(0x40) // Get free memory pointer
  67. // | | ↓ ptr ... ↓ ptr + 0x0B (start) ... ↓ ptr + 0x20 ... ↓ ptr + 0x40 ... |
  68. // |-------------------|---------------------------------------------------------------------------|
  69. // | bytecodeHash | CCCCCCCCCCCCC...CC |
  70. // | salt | BBBBBBBBBBBBB...BB |
  71. // | deployer | 000000...0000AAAAAAAAAAAAAAAAAAA...AA |
  72. // | 0xFF | FF |
  73. // |-------------------|---------------------------------------------------------------------------|
  74. // | memory | 000000...00FFAAAAAAAAAAAAAAAAAAA...AABBBBBBBBBBBBB...BBCCCCCCCCCCCCC...CC |
  75. // | keccak(start, 85) | ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ |
  76. mstore(add(ptr, 0x40), bytecodeHash)
  77. mstore(add(ptr, 0x20), salt)
  78. mstore(ptr, deployer) // Right-aligned with 12 preceding garbage bytes
  79. let start := add(ptr, 0x0b) // The hashed data starts at the final garbage byte which we will set to 0xff
  80. mstore8(start, 0xff)
  81. addr := and(keccak256(start, 85), 0xffffffffffffffffffffffffffffffffffffffff)
  82. }
  83. }
  84. }