-
Notifications
You must be signed in to change notification settings - Fork 49
zksync factory #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wolfy-nft
wants to merge
4
commits into
me-foundation:main
Choose a base branch
from
wolfy-nft:adam/zksync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
zksync factory #170
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ artifacts | |
| dist | ||
| .DS_Store | ||
| out/ | ||
| zkout/ | ||
| lcov.info | ||
| lcov.info.pruned | ||
| coverage/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.22; | ||
|
|
||
| import {Ownable} from "solady/src/auth/Ownable.sol"; | ||
| import {TokenStandard} from "../../common/Structs.sol"; | ||
| import {MagicDropTokenImplRegistry} from "../../registry/MagicDropTokenImplRegistry.sol"; | ||
| import {ZKProxy} from "./ZKProxy.sol"; | ||
|
|
||
| /// @title MagicDropCloneFactory | ||
| /// @notice A zksync compatible factory contract for creating and managing clones of MagicDrop contracts | ||
| contract MagicDropCloneFactory is Ownable { | ||
| /*============================================================== | ||
| = CONSTANTS = | ||
| ==============================================================*/ | ||
|
|
||
| MagicDropTokenImplRegistry private _registry; | ||
| bytes4 private constant INITIALIZE_SELECTOR = bytes4(keccak256("initialize(string,string,address)")); | ||
|
|
||
| /*============================================================== | ||
| = EVENTS = | ||
| ==============================================================*/ | ||
|
|
||
| event MagicDropFactoryInitialized(); | ||
| event NewContractInitialized( | ||
| address contractAddress, address initialOwner, uint32 implId, TokenStandard standard, string name, string symbol | ||
| ); | ||
| event Withdrawal(address to, uint256 amount); | ||
|
|
||
| /*============================================================== | ||
| = ERRORS = | ||
| ==============================================================*/ | ||
|
|
||
| error InitializationFailed(); | ||
| error RegistryAddressCannotBeZero(); | ||
| error InsufficientDeploymentFee(); | ||
| error WithdrawalFailed(); | ||
| error InitialOwnerCannotBeZero(); | ||
|
|
||
| /*============================================================== | ||
| = CONSTRUCTOR = | ||
| ==============================================================*/ | ||
|
|
||
| /// @param initialOwner The address of the initial owner | ||
| /// @param registry The address of the registry contract | ||
| constructor(address initialOwner, address registry) public { | ||
| if (registry == address(0)) { | ||
| revert RegistryAddressCannotBeZero(); | ||
| } | ||
|
|
||
| _registry = MagicDropTokenImplRegistry(registry); | ||
| _initializeOwner(initialOwner); | ||
|
|
||
| emit MagicDropFactoryInitialized(); | ||
| } | ||
|
|
||
| /*============================================================== | ||
| = PUBLIC WRITE METHODS = | ||
| ==============================================================*/ | ||
|
|
||
| /// @notice Creates a new deterministic clone of a MagicDrop contract | ||
| /// @param name The name of the new contract | ||
| /// @param symbol The symbol of the new contract | ||
| /// @param standard The token standard of the new contract | ||
| /// @param initialOwner The initial owner of the new contract | ||
| /// @param implId The implementation ID | ||
| /// @param salt A unique salt for deterministic address generation | ||
| /// @return The address of the newly created contract | ||
| function createContractDeterministic( | ||
| string calldata name, | ||
| string calldata symbol, | ||
| TokenStandard standard, | ||
| address payable initialOwner, | ||
| uint32 implId, | ||
| bytes32 salt | ||
| ) external payable returns (address) { | ||
| address impl; | ||
| // Retrieve the implementation address from the registry | ||
| if (implId == 0) { | ||
| impl = _registry.getDefaultImplementation(standard); | ||
| } else { | ||
| impl = _registry.getImplementation(standard, implId); | ||
| } | ||
|
|
||
| if (initialOwner == address(0)) { | ||
| revert InitialOwnerCannotBeZero(); | ||
| } | ||
|
|
||
| // Retrieve the deployment fee for the implementation and ensure the caller has sent the correct amount | ||
| uint256 deploymentFee = _registry.getDeploymentFee(standard, implId); | ||
| if (msg.value < deploymentFee) { | ||
| revert InsufficientDeploymentFee(); | ||
| } | ||
|
|
||
| // Create a deterministic clone of the implementation contract | ||
| address instance = address(new ZKProxy{salt: salt}(impl)); | ||
|
|
||
| // Initialize the newly created contract | ||
| (bool success,) = instance.call(abi.encodeWithSelector(INITIALIZE_SELECTOR, name, symbol, initialOwner)); | ||
| if (!success) { | ||
| revert InitializationFailed(); | ||
| } | ||
|
|
||
| emit NewContractInitialized({ | ||
| contractAddress: instance, | ||
| initialOwner: initialOwner, | ||
| implId: implId, | ||
| standard: standard, | ||
| name: name, | ||
| symbol: symbol | ||
| }); | ||
|
|
||
| return instance; | ||
| } | ||
|
|
||
| /// @notice Creates a new clone of a MagicDrop contract | ||
| /// @param name The name of the new contract | ||
| /// @param symbol The symbol of the new contract | ||
| /// @param standard The token standard of the new contract | ||
| /// @param initialOwner The initial owner of the new contract | ||
| /// @param implId The implementation ID | ||
| /// @return The address of the newly created contract | ||
| function createContract( | ||
| string calldata name, | ||
| string calldata symbol, | ||
| TokenStandard standard, | ||
| address payable initialOwner, | ||
| uint32 implId | ||
| ) external payable returns (address) { | ||
| address impl; | ||
| // Retrieve the implementation address from the registry | ||
| if (implId == 0) { | ||
| impl = _registry.getDefaultImplementation(standard); | ||
| } else { | ||
| impl = _registry.getImplementation(standard, implId); | ||
| } | ||
|
|
||
| if (initialOwner == address(0)) { | ||
| revert InitialOwnerCannotBeZero(); | ||
| } | ||
|
|
||
| // Retrieve the deployment fee for the implementation and ensure the caller has sent the correct amount | ||
| uint256 deploymentFee = _registry.getDeploymentFee(standard, implId); | ||
| if (msg.value < deploymentFee) { | ||
| revert InsufficientDeploymentFee(); | ||
| } | ||
|
|
||
| // Create a non-deterministic clone of the implementation contract | ||
| address instance = address(new ZKProxy(impl)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replaced LibClone |
||
|
|
||
| // Initialize the newly created contract | ||
| (bool success,) = instance.call(abi.encodeWithSelector(INITIALIZE_SELECTOR, name, symbol, initialOwner)); | ||
| if (!success) { | ||
| revert InitializationFailed(); | ||
| } | ||
|
|
||
| emit NewContractInitialized({ | ||
| contractAddress: instance, | ||
| initialOwner: initialOwner, | ||
| implId: implId, | ||
| standard: standard, | ||
| name: name, | ||
| symbol: symbol | ||
| }); | ||
|
|
||
| return instance; | ||
| } | ||
|
|
||
| /*============================================================== | ||
| = PUBLIC VIEW METHODS = | ||
| ==============================================================*/ | ||
|
|
||
| /// @notice Retrieves the address of the registry contract | ||
| /// @return The address of the registry contract | ||
| function getRegistry() external view returns (address) { | ||
| return address(_registry); | ||
| } | ||
|
|
||
| /*============================================================== | ||
| = ADMIN OPERATIONS = | ||
| ==============================================================*/ | ||
|
|
||
| /// @notice Withdraws the contract's balance | ||
| function withdraw(address to) external onlyOwner { | ||
| (bool success,) = to.call{value: address(this).balance}(""); | ||
| if (!success) { | ||
| revert WithdrawalFailed(); | ||
| } | ||
|
|
||
| emit Withdrawal(to, address(this).balance); | ||
| } | ||
|
|
||
| /// @dev Overriden to prevent double-initialization of the owner. | ||
| function _guardInitializeOwner() internal pure virtual override returns (bool) { | ||
| return true; | ||
| } | ||
|
|
||
| /// @notice Receives ETH | ||
| receive() external payable {} | ||
|
|
||
| /// @notice Fallback function to receive ETH | ||
| fallback() external payable {} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| // This is a ZKSync compatible proxy and a replacement for OZ Clones | ||
| contract ZKProxy { | ||
| address immutable implementation; | ||
|
|
||
| constructor(address _implementation) { | ||
| implementation = _implementation; | ||
| } | ||
|
|
||
| fallback() external payable { | ||
| address impl = implementation; | ||
| assembly { | ||
| // The pointer to the free memory slot | ||
| let ptr := mload(0x40) | ||
| // Copy function signature and arguments from calldata at zero position into memory at pointer position | ||
| calldatacopy(ptr, 0, calldatasize()) | ||
| // Delegatecall method of the implementation contract returns 0 on error | ||
| let result := delegatecall(gas(), impl, ptr, calldatasize(), 0, 0) | ||
| // Get the size of the last return data | ||
| let size := returndatasize() | ||
| // Copy the size length of bytes from return data at zero position to pointer position | ||
| returndatacopy(ptr, 0, size) | ||
| // Depending on the result value | ||
| switch result | ||
| case 0 { | ||
| // End execution and revert state changes | ||
| revert(ptr, size) | ||
| } | ||
| default { | ||
| // Return data with length of size at pointers position | ||
| return(ptr, size) | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced LibClone