The _currentExchangeRate of the Vault contract can not increase, and always be lower than or equal to _assetUnit. Therefore, when the vault is undercollateralized (_currentExchangeRate < _assetUnit), it canβt be further collateralized.
function _currentExchangeRate() internal view returns (uint256) {
uint256 _totalSupplyAmount = _totalSupply();
uint256 _totalSupplyToAssets = _convertToAssets(
_totalSupplyAmount,
_lastRecordedExchangeRate,
Math.Rounding.Down
);
uint256 _withdrawableAssets = _yieldVault.maxWithdraw(address(this));
if (_withdrawableAssets > _totalSupplyToAssets) {
_withdrawableAssets = _withdrawableAssets - (_withdrawableAssets - _totalSupplyToAssets);
}
if (_totalSupplyAmount != 0 && _withdrawableAssets != 0) {
return _withdrawableAssets.mulDiv(_assetUnit, _totalSupplyAmount, Math.Rounding.Down);
}
return _assetUnit;
}
In case _totalSupplyAmount != 0 && _withdrawableAssets != 0, _currentExchangeRate function will return a value _withdrawableAssets * _assetUnit / _totalSupplyAmount.
However _withdrawableAssets can not exceed _totalSupplyToAssets, which is equal to _totalSupplyAmount * _lastRecordedExchangeRate / _assetUnit.
Therefore, _currentExchangeRate always be lower than or equal to _lastRecordedExchangeRate.
Testing:
Add this assert line and run forge test , all tests will passed.
if (_totalSupplyAmount != 0 && _withdrawableAssets != 0) {
assert(_withdrawableAssets.mulDiv(_assetUnit, _totalSupplyAmount, Math.Rounding.Down) <= _assetUnit);
return _withdrawableAssets.mulDiv(_assetUnit, _totalSupplyAmount, Math.Rounding.Down);
}
Manual Review
Remove these lines of code that limit the _withdrawableAssets
if (_withdrawableAssets > _totalSupplyToAssets) {
_withdrawableAssets = _withdrawableAssets - (_withdrawableAssets - _totalSupplyToAssets);
}
Context
The text was updated successfully, but these errors were encountered:
All reactions