Skip to content

Commit

Permalink
fix: logic and e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albertfolch-redeemeum committed Oct 20, 2023
1 parent a4418db commit 4dda871
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 44 deletions.
130 changes: 106 additions & 24 deletions e2e/tests/core-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } =
Expand Down Expand Up @@ -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 } =
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 = {
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 = {
Expand All @@ -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);
}
);

Expand Down
36 changes: 18 additions & 18 deletions packages/core-sdk/src/offers/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export class OfferMixin extends BaseCoreSDK {
): Promise<boolean> {
const offer = await this.getOfferById(offerId);

if (!offer.condition) {
if (!offer?.condition) {
return true;

Check warning on line 420 in packages/core-sdk/src/offers/mixin.ts

View check run for this annotation

Codecov / codecov/patch

packages/core-sdk/src/offers/mixin.ts#L420

Added line #L420 was not covered by tests
}
const getCanTokenIdBeUsedToCommit = async (): Promise<
Expand Down Expand Up @@ -458,28 +458,22 @@ 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;
};
return canTokenIdBeUsedToCommit;
};

const getCurrentCommits = async (): Promise<number> => {
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
}
}
});

Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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++;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ type ConditionalCommitAuthorizedEventLog @entity {
hash: String!
type: EventType!
timestamp: BigInt!
buyerAddress: String!
buyerAddress: Bytes!

offerId: String!
groupId: String!
Expand Down
3 changes: 2 additions & 1 deletion packages/subgraph/src/entities/event-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4dda871

Please sign in to comment.