|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.22; |
| 3 | + |
| 4 | +import {Ownable} from "solady/src/auth/Ownable.sol"; |
| 5 | +import {TokenStandard} from "contracts/common/Structs.sol"; |
| 6 | +import {MagicDropTokenImplRegistry} from "contracts/registry/MagicDropTokenImplRegistry.sol"; |
| 7 | +import {Proxy} from "contracts/factory/zksync/Proxy.sol"; |
| 8 | + |
| 9 | +/// @title MagicDropCloneFactory |
| 10 | +/// @notice A factory contract for creating and managing clones of MagicDrop contracts |
| 11 | +/// @dev This contract uses the UUPS proxy pattern |
| 12 | +/// @dev ZKsync compatible version |
| 13 | +contract MagicDropCloneFactory is Ownable { |
| 14 | + /*============================================================== |
| 15 | + = STORAGE = |
| 16 | + ==============================================================*/ |
| 17 | + |
| 18 | + /// @notice The registry contract |
| 19 | + MagicDropTokenImplRegistry private _registry; |
| 20 | + |
| 21 | + /*============================================================== |
| 22 | + = CONSTANTS = |
| 23 | + ==============================================================*/ |
| 24 | + |
| 25 | + bytes4 private constant INITIALIZE_SELECTOR = bytes4(keccak256("initialize(string,string,address,uint256)")); |
| 26 | + |
| 27 | + /*============================================================== |
| 28 | + = EVENTS = |
| 29 | + ==============================================================*/ |
| 30 | + |
| 31 | + event MagicDropFactoryInitialized(); |
| 32 | + event NewContractInitialized( |
| 33 | + address contractAddress, |
| 34 | + address initialOwner, |
| 35 | + uint32 implId, |
| 36 | + TokenStandard standard, |
| 37 | + string name, |
| 38 | + string symbol, |
| 39 | + uint256 mintFee |
| 40 | + ); |
| 41 | + event Withdrawal(address to, uint256 amount); |
| 42 | + |
| 43 | + /*============================================================== |
| 44 | + = ERRORS = |
| 45 | + ==============================================================*/ |
| 46 | + |
| 47 | + error InitializationFailed(); |
| 48 | + error RegistryAddressCannotBeZero(); |
| 49 | + error InsufficientDeploymentFee(); |
| 50 | + error WithdrawalFailed(); |
| 51 | + error InitialOwnerCannotBeZero(); |
| 52 | + error NewImplementationCannotBeZero(); |
| 53 | + |
| 54 | + /*============================================================== |
| 55 | + = INITIALIZER = |
| 56 | + ==============================================================*/ |
| 57 | + |
| 58 | + /// @dev MagicDropCloneFactory constructor. |
| 59 | + /// @param initialOwner The address of the initial owner |
| 60 | + /// @param registry The address of the registry contract |
| 61 | + constructor(address initialOwner, address registry) { |
| 62 | + if (registry == address(0)) { |
| 63 | + revert RegistryAddressCannotBeZero(); |
| 64 | + } |
| 65 | + |
| 66 | + _registry = MagicDropTokenImplRegistry(registry); |
| 67 | + _initializeOwner(initialOwner); |
| 68 | + |
| 69 | + emit MagicDropFactoryInitialized(); |
| 70 | + } |
| 71 | + |
| 72 | + /*============================================================== |
| 73 | + = PUBLIC WRITE METHODS = |
| 74 | + ==============================================================*/ |
| 75 | + |
| 76 | + /// @notice Creates a new deterministic clone of a MagicDrop contract |
| 77 | + /// @param name The name of the new contract |
| 78 | + /// @param symbol The symbol of the new contract |
| 79 | + /// @param standard The token standard of the new contract |
| 80 | + /// @param initialOwner The initial owner of the new contract |
| 81 | + /// @param implId The implementation ID |
| 82 | + /// @param salt A unique salt for deterministic address generation |
| 83 | + /// @return The address of the newly created contract |
| 84 | + function createContractDeterministic( |
| 85 | + string calldata name, |
| 86 | + string calldata symbol, |
| 87 | + TokenStandard standard, |
| 88 | + address payable initialOwner, |
| 89 | + uint32 implId, |
| 90 | + bytes32 salt |
| 91 | + ) external payable returns (address) { |
| 92 | + address impl; |
| 93 | + // Retrieve the implementation address from the registry |
| 94 | + if (implId == 0) { |
| 95 | + impl = _registry.getDefaultImplementation(standard); |
| 96 | + } else { |
| 97 | + impl = _registry.getImplementation(standard, implId); |
| 98 | + } |
| 99 | + |
| 100 | + if (initialOwner == address(0)) { |
| 101 | + revert InitialOwnerCannotBeZero(); |
| 102 | + } |
| 103 | + |
| 104 | + // Retrieve the deployment fee for the implementation and ensure the caller has sent the correct amount |
| 105 | + uint256 deploymentFee = _registry.getDeploymentFee(standard, implId); |
| 106 | + if (msg.value < deploymentFee) { |
| 107 | + revert InsufficientDeploymentFee(); |
| 108 | + } |
| 109 | + |
| 110 | + // Retrieve the mint fee for the implementation |
| 111 | + uint256 mintFee = _registry.getMintFee(standard, implId); |
| 112 | + |
| 113 | + // Create a unique salt by combining original salt with chain ID and sender |
| 114 | + bytes32 _salt = keccak256(abi.encode(salt, block.chainid, msg.sender)); |
| 115 | + // Create a deterministic clone of the implementation contract |
| 116 | + address instance = address(new Proxy{salt: _salt}(impl)); |
| 117 | + |
| 118 | + // Initialize the newly created contract |
| 119 | + (bool success,) = |
| 120 | + instance.call(abi.encodeWithSelector(INITIALIZE_SELECTOR, name, symbol, initialOwner, mintFee)); |
| 121 | + if (!success) { |
| 122 | + revert InitializationFailed(); |
| 123 | + } |
| 124 | + |
| 125 | + emit NewContractInitialized({ |
| 126 | + contractAddress: instance, |
| 127 | + initialOwner: initialOwner, |
| 128 | + implId: implId, |
| 129 | + standard: standard, |
| 130 | + name: name, |
| 131 | + symbol: symbol, |
| 132 | + mintFee: mintFee |
| 133 | + }); |
| 134 | + |
| 135 | + return instance; |
| 136 | + } |
| 137 | + |
| 138 | + /// @notice Creates a new clone of a MagicDrop contract |
| 139 | + /// @param name The name of the new contract |
| 140 | + /// @param symbol The symbol of the new contract |
| 141 | + /// @param standard The token standard of the new contract |
| 142 | + /// @param initialOwner The initial owner of the new contract |
| 143 | + /// @param implId The implementation ID |
| 144 | + /// @return The address of the newly created contract |
| 145 | + function createContract( |
| 146 | + string calldata name, |
| 147 | + string calldata symbol, |
| 148 | + TokenStandard standard, |
| 149 | + address payable initialOwner, |
| 150 | + uint32 implId |
| 151 | + ) external payable returns (address) { |
| 152 | + address impl; |
| 153 | + // Retrieve the implementation address from the registry |
| 154 | + if (implId == 0) { |
| 155 | + impl = _registry.getDefaultImplementation(standard); |
| 156 | + } else { |
| 157 | + impl = _registry.getImplementation(standard, implId); |
| 158 | + } |
| 159 | + |
| 160 | + if (initialOwner == address(0)) { |
| 161 | + revert InitialOwnerCannotBeZero(); |
| 162 | + } |
| 163 | + |
| 164 | + // Retrieve the deployment fee for the implementation and ensure the caller has sent the correct amount |
| 165 | + uint256 deploymentFee = _registry.getDeploymentFee(standard, implId); |
| 166 | + if (msg.value < deploymentFee) { |
| 167 | + revert InsufficientDeploymentFee(); |
| 168 | + } |
| 169 | + |
| 170 | + // Retrieve the mint fee for the implementation |
| 171 | + uint256 mintFee = _registry.getMintFee(standard, implId); |
| 172 | + |
| 173 | + // Create a non-deterministic clone of the implementation contract |
| 174 | + address instance = address(new Proxy(impl)); |
| 175 | + |
| 176 | + // Initialize the newly created contract |
| 177 | + (bool success,) = |
| 178 | + instance.call(abi.encodeWithSelector(INITIALIZE_SELECTOR, name, symbol, initialOwner, mintFee)); |
| 179 | + if (!success) { |
| 180 | + revert InitializationFailed(); |
| 181 | + } |
| 182 | + |
| 183 | + emit NewContractInitialized({ |
| 184 | + contractAddress: instance, |
| 185 | + initialOwner: initialOwner, |
| 186 | + implId: implId, |
| 187 | + standard: standard, |
| 188 | + name: name, |
| 189 | + symbol: symbol, |
| 190 | + mintFee: mintFee |
| 191 | + }); |
| 192 | + |
| 193 | + return instance; |
| 194 | + } |
| 195 | + |
| 196 | + /*============================================================== |
| 197 | + = PUBLIC VIEW METHODS = |
| 198 | + ==============================================================*/ |
| 199 | + |
| 200 | + /// @notice Retrieves the address of the registry contract |
| 201 | + /// @return The address of the registry contract |
| 202 | + function getRegistry() external view returns (address) { |
| 203 | + return address(_registry); |
| 204 | + } |
| 205 | + |
| 206 | + /*============================================================== |
| 207 | + = ADMIN OPERATIONS = |
| 208 | + ==============================================================*/ |
| 209 | + |
| 210 | + /// @notice Withdraws the contract's balance |
| 211 | + function withdraw(address to) external onlyOwner { |
| 212 | + (bool success,) = to.call{value: address(this).balance}(""); |
| 213 | + if (!success) { |
| 214 | + revert WithdrawalFailed(); |
| 215 | + } |
| 216 | + |
| 217 | + emit Withdrawal(to, address(this).balance); |
| 218 | + } |
| 219 | + |
| 220 | + /// @dev Overriden to prevent double-initialization of the owner. |
| 221 | + function _guardInitializeOwner() internal pure virtual override returns (bool) { |
| 222 | + return true; |
| 223 | + } |
| 224 | + |
| 225 | + /// @notice Receives ETH |
| 226 | + receive() external payable {} |
| 227 | + |
| 228 | + /// @notice Fallback function to receive ETH |
| 229 | + fallback() external payable {} |
| 230 | +} |
0 commit comments