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 interface update #39

Merged
merged 2 commits into from
Jan 25, 2024
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
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