Skip to content

Commit

Permalink
address some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Jan 6, 2024
1 parent a923ed5 commit 57294b4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 81 deletions.
137 changes: 67 additions & 70 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2831,11 +2831,12 @@ pub fn attempt_burn_user_lp_shares_for_risk_reduction(
state: &State,
user: &mut User,
margin_calc: MarginCalculation,
user_key: &Pubkey,
user_key: Pubkey,
perp_market_map: &PerpMarketMap,
spot_market_map: &SpotMarketMap,
oracle_map: &mut OracleMap,
clock: &Clock,
market_index: u16,
) -> DriftResult {
let now = clock.unix_timestamp;

Check warning on line 2841 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2841

Added line #L2841 was not covered by tests
// attempt to burn lp shares if user has a custom margin ratio set and its breached with orders
Expand All @@ -2844,97 +2845,93 @@ pub fn attempt_burn_user_lp_shares_for_risk_reduction(
now.safe_sub(user.last_add_perp_lp_shares_ts)?;
// avoid spamming update if orders have already been set
if time_since_last_liquidity_change >= state.lp_cooldown_time.cast()? {

Check warning on line 2847 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2847

Added line #L2847 was not covered by tests
let set_reduce_only_orders = true; // todo
for position_index in 0..user.perp_positions.len() {
let market_index = user.perp_positions[position_index].market_index;
_burn_user_lp_shares_for_risk_reduction(
state,
user,
user_key,
market_index,
perp_market_map,
spot_market_map,
oracle_map,
clock,
set_reduce_only_orders,
)?;
}
burn_user_lp_shares_for_risk_reduction(
state,
user,
user_key,

Check warning on line 2851 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2851

Added line #L2851 was not covered by tests
market_index,
perp_market_map,
spot_market_map,
oracle_map,
clock,
)?;
user.last_add_perp_lp_shares_ts = now;

Check warning on line 2858 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2858

Added line #L2858 was not covered by tests
}
}

Ok(())

Check warning on line 2862 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2862

Added line #L2862 was not covered by tests
}

