Skip to content

Commit

Permalink
program: fix risk reduction dlp burn from not being step size
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Jan 18, 2024
1 parent 443e16c commit 415b783
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 8 additions & 3 deletions programs/drift/src/controller/lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::{msg, Pubkey};

use crate::bn::U192;
use crate::controller;
use crate::controller::position::PositionDelta;
use crate::controller::position::{get_position_index, PositionDelta};
use crate::controller::position::{update_position_and_market, update_quote_asset_amount};
use crate::emit;
use crate::error::{DriftResult, ErrorCode};
Expand Down Expand Up @@ -357,8 +357,13 @@ pub fn remove_perp_lp_shares(
market_index: u16,
now: i64,
) -> DriftResult<()> {
let position_index = get_position_index(&user.perp_positions, market_index)?;

Check warning on line 360 in programs/drift/src/controller/lp.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/lp.rs#L360

Added line #L360 was not covered by tests

// standardize n shares to burn
let shares_to_burn: u64 = {
// account for issue where lp shares are smaller than step size
let shares_to_burn = if user.perp_positions[position_index].lp_shares == shares_to_burn {
shares_to_burn

Check warning on line 365 in programs/drift/src/controller/lp.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/lp.rs#L364-L365

Added lines #L364 - L365 were not covered by tests
} else {
let market = perp_market_map.get_ref(&market_index)?;
crate::math::orders::standardize_base_asset_amount(
shares_to_burn.cast()?,
Expand All @@ -382,7 +387,7 @@ pub fn remove_perp_lp_shares(

controller::funding::settle_funding_payment(user, &user_key, &mut market, now)?;

let position = user.get_perp_position_mut(market_index)?;
let position = &mut user.perp_positions[position_index];

Check warning on line 390 in programs/drift/src/controller/lp.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/lp.rs#L390

Added line #L390 was not covered by tests

validate!(
position.lp_shares >= shares_to_burn,
Expand Down
8 changes: 6 additions & 2 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2890,10 +2890,14 @@ pub fn burn_user_lp_shares_for_risk_reduction(
};

let order_step_size = market.amm.order_step_size;

let lp_shares_to_burn =

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L2894 was not covered by tests
standardize_base_asset_amount(lp_shares, order_step_size)?.max(lp_shares);

let (position_delta, pnl) = burn_lp_shares(
&mut user.perp_positions[position_index],
&mut market,
lp_shares.safe_div(3)?.max(order_step_size),
lp_shares_to_burn,
oracle_price,
)?;

Expand All @@ -2902,7 +2906,7 @@ pub fn burn_user_lp_shares_for_risk_reduction(
ts: clock.unix_timestamp,
action: LPAction::RemoveLiquidity,
user: user_key,
n_shares: lp_shares,
n_shares: lp_shares_to_burn,
market_index,
delta_base_asset_amount: position_delta.base_asset_amount,
delta_quote_asset_amount: position_delta.quote_asset_amount,
Expand Down

0 comments on commit 415b783

Please sign in to comment.