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

fix(exchangeRate): add 1:1 exchange rate #18

Conversation

PierrickGT
Copy link
Contributor

Base automatically changed from gen-253-c4-issue-272 to gen-197-c4-issue-435-mintyieldfee-does-not-check-maxmint August 15, 2023 03:13
src/Vault.sol Outdated
// Case when `_withdrawableAssets == 0` but `_depositedAssets != 0`.
// The Vault is entirely undercollateralized
// or the YieldVault has paused withdrawals and shares can't be redeemed.
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that there is no historic _lastRecordedExchangeRate, it doesn't feel like this function serves a strong purpose, and it's doubling the number of mulDiv that convertToShares and convertToAssets have to do.

I imagine this function could be refactored into a separate function that checks available assets:

function _collateral() {
    uint256 _depositedAssets = _totalSupply();
    uint256 _withdrawableAssets = _yieldVault.maxWithdraw(address(this));

    // If the Vault is collateralized, users can only withdraw the amount of underlying assets they deposited.
    // An exchange rate of 1 is returned to exclude the amount of yield generated by the YieldVault.
    if (_withdrawableAssets >= _depositedAssets) {
      return _depositedAssets;
    }
    return _withdrawableAssets;
}

Then in your convertToShares and convertToAssets you're only doing 1 mulDiv:

function _convertToShares(
    uint256 _assets,
    Math.Rounding _rounding
  ) internal view virtual override returns (uint256) {
    uint totalSupplyShares = _totalSupply();
    return
      (_assets == 0 || totalSupplyShares == 0)
        ? _assets
        : _assets.mulDiv(totalSupplyShares, _collateral(), _rounding);
  }

and vice versa for assets.

This reduces the mulDiv, and makes the exchange rate clearer I think (less logic).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But then once the Vault becomes undercollateralized, any yield that has not been claimed won't be withdrawable since we wouldn't include it in the exchange rate calculation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes we would; the _collateral() would be equal to _withdrawableAssets because it's undercollateralized.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the _collateral() function in this commit: 81fe36a

@PierrickGT PierrickGT merged commit 1017a67 into gen-197-c4-issue-435-mintyieldfee-does-not-check-maxmint Aug 16, 2023
1 of 2 checks passed
@PierrickGT PierrickGT deleted the gen-228-c4-issue-143 branch August 16, 2023 20:37
@PierrickGT PierrickGT restored the gen-228-c4-issue-143 branch August 16, 2023 22:04
@PierrickGT PierrickGT deleted the gen-228-c4-issue-143 branch August 16, 2023 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants