-
Notifications
You must be signed in to change notification settings - Fork 37
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
fix(exchangeRate): add 1:1 exchange rate #18
Conversation
562ca83
to
32b1b61
Compare
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; |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
1017a67
into
gen-197-c4-issue-435-mintyieldfee-does-not-check-maxmint
Issue: code-423n4/2023-07-pooltogether-findings#143