From 415b783d7e58efa5465a8592c454b50df86e04da Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Thu, 18 Jan 2024 11:57:19 -0500 Subject: [PATCH] program: fix risk reduction dlp burn from not being step size --- programs/drift/src/controller/lp.rs | 11 ++++++++--- programs/drift/src/controller/orders.rs | 8 ++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/programs/drift/src/controller/lp.rs b/programs/drift/src/controller/lp.rs index 49ac59987..3bf13cba9 100644 --- a/programs/drift/src/controller/lp.rs +++ b/programs/drift/src/controller/lp.rs @@ -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}; @@ -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)?; + // 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 + } else { let market = perp_market_map.get_ref(&market_index)?; crate::math::orders::standardize_base_asset_amount( shares_to_burn.cast()?, @@ -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]; validate!( position.lp_shares >= shares_to_burn, diff --git a/programs/drift/src/controller/orders.rs b/programs/drift/src/controller/orders.rs index 2fd0a31dd..850e08c1c 100644 --- a/programs/drift/src/controller/orders.rs +++ b/programs/drift/src/controller/orders.rs @@ -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 = + 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, )?; @@ -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,