Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redemption fee constant as a computed property + fee parameters setters tests #25

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions src/FeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ contract FeeCalculator is IDepositFeeCalculator, IRedemptionFeeCalculator, Ownab

SD59x18 private redemptionFeeScale = sd(0.3 * 1e18);
SD59x18 private redemptionFeeShift = sd(0.1 * 1e18); //-log10(0+0.1)=1 -> 10^-1
SD59x18 private redemptionFeeConstant = redemptionFeeScale * (one + redemptionFeeShift).log10(); //0.0413926851582251=log10(1+0.1)

function redemptionFeeConstant() private view returns (SD59x18) {
return redemptionFeeScale * (one + redemptionFeeShift).log10(); //0.0413926851582251=log10(1+0.1)
}

SD59x18 private singleAssetRedemptionRelativeFee = sd(0.1 * 1e18);
SD59x18 private dustAssetRedemptionRelativeFee = sd(0.3 * 1e18);

Expand Down Expand Up @@ -86,18 +90,6 @@ contract FeeCalculator is IDepositFeeCalculator, IRedemptionFeeCalculator, Ownab
redemptionFeeShift = redemptionFeeShiftSD;
}

/// @notice Sets the redemption fee constant.
/// @dev Can only be called by the current owner.
/// @param _redemptionFeeConstant The new redemption fee shift.
function setRedemptionFeeConstant(int256 _redemptionFeeConstant) external onlyOwner {
SD59x18 redemptionFeeConstantSD = sd(_redemptionFeeConstant);
require(
redemptionFeeConstantSD >= zero && redemptionFeeConstantSD <= one,
"Redemption fee constant must be between 0 and 1"
);
redemptionFeeConstant = redemptionFeeConstantSD;
}

/// @notice Sets the single asset redemption relative fee.
/// @dev Can only be called by the current owner.
/// @param _singleAssetRedemptionRelativeFee The new single asset redemption relative fee.
Expand Down Expand Up @@ -313,7 +305,7 @@ contract FeeCalculator is IDepositFeeCalculator, IRedemptionFeeCalculator, Ownab
//redemption_fee = scale * (tb * log10(b+shift) - ta * log10(a+shift)) + constant*amount;
SD59x18 i_a = ta * (da + redemptionFeeShift).log10();
SD59x18 i_b = tb * (db + redemptionFeeShift).log10();
SD59x18 fee_float = redemptionFeeScale * (i_b - i_a) + redemptionFeeConstant * amount_float;
SD59x18 fee_float = redemptionFeeScale * (i_b - i_a) + redemptionFeeConstant() * amount_float;

/*
@dev
Expand Down
119 changes: 107 additions & 12 deletions test/FeeCalculator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -683,12 +683,6 @@ contract FeeCalculatorTest is Test {
feeCalculator.setRedemptionFeeShift(invalid);
}

function testSetRedemptionFeeConstantReverts() public {
int256 invalid = (1.6 * 1e18);
vm.expectRevert("Redemption fee constant must be between 0 and 1");
feeCalculator.setRedemptionFeeConstant(invalid);
}

function testSetSingleAssetRedemptionRelativeFeeReverts() public {
int256 invalid = (1.7 * 1e18);
vm.expectRevert("Single asset redemption relative fee must be between 0 and 1");
Expand Down Expand Up @@ -731,12 +725,6 @@ contract FeeCalculatorTest is Test {
feeCalculator.setRedemptionFeeShift(invalid);
}

function testSetRedemptionFeeConstantNegativeReverts() public {
int256 invalid = (-0.6 * 1e18);
vm.expectRevert("Redemption fee constant must be between 0 and 1");
feeCalculator.setRedemptionFeeConstant(invalid);
}

function testSetSingleAssetRedemptionRelativeFeeNegativeReverts() public {
int256 invalid = (-0.7 * 1e18);
vm.expectRevert("Single asset redemption relative fee must be between 0 and 1");
Expand All @@ -748,4 +736,111 @@ contract FeeCalculatorTest is Test {
vm.expectRevert("Dust asset redemption relative fee must be between 0 and 1");
feeCalculator.setDustAssetRedemptionRelativeFee(invalid);
}

function testSetDepositFeeScale() public {
// Arrange
// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 500 * 1e18);
feeCalculator.setDepositFeeScale(0.09 * 1e18);

// Act
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), 100 * 1e18);

// Assert
assertEq(fees[0], 9718378209069523938 / 2);
}

function testSetDepositFeeRatioScale() public {
// Arrange
// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 500 * 1e18);
feeCalculator.setDepositFeeRatioScale(0.2 * 1e18);

// Act
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), 100 * 1e18);

// Assert
assertEq(fees[0], 1299819671838098442);
}

function testSetSingleAssetDepositRelativeFee() public {
// Arrange
// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 1000 * 1e18);
feeCalculator.setSingleAssetDepositRelativeFee(0.67 * 1e18);

// Act
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), 100 * 1e18);

// Assert
assertEq(fees[0], 67 * 1e18);
}

function testSetRedemptionFeeScale() public {
// Arrange
// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 500 * 1e18);
feeCalculator.setRedemptionFeeScale(0.4 * 1e18);

// Act
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), 100e18);

// Assert
assertEq(fees[0], 3778028623870480400);
}

function testSetRedemptionFeeShift() public {
// Arrange
// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 500 * 1e18);
feeCalculator.setRedemptionFeeShift(0.5 * 1e18);

// Act
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), 100e18);

// Assert
assertEq(fees[0], 2303907724666580660);
}

function testSetSingleAssetRedemptionRelativeFee() public {
// Arrange
// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 1000 * 1e18);
feeCalculator.setSingleAssetRedemptionRelativeFee(0.83 * 1e18);

// Act
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), 100 * 1e18);

assertEq(fees[0], 83 * 1e18);
}

function testSetDustAssetRedemptionRelativeFee() public {
// Arrange
// Set up your test data
uint256 depositAmount = 2323662174650;

// Set up mock pool
mockPool.setTotalSupply(56636794628913227180683983236);
mockToken.setTokenBalance(address(mockPool), 55661911070827884041095553095);
feeCalculator.setDustAssetRedemptionRelativeFee(0.91 * 1e18);

// Act
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), depositAmount);

// Assert
assertEq(fees[0], depositAmount * 91 / 100);
}
}