Skip to content

Commit

Permalink
Merge pull request #19 from neutral-protocol/feature/cover-uncovered-…
Browse files Browse the repository at this point in the history
…branches-with-tests

cover uncovered branches with tests
  • Loading branch information
PawelTroka authored Nov 28, 2023
2 parents e920562 + 5058880 commit f93a290
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/FeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ contract FeeCalculator is IDepositFeeCalculator, IRedemptionFeeCalculator {
/// @param total The total supply of the pool.
/// @return The calculated deposit fee.
function getDepositFee(uint256 amount, uint256 current, uint256 total) private view returns (uint256) {
require(total >= current);
require(
total >= current,
"The total volume in the pool must be greater than or equal to the volume for an individual asset"
);

SD59x18 amount_float = sd(int256(amount));

Expand Down Expand Up @@ -194,8 +197,11 @@ contract FeeCalculator is IDepositFeeCalculator, IRedemptionFeeCalculator {
/// @param total The total supply of the pool.
/// @return The calculated redemption fee.
function getRedemptionFee(uint256 amount, uint256 current, uint256 total) private view returns (uint256) {
require(total >= current);
require(amount <= current);
require(
total >= current,
"The total volume in the pool must be greater than or equal to the volume for an individual asset"
);
require(amount <= current, "The amount to be redeemed cannot exceed the current balance of the pool");

SD59x18 amount_float = sd(int256(amount));

Expand Down
113 changes: 113 additions & 0 deletions test/FeeCalculator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,70 @@ contract FeeCalculatorTest is Test {
feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), depositAmount);
}

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

// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 1500 * 1e18);

// Act
vm.expectRevert(
"The total volume in the pool must be greater than or equal to the volume for an individual asset"
);
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), depositAmount);
}

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

// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 1500 * 1e18);

// Act & Assert
vm.expectRevert(
"The total volume in the pool must be greater than or equal to the volume for an individual asset"
);
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), depositAmount);
}

function testCalculateRedemptionFees_AmountGreaterThanCurrent_ExceptionShouldBeThrown() public {
// Arrange
// Set up your test data
uint256 depositAmount = 600 * 1e18;

// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 500 * 1e18);

// Act
vm.expectRevert("The amount to be redeemed cannot exceed the current balance of the pool");
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), depositAmount);
}

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

// Set up mock pool
mockPool.setTotalSupply(1000 * 1e18);
mockToken.setTokenBalance(address(mockPool), 500 * 1e18);

// Act & Assert
vm.expectRevert("redemptionAmount must be > 0");
(address[] memory recipients, uint256[] memory fees) =
feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), depositAmount);
}

function testCalculateDepositFees_EmptyPool_FeeCappedAt10Percent() public {
// Arrange
// Set up your test data
Expand Down Expand Up @@ -837,6 +901,55 @@ contract FeeCalculatorTest is Test {
return sum;
}

function testFeeSetup_RecipientsEmpty_ShouldThrowError() public {
// Arrange
// Set up your test data
address[] memory _recipients = new address[](0);
uint256[] memory _feeShares = new uint256[](0);

// Act & Assert
vm.expectRevert("Total shares must equal 100");
feeCalculator.feeSetup(_recipients, _feeShares);
}

function testFeeSetup_RecipientsAndSharesDifferentLength_ShouldThrowError() public {
// Arrange
// Set up your test data
address feeRecipient1 = 0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B;
address feeRecipient2 = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
address[] memory _recipients = new address[](2);
_recipients[0] = feeRecipient1;
_recipients[1] = feeRecipient2;
uint256[] memory _feeShares = new uint256[](3);
_feeShares[0] = 20;
_feeShares[1] = 20;
_feeShares[2] = 60;

// Act & Assert
vm.expectRevert("Recipients and shares arrays must have the same length");
feeCalculator.feeSetup(_recipients, _feeShares);
}

function testFeeSetup_RecipientsAndSharesSameLengthButSharesSumUpTo101_ShouldThrowError() public {
// Arrange
// Set up your test data
address feeRecipient1 = 0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B;
address feeRecipient2 = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
address feeRecipient3 = 0xb1bB642E2CD1E3254d3395f02483e3DA7baF2d83;
address[] memory _recipients = new address[](3);
_recipients[0] = feeRecipient1;
_recipients[1] = feeRecipient2;
_recipients[2] = feeRecipient3;
uint256[] memory _feeShares = new uint256[](3);
_feeShares[0] = 21;
_feeShares[1] = 20;
_feeShares[2] = 60;

// Act & Assert
vm.expectRevert("Total shares must equal 100");
feeCalculator.feeSetup(_recipients, _feeShares);
}

function testFeeSetupFuzzy(address[] memory recipients, uint8 firstShare) public {
vm.assume(recipients.length <= 100);
vm.assume(recipients.length > 1); //at least two recipients
Expand Down

0 comments on commit f93a290

Please sign in to comment.