Skip to content

Commit

Permalink
Merge pull request #39 from neutral-protocol/interface-update
Browse files Browse the repository at this point in the history
Redemption interface update
  • Loading branch information
0xmichalis authored Jan 25, 2024
2 parents 0a44deb + 18bbf83 commit f4abb88
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 83 deletions.
15 changes: 10 additions & 5 deletions src/FeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ contract FeeCalculator is IFeeCalculator, Ownable {
}

/// @notice Calculates the deposit fee for a given amount.
/// @param tco2 The address of the TCO2 token.
/// @param pool The address of the pool.
/// @param tco2 The address of the TCO2 token.
/// @param depositAmount The amount to be deposited.
/// @return feeDistribution How the fee is meant to be
/// distributed among the fee recipients.
function calculateDepositFees(address tco2, address pool, uint256 depositAmount)
function calculateDepositFees(address pool, address tco2, uint256 depositAmount)
external
view
override
Expand Down Expand Up @@ -177,17 +177,22 @@ contract FeeCalculator is IFeeCalculator, Ownable {
}

/// @notice Calculates the redemption fees for a given amount.
/// @param tco2 The address of the TCO2 token.
/// @param pool The address of the pool.
/// @param redemptionAmount The amount to be redeemed.
/// @param tco2s The addresses of the TCO2 token.
/// @param redemptionAmounts The amounts to be redeemed.
/// @return feeDistribution How the fee is meant to be
/// distributed among the fee recipients.
function calculateRedemptionFees(address tco2, address pool, uint256 redemptionAmount)
function calculateRedemptionFees(address pool, address[] calldata tco2s, uint256[] calldata redemptionAmounts)
external
view
override
returns (FeeDistribution memory feeDistribution)
{
require(tco2s.length == redemptionAmounts.length, "length mismatch");
require(tco2s.length == 1, "only one");
address tco2 = tco2s[0];
uint256 redemptionAmount = redemptionAmounts[0];

require(redemptionAmount > 0, "redemptionAmount must be > 0");

uint256 feeAmount = getRedemptionFee(redemptionAmount, getTokenBalance(pool, tco2), getTotalSupply(pool));
Expand Down
10 changes: 5 additions & 5 deletions src/interfaces/IFeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ struct FeeDistribution {
/// @notice This interface defines methods for calculating fees.
interface IFeeCalculator {
/// @notice Calculates the deposit fee for a given amount.
/// @param tco2 The address of the TCO2 token.
/// @param pool The address of the pool.
/// @param tco2 The address of the TCO2 token.
/// @param depositAmount The amount to be deposited.
/// @return feeDistribution How the fee is meant to be
/// distributed among the fee recipients.
function calculateDepositFees(address tco2, address pool, uint256 depositAmount)
function calculateDepositFees(address pool, address tco2, uint256 depositAmount)
external
view
returns (FeeDistribution memory feeDistribution);

/// @notice Calculates the redemption fees for a given amount.
/// @param tco2 The address of the TCO2 token.
/// @param pool The address of the pool.
/// @param redemptionAmount The amount to be redeemed.
/// @param tco2s The addresses of the TCO2 token.
/// @param redemptionAmounts The amounts to be redeemed.
/// @return feeDistribution How the fee is meant to be
/// distributed among the fee recipients.
function calculateRedemptionFees(address tco2, address pool, uint256 redemptionAmount)
function calculateRedemptionFees(address pool, address[] calldata tco2s, uint256[] calldata redemptionAmounts)
external
view
returns (FeeDistribution memory feeDistribution);
Expand Down
20 changes: 13 additions & 7 deletions test/FeeCalculator.fuzzy.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ contract FeeCalculatorTestFuzzy is Test {
mockToken.setTokenBalance(address(mockPool), 1e9 * 1e18);

vm.expectRevert("Fee must be greater than 0");
feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), depositAmount);
feeCalculator.calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
}

function testCalculateDepositFeesFuzzy(uint256 depositAmount, uint256 current, uint256 total) public {
Expand All @@ -68,7 +68,7 @@ contract FeeCalculatorTestFuzzy is Test {
mockToken.setTokenBalance(address(mockPool), current);

// Act
try feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), depositAmount) {}
try feeCalculator.calculateDepositFees(address(mockPool), address(mockToken), depositAmount) {}
catch Error(string memory reason) {
assertTrue(
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
Expand Down Expand Up @@ -119,8 +119,13 @@ contract FeeCalculatorTestFuzzy is Test {
bool oneTimeRedemptionFailed = false;
uint256 multipleTimesRedemptionFailedCount = 0;

address[] memory tco2s = new address[](1);
tco2s[0] = address(mockToken);
uint256[] memory redemptionAmounts = new uint256[](1);
redemptionAmounts[0] = redemptionAmount;

// Act
try feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), redemptionAmount) returns (
try feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, redemptionAmounts) returns (
FeeDistribution memory feeDistribution
) {
oneTimeFee = feeDistribution.shares.sumOf();
Expand All @@ -145,7 +150,8 @@ contract FeeCalculatorTestFuzzy is Test {

for (uint256 i = 0; i < numberOfRedemptions; i++) {
uint256 redemption = equalRedemption + (i == 0 ? restRedemption : 0);
try feeCalculator.calculateRedemptionFees(address(mockToken), address(mockPool), redemption) returns (
redemptionAmounts[0] = redemption;
try feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, redemptionAmounts) returns (
FeeDistribution memory feeDistribution
) {
feeFromDividedRedemptions += feeDistribution.shares.sumOf();
Expand Down Expand Up @@ -203,7 +209,7 @@ contract FeeCalculatorTestFuzzy is Test {
uint256 oneTimeFee = 0;

// Act
try feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), depositAmount) returns (
try feeCalculator.calculateDepositFees(address(mockPool), address(mockToken), depositAmount) returns (
FeeDistribution memory feeDistribution
) {
oneTimeFee = feeDistribution.shares.sumOf();
Expand All @@ -223,7 +229,7 @@ contract FeeCalculatorTestFuzzy is Test {
for (uint256 i = 0; i < numberOfDeposits; i++) {
uint256 deposit = equalDeposit + (i == 0 ? restDeposit : 0);

try feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), deposit) returns (
try feeCalculator.calculateDepositFees(address(mockPool), address(mockToken), deposit) returns (
FeeDistribution memory feeDistribution
) {
feeFromDividedDeposits += feeDistribution.shares.sumOf();
Expand Down Expand Up @@ -278,7 +284,7 @@ contract FeeCalculatorTestFuzzy is Test {

// Act
FeeDistribution memory feeDistribution =
feeCalculator.calculateDepositFees(address(mockToken), address(mockPool), depositAmount);
feeCalculator.calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
address[] memory gotRecipients = feeDistribution.recipients;
uint256[] memory fees = feeDistribution.shares;

Expand Down
Loading

0 comments on commit f4abb88

Please sign in to comment.