Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

program: fix risk reduction dlp burn from not being step size #826

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
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 @@

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
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 @@
};

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.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 @@
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
Loading