Skip to content

Commit

Permalink
Add generic tests for vouchers
Browse files Browse the repository at this point in the history
  • Loading branch information
anajuliabit committed Aug 7, 2023
1 parent 7b31d24 commit 9b649ef
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 48 deletions.
54 changes: 48 additions & 6 deletions test/upgrade/2.2.1-2.3.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ const {
} = require("../util/utils");
const { limits: protocolLimits } = require("../../scripts/config/protocol-parameters.js");

const { deploySuite, populateProtocolContract, getProtocolContractState, revertState } = require("../util/upgrade");
const {
deploySuite,
populateProtocolContract,
getProtocolContractState,
revertState,
getStorageLayout,
getVoucherContractState,
populateVoucherContract,
} = require("../util/upgrade");
const { deployMockTokens } = require("../../scripts/util/deploy-mock-tokens");
const { getGenericContext } = require("./01_generic");
const { getGenericContext: getGenericContextVoucher } = require("./clients/01_generic");
const { oneWeek, oneMonth, VOUCHER_NAME, VOUCHER_SYMBOL } = require("../util/constants");

const version = "2.3.0";
Expand Down Expand Up @@ -119,6 +128,18 @@ describe("[@skip-on-coverage] After facet upgrade, everything is still operation
true
);

const preUpgradeStorageLayout = await getStorageLayout("BosonVoucher");
const preUpgradeEntitiesVoucher = await populateVoucherContract(
deployer,
protocolDiamondAddress,
contractsBefore,
mockContracts,
undefined,
true
);

const voucherContractState = await getVoucherContractState(preUpgradeEntitiesVoucher);

({ bundleHandler, exchangeHandler, twinHandler, disputeHandler } = contractsBefore);

let getFunctionHashesClosure = getStateModifyingFunctionsHashes(
Expand Down Expand Up @@ -293,6 +314,21 @@ describe("[@skip-on-coverage] After facet upgrade, everything is still operation
includeTests
)
);

context(
"Generic tests on Voucher",
getGenericContextVoucher(
deployer,
protocolDiamondAddress,
contractsAfter,
mockContracts,
voucherContractState,
preUpgradeEntitiesVoucher,
preUpgradeStorageLayout,
snapshot
)
);

// } catch (err) {
// // revert to latest version of scripts and contracts
// revertState();
Expand Down Expand Up @@ -883,13 +919,17 @@ describe("[@skip-on-coverage] After facet upgrade, everything is still operation
);
});

