Skip to content

Commit

Permalink
program: fix risk reduction dlp burn from not being step size (#826)
Browse files Browse the repository at this point in the history
* program: fix risk reduction dlp burn from not being step size

* CHANGELOG

* fix lp burn
  • Loading branch information
crispheaney committed Jan 18, 2024
1 parent 443e16c commit 97971dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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)?;

// 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()?,
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];

validate!(
position.lp_shares >= shares_to_burn,
Expand Down
10 changes: 7 additions & 3 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down 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 =
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,
)?;

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 97971dd

Please sign in to comment.