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

Example of returning prepared tx from mint client #334

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/protocol-sdk/src/mint/mint-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("mint-helper", () => {
const targetTokenId = 1n;
const minter = new MintClient(zora);

const { send } = await minter.mintToken({
const { simulateContractParameters: params } = await minter.makePrepareMintTokenParams({
address: targetContract,
tokenId: targetTokenId,
publicClient,
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,7 +75,7 @@ describe("mint-helper", () => {
const targetTokenId = undefined;
const minter = new MintClient(zora);

const { send } = await minter.mintToken({
const { simulateContractParameters: prepared } = await minter.makePrepareMintTokenParams({
address: targetContract,
tokenId: targetTokenId,
publicClient,
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
47 changes: 28 additions & 19 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,7 +54,7 @@ export class MintClient extends ClientBase {
this.apiClient = apiClient;
}

async mintToken({
async makePrepareMintTokenParams({
publicClient,
sender,
address,
Expand All @@ -66,7 +66,10 @@ export class MintClient extends ClientBase {
sender: Address;
tokenId?: bigint | number | string;
mintArguments: MintArguments;
}) {
}): Promise<{
simulateContractParameters: SimulateContractParameters,
mintable: any
}> {
if (tokenId) {
tokenId = BigInt(tokenId);
}
Expand Down Expand Up @@ -97,12 +100,14 @@ 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,
mintable,
Expand All @@ -112,8 +117,8 @@ export class MintClient extends ClientBase {
}
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,
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