diff --git a/src/FeeCalculator.sol b/src/FeeCalculator.sol index 04d67cf..5a2f5e8 100644 --- a/src/FeeCalculator.sol +++ b/src/FeeCalculator.sol @@ -27,6 +27,8 @@ contract FeeCalculator is IFeeCalculator, Ownable { SD59x18 private redemptionFeeScale = sd(0.3 * 1e18); SD59x18 private redemptionFeeShift = sd(0.1 * 1e18); //-log10(0+0.1)=1 -> 10^-1 + uint256 private estimateTCO2RedemptionAmountIterations = 40; + function redemptionFeeConstant() private view returns (SD59x18) { return redemptionFeeScale * (one + redemptionFeeShift).log10(); //0.0413926851582251=log10(1+0.1) } @@ -115,6 +117,16 @@ contract FeeCalculator is IFeeCalculator, Ownable { dustAssetRedemptionRelativeFee = dustAssetRedemptionRelativeFeeSD; } + /// @notice Sets the estimate TCO2 redemption amount iterations. + /// @dev Can only be called by the current owner. + /// @param _estimateTCO2RedemptionAmountIterations The new estimate TCO2 redemption amount iterations. + function setEstimateTCO2RedemptionAmountIterations(uint256 _estimateTCO2RedemptionAmountIterations) + external + onlyOwner + { + estimateTCO2RedemptionAmountIterations = _estimateTCO2RedemptionAmountIterations; + } + /// @notice Sets up the fee distribution among recipients. /// @dev Can only be called by the current owner. /// @param recipients The addresses of the fee recipients. @@ -176,6 +188,47 @@ contract FeeCalculator is IFeeCalculator, Ownable { feeDistribution.shares = shares; } + /// @notice Estimates the TCO2 token redemption amount for a given pool token redemption amount. + /// @dev Client that want to use a fixed pool token amount should use this function first to go from POOL to an approximation of the TCO2 they will get back, then use TCO2 to calculate the actual redemption fees they need to pay with calculateRedemptionFees. + /// @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 + + for (uint256 i = 0; i < estimateTCO2RedemptionAmountIterations; 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. @@ -192,14 +245,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. diff --git a/src/interfaces/IFeeCalculator.sol b/src/interfaces/IFeeCalculator.sol index 69266a3..3688978 100644 --- a/src/interfaces/IFeeCalculator.sol +++ b/src/interfaces/IFeeCalculator.sol @@ -35,4 +35,15 @@ interface IFeeCalculator { external view returns (FeeDistribution memory feeDistribution); + + /// @notice Estimates the TCO2 token redemption amount for a given pool token redemption amount. + /// @dev Client that want to use a fixed pool token amount should use this function first to go from POOL to an approximation of the TCO2 they will get back, then use TCO2 to calculate the actual redemption fees they need to pay with calculateRedemptionFees. + /// @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 estimatedTCO2Amount estimated TCO2 token redemption amount for a give pool token redemption amount. + function estimateTCO2RedemptionAmount(address pool, address tco2, uint256 poolAmount) + external + view + returns (uint256 estimatedTCO2Amount); } diff --git a/test/FeeCalculator.fuzzy.t.sol b/test/FeeCalculator.fuzzy.t.sol index d9d2983..2f4a1a9 100644 --- a/test/FeeCalculator.fuzzy.t.sol +++ b/test/FeeCalculator.fuzzy.t.sol @@ -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 >= _current); + vm.assume(_redemptionAmount < _current / 2); //because of the fee that stays in the pool the redemption amount needs to be lower than current amount + 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] = estimatedRedemptionAmount; + + uint256 estimatedPoolAmount = estimatedRedemptionAmount + + feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, estimatedRedemptionAmounts).shares[0]; + + // Assert + assertApproxEqRel(poolAmount, estimatedPoolAmount, 0.01 * 1e18); //1% slippage allowed + } } diff --git a/test/FeeCalculator.t.sol b/test/FeeCalculator.t.sol index a14be2f..64e0724 100644 --- a/test/FeeCalculator.t.sol +++ b/test/FeeCalculator.t.sol @@ -968,4 +968,84 @@ 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] = estimatedRedemptionAmount; + + uint256 estimatedPoolAmount = estimatedRedemptionAmount + + feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, estimatedRedemptionAmounts).shares[0]; + + // Assert + assertApproxEqRel(poolAmount, estimatedPoolAmount, 0.01 * 1e18); //1% slippage allowed + } + + function testSetEstimateTCO2RedemptionAmountIterations_NormalCase_ShouldIncreaseAccuracyWithMoreIterations() + 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; + uint256 lowIterations = 5; + uint256 highIterations = 100; + mockPool.setTotalSupply(total); + mockToken.setTokenBalance(address(mockPool), current); + + // Act + uint256 poolAmount = redemptionAmount + + feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, redemptionAmounts).shares[0]; + + feeCalculator.setEstimateTCO2RedemptionAmountIterations(lowIterations); + uint256 estimatedRedemptionAmountLowPrecision = + feeCalculator.estimateTCO2RedemptionAmount(address(mockPool), address(mockToken), poolAmount); + + uint256[] memory estimatedRedemptionAmountsLowPrecision = new uint256[](1); + estimatedRedemptionAmountsLowPrecision[0] = estimatedRedemptionAmountLowPrecision; + + uint256 estimatedPoolAmountLowPrecision = estimatedRedemptionAmountLowPrecision + + feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, estimatedRedemptionAmountsLowPrecision).shares[0]; + + feeCalculator.setEstimateTCO2RedemptionAmountIterations(highIterations); + uint256 estimatedRedemptionAmountHighPrecision = + feeCalculator.estimateTCO2RedemptionAmount(address(mockPool), address(mockToken), poolAmount); + + uint256[] memory estimatedRedemptionAmountsHighPrecision = new uint256[](1); + estimatedRedemptionAmountsHighPrecision[0] = estimatedRedemptionAmountHighPrecision; + + uint256 estimatedPoolAmountHighPrecision = estimatedRedemptionAmountHighPrecision + + feeCalculator.calculateRedemptionFees(address(mockPool), tco2s, estimatedRedemptionAmountsHighPrecision) + .shares[0]; + + // Assert + assertApproxEqRel(poolAmount, estimatedPoolAmountLowPrecision, 0.05 * 1e18); //0.05% slippage allowed + assertApproxEqRel(poolAmount, estimatedPoolAmountHighPrecision, 1); //super minimal slippage allowed + } }