unwind()
/**
* @notice unwind the position in adverse funding rate scenarios, settle short position
* and pull funds from the margin account. Then converts the long position back
* to want.
* @dev only callable by the owner
*/
function unwind() public onlyAuthorised {
require(!isUnwind, "unwound");
isUnwind = true;
mcLiquidityPool.forceToSyncState();
// swap long asset back to want
_swap(IERC20(long).balanceOf(address(this)), long, want);
// check if the perpetual is in settlement, if it is then settle it
// otherwise unwind the fund as normal.
if (!_settle()) {
// close the short position
_closeAllPerpPositions();
// withdraw all cash in the margin account
mcLiquidityPool.withdraw(
perpetualIndex,
address(this),
getMargin()
);
}
// reset positions
positions.perpContracts = 0;
positions.margin = getMargin();
positions.unitAccumulativeFunding = getUnitAccumulativeFunding();
emit StrategyUnwind(IERC20(want).balanceOf(address(this)));
}Last updated