Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: checkTokenGatedCondition and typescript compilation warning #608

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/tests/core-sdk-funds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe("core-sdk-funds", () => {
});

describe("locked funds", () => {
test("completed exchanges should be excluded when coputing the locked funds value", async () => {
test("completed exchanges should be excluded when computing the locked funds value", async () => {
const { sellerCoreSDK, buyerCoreSDK, sellerWallet, buyerWallet } =
await initSellerAndBuyerSDKs(seedWallet);
const sellers = await ensureCreatedSeller(sellerWallet);
Expand Down
27 changes: 23 additions & 4 deletions e2e/tests/core-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,11 +784,12 @@ describe("core-sdk", () => {
"ERC721-pertokenid-tokenrange",
"ERC1155-peraddress",
"ERC1155-pertokenid",
"ERC20"
"ERC20-threshold",
"ERC20-commits"
])(
`create an offer with condition on %p and buyer does NOT meet the condition of that token gated`,
async (token) => {
const tokenId = Date.now();
let tokenId = Date.now();

const { sellerCoreSDK, buyerCoreSDK, sellerWallet, buyerWallet } =
await initSellerAndBuyerSDKs(seedWallet);
Expand Down Expand Up @@ -869,7 +870,8 @@ describe("core-sdk", () => {
threshold: "3",
maxCommits: "1"
};
} else if (token === "ERC20") {
} else if (token === "ERC20-threshold") {
tokenId = 0;
await ensureMintedAndAllowedTokens([buyerWallet], "5");
conditionToCreate = {
method: EvaluationMethod.Threshold,
Expand All @@ -881,6 +883,19 @@ describe("core-sdk", () => {
threshold: "7000000000000000000",
maxCommits: "1"
};
} else if (token === "ERC20-commits") {
tokenId = 0;
await ensureMintedAndAllowedTokens([buyerWallet], "5");
conditionToCreate = {
method: EvaluationMethod.Threshold,
tokenType: TokenType.FungibleToken,
tokenAddress: MOCK_ERC20_ADDRESS,
gatingType: GatingType.PerAddress,
minTokenId: "0",
maxTokenId: "0",
threshold: "1",
maxCommits: "1"
};
}

const createOfferCondTx = await sellerCoreSDK.createOfferWithCondition(
Expand All @@ -899,7 +914,11 @@ describe("core-sdk", () => {
throw new Error(`offerId is not defined ${offerId}`);
}
if (
["ERC721-pertokenid-tokenrange", "ERC1155-pertokenid"].includes(token)
[
"ERC721-pertokenid-tokenrange",
"ERC1155-pertokenid",
"ERC20-commits"
].includes(token)
) {
// let's use the tokenId to make it fail
await (
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"url": "https://github.com/bosonprotocol/core-components/issues"
},
"homepage": "https://github.com/bosonprotocol/core-components#readme",
"packageManager": "npm@8.11.0",
"devDependencies": {
"@graphql-codegen/cli": "^2.6.2",
"@graphql-codegen/typescript": "^2.5.1",
Expand Down
24 changes: 15 additions & 9 deletions packages/core-sdk/src/offers/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import { batchTasks } from "../utils/promises";
import { ExchangesMixin } from "../exchanges/mixin";
import { EventLogsMixin } from "../event-logs/mixin";
import { MetadataMixin } from "../metadata/mixin";
import { AccountsMixin } from "../accounts/mixin";

export class OfferMixin extends BaseCoreSDK {
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -361,7 +361,7 @@
*/
public async getExchangeTokenInfo(
exchangeToken: string
): Promise<ITokenInfo> {
): Promise<ITokenInfo | undefined> {
if (this._tokenInfoManager === undefined) {
this._tokenInfoManager = new TokenInfoManager(
this._chainId,
Expand Down Expand Up @@ -419,14 +419,15 @@
if (!offer?.condition) {
return true;
}
const offerConditionId = offer.condition.id;
const getCanTokenIdBeUsedToCommit = async (): Promise<
(tokenId: string) => boolean
> => {
const conditionalCommitLogs = await (
this as unknown as EventLogsMixin
).getConditionalCommitAuthorizedEventLogs({
conditionalCommitAuthorizedLogsFilter: {
groupId: offer.condition.id, // all offers of the same product have the same condition.id
groupId: offerConditionId, // all offers of the same product have the same condition.id
buyerAddress
}
});
Expand Down Expand Up @@ -462,23 +463,28 @@
return true;
}
const log = tokenIdToAvailableCommitsMap.get(tokenId);
if (!log) {
return true;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L467 was not covered by tests
}
return Number(log.maxCommits) - Number(log.commitCount) > 0;
};
return canTokenIdBeUsedToCommit;
};

const getCurrentCommits = async (): Promise<number> => {
const exchanges = await (this as unknown as ExchangesMixin).getExchanges({
const buyers = await (this as unknown as AccountsMixin).getBuyers({
buyersFilter: {
wallet: buyerAddress
},
includeExchanges: true,
exchangesFilter: {
buyer: buyerAddress,
offer_: {
condition: offer.condition.id
condition: offerConditionId
}
}
});

const currentCommits = exchanges.length;
return currentCommits;
const [buyer] = buyers ?? [];
return buyer?.exchanges?.length ?? 0;
};

const concurrencyLimit = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ const TokenGated = ({
(async () => {
if (condition?.tokenAddress && condition?.tokenType === 0) {
try {
const { name, decimals, symbol } = await coreSDK.getExchangeTokenInfo(
const result = await coreSDK.getExchangeTokenInfo(
condition.tokenAddress
);
setTokenInfo({ name, decimals: decimals?.toString(), symbol });
const { name = "", decimals, symbol = "" } = result ?? {};
setTokenInfo({ name, decimals: decimals?.toString() ?? "", symbol });
} catch (error) {
setTokenInfo({ name: "", decimals: "", symbol: "" });
}
Expand Down
Loading