Skip to content

Commit

Permalink
fix: checkTokenGatedCondition and typescript compilation warning (#608)
Browse files Browse the repository at this point in the history
* fix: typescript compilation warning

* fix: build

* try remove packageManager from package.json

* fix: token gate commit

---------

Co-authored-by: Ludovic Levalleux <levalleux_ludo@hotmail.com>
  • Loading branch information
albertfolch-redeemeum and levalleux-ludo committed Nov 21, 2023
1 parent 818155d commit e466832
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
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 { ITokenInfo, TokenInfoManager } from "../utils/tokenInfoManager";
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 @@ export class OfferMixin extends BaseCoreSDK {
*/
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 @@ export class OfferMixin extends BaseCoreSDK {
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 @@ export class OfferMixin extends BaseCoreSDK {
return true;
}
const log = tokenIdToAvailableCommitsMap.get(tokenId);
if (!log) {
return true;
}
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

0 comments on commit e466832

Please sign in to comment.