Skip to content

Commit

Permalink
pr remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
albertfolch-redeemeum committed May 23, 2024
1 parent bc646d2 commit c164117
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 62 deletions.
27 changes: 14 additions & 13 deletions contracts/protocol/bases/OfferBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,24 @@ contract OfferBase is ProtocolBase, IBosonOfferEvents {

// condition for successful payout when exchange final state is canceled
if (_offer.buyerCancelPenalty > offerPrice) revert InvalidOfferPenalty();
if (_offer.priceType == PriceType.Static) {
// Calculate and set the protocol fee
uint256 protocolFee = getProtocolFee(_offer.exchangeToken, offerPrice);

// Calculate and set the protocol fee
uint256 protocolFee = getProtocolFee(_offer.exchangeToken, offerPrice);
// Calculate the agent fee amount
uint256 agentFeeAmount = (agent.feePercentage * offerPrice) / HUNDRED_PERCENT;

// Calculate the agent fee amount
uint256 agentFeeAmount = (agent.feePercentage * offerPrice) / HUNDRED_PERCENT;
uint256 totalOfferFeeLimit = (limits.maxTotalOfferFeePercentage * offerPrice) / HUNDRED_PERCENT;

uint256 totalOfferFeeLimit = (limits.maxTotalOfferFeePercentage * offerPrice) / HUNDRED_PERCENT;
// Sum of agent fee amount and protocol fee amount should be <= offer fee limit and less that fee limit set by seller
uint256 totalFeeAmount = agentFeeAmount + protocolFee;
if (totalFeeAmount > totalOfferFeeLimit) revert AgentFeeAmountTooHigh();
if (totalFeeAmount > _feeLimit) revert TotalFeeExceedsLimit();

// Sum of agent fee amount and protocol fee amount should be <= offer fee limit and less that fee limit set by seller
uint256 totalFeeAmount = agentFeeAmount + protocolFee;
if (totalFeeAmount > totalOfferFeeLimit) revert AgentFeeAmountTooHigh();
if (totalFeeAmount > _feeLimit) revert TotalFeeExceedsLimit();

// Set offer fees props individually since calldata structs can't be copied to storage
offerFees.protocolFee = protocolFee;
offerFees.agentFee = agentFeeAmount;
// Set offer fees props individually since calldata structs can't be copied to storage
offerFees.protocolFee = protocolFee;
offerFees.agentFee = agentFeeAmount;
}
}

// Store the agent id for the offer
Expand Down
1 change: 0 additions & 1 deletion contracts/protocol/bases/PriceDiscoveryBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ contract PriceDiscoveryBase is ProtocolBase {
if (actualPrice < _offer.buyerCancelPenalty) {
revert PriceDoesNotCoverPenalty();
}
return actualPrice;
}

