Skip to content

Commit

Permalink
fix: inconsistent Liquidations in BaseStrategy Emergency Exit Mode (#311
Browse files Browse the repository at this point in the history
)

* fix: inconsistent Liquidations in BaseStrategy Emergency Exit Mode

* refactor: move code back into harvest
  • Loading branch information
Steffel committed May 16, 2021
1 parent 35cf726 commit 6159b87
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
23 changes: 14 additions & 9 deletions contracts/BaseStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,19 @@ abstract contract BaseStrategy {
* liquidation. If there is a difference between them, `_loss` indicates whether the
* difference is due to a realized loss, or if there is some other sitution at play
* (e.g. locked funds) where the amount made available is less than what is needed.
* This function is used during emergency exit instead of `prepareReturn()` to
* liquidate all of the Strategy's positions back to the Vault.
*
* NOTE: The invariant `_liquidatedAmount + _loss <= _amountNeeded` should always be maintained
*/
function liquidatePosition(uint256 _amountNeeded) internal virtual returns (uint256 _liquidatedAmount, uint256 _loss);

/**
* Liquidate everything and returns the amount that got freed.
* This function is used during emergency exit instead of `prepareReturn()` to
* liquidate all of the Strategy's positions back to the Vault.
*/

function liquidateAllPositions() internal virtual returns (uint256 _amountFreed);

/**
* @notice
* Provide a signal to the keeper that `tend()` should be called. The
Expand Down Expand Up @@ -720,14 +726,13 @@ abstract contract BaseStrategy {
uint256 debtPayment = 0;
if (emergencyExit) {
// Free up as much capital as possible
uint256 totalAssets = estimatedTotalAssets();
// NOTE: use the larger of total assets or debt outstanding to book losses properly
(debtPayment, loss) = liquidatePosition(totalAssets > debtOutstanding ? totalAssets : debtOutstanding);
// NOTE: take up any remainder here as profit
if (debtPayment > debtOutstanding) {
profit = debtPayment.sub(debtOutstanding);
debtPayment = debtOutstanding;
uint256 amountFreed = liquidateAllPositions();
if (amountFreed < debtOutstanding) {
loss = debtOutstanding.sub(amountFreed);
} else if (amountFreed > debtOutstanding) {
profit = amountFreed.sub(debtOutstanding);
}
debtPayment = debtOutstanding.sub(loss);
} else {
// Free up returns for Vault to pull
(profit, loss, debtPayment) = prepareReturn(debtOutstanding);
Expand Down
5 changes: 5 additions & 0 deletions contracts/test/TestStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,9 @@ contract TestStrategy is BaseStrategyInitializable {
protected[0] = protectedToken;
return protected;
}

function liquidateAllPositions() internal override returns (uint256 amountFreed) {
uint256 totalAssets = want.balanceOf(address(this));
amountFreed = totalAssets;
}
}

0 comments on commit 6159b87

Please sign in to comment.