Skip to content

Commit

Permalink
simplify prepared
Browse files Browse the repository at this point in the history
  • Loading branch information
oveddan committed Nov 15, 2023
1 parent d84721a commit e76bdf3
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 83 deletions.
11 changes: 6 additions & 5 deletions packages/protocol-sdk/src/mint/mint-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe("mint-helper", () => {
const targetTokenId = 1n;
const minter = createMintClient({ chain: zora });

const { simulateContractParameters: params } =
await minter.makePrepareMintTokenParams({
const params =
await minter.makePrepareMint1155TokenParams({
publicClient,
minterAccount: creatorAccount,
mintable: await minter.getMintable({
Expand Down Expand Up @@ -75,8 +75,8 @@ describe("mint-helper", () => {
const targetTokenId = undefined;
const minter = createMintClient({ chain: zora });

const { simulateContractParameters: prepared } =
await minter.makePrepareMintTokenParams({
const params =
await minter.makePrepareMint721TokenParams({
mintable: await minter.getMintable({
tokenContract: targetContract,
tokenId: targetTokenId,
Expand All @@ -95,7 +95,8 @@ describe("mint-helper", () => {
args: [creatorAccount],
});

const simulated = await publicClient.simulateContract(prepared);
const simulated = await publicClient.simulateContract(params);


const hash = await walletClient.writeContract(simulated.request);

Expand Down
176 changes: 98 additions & 78 deletions packages/protocol-sdk/src/mint/mint-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,50 @@ type MintArguments = {
mintToAddress: Address;
};

type MintParameters = {
mintArguments: MintArguments;
publicClient: PublicClient;
mintable: MintableGetTokenResponse;
sender: Address;
};

const zora721Abi = parseAbi([
"function mintWithRewards(address recipient, uint256 quantity, string calldata comment, address mintReferral) external payable",
"function zoraFeeForAmount(uint256 amount) public view returns (address, uint256)",
] as const);


export type MintContractType = '721' | '1155';


export function getMintType(mintable: MintableGetTokenResponse): MintContractType | undefined {
const mintContextType = mintable.mint_context?.mint_context_type;

if (mintContextType === 'zora_create') return '721';
if (mintContextType === 'zora_create_1155') return '1155';

return;
}

function validateMintable(mintable: MintableGetTokenResponse | undefined) {
if (!mintable) {
throw new MintError("No mintable found");
}

if (!mintable.is_active) {
throw new MintInactiveError("Minting token is inactive");
}

if (!mintable.mint_context) {
throw new MintError("No minting context data from zora API");
}

if (
!["zora_create", "zora_create_1155"].includes(
mintable.mint_context?.mint_context_type!,
)
) {
throw new MintError(
`Mintable type ${mintable.mint_context.mint_context_type} is currently unsupported.`,
);
}

return mintable.mint_context.mint_context_type;
}

class MintClient extends ClientBase {
apiClient: typeof MintAPIClient;

Expand All @@ -61,7 +93,7 @@ class MintClient extends ClientBase {
tokenContract: Address;
tokenId?: bigint | number | string;
}) {
return this.apiClient.getMintable(
return await this.apiClient.getMintable(
{
chain_name: this.network.zoraBackendChainName,
collection_address: tokenContract,
Expand All @@ -70,76 +102,39 @@ class MintClient extends ClientBase {
);
}

async makePrepareMintTokenParams({
/**
* Prepare parameters to submit for a mint transaction, using a mintable token from the Zora API.
*
* @param params Params for minting
* @param params.publicClient Optional public client to use for reading contract state
* @param params.minterAccount Account that is to submit the mint transaction
* @param params.mintable Mintable token from the Zora API
* @param params.mintArguments Arguments for minting
*/
async makePrepareMint1155TokenParams({
publicClient,
minterAccount,
mintable,
mintArguments,
}: {
publicClient: PublicClient;
publicClient: PublicClient | undefined;
mintable: MintableGetTokenResponse;
minterAccount: Address;
mintArguments: MintArguments;
}): Promise<{ simulateContractParameters: SimulateContractParameters }> {
if (!mintable) {
throw new MintError("No mintable found");
}

if (!mintable.is_active) {
throw new MintInactiveError("Minting token is inactive");
}

if (!mintable.mint_context) {
throw new MintError("No minting context data from zora API");
}

if (
!["zora_create", "zora_create_1155"].includes(
mintable.mint_context?.mint_context_type!,
)
) {
throw new MintError(
`Mintable type ${mintable.mint_context.mint_context_type} is currently unsupported.`,
);
}
}) {
const mintContextType = validateMintable(mintable);

const thisPublicClient = this.getPublicClient(publicClient);

if (mintable.mint_context.mint_context_type === "zora_create_1155") {
return {
simulateContractParameters: await this.prepareMintZora1155({
publicClient: thisPublicClient,
mintArguments,
sender: minterAccount,
mintable,
}),
};
}
if (mintable.mint_context.mint_context_type === "zora_create") {
return {
simulateContractParameters: await this.prepareMintZora721({
publicClient: thisPublicClient,
mintArguments,
sender: minterAccount,
mintable,
}),
};
if (mintContextType !== "zora_create_1155") {
throw new Error("Minted token type must be for 1155");
}

throw new Error("Mintable type not found or recognized.");
}

private async prepareMintZora1155({
mintable,
sender,
publicClient,
mintArguments,
}: MintParameters) {
const mintQuantity = BigInt(mintArguments.quantityToMint);

const address = mintable.collection.address as Address;

const mintFee = await publicClient.readContract({
const mintFee = await thisPublicClient.readContract({
abi: zoraCreator1155ImplABI,
functionName: "mintFee",
address,
Expand All @@ -153,13 +148,10 @@ class MintClient extends ClientBase {
},
);

const result: SimulateContractParameters<
typeof zoraCreator1155ImplABI,
"mintWithRewards"
> = {
const result = {
abi: zoraCreator1155ImplABI,
functionName: "mintWithRewards",
account: sender,
account: minterAccount,
value: (mintFee + BigInt(mintable.cost.native_price.raw)) * mintQuantity,
address,
/* args: minter, tokenId, quantity, minterArguments, mintReferral */
Expand All @@ -174,31 +166,56 @@ class MintClient extends ClientBase {
]),
mintArguments.mintReferral || zeroAddress,
],
};
} satisfies SimulateContractParameters<
typeof zoraCreator1155ImplABI,
"mintWithRewards"
>;

return result;
}

private async prepareMintZora721({
mintable,
/**
* Prepare parameters to submit for a mint transaction, using a mintable token from the Zora API.
*
* @param params Params for minting
* @param params.publicClient Optional public client to use for reading contract state
* @param params.minterAccount Account that is to submit the mint transaction
* @param params.mintable Mintable token from the Zora API
* @param params.mintArguments Arguments for minting
*/
async makePrepareMint721TokenParams({
publicClient,
sender,
minterAccount,
mintable,
mintArguments,
}: MintParameters) {
const [_, mintFee] = await publicClient.readContract({
}: {
publicClient: PublicClient | undefined;
mintable: MintableGetTokenResponse;
minterAccount: Address;
mintArguments: MintArguments;
}): Promise<SimulateContractParameters<
typeof zora721Abi,
"mintWithRewards"
>> {
const mintContextType = validateMintable(mintable);

const thisPublicClient = this.getPublicClient(publicClient);

if (mintContextType !== "zora_create") {
throw new Error("Minted token type must be for 1155");
}

const [_, mintFee] = await thisPublicClient.readContract({
abi: zora721Abi,
address: mintable.contract_address as Address,
functionName: "zoraFeeForAmount",
args: [BigInt(mintArguments.quantityToMint)],
});

const result: SimulateContractParameters<
typeof zora721Abi,
"mintWithRewards"
> = {
const result = {
abi: zora721Abi,
address: mintable.contract_address as Address,
account: sender,
account: minterAccount,
functionName: "mintWithRewards",
value:
mintFee +
Expand All @@ -211,7 +228,10 @@ class MintClient extends ClientBase {
mintArguments.mintComment || "",
mintArguments.mintReferral || zeroAddress,
],
};
} satisfies SimulateContractParameters<
typeof zora721Abi,
"mintWithRewards"
>;

return result;
}
Expand Down

0 comments on commit e76bdf3

Please sign in to comment.