Skip to content

Commit

Permalink
remove operator overloading
Browse files Browse the repository at this point in the history
  • Loading branch information
oveddan committed Oct 25, 2023
1 parent 1da50ec commit 38f04f0
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 180 deletions.
19 changes: 12 additions & 7 deletions packages/1155-contracts/package/preminter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,19 @@ describe("ZoraCreator1155Preminter", () => {
});

// recover and verify address is correct
const [, , recoveredAddress] = await publicClient.readContract({
const [isValidSignature] = await publicClient.readContract({
abi: preminterAbi,
address: preminterAddress,
functionName: "isValidSignature",
args: [contractConfig, premintConfig, signedMessage],
functionName: "isValidSignatureV2",
args: [
contractConfig.contractAdmin,
contractAddress,
premintConfig,
signedMessage,
],
});

expect(recoveredAddress).to.equal(creatorAccount);
expect(isValidSignature).toBe(true);
},

20 * 1000
Expand Down Expand Up @@ -277,7 +282,7 @@ describe("ZoraCreator1155Preminter", () => {
// parameters are required to call this function
const mintHash = await walletClient.writeContract({
abi: preminterAbi,
functionName: "premint",
functionName: "premintV2",
account: collectorAccount,
address: preminterAddress,
args: [
Expand Down Expand Up @@ -352,7 +357,7 @@ describe("ZoraCreator1155Preminter", () => {
// it should create a new token against the existing contract
const mintHash2 = await walletClient.writeContract({
abi: preminterAbi,
functionName: "premint",
functionName: "premintV2",
account: collectorAccount,
address: preminterAddress,
args: [
Expand Down Expand Up @@ -450,7 +455,7 @@ describe("ZoraCreator1155Preminter", () => {
// parameters are required to call this function
const mintHash = await walletClient.writeContract({
abi: preminterAbi,
functionName: "premint",
functionName: "premintV2",
account: collectorAccount,
address: preminterAddress,
args: [
Expand Down
26 changes: 12 additions & 14 deletions packages/1155-contracts/package/preminter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ import { ExtractAbiFunction, AbiParametersToPrimitiveTypes } from "abitype";
import { zoraCreator1155PremintExecutorImplABI as preminterAbi } from "./wagmiGenerated";
import { TypedDataDefinition } from "viem";

type PremintInputs = ExtractAbiFunction<
type PremintV1Inputs = ExtractAbiFunction<
typeof preminterAbi,
"premint"
"premintV1"
>["inputs"];

type PreminterHashDataTypes = AbiParametersToPrimitiveTypes<PremintInputs>;
type PremintV2Inputs = ExtractAbiFunction<
typeof preminterAbi,
"premintV2"
>["inputs"];

type PreminterV1HashDataTypes = AbiParametersToPrimitiveTypes<PremintV1Inputs>;
type PreminterV2HashDataTypes = AbiParametersToPrimitiveTypes<PremintV2Inputs>;

export type ContractCreationConfig = PreminterHashDataTypes[0];
export type PremintConfigs = PreminterHashDataTypes[1];
export type ContractCreationConfig = PreminterV2HashDataTypes[0];
export type PremintConfigV1 = PreminterV1HashDataTypes[1];
export type PremintConfigV2 = PreminterV2HashDataTypes[1];

export type TokenCreationConfigV1 = PremintConfigV2["tokenConfig"];
export type PremintConfigV2 = Extract<
PremintConfigs,
{
tokenConfig: {
createReferral: string;
};
}
>;
export type PremintConfigV1 = Exclude<PremintConfigs, PremintConfigV2>;
export type TokenCreationConfigV2 = PremintConfigV2["tokenConfig"];

const premintV2Types = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,19 @@ import {IMinter1155} from "../interfaces/IMinter1155.sol";
import {ERC1155DelegationStorageV1} from "../delegation/ERC1155DelegationStorageV1.sol";
import {ZoraCreator1155PremintExecutorImplLib} from "./ZoraCreator1155PremintExecutorImplLib.sol";
import {PremintEncoding, ZoraCreator1155Attribution, ContractCreationConfig, PremintConfig, PremintConfigV2, TokenCreationConfig, TokenCreationConfigV2} from "./ZoraCreator1155Attribution.sol";
import {IZoraCreator1155PremintExecutor, ILegacyZoraCreator1155PremintExecutor} from "../interfaces/IZoraCreator1155PremintExecutor.sol";

interface IZoraCreator1155PremintV1Signatures {
function delegateSetupNewToken(PremintConfig calldata premintConfig, bytes calldata signature, address sender) external returns (uint256 newTokenId);
}

// interface for legacy v1 of premint executor methods
// maintained in order to not break existing calls
// to legacy api when this api is upgraded
interface ILegacyZoraCreator1155PremintExecutor {
event Preminted(
address indexed contractAddress,
uint256 indexed tokenId,
bool indexed createdNewContract,
uint32 uid,
ContractCreationConfig contractConfig,
TokenCreationConfig tokenConfig,
address minter,
uint256 quantityMinted
);

function premint(
ContractCreationConfig calldata contractConfig,
PremintConfig calldata premintConfig,
bytes calldata signature,
uint256 quantityToMint,
string calldata mintComment
) external payable returns (uint256 newTokenId);
}

struct MintArguments {
address mintRecipient;
string mintComment;
address mintReferral;
}

/// @title Enables creation of and minting tokens on Zora1155 contracts transactions using eip-712 signatures.
/// Signature must provided by the contract creator, or an account that's permitted to create new tokens on the contract.
/// Mints the first x tokens to the executor of the transaction.
/// @author @oveddan
contract ZoraCreator1155PremintExecutorImpl is
ILegacyZoraCreator1155PremintExecutor,
IZoraCreator1155PremintExecutor,
Ownable2StepUpgradeable,
UUPSUpgradeable,
IHasContractName,
Expand All @@ -71,15 +43,6 @@ contract ZoraCreator1155PremintExecutorImpl is
__UUPSUpgradeable_init();
}

event PremintedV2(
address indexed contractAddress,
uint256 indexed tokenId,
bool indexed createdNewContract,
uint32 uid,
address minter,
uint256 quantityMinted
);

/// Creates a new token on the given erc1155 contract on behalf of a creator, and mints x tokens to the executor of this transaction.
/// If the erc1155 contract hasn't been created yet, it will be created with the given config within this same transaction.
/// The creator must sign the intent to create the token, and must have mint new token permission on the erc1155 contract,
Expand All @@ -90,30 +53,41 @@ contract ZoraCreator1155PremintExecutorImpl is
/// @param signature Signature of the creator of the token, which must match the signer of the premint config, or have permission to create new tokens on the erc1155 contract if it's already been created
/// @param quantityToMint How many tokens to mint to the executor of this transaction once the token is created
/// @param mintArguments Abi encoded additional mint arguments: including mintComment and mintReferral
function premint(
function premintV2(
ContractCreationConfig calldata contractConfig,
PremintConfigV2 calldata premintConfig,
bytes calldata signature,
uint256 quantityToMint,
MintArguments calldata mintArguments
) external payable returns (uint256 newTokenId) {
) external payable returns (PremintResult memory result) {
(bytes memory encodedPremint, bytes32 premintVersion) = PremintEncoding.encodePremintV2(premintConfig);
address fixedPriceMinter = premintConfig.tokenConfig.fixedPriceMinter;
uint32 uid = premintConfig.uid;

// we wrap this here to get around stack too deep issues
{
newTokenId = _premint({
result = ZoraCreator1155PremintExecutorImplLib.premint({
zora1155Factory: zora1155Factory,
contractConfig: contractConfig,
encodedPremintConfig: encodedPremint,
premintVersion: premintVersion,
signature: signature,
quantityToMint: quantityToMint,
fixedPriceMinter: fixedPriceMinter,
uid: uid,
mintArguments: mintArguments
});
}

{
emit PremintedV2({
contractAddress: result.contractAddress,
tokenId: result.tokenId,
createdNewContract: result.createdNewContract,
uid: uid,
minter: msg.sender,
quantityMinted: quantityToMint
});
}
}

/// Creates a new token on the given erc1155 contract on behalf of a creator, and mints x tokens to the executor of this transaction.
Expand All @@ -126,74 +100,34 @@ contract ZoraCreator1155PremintExecutorImpl is
/// @param signature Signature of the creator of the token, which must match the signer of the premint config, or have permission to create new tokens on the erc1155 contract if it's already been created
/// @param quantityToMint How many tokens to mint to the executor of this transaction once the token is created
/// @param mintArguments Abi encoded additional mint arguments: including mintComment and mintReferral
function premint(
function premintV1(
ContractCreationConfig calldata contractConfig,
PremintConfig calldata premintConfig,
bytes calldata signature,
uint256 quantityToMint,
MintArguments memory mintArguments
) public payable returns (uint256 newTokenId) {
) public payable returns (PremintResult memory result) {
(bytes memory encodedPremint, bytes32 premintVersion) = PremintEncoding.encodePremintV1(premintConfig);

return
_premint({
contractConfig: contractConfig,
encodedPremintConfig: encodedPremint,
premintVersion: premintVersion,
signature: signature,
quantityToMint: quantityToMint,
fixedPriceMinter: premintConfig.tokenConfig.fixedPriceMinter,
uid: premintConfig.uid,
mintArguments: mintArguments
});
}

function _premint(
ContractCreationConfig calldata contractConfig,
bytes memory encodedPremintConfig,
bytes32 premintVersion,
bytes calldata signature,
uint256 quantityToMint,
address fixedPriceMinter,
uint32 uid,
MintArguments memory mintArguments
) private returns (uint256 newTokenId) {
// get or create the contract with the given params
// contract address is deterministic.
(IZoraCreator1155 tokenContract, bool isNewContract) = ZoraCreator1155PremintExecutorImplLib.getOrCreateContract(zora1155Factory, contractConfig);

// pass the signature and the premint config to the token contract to create the token.
// The token contract will verify the signature and that the signer has permission to create a new token.
// and then create and setup the token using the given token config.
newTokenId = tokenContract.delegateSetupNewToken(encodedPremintConfig, premintVersion, signature, msg.sender);

_performMint(tokenContract, fixedPriceMinter, newTokenId, quantityToMint, mintArguments);

// emit Preminted event
emit PremintedV2(address(tokenContract), newTokenId, isNewContract, uid, msg.sender, quantityToMint);
}

function _performMint(
IZoraCreator1155 tokenContract,
address fixedPriceMinter,
uint256 tokenId,
uint256 quantityToMint,
MintArguments memory mintArguments
) internal {
bytes memory mintSettings = abi.encode(mintArguments.mintRecipient, mintArguments.mintComment);
if (quantityToMint != 0)
// mint the number of specified tokens to the executor
tokenContract.mintWithRewards{value: msg.value}(IMinter1155(fixedPriceMinter), tokenId, quantityToMint, mintSettings, mintArguments.mintReferral);
}

function isValidSignature(
address originalPremintCreator,
address contractAddress,
bytes32 hashedPremint,
bytes32 signatureVersion,
bytes calldata signature
) public view returns (bool isValid, address recoveredSigner) {
return ZoraCreator1155Attribution.isValidSignature(originalPremintCreator, contractAddress, hashedPremint, signatureVersion, signature);
result = ZoraCreator1155PremintExecutorImplLib.premint({
zora1155Factory: zora1155Factory,
contractConfig: contractConfig,
encodedPremintConfig: encodedPremint,
premintVersion: premintVersion,
signature: signature,
quantityToMint: quantityToMint,
fixedPriceMinter: premintConfig.tokenConfig.fixedPriceMinter,
mintArguments: mintArguments
});

emit PremintedV2({
contractAddress: result.contractAddress,
tokenId: result.tokenId,
createdNewContract: result.createdNewContract,
uid: premintConfig.uid,
minter: msg.sender,
quantityMinted: quantityToMint
});
}

/// Gets the deterministic contract address for the given contract creation config.
Expand All @@ -212,35 +146,44 @@ contract ZoraCreator1155PremintExecutorImpl is
return (true, ERC1155DelegationStorageV1(contractAddress).delegatedTokenId(uid));
}

/// deprecated - use isValidSignatureV1
function isValidSignature(
ContractCreationConfig calldata contractConfig,
PremintConfig calldata premintConfig,
bytes calldata signature
) public view returns (bool isValid, address contractAddress, address recoveredSigner) {
contractAddress = getContractAddress(contractConfig);

(isValid, recoveredSigner) = isValidSignatureV1(contractConfig.contractAdmin, contractAddress, premintConfig, signature);
}

function isValidSignatureV1(
address originalContractAdmin,
address contractAddress,
PremintConfig calldata premintConfig,
bytes calldata signature
) public view returns (bool isValid, address recoveredSigner) {
bytes32 hashedPremint = ZoraCreator1155Attribution.hashPremint(premintConfig);

(isValid, recoveredSigner) = isValidSignature(
contractConfig.contractAdmin,
(isValid, recoveredSigner) = ZoraCreator1155Attribution.isValidSignature(
originalContractAdmin,
contractAddress,
hashedPremint,
ZoraCreator1155Attribution.HASHED_VERSION_1,
signature
);
}

function isValidSignature(
ContractCreationConfig calldata contractConfig,
function isValidSignatureV2(
address originalContractAdmin,
address contractAddress,
PremintConfigV2 calldata premintConfig,
bytes calldata signature
) public view returns (bool isValid, address contractAddress, address recoveredSigner) {
contractAddress = getContractAddress(contractConfig);

) public view returns (bool isValid, address recoveredSigner) {
bytes32 hashedPremint = ZoraCreator1155Attribution.hashPremint(premintConfig);

(isValid, recoveredSigner) = isValidSignature(
contractConfig.contractAdmin,
(isValid, recoveredSigner) = ZoraCreator1155Attribution.isValidSignature(
originalContractAdmin,
contractAddress,
hashedPremint,
ZoraCreator1155Attribution.HASHED_VERSION_2,
Expand Down Expand Up @@ -284,6 +227,6 @@ contract ZoraCreator1155PremintExecutorImpl is
// encode legacy mint arguments to call current function:
MintArguments memory mintArguments = MintArguments({mintRecipient: msg.sender, mintComment: mintComment, mintReferral: address(0)});

return premint(contractConfig, premintConfig, signature, quantityToMint, mintArguments);
return premintV1(contractConfig, premintConfig, signature, quantityToMint, mintArguments).tokenId;
}
}
Loading

0 comments on commit 38f04f0

Please sign in to comment.