-
Notifications
You must be signed in to change notification settings - Fork 0
9 - emergencyWithdraw override function to withdraw funds after strategy shutdown #44
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
Open
mcclurejt
wants to merge
3
commits into
main
Choose a base branch
from
mcclurejt/fix/emergencywithdraw-override
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| import { IMultiToken } from "hyperdrive/contracts/src/interfaces/IMultiToken.sol"; | ||
| import { AssetId } from "hyperdrive/contracts/src/libraries/AssetId.sol"; | ||
| import { IEverlongStrategy } from "../../../contracts/interfaces/IEverlongStrategy.sol"; | ||
| import { EVERLONG_STRATEGY_KIND, EVERLONG_VERSION } from "../../../contracts/libraries/Constants.sol"; | ||
| import { EverlongTest } from "../EverlongTest.sol"; | ||
|
|
||
| /// @dev Tests emergency withdraw functionality. | ||
| contract TestEmergencyWithdraw is EverlongTest { | ||
| function test_call_from_non_management_failure() external { | ||
| // Shut down the strategy. | ||
| vm.startPrank(strategy.emergencyAdmin()); | ||
| strategy.shutdownStrategy(); | ||
| vm.stopPrank(); | ||
|
|
||
| // Ensure calling emergencyWithdraw from a random address fails. | ||
| vm.startPrank(alice); | ||
| vm.expectRevert(); | ||
| strategy.emergencyWithdraw(type(uint256).max); | ||
| vm.stopPrank(); | ||
|
|
||
| // Ensure calling emergencyWithdraw from the keeper address fails. | ||
| vm.startPrank(keeper); | ||
| vm.expectRevert(); | ||
| strategy.emergencyWithdraw(type(uint256).max); | ||
| vm.stopPrank(); | ||
|
|
||
| // Ensure calling emergencyWithdraw from the keeper contract address | ||
| // fails. | ||
| vm.startPrank(address(keeperContract)); | ||
| vm.expectRevert(); | ||
| strategy.emergencyWithdraw(type(uint256).max); | ||
| vm.stopPrank(); | ||
| } | ||
|
|
||
| /// @dev Ensure strategy can be shutdown when it has no positions. | ||
| function test_no_positions_open() external { | ||
| // Ensure the strategy has no open positions. | ||
| assertEq(IEverlongStrategy(address(strategy)).positionCount(), 0); | ||
|
|
||
| // Shut down the strategy and call `emergencyWithdraw`. | ||
| vm.startPrank(strategy.emergencyAdmin()); | ||
| strategy.shutdownStrategy(); | ||
| strategy.emergencyWithdraw(type(uint256).max); | ||
| vm.stopPrank(); | ||
| } | ||
|
|
||
| /// @dev Ensure strategy can be shutdown when it has positions. | ||
| function test_positions_open() external { | ||
| // Deposit into the vault and "rebalance" to open a position in the | ||
| // strategy. | ||
| depositVault(100e18, alice, true); | ||
| rebalance(); | ||
|
|
||
| // Ensure the strategy has one open position. | ||
| assertEq(IEverlongStrategy(address(strategy)).positionCount(), 1); | ||
|
|
||
| // Get the position. | ||
| IEverlongStrategy.EverlongPosition memory position = IEverlongStrategy( | ||
| address(strategy) | ||
| ).positionAt(0); | ||
|
|
||
| // Record the strategy's balance of longs for that position. | ||
| uint256 strategyLongBalance = IMultiToken(hyperdrive).balanceOf( | ||
| AssetId.encodeAssetId( | ||
| AssetId.AssetIdPrefix.Long, | ||
| uint256(position.maturityTime) | ||
| ), | ||
| address(strategy) | ||
| ); | ||
|
|
||
| // Shut down the strategy and call `emergencyWithdraw`. | ||
| vm.startPrank(strategy.emergencyAdmin()); | ||
| strategy.shutdownStrategy(); | ||
| strategy.emergencyWithdraw(type(uint256).max); | ||
| vm.stopPrank(); | ||
|
|
||
| // Ensure the emergency admin address's long balance matches the strategy's | ||
| // long balance prior to the emergency withdraw. | ||
| assertEq( | ||
| strategyLongBalance, | ||
| IMultiToken(hyperdrive).balanceOf( | ||
| AssetId.encodeAssetId( | ||
| AssetId.AssetIdPrefix.Long, | ||
| uint256(position.maturityTime) | ||
| ), | ||
| address(strategy.emergencyAdmin()) | ||
| ) | ||
| ); | ||
|
|
||
| // Ensure the strategy has no positions left. | ||
| assertEq(IEverlongStrategy(address(strategy)).positionCount(), 0); | ||
| } | ||
|
|
||
| function test_maxBondAmount() external { | ||
| // Deposit into the vault and "rebalance" to open a position in the | ||
| // strategy. | ||
| depositVault(100e18, alice, true); | ||
| rebalance(); | ||
|
|
||
| // Ensure the strategy has one open position. | ||
| assertEq(IEverlongStrategy(address(strategy)).positionCount(), 1); | ||
|
|
||
| // Get the position. | ||
| IEverlongStrategy.EverlongPosition memory position = IEverlongStrategy( | ||
| address(strategy) | ||
| ).positionAt(0); | ||
|
|
||
| // Record the strategy's balance of longs for that position. | ||
| uint256 strategyLongBalance = IMultiToken(hyperdrive).balanceOf( | ||
| AssetId.encodeAssetId( | ||
| AssetId.AssetIdPrefix.Long, | ||
| uint256(position.maturityTime) | ||
| ), | ||
| address(strategy) | ||
| ); | ||
|
|
||
| // Shut down the strategy and call `emergencyWithdraw` with | ||
| // `_maxBondAmount` set to a value less than the position's bond amount. | ||
| vm.startPrank(strategy.emergencyAdmin()); | ||
| strategy.shutdownStrategy(); | ||
| strategy.emergencyWithdraw(strategyLongBalance - 1); | ||
| vm.stopPrank(); | ||
|
|
||
| // Ensure the emergency admin address's long balance is zero since no | ||
| // positions were transferred. | ||
| assertEq( | ||
| 0, | ||
| IMultiToken(hyperdrive).balanceOf( | ||
| AssetId.encodeAssetId( | ||
| AssetId.AssetIdPrefix.Long, | ||
| uint256(position.maturityTime) | ||
| ), | ||
| address(strategy.emergencyAdmin()) | ||
| ) | ||
| ); | ||
|
|
||
| // Ensure the strategy still has one open position. | ||
| assertEq(IEverlongStrategy(address(strategy)).positionCount(), 1); | ||
|
|
||
| // Call `emergencyWithdraw` with `_maxBondAmount` set to a value more | ||
| // than the position's bond amount. | ||
| vm.startPrank(strategy.emergencyAdmin()); | ||
| strategy.emergencyWithdraw(strategyLongBalance + 1); | ||
| vm.stopPrank(); | ||
|
|
||
| // Ensure the emergency admin address's long balance matches the strategy's | ||
| // long balance prior to the emergency withdraw. | ||
| assertEq( | ||
| strategyLongBalance, | ||
| IMultiToken(hyperdrive).balanceOf( | ||
| AssetId.encodeAssetId( | ||
| AssetId.AssetIdPrefix.Long, | ||
| uint256(position.maturityTime) | ||
| ), | ||
| address(strategy.emergencyAdmin()) | ||
| ) | ||
| ); | ||
|
|
||
| // Ensure the strategy has no positions left. | ||
| assertEq(IEverlongStrategy(address(strategy)).positionCount(), 0); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there might be a lot of open positions and closing them is not cheap. worst case, you can't fit them in a single block. consider just closing positions until at least
amountparameter is reached (can still fully close the individual positions, something likewhile (!empty && closed < amount) { ... closed += bondAmount }. And then change the natspec that amount is actually a bond amount).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the change as described with one difference...
I found it more straightforward to have the parameter be a hard limit, meaning that the amount of bonds transferred is guaranteed not to exceed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question: according to the current implementation, the limit is put on the bond amount, and the current position to work on is
position = _portfolio.head();. Then, is there a possibility that once a very big position holding more than themaxBondAmountstands in the way, you won't be able to get around it even though there are still other smaller positions could have been able to be closed?If the concern is on the gas cost & block size limit, maybe imposing the limit on the # of positions closed works better than using the bond amount?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this queue is ordered and you can inspect it from off-chain and then see how much needs to be closed. I think in practice it doesn't make much difference if you use
maxBondAmountfor the bond amount or number of positions to close.I just felt like bond amount was more flexible.
Maybe you're suggesting passing in an array of maturities to close so you can skip the head, but unfortunately this
uint256parameter is given by yearn's API and you can't change it.