Skip to content

Commit

Permalink
Example of returning prepared tx from mint client (#334)
Browse files Browse the repository at this point in the history
* Instead of having the clients write the transaction, have them prepare them, making them client agnostic

* rename

---------

Co-authored-by: iain nash <[email protected]>
  • Loading branch information
oveddan and iainnash authored Nov 8, 2023
1 parent 2bc63d7 commit 5e4b6fa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
22 changes: 14 additions & 8 deletions packages/protocol-sdk/src/mint/mint-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ 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,
mintable: await minter.getMintable({
tokenId: targetTokenId,
Expand All @@ -42,7 +44,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 @@ -71,11 +76,8 @@ describe("mint-helper", () => {
const targetTokenId = undefined;
const minter = new MintClient(zora);

const { send } = await minter.mintToken({
mintable: await minter.getMintable({
address: targetContract,
tokenId: targetTokenId,
}),
const { simulateContractParameters: prepared } = await minter.makePrepareMintTokenParams({
mintable: await minter.getMintable({address: targetContract, tokenId: targetTokenId}),
publicClient,
sender: creatorAccount,
mintArguments: {
Expand All @@ -89,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
42 changes: 24 additions & 18 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 @@ -70,7 +70,7 @@ export class MintClient extends ClientBase {
);
}

async mintToken({
async makePrepareMintTokenParams({
publicClient,
sender,
mintable,
Expand Down Expand Up @@ -103,10 +103,12 @@ export class MintClient extends ClientBase {
);
}

const thisPublicClient = this.getPublicClient(publicClient);

if (mintable.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 @@ -116,8 +118,8 @@ export class MintClient extends ClientBase {
}
if (mintable.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 @@ -129,7 +131,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 @@ -153,8 +155,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 @@ -173,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 @@ -191,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.contract_address as Address,
account: sender,
Expand All @@ -208,8 +214,8 @@ export class MintClient extends ClientBase {
mintArguments.mintComment || "",
mintArguments.mintReferral || zeroAddress,
],
});
return await walletClient.writeContract(request);
};
}

return result;
}
}
2 changes: 1 addition & 1 deletion packages/protocol-sdk/src/premint/premint-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { foundry } from "viem/chains";
import { describe, expect, vi } from "vitest";
import { PremintClient } from "./premint-client";
import { BackendChainNamesLookup } from "../apis/client-base";
import { anvilTest } from "src/anvil";
import { BackendChainNamesLookup } from "src/apis/chain-constants";

describe("ZoraCreator1155Premint", () => {
anvilTest(
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol-sdk/src/premint/premint-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import type {
} from "./premint-api-client";
import { PremintAPIClient } from "./premint-api-client";
import type { DecodeEventLogReturnType } from "viem";
import { ClientBase, REWARD_PER_TOKEN } from "../apis/client-base";
import { ClientBase } from "../apis/client-base";
import { OPEN_EDITION_MINT_SIZE } from "../constants";
import { REWARD_PER_TOKEN } from "src/apis/chain-constants";

type MintArgumentsSettings = {
tokenURI: string;
Expand Down

0 comments on commit 5e4b6fa

Please sign in to comment.