Skip to content

Commit

Permalink
refactor: address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
0xlucian committed Dec 3, 2024
1 parent d9e78dc commit 1c09ace
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
6 changes: 3 additions & 3 deletions contracts/domain/BosonErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ interface BosonErrors {
// Price does not cover the cancellation penalty
error PriceDoesNotCoverPenalty();

//Fee Table related
// Exchange token should be different than $BOSON when requesting feePercentage
error InvalidExchangeToken();
// Fee Table related
// Thrown if asset is not supported in feeTable feature.
error FeeTableAssetNotSupported();
}
13 changes: 12 additions & 1 deletion contracts/interfaces/handlers/IBosonConfigHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IBosonConfigEvents } from "../events/IBosonConfigEvents.sol";
*
* @notice Handles management of configuration within the protocol.
*
* The ERC-165 identifier for this interface is: 0x13fd085d
* The ERC-165 identifier for this interface is: 0xe9aa49b6
*/
interface IBosonConfigHandler is IBosonConfigEvents, BosonErrors {
/**
Expand Down Expand Up @@ -149,6 +149,17 @@ interface IBosonConfigHandler is IBosonConfigEvents, BosonErrors {
uint256[] calldata _feePercentages
) external;

/**
* @notice Gets the current fee table for a given token.
*
* @param _tokenAddress - the address of the token
* @return priceRanges - array of token price ranges
* @return feePercentages - array of fee percentages corresponding to each price range
*/
function getProtocolFeeTable(
address _tokenAddress
) external view returns (uint256[] memory priceRanges, uint256[] memory feePercentages);

/**
* @notice Gets the default protocol fee percentage.
*
Expand Down
4 changes: 2 additions & 2 deletions contracts/protocol/bases/ProtocolBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ abstract contract ProtocolBase is PausableBase, ReentrancyGuardBase, BosonErrors
* @return feePercentage - the protocol fee percentage based on token price (using protocol fee table)
*/
function _getFeePercentage(address _exchangeToken, uint256 _price) internal view returns (uint256 feePercentage) {
if (_exchangeToken == protocolAddresses().token) revert InvalidExchangeToken();
if (_exchangeToken == protocolAddresses().token) revert FeeTableAssetNotSupported();

ProtocolLib.ProtocolFees storage fees = protocolFees();
uint256[] storage priceRanges = fees.tokenPriceRanges[_exchangeToken];
Expand All @@ -721,7 +721,7 @@ abstract contract ProtocolBase is PausableBase, ReentrancyGuardBase, BosonErrors
// If the token has a custom fee table, find the appropriate percentage
uint256 priceRangesLength = priceRanges.length;
if (priceRangesLength > 0) {
for (uint256 i; i < priceRangesLength; ++i) {
for (uint256 i; i < priceRangesLength - 1; ++i) {
if (_price <= priceRanges[i]) {
// Return the fee percentage for the matching price range
return feePercentages[i];
Expand Down
22 changes: 21 additions & 1 deletion contracts/protocol/facets/ConfigHandlerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ contract ConfigHandlerFacet is IBosonConfigHandler, ProtocolBase {
* @notice Sets the feeTable for a specific token given price ranges and fee tiers for
* the corresponding price ranges.
*
* Reverts if token is $BOSON.
* Reverts if the number of fee percentages does not match the number of price ranges.
* Reverts if the price ranges are not in ascending order.
* Reverts if any of the fee percentages value is above 100%.
Expand All @@ -248,6 +249,7 @@ contract ConfigHandlerFacet is IBosonConfigHandler, ProtocolBase {
uint256[] calldata _priceRanges,
uint256[] calldata _feePercentages
) external override onlyRole(ADMIN) nonReentrant {
if (_tokenAddress == protocolAddresses().token) revert FeeTableAssetNotSupported();
if (_priceRanges.length != _feePercentages.length) revert ArrayLengthMismatch();
// Clear existing price ranges and percentage tiers
delete protocolFees().tokenPriceRanges[_tokenAddress];
Expand All @@ -260,6 +262,24 @@ contract ConfigHandlerFacet is IBosonConfigHandler, ProtocolBase {
emit FeeTableUpdated(_tokenAddress, _priceRanges, _feePercentages, msgSender());
}

/**
* @notice Gets the current fee table for a given token.
*
* @dev This funciton is used to check price ranges config. If you need to apply percentage based on
* _exchangeToken and offerPrice, use getProtocolFeePercentage(address,uint256)
*
* @param _tokenAddress - the address of the token
* @return priceRanges - array of token price ranges
* @return feePercentages - array of fee percentages corresponding to each price range
*/
function getProtocolFeeTable(
address _tokenAddress
) external view returns (uint256[] memory priceRanges, uint256[] memory feePercentages) {
ProtocolLib.ProtocolFees storage fees = protocolFees();
priceRanges = fees.tokenPriceRanges[_tokenAddress];
feePercentages = fees.tokenFeePercentages[_tokenAddress];
}

/**
* @notice Gets the default protocol fee percentage.
*
Expand Down Expand Up @@ -638,7 +658,7 @@ contract ConfigHandlerFacet is IBosonConfigHandler, ProtocolBase {
*/
function setTokenPriceRanges(address _tokenAddress, uint256[] calldata _priceRanges) internal {
for (uint256 i = 1; i < _priceRanges.length; ++i) {
if (_priceRanges[i] < _priceRanges[i - 1]) revert NonAscendingOrder();
if (_priceRanges[i] <= _priceRanges[i - 1]) revert NonAscendingOrder();
}
protocolFees().tokenPriceRanges[_tokenAddress] = _priceRanges;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/config/revert-reasons.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,5 @@ exports.RevertReasons = {
PRICE_DOES_NOT_COVER_PENALTY: "PriceDoesNotCoverPenalty",

//Fee Table related
INVALID_EXCHANGE_TOKEN: "InvalidExchangeToken",
FEE_TABLE_ASSET_NOT_SUPOPRTED: "FeeTableAssetNotSupported",
};
27 changes: 26 additions & 1 deletion test/protocol/ConfigHandlerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,13 @@ describe("IBosonConfigHandler", function () {
configHandler.connect(rando).setProtocolFeeTable(usdcAddress, feePriceRanges, feePercentages)
).to.revertedWithCustomError(bosonErrors, RevertReasons.ACCESS_DENIED);
});
it("should revert if _tokenAddress is the BOSON token", async function () {
await expect(
configHandler
.connect(deployer)
.setProtocolFeeTable(await token.getAddress(), feePriceRanges, feePercentages)
).to.be.revertedWithCustomError(bosonErrors, RevertReasons.FEE_TABLE_ASSET_NOT_SUPOPRTED);
});
it("price ranges count different from fee percents", async function () {
const newPriceRanges = [...feePriceRanges, parseUnits("10", "ether").toString()];
await expect(
Expand Down Expand Up @@ -1042,7 +1049,7 @@ describe("IBosonConfigHandler", function () {
const randomPrice = 10000;
await expect(
configHandler.connect(rando).getProtocolFeePercentage(await token.getAddress(), randomPrice)
).to.revertedWithCustomError(bosonErrors, RevertReasons.INVALID_EXCHANGE_TOKEN);
).to.revertedWithCustomError(bosonErrors, RevertReasons.FEE_TABLE_ASSET_NOT_SUPOPRTED);
});
});
});
Expand Down Expand Up @@ -1125,6 +1132,24 @@ describe("IBosonConfigHandler", function () {
minDisputePeriod,
"Invalid min dispute period"
);
it("Should return the correct fee table for a token", async function () {
const feePriceRanges = [
parseUnits("1", "ether").toString(),
parseUnits("2", "ether").toString(),
parseUnits("5", "ether").toString(),
];
const feePercentages = [500, 1000, 2000]; // 5%, 10%, 20%

await configHandler.connect(deployer).setProtocolFeeTable(usdc.address, feePriceRanges, feePercentages);

const [retrievedRanges, retrievedPercentages] = await configHandler.getProtocolFeeTable(usdc.address);

expect(retrievedRanges.map((r) => r.toString())).to.deep.equal(feePriceRanges, "Incorrect price ranges");
expect(retrievedPercentages.map((p) => p.toNumber())).to.deep.equal(
feePercentages,
"Incorrect fee percentages"
);
});
});
});
});
Expand Down

0 comments on commit 1c09ace

Please sign in to comment.