From 97971ddfbd12ca62678fd8da2f6a299b129352a8 Mon Sep 17 00:00:00 2001 From: lil perp Date: Thu, 18 Jan 2024 12:37:09 -0500 Subject: [PATCH] program: fix risk reduction dlp burn from not being step size (#826) * program: fix risk reduction dlp burn from not being step size * CHANGELOG * fix lp burn --- CHANGELOG.md | 2 ++ programs/drift/src/controller/lp.rs | 11 ++++++++--- programs/drift/src/controller/orders.rs | 10 +++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a15f5a399..1614e6479 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixes +- program: standardize lp shares in attempt_burn_user_lp_shares_for_risk_reduction ([#826](https://github.com/drift-labs/protocol-v2/pull/826)) + ### Breaking ## [2.54.0] - 2023-01-15 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..c0d12deb6 100644 --- a/programs/drift/src/controller/orders.rs +++ b/programs/drift/src/controller/orders.rs @@ -1,6 +1,6 @@ use std::cell::RefMut; use std::collections::BTreeMap; -use std::ops::DerefMut; +use std::ops::{DerefMut, Div}; use std::u64; use anchor_lang::prelude::*; @@ -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.div(3), 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,