-
Notifications
You must be signed in to change notification settings - Fork 209
program: add max margin ratio per position #1847
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d58dce4
program: add max margin ratio perp position
crispheaney caa26db
program: test for custom perp position margin ratio
crispheaney 7dbca5c
test
crispheaney 16f9208
make max margin ratio persist
crispheaney 0a4bc12
update user level max margin ratio
crispheaney f19f3b5
naming nit
crispheaney 18e2dd7
Merge branch 'master' into crispheaney/per-position-max-margin-ratio
crispheaney 4bd6baa
CHANGELOG
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
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
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
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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ use crate::math::constants::{ | |
| }; | ||
| use crate::math::position::calculate_base_asset_value_and_pnl_with_oracle_price; | ||
|
|
||
| use crate::MARGIN_PRECISION; | ||
| use crate::{validate, PRICE_PRECISION_I128}; | ||
| use crate::{validation, PRICE_PRECISION_I64}; | ||
|
|
||
|
|
@@ -27,6 +28,7 @@ use crate::state::spot_market_map::SpotMarketMap; | |
| use crate::state::user::{MarketType, OrderFillSimulation, PerpPosition, User}; | ||
| use num_integer::Roots; | ||
| use std::cmp::{max, min, Ordering}; | ||
| use std::collections::BTreeMap; | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
@@ -535,6 +537,12 @@ pub fn calculate_margin_requirement_and_total_collateral_and_liability_info( | |
| 0, | ||
| )?; | ||
|
|
||
| let perp_position_custom_margin_ratio = if context.margin_type == MarginRequirementType::Initial { | ||
| market_position.max_margin_ratio as u32 | ||
| } else { | ||
| 0_u32 | ||
| }; | ||
|
|
||
| let ( | ||
| perp_margin_requirement, | ||
| weighted_pnl, | ||
|
|
@@ -547,7 +555,7 @@ pub fn calculate_margin_requirement_and_total_collateral_and_liability_info( | |
| oracle_price_data, | ||
| &strict_quote_price, | ||
| context.margin_type, | ||
| user_custom_margin_ratio, | ||
| user_custom_margin_ratio.max(perp_position_custom_margin_ratio), | ||
| user_high_leverage_mode, | ||
| calculation.track_open_orders_fraction(), | ||
| )?; | ||
|
|
@@ -884,6 +892,42 @@ pub fn validate_spot_margin_trading( | |
| Ok(()) | ||
| } | ||
|
|
||
| pub fn get_margin_calculation_for_disable_high_leverage_mode( | ||
| user: &mut User, | ||
| perp_market_map: &PerpMarketMap, | ||
| spot_market_map: &SpotMarketMap, | ||
| oracle_map: &mut OracleMap, | ||
| ) -> DriftResult<MarginCalculation> { | ||
| let custom_margin_ratio_before = user.max_margin_ratio; | ||
|
|
||
|
|
||
| let mut perp_position_max_margin_ratio_map = BTreeMap::new(); | ||
| for (index, position) in user.perp_positions.iter_mut().enumerate() { | ||
| if position.max_margin_ratio == 0 { | ||
| continue; | ||
| } | ||
|
|
||
| perp_position_max_margin_ratio_map.insert(index, position.max_margin_ratio); | ||
| position.max_margin_ratio = 0; | ||
| } | ||
|
|
||
| let margin_buffer = MARGIN_PRECISION / 100; // 1% buffer | ||
| let margin_calc = calculate_margin_requirement_and_total_collateral_and_liability_info( | ||
| user, | ||
| perp_market_map, | ||
| spot_market_map, | ||
| oracle_map, | ||
| MarginContext::standard(MarginRequirementType::Initial).margin_buffer(margin_buffer), | ||
| )?; | ||
|
|
||
| user.max_margin_ratio = custom_margin_ratio_before; | ||
| for (index, position) in perp_position_max_margin_ratio_map.iter() { | ||
|
||
| user.perp_positions[*index].max_margin_ratio = *position; | ||
| } | ||
|
|
||
| Ok(margin_calc) | ||
| } | ||
|
|
||
| pub fn calculate_user_equity( | ||
| user: &User, | ||
| perp_market_map: &PerpMarketMap, | ||
|
|
||
Oops, something went wrong.
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.