Skip to content

Commit

Permalink
move more logic to lib, reducing code size
Browse files Browse the repository at this point in the history
  • Loading branch information
oveddan committed Oct 19, 2023
1 parent 63da27d commit a281f58
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -366,49 +370,58 @@ 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(
PremintConfig calldata premintConfig,
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 {
Expand Down
72 changes: 15 additions & 57 deletions packages/1155-contracts/src/nft/ZoraCreator1155Impl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -756,80 +756,50 @@ 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(
PremintConfigV2 calldata premintConfig,
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;

Expand All @@ -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);
}
}

0 comments on commit a281f58

Please sign in to comment.