123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.0.0) (proxy/Clones.sol)
- pragma solidity ^0.8.20;
- import {Errors} from "../utils/Errors.sol";
- /**
- * @dev https://eips.ethereum.org/EIPS/eip-1167[ERC-1167] is a standard for
- * deploying minimal proxy contracts, also known as "clones".
- *
- * > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
- * > a minimal bytecode implementation that delegates all calls to a known, fixed address.
- *
- * The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
- * (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
- * deterministic method.
- */
- library Clones {
- /**
- * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
- *
- * This function uses the create opcode, which should never revert.
- */
- function clone(address implementation) internal returns (address instance) {
- return clone(implementation, 0);
- }
- /**
- * @dev Same as {xref-Clones-clone-address-}[clone], but with a `value` parameter to send native currency
- * to the new contract.
- *
- * NOTE: Using a non-zero value at creation will require the contract using this function (e.g. a factory)
- * to always have enough balance for new deployments. Consider exposing this function under a payable method.
- */
- function clone(address implementation, uint256 value) internal returns (address instance) {
- if (address(this).balance < value) {
- revert Errors.InsufficientBalance(address(this).balance, value);
- }
- /// @solidity memory-safe-assembly
- assembly {
- // Stores the bytecode after address
- mstore(0x20, 0x5af43d82803e903d91602b57fd5bf3)
- // implementation address
- mstore(0x11, implementation)
- // Packs the first 3 bytes of the `implementation` address with the bytecode before the address.
- mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
- instance := create(value, 0x09, 0x37)
- }
- if (instance == address(0)) {
- revert Errors.FailedDeployment();
- }
- }
- /**
- * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
- *
- * This function uses the create2 opcode and a `salt` to deterministically deploy
- * the clone. Using the same `implementation` and `salt` multiple time will revert, since
- * the clones cannot be deployed twice at the same address.
- */
- function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
- return cloneDeterministic(implementation, salt, 0);
- }
- /**
- * @dev Same as {xref-Clones-cloneDeterministic-address-bytes32-}[cloneDeterministic], but with
- * a `value` parameter to send native currency to the new contract.
- *
- * NOTE: Using a non-zero value at creation will require the contract using this function (e.g. a factory)
- * to always have enough balance for new deployments. Consider exposing this function under a payable method.
- */
- function cloneDeterministic(
- address implementation,
- bytes32 salt,
- uint256 value
- ) internal returns (address instance) {
- if (address(this).balance < value) {
- revert Errors.InsufficientBalance(address(this).balance, value);
- }
- /// @solidity memory-safe-assembly
- assembly {
- // Stores the bytecode after address
- mstore(0x20, 0x5af43d82803e903d91602b57fd5bf3)
- // implementation address
- mstore(0x11, implementation)
- // Packs the first 3 bytes of the `implementation` address with the bytecode before the address.
- mstore(0x00, or(shr(0x88, implementation), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
- instance := create2(value, 0x09, 0x37, salt)
- }
- if (instance == address(0)) {
- revert Errors.FailedDeployment();
- }
- }
- /**
- * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
- */
- function predictDeterministicAddress(
- address implementation,
- bytes32 salt,
- address deployer
- ) internal pure returns (address predicted) {
- /// @solidity memory-safe-assembly
- assembly {
- let ptr := mload(0x40)
- mstore(add(ptr, 0x38), deployer)
- mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff)
- mstore(add(ptr, 0x14), implementation)
- mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73)
- mstore(add(ptr, 0x58), salt)
- mstore(add(ptr, 0x78), keccak256(add(ptr, 0x0c), 0x37))
- predicted := keccak256(add(ptr, 0x43), 0x55)
- }
- }
- /**
- * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
- */
- function predictDeterministicAddress(
- address implementation,
- bytes32 salt
- ) internal view returns (address predicted) {
- return predictDeterministicAddress(implementation, salt, address(this));
- }
- }
|