From 9b649ef5df43771c83b4c0610d13965a8fdde611 Mon Sep 17 00:00:00 2001 From: Ana Julia Date: Thu, 3 Aug 2023 22:25:38 -0300 Subject: [PATCH] Add generic tests for vouchers --- test/upgrade/2.2.1-2.3.0.js | 54 ++++++++++++++-- .../clients/BosonVoucher-2.1.0-2.2.0.js | 2 +- test/util/upgrade.js | 61 ++++++------------- 3 files changed, 69 insertions(+), 48 deletions(-) diff --git a/test/upgrade/2.2.1-2.3.0.js b/test/upgrade/2.2.1-2.3.0.js index 48d9fae68..0f4759fb5 100644 --- a/test/upgrade/2.2.1-2.3.0.js +++ b/test/upgrade/2.2.1-2.3.0.js @@ -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"; @@ -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( @@ -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(); @@ -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 () { 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, @@ -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; @@ -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, diff --git a/test/upgrade/clients/BosonVoucher-2.1.0-2.2.0.js b/test/upgrade/clients/BosonVoucher-2.1.0-2.2.0.js index cc4aad357..4fda393ac 100644 --- a/test/upgrade/clients/BosonVoucher-2.1.0-2.2.0.js +++ b/test/upgrade/clients/BosonVoucher-2.1.0-2.2.0.js @@ -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); diff --git a/test/util/upgrade.js b/test/util/upgrade.js index d2da5aae0..067bb80e0 100644 --- a/test/util/upgrade.js +++ b/test/util/upgrade.js @@ -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(); @@ -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, @@ -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), @@ -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++) { @@ -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); @@ -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 @@ -1698,6 +1688,7 @@ async function populateVoucherContract( await accountHandler .connect(connectedWallet) .createDisputeResolver(disputeResolver, disputeResolverFees, sellerAllowList); + DR = { wallet: connectedWallet, id: disputeResolver.id, @@ -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({ @@ -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; @@ -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) ) @@ -1904,7 +1883,7 @@ async function getVoucherContractState({ bosonVouchers, exchanges, sellers, buye ); bosonVouchersState.push({ - suppportstInterface, + supportstInterface, sellerId, contractURI, getRoyaltyPercentage,