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

estimate tco2 redemption amount given pool redemption amount #40

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
56 changes: 54 additions & 2 deletions src/FeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,47 @@ contract FeeCalculator is IFeeCalculator, Ownable {
feeDistribution.shares = shares;
}

/// @notice Estimates the TCO2 token redemption amount for a given pool token redemption amount.
/// @param pool The address of the pool.
/// @param tco2 The address of the TCO2 token.
/// @param poolAmount the pool token amount to be redeemed.
/// @return estimated TCO2 token redemption amount for a give pool token redemption amount.
function estimateTCO2RedemptionAmount(address pool, address tco2, uint256 poolAmount)
external
view
override
returns (uint256)
{
uint256 current = getTokenBalance(pool, tco2);
uint256 total = getTotalSupply(pool);

// @dev this is implicit function, so we need to estimate
// redemptionAmount = poolAmount - calculateRedemptionFee(redemptionAmount, current, total)

//poolAmount >= tco2Amount
uint256 minTCO2Amount = poolAmount - calculateRedemptionFee(poolAmount, current, total);
uint256 maxTCO2Amount = poolAmount;
uint256 tco2RedemptionAmount = (minTCO2Amount + maxTCO2Amount) / 2; //midpoint
uint256 maxIterations = 40;
PawelTroka marked this conversation as resolved.
Show resolved Hide resolved

for (uint256 i = 0; i < maxIterations; i++) {
uint256 feeAmount = calculateRedemptionFee(tco2RedemptionAmount, current, total);
uint256 estimatedPoolAmount = tco2RedemptionAmount + feeAmount;

bool isOverestimated = (estimatedPoolAmount > poolAmount);

if (isOverestimated) {
maxTCO2Amount = tco2RedemptionAmount;
} else {
minTCO2Amount = tco2RedemptionAmount;
}

tco2RedemptionAmount = (minTCO2Amount + maxTCO2Amount) / 2; //midpoint
}

return tco2RedemptionAmount;
}

/// @notice Calculates the redemption fees for a given amount.
/// @param pool The address of the pool.
/// @param tco2s The addresses of the TCO2 token.
Expand All @@ -192,14 +233,25 @@ contract FeeCalculator is IFeeCalculator, Ownable {
require(tco2s.length == 1, "only one");
address tco2 = tco2s[0];
uint256 redemptionAmount = redemptionAmounts[0];
uint256 feeAmount = calculateRedemptionFee(redemptionAmount, getTokenBalance(pool, tco2), getTotalSupply(pool));
feeDistribution = calculateFeeShares(feeAmount);
}

/// @notice Calculates the redemption fee for a given amount.
/// @param redemptionAmount The amount to be redeemed.
/// @return The calculated redemption fee.
function calculateRedemptionFee(uint256 redemptionAmount, uint256 current, uint256 total)
private
view
returns (uint256)
{
require(redemptionAmount > 0, "redemptionAmount must be > 0");

uint256 feeAmount = getRedemptionFee(redemptionAmount, getTokenBalance(pool, tco2), getTotalSupply(pool));
uint256 feeAmount = getRedemptionFee(redemptionAmount, current, total);

require(feeAmount <= redemptionAmount, "Fee must be lower or equal to redemption amount");
require(feeAmount > 0, "Fee must be greater than 0");
feeDistribution = calculateFeeShares(feeAmount);
return feeAmount;
}

/// @notice Gets the balance of the TCO2 token in a given pool.
Expand Down
10 changes: 10 additions & 0 deletions src/interfaces/IFeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ interface IFeeCalculator {
external
view
returns (FeeDistribution memory feeDistribution);

/// @notice Estimates the TCO2 token redemption amount for a given pool token redemption amount.
PawelTroka marked this conversation as resolved.
Show resolved Hide resolved
/// @param pool The address of the pool.
/// @param tco2 The address of the TCO2 token.
/// @param poolAmount the pool token amount to be redeemed.
/// @return estimated TCO2 token redemption amount for a give pool token redemption amount.
function estimateTCO2RedemptionAmount(address pool, address tco2, uint256 poolAmount)
external
view
returns (uint256);
PawelTroka marked this conversation as resolved.
Show resolved Hide resolved
}
43 changes: 43 additions & 0 deletions test/FeeCalculator.fuzzy.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,47 @@ contract FeeCalculatorTestFuzzy is Test {
}
assertApproxEqAbs(fees[recipients.length - 1], 11526003792614720250 * (equalShare + leftShare) / 100, 1);
}

function testEstimateTCO2RedemptionAmount_FuzzyCase_ShouldGiveResultsWithSmallSlippage(
uint128 _redemptionAmount,
uint128 _current,
uint128 _total
) public {
vm.assume(_total / 2 >= _current);
PawelTroka marked this conversation as resolved.
Show resolved Hide resolved
vm.assume(_redemptionAmount <= _current / 2);
vm.assume(_redemptionAmount < 1e20 * 1e18);
vm.assume(_total < 1e20 * 1e18);
vm.assume(_redemptionAmount > 1e-6 * 1e18);
vm.assume(_current > 1e12);

// Arrange
// Set up your test data
uint256 redemptionAmount = _redemptionAmount;
uint256 current = _current;
uint256 total = _total;

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

// Set up mock pool
mockPool.setTotalSupply(total);
mockToken.setTokenBalance(address(mockPool), current);

// Act
uint256 poolAmount = redemptionAmount
+ feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, redemptionAmounts).shares[0];
uint256 estimatedRedemptionAmount =
feeCalculator.estimateTCO2RedemptionAmount(address(mockPool), address(mockToken), poolAmount);

uint256[] memory estimatedRedemptionAmounts = new uint256[](1);
estimatedRedemptionAmounts[0] = redemptionAmount;

uint256 estimatedPoolAmount = estimatedRedemptionAmount
+ feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, estimatedRedemptionAmounts).shares[0];

// Assert
assertApproxEqRel(poolAmount, estimatedPoolAmount, 0.01 * 1e18); //1% slippage allowed
}
}
31 changes: 31 additions & 0 deletions test/FeeCalculator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,35 @@ contract FeeCalculatorTest is Test {
// Assert
assertEq(fees[0], redemptionAmount * 91 / 100);
}

function testEstimateTCO2RedemptionAmount_NormalCase_ShouldGiveResultsWithSmallSlippage() public {
// Arrange
// Set up your test data
uint256 redemptionAmount = 100 * 1e18;
address[] memory tco2s = new address[](1);
tco2s[0] = address(mockToken);
uint256[] memory redemptionAmounts = new uint256[](1);
redemptionAmounts[0] = redemptionAmount;

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

// Act
uint256 poolAmount = redemptionAmount
+ feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, redemptionAmounts).shares[0];
uint256 estimatedRedemptionAmount =
feeCalculator.estimateTCO2RedemptionAmount(address(mockPool), address(mockToken), poolAmount);

uint256[] memory estimatedRedemptionAmounts = new uint256[](1);
estimatedRedemptionAmounts[0] = redemptionAmount;

uint256 estimatedPoolAmount = estimatedRedemptionAmount
+ feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, estimatedRedemptionAmounts).shares[0];

// Assert
assertApproxEqRel(poolAmount, estimatedPoolAmount, 0.01 * 1e18); //1% slippage allowed
}
}
Loading