Clones.sol 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (proxy/Clones.sol)
  3. pragma solidity ^0.8.20;
  4. import {Errors} from "../utils/Errors.sol";
  5. /**
  6. * @dev https://eips.ethereum.org/EIPS/eip-1167[ERC-1167] is a standard for
  7. * deploying minimal proxy contracts, also known as "clones".
  8. *
  9. * > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
  10. * > a minimal bytecode implementation that delegates all calls to a known, fixed address.
  11. *
  12. * The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
  13. * (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
  14. * deterministic method.
  15. */
  16. library Clones {
  17. /**
  18. * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
  19. *
  20. * This function uses the create opcode, which should never revert.
  21. */
  22. function clone(address implementation) internal returns (address instance) {
  23. return clone(implementation, 0);
  24. }
  25. /**
  26. * @dev Same as {xref-Clones-clone-address-}[clone], but with a `value` parameter to send native currency
  27. * to the new contract.
  28. *
  29. * NOTE: Using a non-zero value at creation will require the contract using this function (e.g. a factory)
  30. * to always have enough balance for new deployments. Consider exposing this function under a payable method.
  31. */
  32. function clone(address implementation, uint256 value) internal returns (address instance) {
  33. if (address(this).balance < value) {
  34. revert Errors.InsufficientBalance(address(this).balance, value);
  35. }
  36. /// @solidity memory-safe-assembly
  37. assembly {
  38. // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
  39. // of the `implementation` address with the bytecode before the address.
  40. mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
  41. // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
  42. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
  43. instance := create(value, 0x09, 0x37)
  44. }
  45. if (instance == address(0)) {
  46. revert Errors.FailedDeployment();
  47. }
  48. }
  49. /**
  50. * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
  51. *
  52. * This function uses the create2 opcode and a `salt` to deterministically deploy
  53. * the clone. Using the same `implementation` and `salt` multiple time will revert, since
  54. * the clones cannot be deployed twice at the same address.
  55. */
  56. function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
  57. return cloneDeterministic(implementation, salt, 0);
  58. }
  59. /**
  60. * @dev Same as {xref-Clones-cloneDeterministic-address-bytes32-}[cloneDeterministic], but with
  61. * a `value` parameter to send native currency to the new contract.
  62. *
  63. * NOTE: Using a non-zero value at creation will require the contract using this function (e.g. a factory)
  64. * to always have enough balance for new deployments. Consider exposing this function under a payable method.
  65. */
  66. function cloneDeterministic(
  67. address implementation,
  68. bytes32 salt,
  69. uint256 value
  70. ) internal returns (address instance) {
  71. if (address(this).balance < value) {
  72. revert Errors.InsufficientBalance(address(this).balance, value);
  73. }
  74. /// @solidity memory-safe-assembly
  75. assembly {
  76. // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
  77. // of the `implementation` address with the bytecode before the address.
  78. mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
  79. // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
  80. mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
  81. instance := create2(value, 0x09, 0x37, salt)
  82. }
  83. if (instance == address(0)) {
  84. revert Errors.FailedDeployment();
  85. }
  86. }
  87. /**
  88. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  89. */
  90. function predictDeterministicAddress(
  91. address implementation,
  92. bytes32 salt,
  93. address deployer
  94. ) internal pure returns (address predicted) {
  95. /// @solidity memory-safe-assembly
  96. assembly {
  97. let ptr := mload(0x40)
  98. mstore(add(ptr, 0x38), deployer)
  99. mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff)
  100. mstore(add(ptr, 0x14), implementation)
  101. mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73)
  102. mstore(add(ptr, 0x58), salt)
  103. mstore(add(ptr, 0x78), keccak256(add(ptr, 0x0c), 0x37))
  104. predicted := and(keccak256(add(ptr, 0x43), 0x55), 0xffffffffffffffffffffffffffffffffffffffff)
  105. }
  106. }
  107. /**
  108. * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
  109. */
  110. function predictDeterministicAddress(
  111. address implementation,
  112. bytes32 salt
  113. ) internal view returns (address predicted) {
  114. return predictDeterministicAddress(implementation, salt, address(this));
  115. }
  116. }