Skip to content

Commit

Permalink
Refactor until GroupHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
anajuliabit committed Jun 20, 2023
1 parent 9288716 commit 7d1d6d3
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 101 deletions.
2 changes: 1 addition & 1 deletion scripts/domain/Condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Condition {

return Condition.fromObject({
method: parseInt(method),
tokenType,
tokenType: Number(tokenType),
tokenAddress,
tokenId: tokenId.toString(),
threshold: threshold.toString(),
Expand Down
2 changes: 1 addition & 1 deletion scripts/domain/Exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Exchange {
offerId: offerId.toString(),
buyerId: buyerId.toString(),
finalizedDate: finalizedDate.toString(),
state,
state: Number(state),
});
}

Expand Down
5 changes: 3 additions & 2 deletions scripts/domain/Receipt.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const hre = require("hardhat");
const { ZeroAddress} = hre.ethers;
const { ZeroAddress } = hre.ethers;
const DisputeState = require("./DisputeState");
const OfferFees = require("./OfferFees.js");
const TwinReceipt = require("./TwinReceipt.js");
Expand Down Expand Up @@ -121,6 +121,7 @@ class Receipt {
twinReceipts,
] = struct;

console.log(twinReceipts);
return Receipt.fromObject({
exchangeId: exchangeId.toString(),
offerId: offerId.toString(),
Expand All @@ -140,7 +141,7 @@ class Receipt {
disputeResolverId: disputeResolverId.toString(),
disputedDate: disputedDate.toString(),
escalatedDate: escalatedDate.toString(),
disputeState,
disputeState: Number(disputeState),
twinReceipts: twinReceipts.map((twinReceipt) => TwinReceipt.fromStruct(twinReceipt)),
});
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/domain/TwinReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TwinReceipt {
tokenId: tokenId.toString(),
amount: amount.toString(),
tokenAddress: tokenAddress,
tokenType,
tokenType: Number(tokenType),
});
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/util/estimate-limits.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ setupEnvironment["maxPremintedVouchers"] = async function (tokenCount = 10) {

// create the offer
const { offer, offerDates, offerDurations } = await mockOffer();
offer.quantityAvailable = constants.MaxUint256;
offer.quantityAvailable = MaxUint256;
await offerHandler.connect(sellerWallet1).createOffer(offer, offerDates, offerDurations, disputeResolver.id, agentId);

// reserve range
Expand Down
2 changes: 1 addition & 1 deletion test/protocol/DisputeHandlerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ describe("IBosonDisputeHandler", function () {
await expect(
disputeHandler
.connect(assistant)
.resolveDispute(exchangeId, buyerPercentBasisPoints, r, constants.MaxUint256, v)
.resolveDispute(exchangeId, buyerPercentBasisPoints, r, MaxUint256, v)
).to.revertedWith(RevertReasons.INVALID_SIGNATURE);
});

Expand Down
34 changes: 17 additions & 17 deletions test/protocol/ExchangeHandlerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
MaxUint256,
parseEther,
getImpersonatedSigner,
toBeHex,
} = ethers;
const { expect, assert } = require("chai");

Expand Down Expand Up @@ -186,7 +187,7 @@ describe("IBosonExchangeHandler", function () {
// Deploy MockMetaTransactionsHandlerFacet
const MockMetaTransactionsHandlerFacet = await getContractFactory("MockMetaTransactionsHandlerFacet");
const mockMetaTransactionsHandlerFacet = await MockMetaTransactionsHandlerFacet.deploy();
await mockMetaTransactionsHandlerFacet.deployed();
await mockMetaTransactionsHandlerFacet.waitForDeployment();

// Define the facet cut
const facetCuts = [
Expand Down Expand Up @@ -319,7 +320,7 @@ describe("IBosonExchangeHandler", function () {
});

context("👉 commitToOffer()", async function () {
it.only("should emit a BuyerCommitted event", async function () {
it("should emit a BuyerCommitted event", async function () {
// Commit to offer, retrieving the event
tx = await exchangeHandler.connect(buyer).commitToOffer(await buyer.getAddress(), offerId, { value: price });
txReceipt = await tx.wait();
Expand Down Expand Up @@ -730,10 +731,9 @@ describe("IBosonExchangeHandler", function () {
// Create an offer with staring date in the future
// get current block timestamp
const block = await provider.getBlock("latest");
const now = block.timestamp.toString();

// set validFrom date in the past
offerDates.validFrom = BigInt(now) + oneMonth * (6).toString(); // 6 months in the future
offerDates.validFrom = (BigInt(block.timestamp) + oneMonth * 6n).toString(); // 6 months in the future
offerDates.validUntil = BigInt(offerDates.validFrom + 10).toString(); // just after the valid from so it succeeds.

await offerHandler
Expand Down Expand Up @@ -966,7 +966,7 @@ describe("IBosonExchangeHandler", function () {
const impersonatedBosonVoucher = await getImpersonatedSigner(await bosonVoucher.getAddress());
await provider.send("hardhat_setBalance", [
await impersonatedBosonVoucher.getAddress(),
parseEther("10").toHexString(),
toBeHex(parseEther("10")),
]);

// Simulate a second commit with the same token id
Expand Down Expand Up @@ -1412,7 +1412,7 @@ describe("IBosonExchangeHandler", function () {
block = await provider.getBlock(blockNumber);

// Set time forward to run out the dispute period
newTime = BigInt(block.timestamp + disputePeriod + 1);
newTime = BigInt(block.timestamp + disputePeriod + 1).toString();
await setNextBlockTimestamp(newTime);

// Complete exchange
Expand All @@ -1433,7 +1433,7 @@ describe("IBosonExchangeHandler", function () {
block = await provider.getBlock(blockNumber);

// Set time forward to run out the dispute period
newTime = BigInt(block.timestamp + disputePeriod + 1);
newTime = BigInt(block.timestamp + disputePeriod + 1).toString();
await setNextBlockTimestamp(newTime);

// Complete exchange
Expand All @@ -1454,7 +1454,7 @@ describe("IBosonExchangeHandler", function () {
block = await provider.getBlock(blockNumber);

// Set time forward to run out the dispute period
newTime = BigInt(block.timestamp + disputePeriod + 1);
newTime = BigInt(block.timestamp + disputePeriod + 1).toString();
await setNextBlockTimestamp(newTime);

// Create a rando buyer account
Expand Down Expand Up @@ -1613,7 +1613,7 @@ describe("IBosonExchangeHandler", function () {
block = await provider.getBlock(blockNumber);

// Set time forward to run out the dispute period
newTime = BigInt(block.timestamp + disputePeriod + 1);
newTime = BigInt(block.timestamp + disputePeriod + 1).toString();
await setNextBlockTimestamp(newTime);

// Complete exchange
Expand Down Expand Up @@ -1645,7 +1645,7 @@ describe("IBosonExchangeHandler", function () {
block = await provider.getBlock(blockNumber);

// Set time forward to run out the dispute period
newTime = BigInt(block.timestamp + disputePeriod + 1);
newTime = BigInt(block.timestamp + disputePeriod + 1).toString();
await setNextBlockTimestamp(newTime);

// Complete exchange
Expand Down Expand Up @@ -2372,7 +2372,7 @@ describe("IBosonExchangeHandler", function () {
// Deploy contract to test redeem called by another contract
let TestProtocolFunctionsFactory = await getContractFactory("TestProtocolFunctions");
const testProtocolFunctions = await TestProtocolFunctionsFactory.deploy(protocolDiamondAddress);
await testProtocolFunctions.deployed();
await testProtocolFunctions.waitForDeployment();

await testProtocolFunctions.commit(offerId, { value: price });

Expand Down Expand Up @@ -2623,7 +2623,7 @@ describe("IBosonExchangeHandler", function () {
// Deploy contract to test redeem called by another contract
let TestProtocolFunctionsFactory = await getContractFactory("TestProtocolFunctions");
const testProtocolFunctions = await TestProtocolFunctionsFactory.deploy(protocolDiamondAddress);
await testProtocolFunctions.deployed();
await testProtocolFunctions.waitForDeployment();

await testProtocolFunctions.commit(offerId, { value: price });

Expand Down Expand Up @@ -2816,7 +2816,7 @@ describe("IBosonExchangeHandler", function () {
// Deploy contract to test redeem called by another contract
let TestProtocolFunctionsFactory = await getContractFactory("TestProtocolFunctions");
const testProtocolFunctions = await TestProtocolFunctionsFactory.deploy(protocolDiamondAddress);
await testProtocolFunctions.deployed();
await testProtocolFunctions.waitForDeployment();

await testProtocolFunctions.commit(offerId, { value: price });

Expand Down Expand Up @@ -3186,7 +3186,7 @@ describe("IBosonExchangeHandler", function () {
// Deploy contract to test redeem called by another contract
let TestProtocolFunctionsFactory = await getContractFactory("TestProtocolFunctions");
const testProtocolFunctions = await TestProtocolFunctionsFactory.deploy(protocolDiamondAddress);
await testProtocolFunctions.deployed();
await testProtocolFunctions.waitForDeployment();

await testProtocolFunctions.commit(offerId, { value: price });

Expand Down Expand Up @@ -3319,7 +3319,7 @@ describe("IBosonExchangeHandler", function () {

it("new date is not later than the current one", async function () {
// New expiry date is older than current
validUntilDate = BigInt(voucher.validUntilDate) - oneMonth.toString();
validUntilDate = BigInt(voucher.validUntilDate) - oneMonth;

// Attempt to extend voucher, expecting revert
await expect(exchangeHandler.connect(assistant).extendVoucher(exchange.id, validUntilDate)).to.revertedWith(
Expand Down Expand Up @@ -3577,7 +3577,7 @@ describe("IBosonExchangeHandler", function () {
block = await provider.getBlock(blockNumber);

// Set time forward to run out the dispute period
newTime = BigInt(voucherRedeemableFrom + disputePeriod + 1);
newTime = Number(voucherRedeemableFrom + disputePeriod + 1);
await setNextBlockTimestamp(newTime);

// Complete exchange
Expand Down Expand Up @@ -4498,7 +4498,7 @@ describe("IBosonExchangeHandler", function () {
await accountHandler.connect(rando).createAgent(agent);

// Update agentFee
const agentFee = (BigInt(offer.price) * agent.feePercentage) / "10000".toString();
const agentFee = ((BigInt(offer.price) * BigInt(agent.feePercentage)) / 10000n).toString();
offerFees.agentFee = agentFee;

// Create a new offer
Expand Down
28 changes: 22 additions & 6 deletions test/protocol/GroupHandlerTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { ethers } = require("hardhat");
const { ZeroAddress, getSigners, parseUnits, getContractFactory } = ethers;
const { assert, expect } = require("chai");

const Group = require("../../scripts/domain/Group");
Expand Down Expand Up @@ -103,7 +104,12 @@ describe("IBosonGroupHandler", function () {
agentId = "0"; // agent id is optional while creating an offer

// Create a valid seller, then set fields in tests directly
seller = mockSeller(await assistant.getAddress(), await admin.getAddress(), await clerk.getAddress(), await treasury.getAddress());
seller = mockSeller(
await assistant.getAddress(),
await admin.getAddress(),
clerk.address,
await treasury.getAddress()
);
expect(seller.isValid()).is.true;

// VoucherInitValues
Expand All @@ -120,7 +126,7 @@ describe("IBosonGroupHandler", function () {
disputeResolver = mockDisputeResolver(
await assistantDR.getAddress(),
await adminDR.getAddress(),
await clerkDR.getAddress(),
clerkDR.address,
await treasuryDR.getAddress(),
true
);
Expand Down Expand Up @@ -151,8 +157,8 @@ describe("IBosonGroupHandler", function () {
offer.sellerDeposit = parseUnits(`${0.25 + i * 0.1}`, "ether").toString();
offer.buyerCancelPenalty = parseUnits(`${0.05 + i * 0.1}`, "ether").toString();
offer.quantityAvailable = `${(i + 1) * 2}`;
offerDates.validFrom = BigInt(Date.now() + oneMonth * i).toString();
offerDates.validUntil = BigInt(Date.now() + oneMonth * 6 * (i + 1)).toString();
offerDates.validFrom = (BigInt(Date.now()) + oneMonth * BigInt(i)).toString();
offerDates.validUntil = (BigInt(Date.now()) + oneMonth * 6n * BigInt(i + 1)).toString();

// Check if domains are valid
expect(offer.isValid()).is.true;
Expand Down Expand Up @@ -304,7 +310,12 @@ describe("IBosonGroupHandler", function () {

it("Caller is not the seller of all offers", async function () {
// create another seller and an offer
seller = mockSeller(await rando.getAddress(), await rando.getAddress(), ZeroAddress, await rando.getAddress());
seller = mockSeller(
await rando.getAddress(),
await rando.getAddress(),
ZeroAddress,
await rando.getAddress()
);

await accountHandler.connect(rando).createSeller(seller, emptyAuthToken, voucherInitValues);
await offerHandler.connect(rando).createOffer(offer, offerDates, offerDurations, disputeResolverId, agentId); // creates an offer with id 6
Expand Down Expand Up @@ -572,7 +583,12 @@ describe("IBosonGroupHandler", function () {

it("Caller is not the seller of all offers", async function () {
// create another seller and an offer
seller = mockSeller(await rando.getAddress(), await rando.getAddress(), ZeroAddress, await rando.getAddress());
seller = mockSeller(
await rando.getAddress(),
await rando.getAddress(),
ZeroAddress,
await rando.getAddress()
);

await accountHandler.connect(rando).createSeller(seller, emptyAuthToken, voucherInitValues);
await offerHandler.connect(rando).createOffer(offer, offerDates, offerDurations, disputeResolverId, agentId); // creates an offer with id 6
Expand Down
Loading

0 comments on commit 7d1d6d3

Please sign in to comment.