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

WIP: Calculate fees for multiple redemptions at once #28

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 36 additions & 0 deletions src/FeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,42 @@ contract FeeCalculator is IDepositFeeCalculator, IRedemptionFeeCalculator, Ownab
return distributeFeeAmongShares(totalFee);
}

/// @notice Calculates the redemption fees across multiple TCO2s for a given amount.
/// @param tco2s The addresses of the TCO2 tokens.
/// @param pool The address of the pool.
/// @param amounts The amounts to be redeemed per TCO2.
/// @return recipients The addresses of the fee recipients.
/// @return feesDenominatedInPoolTokens The amount of fees each recipient should receive.
function calculateRedemptionFeesMany(address[] calldata tco2s, address pool, uint256[] calldata amounts)
external
view
override
returns (address[] memory recipients, uint256[] memory feesDenominatedInPoolTokens)
{
uint256 tco2Len = tco2s.length;
require(tco2Len == amounts.length, "Length mismatch");

uint256 totalFee = 0;
uint256 totalPoolSupply = getTotalSupply(pool);

for (uint256 i = 0; i < tco2Len; ++i) {
uint256 redemptionAmount = amounts[i];
require(redemptionAmount > 0, "amount must be > 0");
uint256 tco2Balance = getTokenBalance(pool, tco2s[i]);
uint256 feeAmount = getRedemptionFee(redemptionAmount, tco2Balance, totalPoolSupply);
require(feeAmount <= redemptionAmount, "Fee must be lower or equal to redemption amount");
totalFee += feeAmount;
// Update total pool supply to account for the tokens to be burnt
// so the next iteration charges fees using the intermediate
// pool supply.
totalPoolSupply = totalPoolSupply - redemptionAmount + feeAmount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The amount by which the totalPoolSupply will change should be

totalPoolSupply-=redemptionAmount

feeAmount is not being burnt.

The way in works from the user perspective is as follows:

  • user want to redeem the 10 tco2 from the pool
  • fee calculated would be let's say 0.1
  • user will pay 10.1 pool tokens and receive the 10 tco2

This different from the deposits where:

  • user has 10 tco2
  • fee calculated for deposit is 0.1
  • user receives 9.9 pool tokens

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in case someone passes

2 items which both are same tco2

you would need to track the tco2Balance as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way in works from the user perspective is as follows:

  • user want to redeem the 10 tco2 from the pool
  • fee calculated would be let's say 0.1
  • user will pay 10.1 pool tokens and receive the 10 tco2

@kosecki123 @0xmichalis I could be misunderstanding, but this sounds problematic to me, for the following reasons:

  1. If the user wants to redeem all their pool tokens, it requires them to work backwards from their balance to figure out the correct redemption amount. In your example above, if they have exactly 10 pool tokens, they would need to find a value around 9.9 tokens to redeem which totals exactly 10 pool tokens when the fee is added on top. However, because the fee is on a non-linear curve, I suspect this turns into a really complicated mathematical equation to solve, even if there's UI code to do it for the user:

     desiredTCO2Amount + redemptionFee(desiredTCO2Amount, ...) == userPoolBalance
    
  2. If the pool diversity changes slightly to increase the fee in between receiving the quote and submitting the transaction, it could easily increase the fee enough that the amount of pool tokens would exceed their balance of 10, and the transaction would fail.

  3. If instead the fee decreases in that window between quote and execution, then the user gets left with annoying amount of pool token dust which they probably can't make use of because it's not worth the gas fees to get rid of it.

In contrast, if we take the approach that the fee is deducted from the amount of pool tokens the user requests to redeem, then all of these problems go away: the user will always spend exactly 10 pool tokens, and the only thing which can change is how much TCO2 they get back (which BTW will also have a lower bound due to the max fee cap which we are adding).

}

require(totalFee > 0, "Fee must be greater than 0");

return distributeFeeAmongShares(totalFee);
}

/// @notice Gets the balance of the TCO2 token in a given pool.
/// @param pool The address of the pool.
/// @param tco2 The address of the TCO2 token.
Expand Down
10 changes: 10 additions & 0 deletions src/interfaces/IRedemptionFeeCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ interface IRedemptionFeeCalculator {
function calculateRedemptionFees(address tco2, address pool, uint256 depositAmount)
external
returns (address[] memory recipients, uint256[] memory feesDenominatedInPoolTokens);

/// @notice Calculates the redemption fees across multiple TCO2s for a given amount.
/// @param tco2s The addresses of the TCO2 tokens.
/// @param pool The address of the pool.
/// @param amounts The amounts to be redeemed per TCO2.
/// @return recipients The addresses of the fee recipients.
/// @return feesDenominatedInPoolTokens The amount of fees each recipient should receive.
function calculateRedemptionFeesMany(address[] calldata tco2s, address pool, uint256[] calldata amounts)
external
returns (address[] memory recipients, uint256[] memory feesDenominatedInPoolTokens);
}