Create2.sol 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0-rc.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(
  29. uint256 amount,
  30. bytes32 salt,
  31. bytes memory bytecode
  32. ) internal returns (address addr) {
  33. require(address(this).balance >= amount, "Create2: insufficient balance");
  34. require(bytecode.length != 0, "Create2: bytecode length is zero");
  35. /// @solidity memory-safe-assembly
  36. assembly {
  37. addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
  38. }
  39. require(addr != address(0), "Create2: Failed on deploy");
  40. }
  41. /**
  42. * @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the
  43. * `bytecodeHash` or `salt` will result in a new destination address.
  44. */
  45. function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) {
  46. return computeAddress(salt, bytecodeHash, address(this));
  47. }
  48. /**
  49. * @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at
  50. * `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}.
  51. */
  52. function computeAddress(
  53. bytes32 salt,
  54. bytes32 bytecodeHash,
  55. address deployer
  56. ) internal pure returns (address addr) {
  57. /// @solidity memory-safe-assembly
  58. assembly {
  59. let ptr := mload(0x40) // Get free memory pointer
  60. // | | ↓ ptr ... ↓ ptr + 0x0B (start) ... ↓ ptr + 0x20 ... ↓ ptr + 0x40 ... |
  61. // |-------------------|---------------------------------------------------------------------------|
  62. // | bytecodeHash | CCCCCCCCCCCCC...CC |
  63. // | salt | BBBBBBBBBBBBB...BB |
  64. // | deployer | 000000...0000AAAAAAAAAAAAAAAAAAA...AA |
  65. // | 0xFF | FF |
  66. // |-------------------|---------------------------------------------------------------------------|
  67. // | memory | 000000...00FFAAAAAAAAAAAAAAAAAAA...AABBBBBBBBBBBBB...BBCCCCCCCCCCCCC...CC |
  68. // | keccak(start, 85) | ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ |
  69. mstore(add(ptr, 0x40), bytecodeHash)
  70. mstore(add(ptr, 0x20), salt)
  71. mstore(ptr, deployer) // Right-aligned with 12 preceding garbage bytes
  72. let start := add(ptr, 0x0b) // The hashed data starts at the final garbage byte which we will set to 0xff
  73. mstore8(start, 0xff)
  74. addr := keccak256(start, 85)
  75. }
  76. }
  77. }