/**
Expand Down
89 changes: 42 additions & 47 deletions test/protocol/FundsHandlerTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-only-tests/no-only-tests */
const { ethers } = require("hardhat");
const { ZeroAddress, getSigners, provider, parseUnits, getContractAt, getContractFactory, MaxUint256 } = ethers;
const { expect, assert } = require("chai");
Expand Down Expand Up @@ -75,7 +76,7 @@ describe("IBosonFundsHandler", function () {
let buyer, offerToken, offerNative, offerPriceDiscovery;
let mockToken, bosonToken;
let depositAmount;
let offerTokenProtocolFee, offerNativeProtocolFee, price, sellerDeposit;
let offerTokenProtocolFee, offerNativeProtocolFee, priceDiscoveryProtocolFee, price, sellerDeposit;
let offerDates, voucherRedeemableFrom;
let resolutionPeriod, offerDurations;
let protocolFeePercentage, buyerEscalationDepositPercentage;
Expand Down Expand Up @@ -1804,6 +1805,7 @@ describe("IBosonFundsHandler", function () {
// Funds library methods.
// Cannot be invoked directly, so tests calls the methods that use them
context("📋 FundsLib Methods", async function () {
let orderPrice;
beforeEach(async function () {
// Create a valid seller
seller = mockSeller(
Expand Down Expand Up @@ -1864,6 +1866,7 @@ describe("IBosonFundsHandler", function () {
offerPriceDiscovery.quantityAvailable = "2";
offerPriceDiscovery.priceType = PriceType.Discovery;
offerPriceDiscovery.exchangeToken = await mockToken.getAddress();
orderPrice = BigInt(offerPriceDiscovery.price) + 10000n;

offerDates = mo.offerDates;
expect(offerDates.isValid()).is.true;
Expand Down Expand Up @@ -1901,12 +1904,12 @@ describe("IBosonFundsHandler", function () {
resolutionPeriod = offerDurations.resolutionPeriod;

// top up seller's and buyer's account
await mockToken.mint(await assistant.getAddress(), `${2 * sellerDeposit}`);
await mockToken.mint(await buyer.getAddress(), `${2 * price}`);
await mockToken.mint(await assistant.getAddress(), `${20 * sellerDeposit}`);
await mockToken.mint(await buyer.getAddress(), `${20 * price}`);

// approve protocol to transfer the tokens
await mockToken.connect(assistant).approve(protocolDiamondAddress, `${2 * sellerDeposit}`);
await mockToken.connect(buyer).approve(protocolDiamondAddress, `${2 * price}`);
await mockToken.connect(assistant).approve(protocolDiamondAddress, `${20 * sellerDeposit}`);
await mockToken.connect(buyer).approve(protocolDiamondAddress, `${20 * price}`);

// deposit to seller's pool
await fundsHandler
Expand Down Expand Up @@ -5889,7 +5892,7 @@ describe("IBosonFundsHandler", function () {
});

context("👉 releaseFunds() - Price discovery", async function () {
let voucherCloneAddress;
let voucherCloneAddress, order;
beforeEach(async function () {
// ids
protocolId = "0";
Expand All @@ -5910,14 +5913,15 @@ describe("IBosonFundsHandler", function () {
await bosonVoucher.connect(assistant).setApprovalForAll(await priceDiscoveryContract.getAddress(), true);
const tokenId = deriveTokenId(offerPriceDiscovery.id, exchangeId);

const order = {
order = {
seller: assistant.address,
buyer: buyer.address,
voucherContract: voucherCloneAddress,
tokenId: tokenId,
exchangeToken: offerPriceDiscovery.exchangeToken,
price: BigInt(price),
price: orderPrice,
};
priceDiscoveryProtocolFee = applyPercentage(order.price, protocolFeePercentage);

const priceDiscoveryData = priceDiscoveryContract.interface.encodeFunctionData("fulfilBuyOrder", [order]);

Expand Down Expand Up @@ -5951,13 +5955,13 @@ describe("IBosonFundsHandler", function () {
buyerPayoff = 0;

// protocol: protocolFee
protocolPayoff = (2n * BigInt(offerTokenProtocolFee)).toString(); // regular protocolFee + protocolFee from releaseFundsToIntermediateSellers
protocolPayoff = priceDiscoveryProtocolFee.toString();

// seller: price - protocolFee
sellerPayoff = BigInt(order.price) - BigInt(priceDiscoveryFee);
sellerPayoff = BigInt(order.price) - BigInt(protocolPayoff);

// seller: sellerDeposit - protocolFee
sellerPayoff2 = BigInt(offerPriceDiscovery.sellerDeposit) - BigInt(offerTokenProtocolFee);
// seller: sellerDeposit
sellerPayoff2 = BigInt(offerPriceDiscovery.sellerDeposit);
});

it("should emit a FundsReleased event", async function () {
Expand All @@ -5984,13 +5988,13 @@ describe("IBosonFundsHandler", function () {
it("should update state", async function () {
const tokenId = deriveTokenId(offerPriceDiscovery.id, "2");

const order = {
order = {
seller: assistant.address,
buyer: buyer.address,
voucherContract: voucherCloneAddress,
tokenId: tokenId,
exchangeToken: offerPriceDiscovery.exchangeToken,
price: BigInt(price),
price: orderPrice,
};

const priceDiscoveryData = priceDiscoveryContract.interface.encodeFunctionData("fulfilBuyOrder", [order]);
Expand Down Expand Up @@ -6060,12 +6064,12 @@ describe("IBosonFundsHandler", function () {
expectedSellerAvailableFunds.funds[1] = new Funds(
await mockToken.getAddress(),
"Foreign20",
(BigInt(sellerPayoff) + BigInt(sellerPayoff2)) * 2n
(BigInt(sellerPayoff) + BigInt(sellerPayoff2)) * 2n // completed twice
);
expectedProtocolAvailableFunds.funds[0] = new Funds(
await mockToken.getAddress(),
"Foreign20",
BigInt(protocolPayoff) * 2n
BigInt(protocolPayoff) * 2n // completed twice
);
expect(sellersAvailableFunds).to.eql(expectedSellerAvailableFunds);
expect(buyerAvailableFunds).to.eql(expectedBuyerAvailableFunds);
Expand All @@ -6078,7 +6082,7 @@ describe("IBosonFundsHandler", function () {
beforeEach(async function () {
// expected payoffs
// buyer: sellerDeposit + price
buyerPayoff = BigInt(offerPriceDiscovery.sellerDeposit) + BigInt(order.price);
buyerPayoff = BigInt(offerPriceDiscovery.sellerDeposit) + BigInt(orderPrice);

// seller: 0
sellerPayoff = 0;
Expand Down Expand Up @@ -6149,7 +6153,7 @@ describe("IBosonFundsHandler", function () {
voucherContract: voucherCloneAddress,
tokenId: tokenId,
exchangeToken: offerPriceDiscovery.exchangeToken,
price: BigInt(price),
price: orderPrice,
};

const priceDiscoveryData = priceDiscoveryContract.interface.encodeFunctionData("fulfilBuyOrder", [order]);
Expand Down Expand Up @@ -6289,14 +6293,14 @@ describe("IBosonFundsHandler", function () {
// buyer: 0
buyerPayoff = 0;

// protocol: 0
protocolPayoff = BigInt(priceDiscoveryProtocolFee).toString();

// seller: price - protocolFee
sellerPayoff = BigInt(order.price) - BigInt(priceDiscoveryProtocolFee);
sellerPayoff = BigInt(order.price) - BigInt(protocolPayoff);

// seller: sellerDeposit - protocolFee
sellerPayoff2 = BigInt(offerPriceDiscovery.sellerDeposit);

// protocol: 0
protocolPayoff = (BigInt(offerTokenProtocolFee)).toString();
});
it("should emit a FundsReleased event", async function () {
// Retract from the dispute, expecting event
Expand Down Expand Up @@ -6391,15 +6395,15 @@ describe("IBosonFundsHandler", function () {
// buyer: 0
buyerPayoff = 0;

// protocol: 0
protocolPayoff = BigInt(priceDiscoveryProtocolFee).toString();

// seller: price - protocolFee
sellerPayoff = BigInt(order.price) - BigInt(priceDiscoveryProtocolFee);
sellerPayoff = BigInt(order.price) - BigInt(protocolPayoff);

// seller: sellerDeposit - protocolFee
// seller: sellerDeposit
sellerPayoff2 = BigInt(offerPriceDiscovery.sellerDeposit);

// protocol: 0
protocolPayoff = ( BigInt(offerTokenProtocolFee)).toString(); // regular protocolFee + protocolFee from releaseFundsToIntermediateSellers

await setNextBlockTimestamp(Number(timeout) + 1);
});

Expand Down Expand Up @@ -6485,18 +6489,15 @@ describe("IBosonFundsHandler", function () {
// expected payoffs
// buyer: (price + sellerDeposit)*buyerPercentage
buyerPayoff =
((BigInt(order.price) + BigInt(offerPriceDiscovery.sellerDeposit)) *
BigInt(buyerPercentBasisPoints)) /
((BigInt(order.price) + BigInt(offerPriceDiscovery.sellerDeposit)) * BigInt(buyerPercentBasisPoints)) /
10000n;

const sellerPercentBasisPoints = 10000n - BigInt(buyerPercentBasisPoints);

sellerPayoff =
(BigInt(offerPriceDiscovery.sellerDeposit) * (10000n - BigInt(buyerPercentBasisPoints))) / 10000n;

const sellerPricePart =
BigInt(order.price) -
(BigInt(order.price) * sellerPercentBasisPoints) / 10000n;
const sellerPricePart = BigInt(order.price) - (BigInt(order.price) * sellerPercentBasisPoints) / 10000n;
const sellerProtocolFeePart = (BigInt(priceDiscoveryProtocolFee) * sellerPercentBasisPoints) / 10000n;
sellerPayoff2 = BigInt(order.price) - sellerPricePart - sellerProtocolFeePart;

Expand Down Expand Up @@ -6627,12 +6628,12 @@ describe("IBosonFundsHandler", function () {
// buyer: 0
buyerPayoff = 0;

sellerPayoff = BigInt(order.price) - BigInt(priceDiscoveryProtocolFee);
// protocol: protocolFee
protocolPayoff = BigInt(priceDiscoveryProtocolFee).toString();

sellerPayoff2 = BigInt(offerPriceDiscovery.sellerDeposit);
sellerPayoff = BigInt(order.price) - BigInt(protocolPayoff);

// protocol: protocolFee
protocolPayoff = (BigInt(offerTokenProtocolFee)).toString(); // regular protocolFee + protocolFee from releaseFundsToIntermediateSellers
sellerPayoff2 = BigInt(offerPriceDiscovery.sellerDeposit);

// Escalate the dispute
await disputeHandler.connect(buyer).escalateDispute(exchangeId);
Expand Down Expand Up @@ -6734,18 +6735,15 @@ describe("IBosonFundsHandler", function () {
// expected payoffs
// buyer: (price + sellerDeposit + buyerEscalationDeposit)*buyerPercentage
buyerPayoff =
((BigInt(order.price) + BigInt(offerPriceDiscovery.sellerDeposit)) *
BigInt(buyerPercentBasisPoints)) /
((BigInt(order.price) + BigInt(offerPriceDiscovery.sellerDeposit)) * BigInt(buyerPercentBasisPoints)) /
10000n;

const sellerPercentBasisPoints = 10000n - BigInt(buyerPercentBasisPoints);

sellerPayoff =
(BigInt(offerPriceDiscovery.sellerDeposit) * (10000n - BigInt(buyerPercentBasisPoints))) / 10000n;

const sellerPricePart =
BigInt(order.price) -
(BigInt(order.price) * sellerPercentBasisPoints) / 10000n;
const sellerPricePart = BigInt(order.price) - (BigInt(order.price) * sellerPercentBasisPoints) / 10000n;
const sellerProtocolFeePart = (BigInt(priceDiscoveryProtocolFee) * sellerPercentBasisPoints) / 10000n;
sellerPayoff2 = BigInt(order.price) - sellerPricePart - sellerProtocolFeePart;

Expand Down Expand Up @@ -6873,20 +6871,17 @@ describe("IBosonFundsHandler", function () {
// expected payoffs
// buyer: (price + sellerDeposit + buyerEscalationDeposit)*buyerPercentage
buyerPayoff =
((BigInt(order.price) + BigInt(offerPriceDiscovery.sellerDeposit)) *
BigInt(buyerPercentBasisPoints)) /
((BigInt(orderPrice) + BigInt(offerPriceDiscovery.sellerDeposit)) * BigInt(buyerPercentBasisPoints)) /
10000n;

const sellerPercentBasisPoints = 10000n - BigInt(buyerPercentBasisPoints);

sellerPayoff =
(BigInt(offerPriceDiscovery.sellerDeposit) * (10000n - BigInt(buyerPercentBasisPoints))) / 10000n;

const sellerPricePart =
BigInt(order.price) -
(BigInt(order.price) * sellerPercentBasisPoints) / 10000n;
const sellerPricePart = BigInt(orderPrice) - (BigInt(orderPrice) * sellerPercentBasisPoints) / 10000n;
const sellerProtocolFeePart = (BigInt(priceDiscoveryProtocolFee) * sellerPercentBasisPoints) / 10000n;
sellerPayoff2 = BigInt(order.price) - sellerPricePart - sellerProtocolFeePart;
sellerPayoff2 = BigInt(orderPrice) - sellerPricePart - sellerProtocolFeePart;

protocolPayoff = sellerProtocolFeePart;

Expand Down
2 changes: 1 addition & 1 deletion test/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ const offerHandler = {
originalMethod
.apply(target, args)
.then((tx) => {
const lastArg = args.at(6);
const lastArg = args.at(-1);
if (
lastArg &&
typeof lastArg === "object" &&
Expand Down

0 comments on commit c164117

Please sign in to comment.