From b2284c2bca5269545085833850e4afde637c65dc Mon Sep 17 00:00:00 2001 From: Iain Nash Date: Wed, 29 Nov 2023 18:39:19 -0500 Subject: [PATCH] Update mint client to fix reading from subgraph (#392) * update mint client to fix reading from subgraph * update mint-client * update mint client to use subgraph price * add changeset and update the fixedPrice subgraph fetch --- .changeset/sharp-bobcats-sin.md | 5 +++ .../src/create/1155-create-helper.ts | 2 +- .../protocol-sdk/src/mint/mint-api-client.ts | 32 ++++++++++++++++--- packages/protocol-sdk/src/mint/mint-client.ts | 21 +++++++----- 4 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 .changeset/sharp-bobcats-sin.md diff --git a/.changeset/sharp-bobcats-sin.md b/.changeset/sharp-bobcats-sin.md new file mode 100644 index 000000000..9d7232003 --- /dev/null +++ b/.changeset/sharp-bobcats-sin.md @@ -0,0 +1,5 @@ +--- +"@zoralabs/protocol-sdk": patch +--- + +Fix reading the FIXED_PRICE_MINTER from the subgraph diff --git a/packages/protocol-sdk/src/create/1155-create-helper.ts b/packages/protocol-sdk/src/create/1155-create-helper.ts index 84f9b276d..5ec3f8062 100644 --- a/packages/protocol-sdk/src/create/1155-create-helper.ts +++ b/packages/protocol-sdk/src/create/1155-create-helper.ts @@ -45,7 +45,7 @@ export const DEFAULT_SALE_SETTINGS = { }; // Hardcode the permission bit for the minter -const PERMISSION_BIT_MINTER = 2n ** 2n; +const PERMISSION_BIT_MINTER = 4n; type ContractType = | { diff --git a/packages/protocol-sdk/src/mint/mint-api-client.ts b/packages/protocol-sdk/src/mint/mint-api-client.ts index 14ed5cdaa..572c0a5b1 100644 --- a/packages/protocol-sdk/src/mint/mint-api-client.ts +++ b/packages/protocol-sdk/src/mint/mint-api-client.ts @@ -56,18 +56,40 @@ export class MintAPIClient { }: { contractAddress: string; tokenId: bigint; - }): Promise { + }): Promise { const { retries, post } = this.httpClient; return retries(async () => { const response = await post(this.networkConfig.subgraphUrl, { - query: - "query($id: ID!) {\n zoraCreateToken(id: $id) {\n id\n salesStrategies{\n fixedPrice {\n address\n }\n }\n }\n}", + query: `query ($id: ID!) { + zoraCreateToken(id: $id) { + id + salesStrategies(where: {type: "FIXED_PRICE"}) { + type + fixedPrice { + address + pricePerToken + saleEnd + saleStart + maxTokensPerAddress + } + } + } + }`, variables: { id: `${contractAddress.toLowerCase()}-${tokenId.toString()}`, }, }); - return response.zoraCreateToken?.salesStrategies?.find(() => true) - ?.fixedPriceMinterAddress; + + const fixedPrice: { + address: Address; + pricePerToken: string; + } = response.data?.zoraCreateToken?.salesStrategies?.find(() => true) + ?.fixedPrice; + + return { + address: fixedPrice.address as Address, + pricePerToken: BigInt(fixedPrice.pricePerToken), + }; }); } diff --git a/packages/protocol-sdk/src/mint/mint-client.ts b/packages/protocol-sdk/src/mint/mint-client.ts index 0930d05c2..06bdf3d72 100644 --- a/packages/protocol-sdk/src/mint/mint-client.ts +++ b/packages/protocol-sdk/src/mint/mint-client.ts @@ -109,6 +109,7 @@ class MintClient { if (mintContextType === "zora_create_1155") { return await get1155MintCosts({ mintable, + price: BigInt(mintable.cost.native_price.raw), publicClient: this.publicClient, quantityToMint: BigInt(quantityToMint), }); @@ -306,10 +307,12 @@ export type MintCosts = { export async function get1155MintCosts({ mintable, + price, publicClient, quantityToMint, }: { mintable: MintableGetTokenResponse; + price: bigint; publicClient: PublicClient; quantityToMint: bigint; }): Promise { @@ -321,8 +324,7 @@ export async function get1155MintCosts({ }); const mintFeeForTokens = mintFee * quantityToMint; - const tokenPurchaseCost = - BigInt(mintable.cost.native_price.raw) * quantityToMint; + const tokenPurchaseCost = price * quantityToMint; return { mintFee: mintFeeForTokens, @@ -354,19 +356,22 @@ async function makePrepareMint1155TokenParams({ const address = mintable.collection.address as Address; + const tokenFixedPriceMinter = await apiClient.getSalesConfigFixedPrice({ + contractAddress: address, + tokenId: BigInt(mintable.token_id!), + }); + const mintValue = ( await get1155MintCosts({ mintable, + price: + tokenFixedPriceMinter?.pricePerToken || + BigInt(mintable.cost.native_price.raw), publicClient, quantityToMint: mintQuantity, }) ).totalCost; - const tokenFixedPriceMinter = await apiClient.getSalesConfigFixedPrice({ - contractAddress: address, - tokenId: BigInt(mintable.token_id!), - }); - const result = { abi: zoraCreator1155ImplABI, functionName: "mintWithRewards", @@ -375,7 +380,7 @@ async function makePrepareMint1155TokenParams({ address, /* args: minter, tokenId, quantity, minterArguments, mintReferral */ args: [ - (tokenFixedPriceMinter || + (tokenFixedPriceMinter?.address || zoraCreatorFixedPriceSaleStrategyAddress[999]) as Address, BigInt(mintable.token_id!), mintQuantity,