From a281f580801f7843d232ddbb5bc85b5ebd05112c Mon Sep 17 00:00:00 2001 From: Dan Oved Date: Wed, 18 Oct 2023 12:49:56 -0700 Subject: [PATCH] move more logic to lib, reducing code size --- .../delegation/ZoraCreator1155Attribution.sol | 47 +++++++----- .../src/nft/ZoraCreator1155Impl.sol | 72 ++++--------------- 2 files changed, 45 insertions(+), 74 deletions(-) diff --git a/packages/1155-contracts/src/delegation/ZoraCreator1155Attribution.sol b/packages/1155-contracts/src/delegation/ZoraCreator1155Attribution.sol index f93723881..a569c1295 100644 --- a/packages/1155-contracts/src/delegation/ZoraCreator1155Attribution.sol +++ b/packages/1155-contracts/src/delegation/ZoraCreator1155Attribution.sol @@ -352,12 +352,16 @@ library PremintTokenSetup { } } -struct CreatorAttributionParams { +struct DelegatedTokenSetup { bytes32 structHash; string name; string version; address creator; bytes signature; + uint32 uid; + string tokenURI; + uint256 maxSupply; + address createReferral; } library DelegatedTokenCreation { @@ -366,25 +370,31 @@ library DelegatedTokenCreation { bytes calldata signature, address tokenContract, uint256 nextTokenId - ) external returns (CreatorAttributionParams memory creatorAttribution, bytes[] memory tokenSetupActions) { + ) external returns (DelegatedTokenSetup memory params, bytes[] memory tokenSetupActions) { validatePremint(premintConfig); - creatorAttribution.structHash = ZoraCreator1155Attribution.hashPremint(premintConfig); + params.structHash = ZoraCreator1155Attribution.hashPremint(premintConfig); - creatorAttribution.version = ZoraCreator1155Attribution.VERSION_2; + params.version = ZoraCreator1155Attribution.VERSION_2; - creatorAttribution.creator = ZoraCreator1155Attribution.recoverSignerHashed( - creatorAttribution.structHash, + params.creator = ZoraCreator1155Attribution.recoverSignerHashed( + params.structHash, signature, tokenContract, ZoraCreator1155Attribution.HASHED_VERSION_2, block.chainid ); - creatorAttribution.signature = signature; - creatorAttribution.name = ZoraCreator1155Attribution.NAME; + params.signature = signature; + params.name = ZoraCreator1155Attribution.NAME; - tokenSetupActions = PremintTokenSetup.makeSetupNewTokenCalls(nextTokenId, creatorAttribution.creator, premintConfig.tokenConfig); + params.uid = premintConfig.uid; + + tokenSetupActions = PremintTokenSetup.makeSetupNewTokenCalls(nextTokenId, params.creator, premintConfig.tokenConfig); + + params.tokenURI = premintConfig.tokenConfig.tokenURI; + params.maxSupply = premintConfig.tokenConfig.maxSupply; + params.createReferral = premintConfig.tokenConfig.createReferral; } function recoverDelegatedToken( @@ -392,23 +402,26 @@ library DelegatedTokenCreation { bytes calldata signature, address tokenContract, uint256 nextTokenId - ) external returns (CreatorAttributionParams memory creatorAttribution, bytes[] memory tokenSetupActions) { - creatorAttribution.structHash = ZoraCreator1155Attribution.hashPremint(premintConfig); + ) external returns (DelegatedTokenSetup memory params, bytes[] memory tokenSetupActions) { + params.structHash = ZoraCreator1155Attribution.hashPremint(premintConfig); - creatorAttribution.version = ZoraCreator1155Attribution.VERSION_1; + params.version = ZoraCreator1155Attribution.VERSION_1; - creatorAttribution.creator = ZoraCreator1155Attribution.recoverSignerHashed( - creatorAttribution.structHash, + params.creator = ZoraCreator1155Attribution.recoverSignerHashed( + params.structHash, signature, tokenContract, ZoraCreator1155Attribution.HASHED_VERSION_1, block.chainid ); - creatorAttribution.signature = signature; - creatorAttribution.name = ZoraCreator1155Attribution.NAME; + params.signature = signature; + params.name = ZoraCreator1155Attribution.NAME; + + tokenSetupActions = PremintTokenSetup.makeSetupNewTokenCalls(nextTokenId, params.creator, premintConfig.tokenConfig); - tokenSetupActions = PremintTokenSetup.makeSetupNewTokenCalls(nextTokenId, creatorAttribution.creator, premintConfig.tokenConfig); + params.tokenURI = premintConfig.tokenConfig.tokenURI; + params.maxSupply = premintConfig.tokenConfig.maxSupply; } function validatePremint(PremintConfigV2 calldata premintConfig) private view { diff --git a/packages/1155-contracts/src/nft/ZoraCreator1155Impl.sol b/packages/1155-contracts/src/nft/ZoraCreator1155Impl.sol index b6907b9b9..94a033921 100644 --- a/packages/1155-contracts/src/nft/ZoraCreator1155Impl.sol +++ b/packages/1155-contracts/src/nft/ZoraCreator1155Impl.sol @@ -32,7 +32,7 @@ import {TransferHelperUtils} from "../utils/TransferHelperUtils.sol"; import {ZoraCreator1155StorageV1} from "./ZoraCreator1155StorageV1.sol"; import {IZoraCreator1155Errors} from "../interfaces/IZoraCreator1155Errors.sol"; import {ERC1155DelegationStorageV1} from "../delegation/ERC1155DelegationStorageV1.sol"; -import {ZoraCreator1155Attribution, PremintTokenSetup, PremintConfig, PremintConfigV2, DelegatedTokenCreation, CreatorAttributionParams} from "../delegation/ZoraCreator1155Attribution.sol"; +import {ZoraCreator1155Attribution, PremintTokenSetup, PremintConfig, PremintConfigV2, DelegatedTokenCreation, DelegatedTokenSetup} from "../delegation/ZoraCreator1155Attribution.sol"; /// Imagine. Mint. Enjoy. /// @title ZoraCreator1155Impl @@ -281,7 +281,7 @@ contract ZoraCreator1155Impl is return tokenId; } - function _setupNewTokenAndPermission(string calldata newURI, uint256 maxSupply, address user, uint256 permission) internal returns (uint256) { + function _setupNewTokenAndPermission(string memory newURI, uint256 maxSupply, address user, uint256 permission) internal returns (uint256) { uint256 tokenId = _setupNewToken(newURI, maxSupply); _addPermission(tokenId, user, permission); @@ -756,22 +756,14 @@ contract ZoraCreator1155Impl is bytes calldata signature, address sender ) external nonReentrant returns (uint256 newTokenId) { - (CreatorAttributionParams memory creatorAttribution, bytes[] memory tokenSetupActions) = DelegatedTokenCreation.recoverDelegatedToken( + (DelegatedTokenSetup memory params, bytes[] memory tokenSetupActions) = DelegatedTokenCreation.recoverDelegatedToken( premintConfig, signature, address(this), nextTokenId ); - return _delegateSetupNewToken( - premintConfig.uid, - creatorAttribution, - tokenSetupActions, - premintConfig.tokenConfig.tokenURI, - premintConfig.tokenConfig.maxSupply, - address(0), - sender - ); + return _delegateSetupNewToken(params, tokenSetupActions, sender); } function delegateSetupNewToken( @@ -779,57 +771,35 @@ contract ZoraCreator1155Impl is bytes calldata signature, address sender ) external nonReentrant returns (uint256 newTokenId) { - (CreatorAttributionParams memory creatorAttribution, bytes[] memory tokenSetupActions) = DelegatedTokenCreation.recoverDelegatedToken( + (DelegatedTokenSetup memory params, bytes[] memory tokenSetupActions) = DelegatedTokenCreation.recoverDelegatedToken( premintConfig, signature, address(this), nextTokenId ); - return _delegateSetupNewToken( - premintConfig.uid, - creatorAttribution, - tokenSetupActions, - premintConfig.tokenConfig.tokenURI, - premintConfig.tokenConfig.maxSupply, - premintConfig.tokenConfig.createReferral, - sender - ); + return _delegateSetupNewToken(params, tokenSetupActions, sender); } - function _delegateSetupNewToken( - uint32 uid, - CreatorAttributionParams memory creatorAttribution, - bytes[] memory tokenSetupActions, - string calldata tokenURI, - uint256 maxSupply, - address createReferral, - address sender - ) internal returns (uint256 newTokenId) { + function _delegateSetupNewToken(DelegatedTokenSetup memory params, bytes[] memory tokenSetupActions, address sender) internal returns (uint256 newTokenId) { // if a token has already been created for a premint config with this uid: - if (delegatedTokenId[uid] != 0) { + if (delegatedTokenId[params.uid] != 0) { // return its token id - return delegatedTokenId[uid]; + return delegatedTokenId[params.uid]; } // this is what attributes this token to have been created by the original creator - emit CreatorAttribution( - creatorAttribution.structHash, - creatorAttribution.name, - creatorAttribution.version, - creatorAttribution.creator, - creatorAttribution.signature - ); + emit CreatorAttribution(params.structHash, params.name, params.version, params.creator, params.signature); // require that the signer can create new tokens (is a valid creator) - _requireAdminOrRole(creatorAttribution.creator, CONTRACT_BASE_ID, PERMISSION_BIT_MINTER); + _requireAdminOrRole(params.creator, CONTRACT_BASE_ID, PERMISSION_BIT_MINTER); // create the new token; msg sender will have PERMISSION_BIT_ADMIN on the new token - newTokenId = _setupNewTokenAndPermission(tokenURI, maxSupply, msg.sender, PERMISSION_BIT_ADMIN); + newTokenId = _setupNewTokenAndPermission(params.tokenURI, params.maxSupply, msg.sender, PERMISSION_BIT_ADMIN); - _setCreateReferral(newTokenId, createReferral); + _setCreateReferral(newTokenId, params.createReferral); - delegatedTokenId[uid] = newTokenId; + delegatedTokenId[params.uid] = newTokenId; firstMinters[newTokenId] = sender; @@ -840,18 +810,6 @@ contract ZoraCreator1155Impl is _removePermission(newTokenId, msg.sender, PERMISSION_BIT_ADMIN); // grant the token creator as admin of the newly created token - _addPermission(newTokenId, creatorAttribution.creator, PERMISSION_BIT_ADMIN); - } - - function validatePremint(PremintConfigV2 calldata premintConfig) private view { - if (premintConfig.tokenConfig.mintStart != 0 && premintConfig.tokenConfig.mintStart > block.timestamp) { - // if the mint start is in the future, then revert - revert MintNotYetStarted(); - } - if (premintConfig.deleted) { - // if the signature says to be deleted, then dont execute any further minting logic; - // return 0 - revert PremintDeleted(); - } + _addPermission(newTokenId, params.creator, PERMISSION_BIT_ADMIN); } }