pub fn _burn_user_lp_shares_for_risk_reduction(
pub fn burn_user_lp_shares_for_risk_reduction(

Check warning on line 2865 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2865

Added line #L2865 was not covered by tests
state: &State,
user: &mut User,
user_key: &Pubkey,
user_key: Pubkey,
market_index: u16,
perp_market_map: &PerpMarketMap,
spot_market_map: &SpotMarketMap,
oracle_map: &mut OracleMap,
clock: &Clock,
set_reduce_only_orders: bool,
) -> DriftResult {
let perp_position = user.get_perp_position_mut(market_index)?;
if perp_position.is_lp() {
let lp_shares = perp_position.lp_shares;
let market_index = perp_position.market_index;
{
let market = perp_market_map.get_ref(&market_index)?;
let oracle_price_data = oracle_map.get_price_data(&market.amm.oracle)?;
let position_index = get_position_index(&user.perp_positions, market_index)?;
let is_lp = user.perp_positions[position_index].is_lp();
if !is_lp {
return Ok(());

Check warning on line 2878 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2875-L2878

Added lines #L2875 - L2878 were not covered by tests
}

let oracle_price = if market.status == MarketStatus::Settlement {
market.expiry_price
} else {
oracle_price_data.price
};
let lp_shares = user.perp_positions[position_index].lp_shares;

Check warning on line 2881 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2881

Added line #L2881 was not covered by tests

let (position_delta, pnl) = burn_lp_shares(
perp_position,
perp_market_map.get_ref_mut(&market_index)?.deref_mut(),
lp_shares.safe_div(3)?.max(market.amm.order_step_size),
oracle_price,
)?;
let mut market = perp_market_map.get_ref_mut(&market_index)?;
let oracle_price_data = oracle_map.get_price_data(&market.amm.oracle)?;

Check warning on line 2884 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2883-L2884

Added lines #L2883 - L2884 were not covered by tests

// emit LP record for shares removed
emit_stack::<_, { LPRecord::SIZE }>(LPRecord {
ts: clock.unix_timestamp,
action: LPAction::RemoveLiquidity,
user: *user_key,
n_shares: lp_shares,
market_index: perp_position.market_index,
delta_base_asset_amount: position_delta.base_asset_amount,
delta_quote_asset_amount: position_delta.quote_asset_amount,
pnl,
})?;
}
let oracle_price = if market.status == MarketStatus::Settlement {
market.expiry_price

Check warning on line 2887 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2886-L2887

Added lines #L2886 - L2887 were not covered by tests
} else {
oracle_price_data.price

Check warning on line 2889 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2889

Added line #L2889 was not covered by tests
};

if set_reduce_only_orders {
let market = perp_market_map.get_ref(&market_index)?;
let order_step_size = market.amm.order_step_size;

Check warning on line 2892 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2892

Added line #L2892 was not covered by tests
let (position_delta, pnl) = burn_lp_shares(
&mut user.perp_positions[position_index],
&mut market,
lp_shares.safe_div(3)?.max(order_step_size),
oracle_price,

Check warning on line 2897 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2894-L2897

Added lines #L2894 - L2897 were not covered by tests
)?;

let direction_to_close = perp_position.get_direction_to_close();
// emit LP record for shares removed
emit_stack::<_, { LPRecord::SIZE }>(LPRecord {
ts: clock.unix_timestamp,

Check warning on line 2902 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2901-L2902

Added lines #L2901 - L2902 were not covered by tests
action: LPAction::RemoveLiquidity,
user: user_key,

Check warning on line 2904 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2904

Added line #L2904 was not covered by tests
n_shares: lp_shares,
market_index,

Check warning on line 2906 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2906

Added line #L2906 was not covered by tests
delta_base_asset_amount: position_delta.base_asset_amount,
delta_quote_asset_amount: position_delta.quote_asset_amount,
pnl,
})?;

let direction_to_close = user.perp_positions[position_index].get_direction_to_close();

Check warning on line 2912 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2912

Added line #L2912 was not covered by tests

let params = OrderParams::get_aggressive_close_params(
&market,

Check warning on line 2915 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2915

Added line #L2915 was not covered by tests
direction_to_close,
user.perp_positions[position_index]

Check warning on line 2917 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2917

Added line #L2917 was not covered by tests
.base_asset_amount
.unsigned_abs(),
)?;

let params = OrderParams::get_aggressive_close_params(
&market,
direction_to_close,
perp_position.base_asset_amount.unsigned_abs(),
)?;
drop(market);

Check warning on line 2922 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2922

Added line #L2922 was not covered by tests

controller::orders::place_perp_order(
state,
user,
*user_key,
perp_market_map,
spot_market_map,
oracle_map,
clock,
params,
PlaceOrderOptions::default(),
)?;
}
}
controller::orders::place_perp_order(
state,
user,
user_key,

Check warning on line 2927 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2927

Added line #L2927 was not covered by tests
perp_market_map,
spot_market_map,
oracle_map,
clock,
params,
PlaceOrderOptions::default(),

Check warning on line 2933 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2932-L2933

Added lines #L2932 - L2933 were not covered by tests
)?;

Ok(())

Check warning on line 2936 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L2936

Added line #L2936 was not covered by tests
}
Expand Down
27 changes: 16 additions & 11 deletions programs/drift/src/controller/pnl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,24 @@ pub fn settle_expired_position(
perp_market_map,
spot_market_map,
oracle_map,
MarginContext::standard(MarginRequirementType::Initial),
MarginContext::standard(MarginRequirementType::Initial).track_open_orders_fraction()?,

Check warning on line 271 in programs/drift/src/controller/pnl.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/pnl.rs#L271

Added line #L271 was not covered by tests
)?;

attempt_burn_user_lp_shares_for_risk_reduction(
state,
user,
margin_calc,
user_key,
perp_market_map,
spot_market_map,
oracle_map,
clock,
)?;
if !margin_calc.meets_margin_requirement() {

Check warning on line 274 in programs/drift/src/controller/pnl.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/pnl.rs#L274

Added line #L274 was not covered by tests
attempt_burn_user_lp_shares_for_risk_reduction(
state,
user,
margin_calc,
*user_key,

Check warning on line 279 in programs/drift/src/controller/pnl.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/pnl.rs#L278-L279

Added lines #L278 - L279 were not covered by tests
perp_market_map,
spot_market_map,
oracle_map,
clock,
perp_market_index,
)?;

return Ok(());

Check warning on line 287 in programs/drift/src/controller/pnl.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/pnl.rs#L287

Added line #L287 was not covered by tests
}
} else {
// cannot settle pnl this way on a user who is in liquidation territory
if !(meets_maintenance_margin_requirement(
Expand Down

0 comments on commit 57294b4

Please sign in to comment.