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: divide by 0 error in _reportLoss (#306) #307

Closed
wants to merge 1 commit into from

Conversation

fp-crypto
Copy link
Contributor

@fp-crypto fp-crypto commented Apr 30, 2021

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 existing master branch but is fixed in this PR.

fixes #306

@fp-crypto fp-crypto marked this pull request as draft April 30, 2021 00:59
@fp-crypto fp-crypto changed the title chore: demonstrate #306 fix: divide by 0 error in _reportLoss (#306) Apr 30, 2021
@fp-crypto fp-crypto marked this pull request as ready for review April 30, 2021 01:22
tests/functional/vault/test_losses.py Outdated Show resolved Hide resolved
tests/functional/vault/test_losses.py Outdated Show resolved Hide resolved
tests/functional/vault/test_losses.py Outdated Show resolved Hide resolved
tests/functional/vault/test_losses.py Outdated Show resolved Hide resolved
tests/functional/vault/test_losses.py Outdated Show resolved Hide resolved
tests/functional/vault/test_losses.py Show resolved Hide resolved
@poolpitako poolpitako requested a review from fubuloubu April 30, 2021 01:30
Copy link
Member

@fubuloubu fubuloubu left a 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

Copy link
Member

@fubuloubu fubuloubu left a 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.

https://github.com/yearn/yearn-vaults/pull/307/files#diff-b583ba5c4d7b67e56b9084eff9fe883e12a2cdd94a2ea25029322c6f3568d807R974-R976

@fubuloubu
Copy link
Member

fubuloubu commented May 3, 2021

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

@ghost
Copy link

ghost commented May 10, 2021

@fp-crypto need help?

@ghost
Copy link

ghost commented May 12, 2021

Will take care of it.

@ghost ghost closed this May 12, 2021
This pull request was closed.
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.

Div by zero in Vault code
3 participants