-
Notifications
You must be signed in to change notification settings - Fork 179
program: isolation position init #1757
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
crispheaney
wants to merge
54
commits into
crispheaney/rm-lp
Choose a base branch
from
crispheaney/isolated-position
base: crispheaney/rm-lp
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 12 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
b58cda0
init new margin calc
crispheaney 820c232
deposit and transfer into
crispheaney 9efd808
add settle pnl
crispheaney 75b92f8
program: add withdraw
crispheaney 162fc23
add more ix
crispheaney 82463f3
add new meets withdraw req fn
crispheaney fb57e5f
enter/exit liquidation logic
crispheaney 4de579a
moar
crispheaney 085e805
start liquidation logic
crispheaney 4e7db0f
other liquidation fns
crispheaney 8e89ef4
make build work
crispheaney 8062d60
more updates
crispheaney 991dda9
always calc isolated pos
crispheaney c627c1e
rm isolated position market index logic
crispheaney a00f3a9
moar
crispheaney d435dad
program: rm the isolated position market index
crispheaney ed76b47
some tweaks
crispheaney c13a605
rm some old margin code
crispheaney 4a9aadc
tweak meets withdraw requirements
crispheaney 0d56488
rm liquidation mode changing context
crispheaney 584337b
handle liquidation id and bit flags
crispheaney 15c05ee
more liquidation changes
crispheaney adc2815
clean
crispheaney 0de7802
fix force cancel orders
crispheaney 830c7c9
update validate liquidation
crispheaney 5d09739
moar
crispheaney 7392d3e
rename is_being_liquidated
crispheaney 26960c8
start adding test
crispheaney 2ab06e3
program: add validate for liq borrow for perp pnl
crispheaney 9a56326
program: add test for isolated margin calc
crispheaney b171c23
is bankrupt test
crispheaney 2821269
fix cancel orders
crispheaney 424987f
fix set liquidation status
crispheaney b84daf1
more tweaks
crispheaney ea09842
clean up naming
crispheaney cc397f0
update last active slot for isolated position liq
crispheaney 9833303
another liquidation review
crispheaney 9cb040a
add test
crispheaney 8177496
cargo fmt --
crispheaney 9a8ec1a
tweak naming
crispheaney 6ddbaf8
add test to make sure false liquidaiton wont be triggered
crispheaney 2db2907
test meets withdraw
crispheaney 8314bbe
change is bankrupt
crispheaney 654683c
more
crispheaney 4cab732
update uses of exit isolated liquidaiton
crispheaney 6a6a150
moar
crispheaney 7c46187
moar
crispheaney 51ae2eb
reduce diff
crispheaney bae1b6b
moar
crispheaney d2f08ea
modularize some for tests
crispheaney ba8866a
add tests for the pnl for deposit liquidation
crispheaney 9fa04fa
tests for isolated position transfer
crispheaney a732348
test for update spot balance
crispheaney 91baee3
test for settle pnl
crispheaney 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -17,9 +17,7 @@ use crate::math::oracle::{is_oracle_valid_for_action, DriftAction}; | |
|
||
use crate::math::casting::Cast; | ||
use crate::math::margin::{ | ||
calculate_margin_requirement_and_total_collateral_and_liability_info, | ||
meets_maintenance_margin_requirement, meets_settle_pnl_maintenance_margin_requirement, | ||
MarginRequirementType, | ||
}; | ||
use crate::math::position::calculate_base_asset_value_with_expiry_price; | ||
use crate::math::safe_math::SafeMath; | ||
|
@@ -83,14 +81,18 @@ pub fn settle_pnl( | |
|
||
// cannot settle negative pnl this way on a user who is in liquidation territory | ||
if unrealized_pnl < 0 { | ||
let isolated_position_market_index = user.perp_positions[position_index].is_isolated().then_some(market_index); | ||
|
||
// may already be cached | ||
let meets_margin_requirement = match meets_margin_requirement { | ||
Some(meets_margin_requirement) => meets_margin_requirement, | ||
None => meets_settle_pnl_maintenance_margin_requirement( | ||
Some(meets_margin_requirement) if !isolated_position_market_index.is_some() => meets_margin_requirement, | ||
// TODO check margin for isolate position | ||
_ => meets_settle_pnl_maintenance_margin_requirement( | ||
user, | ||
perp_market_map, | ||
spot_market_map, | ||
oracle_map, | ||
isolated_position_market_index, | ||
)?, | ||
}; | ||
|
||
|
@@ -268,17 +270,43 @@ pub fn settle_pnl( | |
); | ||
} | ||
|
||
update_spot_balances( | ||
pnl_to_settle_with_user.unsigned_abs(), | ||
if pnl_to_settle_with_user > 0 { | ||
&SpotBalanceType::Deposit | ||
} else { | ||
&SpotBalanceType::Borrow | ||
}, | ||
spot_market, | ||
user.get_quote_spot_position_mut(), | ||
false, | ||
)?; | ||
if user.perp_positions[position_index].is_isolated() { | ||
let perp_position = &mut user.perp_positions[position_index]; | ||
if pnl_to_settle_with_user < 0 { | ||
let token_amount = perp_position.get_isolated_position_token_amount(spot_market)?; | ||
|
||
validate!( | ||
token_amount >= pnl_to_settle_with_user.unsigned_abs(), | ||
ErrorCode::InsufficientCollateralForSettlingPNL, | ||
"user has insufficient deposit for market {}", | ||
market_index | ||
)?; | ||
} | ||
|
||
update_spot_balances( | ||
pnl_to_settle_with_user.unsigned_abs(), | ||
if pnl_to_settle_with_user > 0 { | ||
&SpotBalanceType::Deposit | ||
} else { | ||
&SpotBalanceType::Borrow | ||
}, | ||
spot_market, | ||
perp_position, | ||
false, | ||
)?; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need to do this for |
||
update_spot_balances( | ||
pnl_to_settle_with_user.unsigned_abs(), | ||
if pnl_to_settle_with_user > 0 { | ||
&SpotBalanceType::Deposit | ||
} else { | ||
&SpotBalanceType::Borrow | ||
}, | ||
spot_market, | ||
user.get_quote_spot_position_mut(), | ||
false, | ||
)?; | ||
} | ||
|
||
update_quote_asset_amount( | ||
&mut user.perp_positions[position_index], | ||
|
@@ -324,8 +352,14 @@ pub fn settle_expired_position( | |
) -> DriftResult { | ||
validate!(!user.is_bankrupt(), ErrorCode::UserBankrupt)?; | ||
|
||
let isolated_position_market_index = if user.get_perp_position(perp_market_index)?.is_isolated() { | ||
Some(perp_market_index) | ||
} else { | ||
None | ||
}; | ||
|
||
// cannot settle pnl this way on a user who is in liquidation territory | ||
if !(meets_maintenance_margin_requirement(user, perp_market_map, spot_market_map, oracle_map)?) | ||
if !(meets_maintenance_margin_requirement(user, perp_market_map, spot_market_map, oracle_map, isolated_position_market_index)?) | ||
{ | ||
return Err(ErrorCode::InsufficientCollateralForSettlingPNL); | ||
} | ||
|
@@ -359,6 +393,7 @@ pub fn settle_expired_position( | |
Some(MarketType::Perp), | ||
Some(perp_market_index), | ||
None, | ||
true, | ||
)?; | ||
|
||
let position_index = match get_position_index(&user.perp_positions, perp_market_index) { | ||
|
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
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.
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.
need to redo margin check?