it("old seller can create a new collection", async function () {
it.only("old seller can create a new collection", async function () {

Check failure on line 922 in test/upgrade/2.2.1-2.3.0.js

View workflow job for this annotation

GitHub Actions / setup

it.only not permitted
const { sellers } = preUpgradeEntities;
const { wallet: sellerWallet, id: sellerId, voucherInitValues, seller } = sellers[0];
const {
wallet: sellerWallet,
id: sellerId,
voucherInitValues,
seller,
voucherContractAddress: expectedDefaultAddress,
} = sellers[0];
const externalId = "new-collection";

const expectedDefaultAddress = calculateContractAddress(await accountHandler.getAddress(), "1");

const expectedCollectionAddress = calculateCloneAddress(
await accountHandler.getAddress(),
beaconProxyAddress,
Expand Down Expand Up @@ -1330,7 +1370,7 @@ describe("[@skip-on-coverage] After facet upgrade, everything is still operation

it("commit exactly at offer expiration timestamp", async function () {
const { offers, buyers } = preUpgradeEntities;
const { offer } = offers[1]; //offer 0 has a condition
const { offer, offerDates } = offers[1]; //offer 0 has a condition
const { wallet: buyer } = buyers[0];
const { mockToken } = mockContracts;

Expand All @@ -1339,6 +1379,8 @@ describe("[@skip-on-coverage] After facet upgrade, everything is still operation
// allow the protocol to transfer the buyer's tokens
await mockToken.connect(buyer).approve(protocolDiamondAddress, offer.price);

await setNextBlockTimestamp(Number(offerDates.validUntil));

// Commit to offer, retrieving the event
await expect(exchangeHandler.connect(buyer).commitToOffer(buyer.address, offer.id)).to.emit(
exchangeHandler,
Expand Down
2 changes: 1 addition & 1 deletion test/upgrade/clients/BosonVoucher-2.1.0-2.2.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("[@skip-on-coverage] After client upgrade, everything is still operatio
protocolContracts,
mockContracts,
undefined, // no existing entities
oldVersion
true
);
voucherContractState = await getVoucherContractState(preUpgradeEntities);

Expand Down
61 changes: 20 additions & 41 deletions test/util/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ async function populateProtocolContract(
];

let nextAccountId = Number(await accountHandler.getNextAccountId());
let voucherIndex = 1;

for (const entity of entities) {
const wallet = Wallet.createRandom();
Expand Down Expand Up @@ -423,20 +422,10 @@ async function populateProtocolContract(
}
// set unique new voucherInitValues
const voucherInitValues = new VoucherInitValues(`http://seller${id}.com/uri`, id * 10);
await accountHandler.connect(connectedWallet).createSeller(seller, authToken, voucherInitValues);
const tx = await accountHandler.connect(connectedWallet).createSeller(seller, authToken, voucherInitValues);

let voucherContractAddress;
if (versionsBelowV2_3.includes(isBefore ? versionTags.oldVersion : versionTags.newVersion)) {
voucherContractAddress = calculateContractAddress(await accountHandler.getAddress(), voucherIndex++);
} else {
const beaconProxyAddress = await calculateBosonProxyAddress(await accountHandler.getAddress());
voucherContractAddress = calculateCloneAddress(
await accountHandler.getAddress(),
beaconProxyAddress,
seller.admin,
""
);
}
const receipt = await tx.wait();
const [, , voucherContractAddress] = receipt.logs.find((e) => e.fragment.name === "SellerCreated").args;

sellers.push({
wallet: connectedWallet,
Expand Down Expand Up @@ -756,7 +745,7 @@ async function getProtocolContractState(
getOfferContractState(offerHandler, offers),
getExchangeContractState(exchangeHandler, exchanges),
getBundleContractState(bundleHandler, bundles),
configHandler ? getConfigContractState(configHandler, isBefore) : Promise.resolve({}),
getConfigContractState(configHandler, isBefore),
getDisputeContractState(disputeHandler, exchanges),
getFundsContractState(fundsHandler, { DRs, sellers, buyers, agents }, isBefore),
getGroupContractState(groupHandler, groups),
Expand Down Expand Up @@ -1521,9 +1510,8 @@ async function getProtocolLookupsPrivateContractState(
// BosonTypes.Seller has 7 fields, but `address payable treasury` and `bool active` are packed into one slot
structFields.push(await getStorageAt(protocolDiamondAddress, structStorageSlot + i));
}
// ToDo: make sure this gets the corrects slots
const metadataUriLength = BigInt(await getStorageAt(protocolDiamondAddress, structStorageSlot + 6n));
const metadataUriSlot = BigInt(keccak256(ethersId(structStorageSlot + 6n)));
const metadataUriSlot = BigInt(ethersId(structStorageSlot + 6n));
const occupiedSlots = metadataUriLength / 32n + 1n;
const metadataUri = [];
for (let i = 0n; i < occupiedSlots; i++) {
Expand Down Expand Up @@ -1637,8 +1625,6 @@ async function populateVoucherContract(
let bosonVouchers = [];
let exchanges = [];

let voucherIndex = 1;

if (existingEntities) {
// If existing entities are provided, we use them instead of creating new ones
({ DR, sellers, buyers, offers, bosonVouchers } = existingEntities);
Expand Down Expand Up @@ -1679,10 +1665,14 @@ async function populateVoucherContract(
// create entities
switch (entity) {
case entityType.DR: {
const clerkAddress = versionsBelowV2_3.includes(isBefore ? versionTags.oldVersion : versionTags.newVersion)
? wallet.address
: ZeroAddress;

const disputeResolver = mockDisputeResolver(
await wallet.getAddress(),
await wallet.getAddress(),
await wallet.getAddress(),
clerkAddress,
await wallet.getAddress(),
true,
true
Expand All @@ -1698,6 +1688,7 @@ async function populateVoucherContract(
await accountHandler
.connect(connectedWallet)
.createDisputeResolver(disputeResolver, disputeResolverFees, sellerAllowList);

DR = {
wallet: connectedWallet,
id: disputeResolver.id,
Expand Down Expand Up @@ -1729,22 +1720,9 @@ async function populateVoucherContract(

// set unique new voucherInitValues
const voucherInitValues = new VoucherInitValues(`http://seller${id}.com/uri`, id * 10);
await accountHandler.connect(connectedWallet).createSeller(seller, authToken, voucherInitValues);

// calculate voucher contract address and cast it to contract instance
let voucherContractAddress;
if (versionsBelowV2_3.includes(isBefore ? versionTags.oldVersion : versionTags.newVersion)) {
voucherContractAddress = calculateContractAddress(await accountHandler.getAddress(), voucherIndex++);
} else {
const beaconProxyAddress = await calculateBosonProxyAddress(await accountHandler.getAddress());
voucherContractAddress = calculateCloneAddress(
await accountHandler.getAddress(),
beaconProxyAddress,
seller.admin,
""
);
}

const tx = await accountHandler.connect(connectedWallet).createSeller(seller, authToken, voucherInitValues);
const receipt = await tx.wait();
const [, , voucherContractAddress] = receipt.logs.find((e) => e.fragment.name === "SellerCreated").args;
const bosonVoucher = await getContractAt("BosonVoucher", voucherContractAddress);

sellers.push({
Expand Down Expand Up @@ -1802,9 +1780,9 @@ async function populateVoucherContract(
offerDates.validUntil = (BigInt(now) + oneMonth * 6n * BigInt(offerId + 1)).toString();

// Set unique offerDurations based on offer id
offerDurations.disputePeriod = `${(offerId + 1) * oneMonth}`;
offerDurations.voucherValid = `${(offerId + 1) * oneMonth}`;
offerDurations.resolutionPeriod = `${(offerId + 1) * oneDay}`;
offerDurations.disputePeriod = `${(offerId + 1) * Number(oneMonth)}`;
offerDurations.voucherValid = `${(offerId + 1) * Number(oneMonth)}`;
offerDurations.resolutionPeriod = `${(offerId + 1) * Number(oneDay)}`;

// choose one DR and agent
const disputeResolverId = DR.disputeResolver.id;
Expand Down Expand Up @@ -1862,7 +1840,8 @@ async function getVoucherContractState({ bosonVouchers, exchanges, sellers, buye
for (const bosonVoucher of bosonVouchers) {
// supports interface
const interfaceIds = await getInterfaceIds(false);
const suppportstInterface = await Promise.all(

const supportstInterface = await Promise.all(
[interfaceIds["IBosonVoucher"], interfaceIds["IERC721"], interfaceIds["IERC2981"]].map((i) =>
bosonVoucher.supportsInterface(i)
)
Expand Down Expand Up @@ -1904,7 +1883,7 @@ async function getVoucherContractState({ bosonVouchers, exchanges, sellers, buye
);

bosonVouchersState.push({
suppportstInterface,
supportstInterface,
sellerId,
contractURI,
getRoyaltyPercentage,
Expand Down

0 comments on commit 9b649ef

Please sign in to comment.