Skip to content
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

Rename some params for better understanding what they mean #335

Merged
merged 10 commits into from
Nov 8, 2023
23 changes: 15 additions & 8 deletions packages/protocol-sdk/src/mint/mint-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ describe("mint-helper", () => {
const targetTokenId = 1n;
const minter = new MintClient(zora);

const { send } = await minter.mintToken({
address: targetContract,
const { simulateContractParameters: params } = await minter.makePrepareMintTokenParams({
tokenContract: targetContract,
tokenId: targetTokenId,
publicClient,
sender: creatorAccount,
minterAccount: creatorAccount,
mintArguments: {
mintToAddress: creatorAccount,
quantityToMint: 1,
Expand All @@ -43,7 +43,10 @@ describe("mint-helper", () => {
functionName: "balanceOf",
args: [creatorAccount, targetTokenId],
});
const hash = await send(walletClient);

const simulationResult = await publicClient.simulateContract(params);

const hash = await walletClient.writeContract(simulationResult.request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
const newBalance = await publicClient.readContract({
abi: zoraCreator1155ImplABI,
Expand Down Expand Up @@ -72,11 +75,11 @@ describe("mint-helper", () => {
const targetTokenId = undefined;
const minter = new MintClient(zora);

const { send } = await minter.mintToken({
address: targetContract,
const { simulateContractParameters: prepared } = await minter.makePrepareMintTokenParams({
tokenContract: targetContract,
tokenId: targetTokenId,
publicClient,
sender: creatorAccount,
minterAccount: creatorAccount,
mintArguments: {
mintToAddress: creatorAccount,
quantityToMint: 1,
Expand All @@ -88,7 +91,11 @@ describe("mint-helper", () => {
functionName: "balanceOf",
args: [creatorAccount],
});
const hash = await send(walletClient);

const simulated = await publicClient.simulateContract(prepared);

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

const receipt = await publicClient.getTransactionReceipt({ hash });
expect(receipt).not.to.be.null;

Expand Down
61 changes: 35 additions & 26 deletions packages/protocol-sdk/src/mint/mint-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
Address,
Chain,
PublicClient,
WalletClient,
encodeAbiParameters,
parseAbi,
parseAbiParameters,
zeroAddress,
} from "viem";
import { ClientBase } from "../apis/client-base";
import { MintAPIClient, MintableGetTokenResponse } from "./mint-api-client";
import { SimulateContractParameters } from "viem";
import {
zoraCreator1155ImplABI,
zoraCreatorFixedPriceSaleStrategyAddress,
Expand Down Expand Up @@ -54,27 +54,30 @@ export class MintClient extends ClientBase {
this.apiClient = apiClient;
}

async mintToken({
async makePrepareMintTokenParams({
publicClient,
sender,
address,
minterAccount,
tokenContract,
tokenId,
mintArguments,
}: {
publicClient: PublicClient;
address: Address;
sender: Address;
tokenContract: Address;
minterAccount: Address;
tokenId?: bigint | number | string;
mintArguments: MintArguments;
}) {
}): Promise<{
simulateContractParameters: SimulateContractParameters,
mintable: any
}> {
if (tokenId) {
tokenId = BigInt(tokenId);
}

const mintable = await this.apiClient.getMintable(
{
chain_name: this.network.zoraBackendChainName,
collection_address: address,
collection_address: tokenContract,
},
{ token_id: tokenId?.toString() },
);
Expand All @@ -97,25 +100,27 @@ export class MintClient extends ClientBase {
);
}

const thisPublicClient = this.getPublicClient(publicClient);

if (
mintable.feed_item.mint_context.mint_context_type === "zora_create_1155"
) {
return {
send: await this.mintZora1155({
publicClient: this.getPublicClient(publicClient),
simulateContractParameters: await this.prepareMintZora1155({
publicClient: thisPublicClient,
mintArguments,
sender,
sender: minterAccount,
mintable,
}),
mintable,
};
}
if (mintable.feed_item.mint_context.mint_context_type === "zora_create") {
return {
send: await this.mintZora721({
publicClient: this.getPublicClient(publicClient),
simulateContractParameters: await this.prepareMintZora721({
publicClient: thisPublicClient,
mintArguments,
sender,
sender: minterAccount,
mintable,
}),
mintable,
Expand All @@ -125,7 +130,7 @@ export class MintClient extends ClientBase {
throw new Error("Mintable type not found or recognized.");
}

private async mintZora1155({
private async prepareMintZora1155({
mintable,
sender,
publicClient,
Expand All @@ -149,8 +154,10 @@ export class MintClient extends ClientBase {
},
);

return async (walletClient: WalletClient) => {
const { request } = await publicClient.simulateContract({
const result: SimulateContractParameters<
typeof zoraCreator1155ImplABI,
'mintWithRewards'
> = {
abi: zoraCreator1155ImplABI,
functionName: "mintWithRewards",
account: sender,
Expand All @@ -170,12 +177,12 @@ export class MintClient extends ClientBase {
]),
mintArguments.mintReferral || zeroAddress,
],
});
return await walletClient.writeContract(request);
};
}

return result;
}

private async mintZora721({
private async prepareMintZora721({
mintable,
publicClient,
sender,
Expand All @@ -188,8 +195,10 @@ export class MintClient extends ClientBase {
args: [BigInt(mintArguments.quantityToMint)],
});

return async (walletClient: WalletClient) => {
const { request } = await publicClient.simulateContract({
const result: SimulateContractParameters<
typeof zora721Abi,
'mintWithRewards'
> = {
abi: zora721Abi,
address: mintable.feed_item.contract_address as Address,
account: sender,
Expand All @@ -205,8 +214,8 @@ export class MintClient extends ClientBase {
mintArguments.mintComment || "",
mintArguments.mintReferral || zeroAddress,
],
});
return await walletClient.writeContract(request);
};
}

return result;
}
}
Loading