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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion contracts/Vault.vy
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,9 @@ def _reportLoss(strategy: address, loss: uint256):
# 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)
ratio_change: uint256 = debtRatio
if self._totalAssets() > 0:
ratio_change = min(precisionFactor * loss * MAX_BPS / self._totalAssets() / precisionFactor, ratio_change)
self.strategies[strategy].debtRatio -= ratio_change
self.debtRatio -= ratio_change

Expand Down
18 changes: 18 additions & 0 deletions tests/functional/vault/test_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ def test_losses(chain, vault, strategy, gov, token):
params = vault.strategies(strategy).dict()
assert params["totalLoss"] == 500
assert params["totalDebt"] == 0


def test_total_loss(chain, vault, strategy, gov, token):
vault.addStrategy(strategy, 10_000, 0, 2 ** 256 - 1, 1_000, {"from": gov})
token.approve(vault, 2 ** 256 - 1, {"from": gov})
vault.deposit(5000, {"from": gov})

strategy.harvest({"from": gov})
assert token.balanceOf(strategy) == 5000

# send all our tokens back to the token contract
token.transfer(token, token.balanceOf(strategy), {"from": strategy})

strategy.harvest({"from": gov})
params = vault.strategies(strategy)
assert params["totalLoss"] == 5000
assert params["totalDebt"] == 0
fp-crypto marked this conversation as resolved.
Show resolved Hide resolved
assert params["debtRatio"] == 0