Skip to content

Commit

Permalink
Update mint client to fix reading from subgraph (#392)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
iainnash authored and kulkarohan committed Dec 8, 2023
1 parent 400d68d commit 0b84095
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-bobcats-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zoralabs/protocol-sdk": patch
---

Fix reading the FIXED_PRICE_MINTER from the subgraph
2 changes: 1 addition & 1 deletion packages/protocol-sdk/src/create/1155-create-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
| {
Expand Down
32 changes: 27 additions & 5 deletions packages/protocol-sdk/src/mint/mint-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,40 @@ export class MintAPIClient {
}: {
contractAddress: string;
tokenId: bigint;
}): Promise<undefined | string> {
}): Promise<undefined | { address: Address; pricePerToken: bigint }> {
const { retries, post } = this.httpClient;
return retries(async () => {
const response = await post<any>(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),
};
});
}

Expand Down
21 changes: 13 additions & 8 deletions packages/protocol-sdk/src/mint/mint-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand Down Expand Up @@ -306,10 +307,12 @@ export type MintCosts = {

export async function get1155MintCosts({
mintable,
price,
publicClient,
quantityToMint,
}: {
mintable: MintableGetTokenResponse;
price: bigint;
publicClient: PublicClient;
quantityToMint: bigint;
}): Promise<MintCosts> {
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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,
Expand Down

0 comments on commit 0b84095

Please sign in to comment.