From 2a0ba02dc4d98d96cfab1f6e830b3fedbc050ba1 Mon Sep 17 00:00:00 2001 From: Steffel <2143646+steffenix@users.noreply.github.com> Date: Thu, 27 May 2021 19:12:40 +0200 Subject: [PATCH] fix: revert changes on debt outstanding (#381) * fix: do not use totalDebt to calculate debtLimit * feat: test failure * docs: add comment * refactor: move test Co-authored-by: Poolpi Tako --- contracts/Vault.vy | 7 +++++-- tests/functional/strategy/test_misc.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/contracts/Vault.vy b/contracts/Vault.vy index 801ea546..a2f7b437 100644 --- a/contracts/Vault.vy +++ b/contracts/Vault.vy @@ -964,6 +964,8 @@ def _reportLoss(strategy: address, loss: uint256): # Also, make sure we reduce our trust with the strategy by the amount of loss if self.debtRatio != 0: # if vault with single strategy that is set to EmergencyOne + # NOTE: The context to this calculation is different than the calculation in `_reportLoss`, + # this calculation intentionally approximates via `totalDebt` to avoid manipulatable results ratio_change: uint256 = min( # NOTE: This calculation isn't 100% precise, the adjustment is ~10%-20% more severe due to EVM math loss * self.debtRatio / self.totalDebt, @@ -1437,10 +1439,11 @@ def _debtOutstanding(strategy: address) -> uint256: # See note on `debtOutstanding()`. if self.debtRatio == 0: return self.strategies[strategy].totalDebt + strategy_debtLimit: uint256 = ( self.strategies[strategy].debtRatio - * self.totalDebt - / self.debtRatio + * self._totalAssets() + / MAX_BPS ) strategy_totalDebt: uint256 = self.strategies[strategy].totalDebt diff --git a/tests/functional/strategy/test_misc.py b/tests/functional/strategy/test_misc.py index 10f841c7..60a55745 100644 --- a/tests/functional/strategy/test_misc.py +++ b/tests/functional/strategy/test_misc.py @@ -186,3 +186,13 @@ def test_set_metadataURI(gov, strategy, strategist, rando): assert strategy.metadataURI() == "ipfs://test3" with brownie.reverts("!authorized"): strategy.setMetadataURI("ipfs://fake", {"from": rando}) + + +def test_reduce_debt_ratio(strategy, vault, gov, chain): + chain.sleep(1) + strategy.harvest({"from": gov}) + assert vault.strategies(strategy).dict()["totalDebt"] > 0 + old_debt_ratio = vault.strategies(strategy).dict()["debtRatio"] + vault.updateStrategyDebtRatio(strategy, old_debt_ratio // 2, {"from": gov}) + + assert vault.debtOutstanding(strategy) > 0