From 4dda8714b0e1f1918b909ddf18d2b2fbcdea4c92 Mon Sep 17 00:00:00 2001 From: Albert Folch Date: Fri, 20 Oct 2023 11:03:55 +0200 Subject: [PATCH] fix: logic and e2e tests --- e2e/tests/core-sdk.test.ts | 130 ++++++++++++++++---- packages/core-sdk/src/offers/mixin.ts | 36 +++--- packages/subgraph/schema.graphql | 2 +- packages/subgraph/src/entities/event-log.ts | 3 +- 4 files changed, 127 insertions(+), 44 deletions(-) diff --git a/e2e/tests/core-sdk.test.ts b/e2e/tests/core-sdk.test.ts index 7d44468ed..f7e3c7228 100644 --- a/e2e/tests/core-sdk.test.ts +++ b/e2e/tests/core-sdk.test.ts @@ -320,7 +320,7 @@ describe("core-sdk", () => { }); test.each(["ERC721", "ERC1155", "ERC20"])( - `create an group on %p token and try to commit outside of that group`, + `create a group on %p token and try to commit outside of that group`, async (token) => { const tokenId = Date.now().toString(); const { sellerCoreSDK, buyerCoreSDK, sellerWallet } = @@ -422,7 +422,7 @@ describe("core-sdk", () => { ); test.each(["ERC721", "ERC1155", "ERC20"])( - `create an group on %p token and buyer successfully commit to of that group`, + `create a group on %p token and buyer successfully commit to of that group`, async (token) => { const tokenId = Date.now().toString(); const { sellerCoreSDK, buyerCoreSDK, sellerWallet, buyerWallet } = @@ -690,11 +690,18 @@ describe("core-sdk", () => { } ); - test.each(["ERC721-threshold", "ERC721-specific", "ERC1155", "ERC20"])( + test.each([ + "ERC721-peraddress-threshold", + "ERC721-peraddress-tokenrange", + "ERC721-pertokenid-tokenrange", + "ERC1155-peraddress", + "ERC1155-pertokenid", + "ERC20" + ])( `create an offer with condition on %p and buyer meets the condition of that token gated`, async (token) => { - const tokenId = Date.now().toString(); - + const tokenId = Date.now(); + const tokenId2 = tokenId + 1; const { sellerCoreSDK, buyerCoreSDK, sellerWallet, buyerWallet } = await initSellerAndBuyerSDKs(seedWallet); @@ -714,9 +721,8 @@ describe("core-sdk", () => { let conditionToCreate; - if (token === "ERC721-threshold") { + if (token === "ERC721-peraddress-threshold") { await ensureMintedERC721(buyerWallet, tokenId); - const tokenId2 = Date.now().toString(); await ensureMintedERC721(buyerWallet, tokenId2); conditionToCreate = { method: EvaluationMethod.Threshold, @@ -728,19 +734,33 @@ describe("core-sdk", () => { threshold: "2", maxCommits: "3" }; - } else if (token === "ERC721-specific") { + } else if (token === "ERC721-peraddress-tokenrange") { await ensureMintedERC721(buyerWallet, tokenId); + await ensureMintedERC721(buyerWallet, tokenId2); conditionToCreate = { method: EvaluationMethod.TokenRange, tokenType: TokenType.NonFungibleToken, tokenAddress: MOCK_ERC721_ADDRESS, gatingType: GatingType.PerAddress, minTokenId: tokenId, - maxTokenId: tokenId, + maxTokenId: tokenId2, + threshold: "0", + maxCommits: "3" + }; + } else if (token === "ERC721-pertokenid-tokenrange") { + await ensureMintedERC721(buyerWallet, tokenId); + await ensureMintedERC721(buyerWallet, tokenId2); + conditionToCreate = { + method: EvaluationMethod.TokenRange, + tokenType: TokenType.NonFungibleToken, + tokenAddress: MOCK_ERC721_ADDRESS, + gatingType: GatingType.PerTokenId, + minTokenId: tokenId, + maxTokenId: tokenId2, threshold: "0", maxCommits: "3" }; - } else if (token === "ERC1155") { + } else if (token === "ERC1155-peraddress") { await ensureMintedERC1155(buyerWallet, tokenId, "4"); conditionToCreate = { method: EvaluationMethod.Threshold, @@ -752,6 +772,18 @@ describe("core-sdk", () => { threshold: "3", maxCommits: "3" }; + } else if (token === "ERC1155-pertokenid") { + await ensureMintedERC1155(buyerWallet, tokenId, "4"); + conditionToCreate = { + method: EvaluationMethod.Threshold, + tokenType: TokenType.MultiToken, + tokenAddress: MOCK_ERC1155_ADDRESS, + gatingType: GatingType.PerTokenId, + minTokenId: tokenId, + maxTokenId: tokenId, + threshold: "3", + maxCommits: "3" + }; } else if (token === "ERC20") { await ensureMintedAndAllowedTokens([buyerWallet], "5"); conditionToCreate = { @@ -774,23 +806,35 @@ describe("core-sdk", () => { conditionToCreate ); - await createOfferCondTx.wait(); + const receipt = await createOfferCondTx.wait(); await waitForGraphNodeIndexing(); + const offerId = buyerCoreSDK.getCreatedOfferIdFromLogs(receipt.logs); const buyerAddress = await buyerWallet.getAddress(); + + if (!offerId) { + throw new Error(`offerId is not defined ${offerId}`); + } + const isMet = await buyerCoreSDK.checkTokenGatedCondition( - conditionToCreate, + offerId, buyerAddress ); - - await expect(isMet).toBe(true); + expect(isMet).toBe(true); } ); - test.each(["ERC721-threshold", "ERC721-specific", "ERC1155", "ERC20"])( - `create an offer with condition on %p and buyer does not meet the condition of that token gated`, + test.each([ + "ERC721-peraddress-threshold", + "ERC721-peraddress-tokenrange", + "ERC721-pertokenid-tokenrange", + "ERC1155-peraddress", + "ERC1155-pertokenid", + "ERC20" + ])( + `create an offer with condition on %p and buyer does NOT meet the condition of that token gated`, async (token) => { - const tokenId = Date.now().toString(); + const tokenId = Date.now(); const { sellerCoreSDK, buyerCoreSDK, sellerWallet, buyerWallet } = await initSellerAndBuyerSDKs(seedWallet); @@ -811,7 +855,7 @@ describe("core-sdk", () => { let conditionToCreate; - if (token === "ERC721-threshold") { + if (token === "ERC721-peraddress-threshold") { await ensureMintedERC721(buyerWallet, tokenId); conditionToCreate = { method: EvaluationMethod.Threshold, @@ -823,7 +867,7 @@ describe("core-sdk", () => { threshold: "2", maxCommits: "3" }; - } else if (token === "ERC721-specific") { + } else if (token === "ERC721-peraddress-tokenrange") { await ensureMintedERC721(sellerWallet, tokenId); conditionToCreate = { method: EvaluationMethod.TokenRange, @@ -835,7 +879,19 @@ describe("core-sdk", () => { threshold: "0", maxCommits: "3" }; - } else if (token === "ERC1155") { + } else if (token === "ERC721-pertokenid-tokenrange") { + await ensureMintedERC721(buyerWallet, tokenId); + conditionToCreate = { + method: EvaluationMethod.TokenRange, + tokenType: TokenType.NonFungibleToken, + tokenAddress: MOCK_ERC721_ADDRESS, + gatingType: GatingType.PerTokenId, + minTokenId: tokenId, + maxTokenId: tokenId, + threshold: "0", + maxCommits: "1" + }; + } else if (token === "ERC1155-peraddress") { await ensureMintedERC1155(buyerWallet, tokenId, "2"); conditionToCreate = { method: EvaluationMethod.Threshold, @@ -847,6 +903,18 @@ describe("core-sdk", () => { threshold: "3", maxCommits: "3" }; + } else if (token === "ERC1155-pertokenid") { + await ensureMintedERC1155(buyerWallet, tokenId, "4"); + conditionToCreate = { + method: EvaluationMethod.Threshold, + tokenType: TokenType.MultiToken, + tokenAddress: MOCK_ERC1155_ADDRESS, + gatingType: GatingType.PerTokenId, + minTokenId: tokenId, + maxTokenId: tokenId, + threshold: "3", + maxCommits: "1" + }; } else if (token === "ERC20") { await ensureMintedAndAllowedTokens([buyerWallet], "5"); conditionToCreate = { @@ -869,16 +937,30 @@ describe("core-sdk", () => { conditionToCreate ); - await createOfferCondTx.wait(); + const receipt = await createOfferCondTx.wait(); await waitForGraphNodeIndexing(); + const offerId = buyerCoreSDK.getCreatedOfferIdFromLogs(receipt.logs); + + if (!offerId) { + throw new Error(`offerId is not defined ${offerId}`); + } + if ( + ["ERC721-pertokenid-tokenrange", "ERC1155-pertokenid"].includes(token) + ) { + // let's use the tokenId to make it fail + await ( + await buyerCoreSDK.commitToConditionalOffer(offerId, tokenId) + ).wait(); + await waitForGraphNodeIndexing(); + } const buyerAddress = await buyerWallet.getAddress(); + const isMet = await buyerCoreSDK.checkTokenGatedCondition( - conditionToCreate, + offerId, buyerAddress ); - - await expect(isMet).toBe(false); + expect(isMet).toBe(false); } ); diff --git a/packages/core-sdk/src/offers/mixin.ts b/packages/core-sdk/src/offers/mixin.ts index d5149feee..6a55493d4 100644 --- a/packages/core-sdk/src/offers/mixin.ts +++ b/packages/core-sdk/src/offers/mixin.ts @@ -416,7 +416,7 @@ export class OfferMixin extends BaseCoreSDK { ): Promise { const offer = await this.getOfferById(offerId); - if (!offer.condition) { + if (!offer?.condition) { return true; } const getCanTokenIdBeUsedToCommit = async (): Promise< @@ -458,6 +458,9 @@ export class OfferMixin extends BaseCoreSDK { } ); const canTokenIdBeUsedToCommit = (tokenId: TokenId): boolean => { + if (!tokenIdToAvailableCommitsMap.has(tokenId)) { + return true; + } const log = tokenIdToAvailableCommitsMap.get(tokenId); return Number(log.maxCommits) - Number(log.commitCount) > 0; }; @@ -465,21 +468,12 @@ export class OfferMixin extends BaseCoreSDK { }; const getCurrentCommits = async (): Promise => { - const products = await ( - this as unknown as MetadataMixin - ).getAllProductsWithVariants({ - productsFilter: { - variants: [`variant-${offer.id}`] - } - }); - - const [product] = products; // it should be only one - const productOffers = product.variants.map((variant) => variant.offer.id); - const exchanges = await (this as unknown as ExchangesMixin).getExchanges({ exchangesFilter: { - offer_in: productOffers, - buyer: buyerAddress + buyer: buyerAddress, + offer_: { + condition: offer.condition.id + } } }); @@ -570,8 +564,11 @@ export class OfferMixin extends BaseCoreSDK { } let tokenId = minTokenId; for await (const owners of batchTasks(promises, concurrencyLimit)) { - if (owners.some((owner) => owner === buyerAddress)) { - return canTokenIdBeUsedToCommit(tokenId.toString()); + if ( + owners.some((owner) => owner === buyerAddress) && + canTokenIdBeUsedToCommit(tokenId.toString()) + ) { + return true; } tokenId++; } @@ -625,9 +622,12 @@ export class OfferMixin extends BaseCoreSDK { let tokenId = minTokenId; for await (const balances of batchTasks(promises, concurrencyLimit)) { if ( - balances.some((balance) => BigNumber.from(balance).gte(threshold)) + balances.some((balance) => + BigNumber.from(balance).gte(threshold) + ) && + canTokenIdBeUsedToCommit(tokenId.toString()) ) { - return canTokenIdBeUsedToCommit(tokenId.toString()); + return true; } tokenId++; } diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index 94cedbfb7..0f1a990a8 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -724,7 +724,7 @@ type ConditionalCommitAuthorizedEventLog @entity { hash: String! type: EventType! timestamp: BigInt! - buyerAddress: String! + buyerAddress: Bytes! offerId: String! groupId: String! diff --git a/packages/subgraph/src/entities/event-log.ts b/packages/subgraph/src/entities/event-log.ts index 255a71133..3323b279a 100644 --- a/packages/subgraph/src/entities/event-log.ts +++ b/packages/subgraph/src/entities/event-log.ts @@ -254,12 +254,13 @@ export function saveConditionalCommitAuthorizedEventLog( eventLog.hash = txHash; eventLog.type = type; eventLog.timestamp = timestamp; - eventLog.buyerAddress = buyerAddress.toString(); + eventLog.buyerAddress = buyerAddress; eventLog.commitCount = commitCount; eventLog.maxCommits = maxCommits; eventLog.gating = gating; eventLog.tokenId = tokenId; eventLog.groupId = groupId; + eventLog.offerId = offerId.toString(); eventLog.save(); return eventLogId;