-
Notifications
You must be signed in to change notification settings - Fork 322
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: divide by 0 error in _reportLoss (#306) #307
Conversation
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.
Please fix your commit messages to align with conventional commits and run black .
on your code updates to get this to pass the CI
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.
The issue here is that the actual bug is that the loss is accounted for before this ratio adjustment calculation is computed. That should be re-ordered to occur prior to the subtraction of the loss.
This would be my suggestion for how to change this PR def _reportLoss(strategy: address, loss: uint256):
# Loss can only be up the amount of debt issued to strategy
totalDebt: uint256 = self.strategies[strategy].totalDebt
assert totalDebt >= loss
+
+ # Also, make sure we reduce our trust with the strategy by the amount of loss
+ precisionFactor: uint256 = self.precisionFactor
+ ratio_change: uint256 = min(
+ precisionFactor * MAX_BPS * loss / self._totalAssets() / precisionFactor,
+ self.strategies[strategy].debtRatio,
+ )
+
+ # Finally, adjust our strategy's parameters by the loss
self.strategies[strategy].totalLoss += loss
self.strategies[strategy].totalDebt = totalDebt - loss
self.totalDebt -= loss
- # Also, make sure we reduce our trust with the strategy by the same amount
- debtRatio: uint256 = self.strategies[strategy].debtRatio
- precisionFactor: uint256 = self.precisionFactor
- ratio_change: uint256 = min(precisionFactor * loss * MAX_BPS / self._totalAssets() / precisionFactor, debtRatio)
self.strategies[strategy].debtRatio -= ratio_change
self.debtRatio -= ratio_change |
@fp-crypto need help? |
Will take care of it. |
This PR fixes a divide by 0 error that manifests when a vault has a total loss and
harvest
is called. The divide by 0 is only triggered when there is a total loss. Included is a new test that demonstrates the issue when run against the existingmaster
branch but is fixed in this PR.fixes #306