Skip to content

Commit

Permalink
chore: add missing tests for changed method
Browse files Browse the repository at this point in the history
  • Loading branch information
levalleux-ludo committed Feb 23, 2024
1 parent 3628f0e commit 79a8b5b
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 44 deletions.
115 changes: 111 additions & 4 deletions e2e/tests/core-sdk-premint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { BigNumber, Wallet } from "ethers";
import { parseEther } from "@ethersproject/units";

import {
createPremintedOfferAddToGroup,
createPremintedOfferWithCondition,
createSeaportOrder,
createSeller,
createSellerAndOffer,
createSellerAndPremintedOffer,
createSellerAndPremintedOfferWithCondition,
ensureMintedERC1155,
initCoreSDKWithFundedWallet,
metadata,
MOCK_ERC1155_ADDRESS,
MOCK_SEAPORT_ADDRESS,
seedWallet14,
Expand All @@ -23,15 +25,12 @@ import {
Range,
TokenType
} from "../../packages/common/src";
import { mockCreateOfferArgs } from "@bosonprotocol/common/tests/mocks";

jest.setTimeout(60_000);

const seedWallet = seedWallet14; // be sure the seedWallet is not used by another test (to allow concurrent run)
const buyerWallet = seedWallet15;

// TODO: add tests with createPremintedOfferAddToGroup(), createPremintedOfferWithCondition(), createSellerAndPremintedOffer(), createSellerAndPremintedOfferWithCondition()

describe("core-sdk-premint", () => {
test("can reserveRange and then preMint some vouchers and there are still some left to preMint", async () => {
const { coreSDK, fundedWallet } = await initCoreSDKWithFundedWallet(
Expand Down Expand Up @@ -360,6 +359,114 @@ describe("orchestration", () => {
expect(Number(offer.range?.end) - Number(offer.range?.start) + 1).toEqual(
Number(premintParameters.reservedRangeLength)
);
expect(offer.condition).toBeTruthy();
expect(offer.condition?.tokenAddress.toLowerCase()).toEqual(
condition.tokenAddress
);
});
test("#createPremintedOfferAddToGroup()", async () => {
const { coreSDK, fundedWallet } = await initCoreSDKWithFundedWallet(
seedWallet
);
await createSeller(coreSDK, fundedWallet.address);

// Ensure the condition token is minted
const tokenID = Date.now().toString();
await ensureMintedERC1155(fundedWallet, tokenID, "5");
const condition = {
method: EvaluationMethod.Threshold,
tokenType: TokenType.MultiToken,
tokenAddress: MOCK_ERC1155_ADDRESS.toLowerCase(),
gatingType: GatingType.PerAddress,
minTokenId: tokenID,
maxTokenId: tokenID,
threshold: "1",
maxCommits: "3"
};
const premintParameters = {
reservedRangeLength: "5",
to: fundedWallet.address
};

const { offer, groupId } = await createPremintedOfferAddToGroup(
coreSDK,
condition,
premintParameters
);
expect(offer).toBeTruthy();
expect(offer.range).toBeTruthy();
expect(offer.range?.owner?.toLowerCase()).toEqual(
premintParameters.to.toLowerCase()
);
expect(Number(offer.range?.end) - Number(offer.range?.start) + 1).toEqual(
Number(premintParameters.reservedRangeLength)
);
expect(offer.condition).toBeTruthy();
expect(offer.condition?.id).toEqual(groupId);
});
test("#createSellerAndPremintedOffer()", async () => {
const { coreSDK, fundedWallet } = await initCoreSDKWithFundedWallet(
seedWallet
);
const premintParameters = {
reservedRangeLength: "5",
to: fundedWallet.address
};

const offer = await createSellerAndPremintedOffer(
coreSDK,
fundedWallet.address,
premintParameters
);
expect(offer).toBeTruthy();
expect(offer.range).toBeTruthy();
expect(offer.range?.owner?.toLowerCase()).toEqual(
premintParameters.to.toLowerCase()
);
expect(Number(offer.range?.end) - Number(offer.range?.start) + 1).toEqual(
Number(premintParameters.reservedRangeLength)
);
});
test("#createSellerAndPremintedOfferWithCondition()", async () => {
const { coreSDK, fundedWallet } = await initCoreSDKWithFundedWallet(
seedWallet
);
// Ensure the condition token is minted
const tokenID = Date.now().toString();
await ensureMintedERC1155(fundedWallet, tokenID, "5");
const condition = {
method: EvaluationMethod.Threshold,
tokenType: TokenType.MultiToken,
tokenAddress: MOCK_ERC1155_ADDRESS.toLowerCase(),
gatingType: GatingType.PerAddress,
minTokenId: tokenID,
maxTokenId: tokenID,
threshold: "1",
maxCommits: "3"
};
const premintParameters = {
reservedRangeLength: "5",
to: fundedWallet.address
};

const offer = await createSellerAndPremintedOfferWithCondition(
coreSDK,
fundedWallet.address,
condition,
premintParameters
);
expect(offer).toBeTruthy();
expect(offer.range).toBeTruthy();
expect(offer.range?.owner?.toLowerCase()).toEqual(
premintParameters.to.toLowerCase()
);
expect(Number(offer.range?.end) - Number(offer.range?.start) + 1).toEqual(
Number(premintParameters.reservedRangeLength)
);
expect(offer.condition).toBeTruthy();
expect(offer.condition?.tokenAddress.toLowerCase()).toEqual(
condition.tokenAddress
);
});
});
function getExchangeIdFromRange(range: Range): number {
Expand Down
175 changes: 174 additions & 1 deletion e2e/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import {
IpfsMetadataStorage
} from "../../packages/ipfs-storage/src";
import { EthersAdapter } from "../../packages/ethers-sdk/src";
import { CreateOfferArgs, PremintParametersStruct } from "./../../packages/common/src/types/offers";
import {
CreateOfferArgs,
PremintParametersStruct
} from "./../../packages/common/src/types/offers";
import { mockCreateOfferArgs } from "../../packages/common/tests/mocks";
import {
ACCOUNT_1,
Expand Down Expand Up @@ -557,6 +560,176 @@ export async function createPremintedOfferWithCondition(
return offer;
}

export async function createPremintedOfferAddToGroup(
coreSDK: CoreSDK,
condition: ConditionStruct,
premintParameters: PremintParametersStruct,
overrides: {
offerParams?: Partial<CreateOfferArgs>;
metadata?: Partial<base.BaseMetadata>;
} = {}
) {
const metadataHash = await coreSDK.storeMetadata({
...metadata,
type: "BASE",
...overrides.metadata
});
const metadataUri = "ipfs://" + metadataHash;

const offerArgs = mockCreateOfferArgs({
metadataHash,
metadataUri,
...overrides.offerParams
});
const groupToCreate = {
offerIds: [],
...condition
};
const createdGroupTx = await coreSDK.createGroup(groupToCreate);
const txReceipt = await createdGroupTx.wait();
const createdGroupIds = await coreSDK.getCreatedGroupIdsFromLogs(
txReceipt.logs
);
expect(createdGroupIds.length).toEqual(1);
const [groupId] = createdGroupIds;

const createOfferTxResponse = await coreSDK.createPremintedOfferAddToGroup(
offerArgs,
premintParameters,
groupId
);
const createOfferTxReceipt = await createOfferTxResponse.wait();
const createdOfferId = coreSDK.getCreatedOfferIdFromLogs(
createOfferTxReceipt.logs
);

await waitForGraphNodeIndexing(createOfferTxReceipt);
const offer = await coreSDK.getOfferById(createdOfferId as string);

return { offer, groupId };
}

export async function createSellerAndPremintedOffer(
coreSDK: CoreSDK,
sellerAddress: string,
premintParameters: PremintParametersStruct,
overrides: {
offerParams?: Partial<CreateOfferArgs>;
metadata?: Partial<base.BaseMetadata>;
sellerMetadata?: Partial<seller.SellerMetadata>;
} = {}
) {
const sellerContractHash = await coreSDK.storeMetadata({
...metadata,
type: "BASE",
...overrides.metadata
});
const sellerContractUri = "ipfs://" + sellerContractHash;
const sellerMetadataHash = await coreSDK.storeMetadata({
...sellerMetadata,
...overrides.sellerMetadata
});
const sellerMetadataUri = "ipfs://" + sellerMetadataHash;

const metadataHash = await coreSDK.storeMetadata({
...metadata,
type: "BASE",
...overrides.metadata
});
const metadataUri = "ipfs://" + metadataHash;

const offerArgs = mockCreateOfferArgs({
metadataHash,
metadataUri,
...overrides.offerParams
});

const txResponse = await coreSDK.createSellerAndPremintedOffer(
{
assistant: sellerAddress,
admin: sellerAddress,
treasury: sellerAddress,
contractUri: sellerContractUri,
royaltyPercentage: "0",
authTokenId: "0",
authTokenType: 0,
metadataUri: sellerMetadataUri
},
offerArgs,
premintParameters
);
const txReceipt = await txResponse.wait();
const createdOfferId = coreSDK.getCreatedOfferIdFromLogs(txReceipt.logs);
if (createdOfferId === null) {
throw new Error("Failed to create seller adn preminted offer");
}
await waitForGraphNodeIndexing(txReceipt);
const offer = await coreSDK.getOfferById(createdOfferId as string);

return offer;
}

export async function createSellerAndPremintedOfferWithCondition(
coreSDK: CoreSDK,
sellerAddress: string,
condition: ConditionStruct,
premintParameters: PremintParametersStruct,
overrides: {
offerParams?: Partial<CreateOfferArgs>;
metadata?: Partial<base.BaseMetadata>;
sellerMetadata?: Partial<seller.SellerMetadata>;
} = {}
) {
const sellerContractHash = await coreSDK.storeMetadata({
...metadata,
type: "BASE",
...overrides.metadata
});
const sellerContractUri = "ipfs://" + sellerContractHash;
const sellerMetadataHash = await coreSDK.storeMetadata({
...sellerMetadata,
...overrides.sellerMetadata
});
const sellerMetadataUri = "ipfs://" + sellerMetadataHash;

const metadataHash = await coreSDK.storeMetadata({
...metadata,
type: "BASE",
...overrides.metadata
});
const metadataUri = "ipfs://" + metadataHash;

const offerArgs = mockCreateOfferArgs({
metadataHash,
metadataUri,
...overrides.offerParams
});

const txResponse = await coreSDK.createSellerAndPremintedOfferWithCondition(
{
assistant: sellerAddress,
admin: sellerAddress,
treasury: sellerAddress,
contractUri: sellerContractUri,
royaltyPercentage: "0",
authTokenId: "0",
authTokenType: 0,
metadataUri: sellerMetadataUri
},
offerArgs,
premintParameters,
condition
);
const txReceipt = await txResponse.wait();
const createdOfferId = coreSDK.getCreatedOfferIdFromLogs(txReceipt.logs);
if (createdOfferId === null) {
throw new Error("Failed to create seller adn preminted offer");
}
await waitForGraphNodeIndexing(txReceipt);
const offer = await coreSDK.getOfferById(createdOfferId as string);

return offer;
}

export async function createSellerAndOfferWithCondition(
coreSDK: CoreSDK,
Expand Down
Loading

0 comments on commit 79a8b5b

Please sign in to comment.