Skip to content
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

program: add test for max order size with imf #696

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion programs/drift/src/math/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,6 @@ pub fn calculate_quote_asset_amount_swapped(
) -> DriftResult<u128> {
let mut quote_asset_reserve_change = match swap_direction {
SwapDirection::Add => quote_asset_reserve_before.safe_sub(quote_asset_reserve_after)?,

SwapDirection::Remove => quote_asset_reserve_after.safe_sub(quote_asset_reserve_before)?,
};

Expand Down
5 changes: 1 addition & 4 deletions programs/drift/src/math/cp_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ pub fn adjust_k_cost(
market_clone.amm.base_asset_amount_with_amm,
0,
&market_clone.amm,
false,
)?;

update_k(&mut market_clone, update_k_result)?;
Expand All @@ -188,7 +187,6 @@ pub fn adjust_k_cost(
market_clone.amm.base_asset_amount_with_amm,
current_net_market_value,
&market_clone.amm,
false,
)?;

Ok(cost)
Expand All @@ -203,15 +201,14 @@ pub fn adjust_k_cost_and_update(
) -> DriftResult<i128> {
// Find the net market value before adjusting k
let current_net_market_value =
calculate_base_asset_value(market.amm.base_asset_amount_with_amm, &market.amm, false)?;
calculate_base_asset_value(market.amm.base_asset_amount_with_amm, &market.amm)?;

update_k(market, update_k_result)?;

let (_new_net_market_value, cost) = calculate_base_asset_value_and_pnl(
market.amm.base_asset_amount_with_amm,
current_net_market_value,
&market.amm,
false,
)?;

Ok(cost)
Expand Down
106 changes: 58 additions & 48 deletions programs/drift/src/math/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,37 +825,41 @@ pub fn calculate_max_perp_order_size(
);
}

let mut order_size = free_collateral
.safe_sub(OPEN_ORDER_MARGIN_REQUIREMENT.cast()?)?
.safe_mul(BASE_PRECISION_I128 / QUOTE_PRECISION_I128)?
.safe_mul(MARGIN_PRECISION_U128.cast()?)?
.safe_div(margin_ratio.cast()?)?
.safe_mul(PRICE_PRECISION_I128)?
.safe_div(oracle_price_data_price.cast()?)?
.safe_mul(PRICE_PRECISION_I128)?
.safe_div(quote_oracle_price.cast()?)?
.cast::<u64>()?;

let updated_margin_ratio = perp_market
.get_margin_ratio(
worst_case_base_asset_amount
.unsigned_abs()
.safe_add(order_size.cast()?)?,
MarginRequirementType::Initial,
)?
.max(user_custom_margin_ratio);

if updated_margin_ratio != margin_ratio {
order_size = free_collateral
let calculate_order_size_and_margin_ratio = |margin_ratio: u32| {
let new_order_size = free_collateral
.safe_sub(OPEN_ORDER_MARGIN_REQUIREMENT.cast()?)?
.safe_mul(BASE_PRECISION_I128 / QUOTE_PRECISION_I128)?
.safe_mul(MARGIN_PRECISION_U128.cast()?)?
.safe_div(updated_margin_ratio.cast()?)?
.safe_div(margin_ratio.cast()?)?
.safe_mul(PRICE_PRECISION_I128)?
.safe_div(oracle_price_data_price.cast()?)?
.safe_mul(PRICE_PRECISION_I128)?
.safe_div(quote_oracle_price.cast()?)?
.cast::<u64>()?;

let new_margin_ratio = perp_market
.get_margin_ratio(
worst_case_base_asset_amount
.unsigned_abs()
.safe_add(new_order_size.cast()?)?,
MarginRequirementType::Initial,
)?
.max(user_custom_margin_ratio);

Ok((new_order_size, new_margin_ratio))
};

let mut order_size = 0_u64;
let mut updated_margin_ratio = margin_ratio;
for _ in 0..6 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using iter method like in sdk? jw, is this going to always under estimate the max (to ensure it goes through?)

let (new_order_size, new_margin_ratio) =
calculate_order_size_and_margin_ratio(updated_margin_ratio)?;
order_size = new_order_size;
updated_margin_ratio = new_margin_ratio;

if new_margin_ratio == margin_ratio {
break;
}
}

standardize_base_asset_amount(
Expand Down Expand Up @@ -1056,37 +1060,43 @@ pub fn calculate_max_spot_order_size(

let precision_increase = 10i128.pow(spot_market.decimals - 6);

let mut order_size = free_collateral
.safe_sub(OPEN_ORDER_MARGIN_REQUIREMENT.cast()?)?
.safe_mul(precision_increase)?
.safe_mul(SPOT_WEIGHT_PRECISION.cast()?)?
.safe_div(free_collateral_delta.cast()?)?
.safe_mul(PRICE_PRECISION_I128)?
.safe_div(max_oracle_price.cast()?)?
.cast::<u64>()?;

// increasing the worst case token amount with new order size may increase margin ration,
// so need to recalculate free collateral delta with updated margin ratio
let updated_free_collateral_delta = calculate_free_collateral_delta_for_spot(
&spot_market,
worst_case_token_amount
.unsigned_abs()
.safe_add(order_size.cast()?)?,
&strict_oracle_price,
direction,
user_custom_liability_weight,
user_custom_asset_weight,
)?;

if updated_free_collateral_delta != free_collateral_delta {
order_size = free_collateral
let calculate_order_size_and_free_collateral_delta = |free_collateral_delta: u32| {
let new_order_size = free_collateral
.safe_sub(OPEN_ORDER_MARGIN_REQUIREMENT.cast()?)?
.safe_mul(precision_increase)?
.safe_mul(SPOT_WEIGHT_PRECISION.cast()?)?
.safe_div(updated_free_collateral_delta.cast()?)?
.safe_div(free_collateral_delta.cast()?)?
.safe_mul(PRICE_PRECISION_I128)?
.safe_div(max_oracle_price.cast()?)?
.cast::<u64>()?;

// increasing the worst case token amount with new order size may increase margin ratio,
// so need to recalculate free collateral delta with updated margin ratio
let new_free_collateral_delta = calculate_free_collateral_delta_for_spot(
&spot_market,
worst_case_token_amount
.unsigned_abs()
.safe_add(new_order_size.cast()?)?,
&strict_oracle_price,
direction,
user_custom_liability_weight,
user_custom_asset_weight,
)?;

Ok((new_order_size, new_free_collateral_delta))
};

let mut order_size = 0_u64;
let mut updated_free_collateral_delta = free_collateral_delta;
for _ in 0..6 {
let (new_order_size, new_free_collateral_delta) =
calculate_order_size_and_free_collateral_delta(updated_free_collateral_delta)?;
order_size = new_order_size;
updated_free_collateral_delta = new_free_collateral_delta;

if updated_free_collateral_delta == free_collateral_delta {
break;
}
}

standardize_base_asset_amount(
Expand Down
Loading
Loading