Skip to content

program: rm lp #1755

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions programs/drift/src/controller/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,7 @@ pub fn formulaic_update_k(

let new_sqrt_k = bn::U192::from(market.amm.sqrt_k)
.safe_mul(bn::U192::from(k_scale_numerator))?
.safe_div(bn::U192::from(k_scale_denominator))?
.max(bn::U192::from(market.amm.user_lp_shares.safe_add(1)?));
.safe_div(bn::U192::from(k_scale_denominator))?;

let update_k_result = get_update_k_result(market, new_sqrt_k, true)?;

Expand Down
56 changes: 0 additions & 56 deletions programs/drift/src/controller/amm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,62 +255,6 @@ fn iterative_no_bounds_formualic_k_tests() {
assert_eq!(market.amm.total_fee_minus_distributions, 985625029);
}

#[test]
fn decrease_k_up_to_user_lp_shares() {
let mut market = PerpMarket {
amm: AMM {
base_asset_reserve: 512295081967,
quote_asset_reserve: 488 * AMM_RESERVE_PRECISION,
sqrt_k: 500 * AMM_RESERVE_PRECISION,
user_lp_shares: 150 * AMM_RESERVE_PRECISION,
peg_multiplier: 50000000,
concentration_coef: MAX_CONCENTRATION_COEFFICIENT,
base_asset_amount_with_amm: -12295081967,
total_fee_minus_distributions: -100 * QUOTE_PRECISION as i128,
total_fee_withdrawn: 100 * QUOTE_PRECISION,
curve_update_intensity: 100,
..AMM::default()
},
..PerpMarket::default()
};
// let prev_sqrt_k = market.amm.sqrt_k;
let (new_terminal_quote_reserve, new_terminal_base_reserve) =
amm::calculate_terminal_reserves(&market.amm).unwrap();
market.amm.terminal_quote_asset_reserve = new_terminal_quote_reserve;
let (min_base_asset_reserve, max_base_asset_reserve) =
amm::calculate_bid_ask_bounds(market.amm.concentration_coef, new_terminal_base_reserve)
.unwrap();
market.amm.min_base_asset_reserve = min_base_asset_reserve;
market.amm.max_base_asset_reserve = max_base_asset_reserve;

// let reserve_price = market.amm.reserve_price().unwrap();
let now = 10000;
let oracle_price_data = OraclePriceData {
price: 50 * PRICE_PRECISION_I64,
confidence: 0,
delay: 2,
has_sufficient_number_of_data_points: true,
};

// negative funding cost
let mut count = 0;
let mut prev_k = market.amm.sqrt_k;
let mut new_k = 0;
while prev_k != new_k && count < 100000 {
let funding_cost = (QUOTE_PRECISION * 100000) as i128;
prev_k = market.amm.sqrt_k;
formulaic_update_k(&mut market, &oracle_price_data, funding_cost, now).unwrap();
new_k = market.amm.sqrt_k;
msg!("quote_asset_reserve:{}", market.amm.quote_asset_reserve);
msg!("new_k:{}", new_k);
count += 1
}

assert_eq!(market.amm.base_asset_amount_with_amm, -12295081967);
assert_eq!(market.amm.sqrt_k, 162234889619);
assert_eq!(market.amm.total_fee_minus_distributions, 29796232175);
}

#[test]
fn update_pool_balances_test_high_util_borrow() {
let mut market = PerpMarket {
Expand Down
63 changes: 4 additions & 59 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use anchor_lang::prelude::*;

use crate::controller::amm::get_fee_pool_tokens;
use crate::controller::funding::settle_funding_payment;
use crate::controller::lp::burn_lp_shares;
use crate::controller::orders;
use crate::controller::orders::{cancel_order, fill_perp_order, place_perp_order};
use crate::controller::position::{
Expand Down Expand Up @@ -181,8 +180,7 @@ pub fn liquidate_perp(
let position_index = get_position_index(&user.perp_positions, market_index)?;
validate!(
user.perp_positions[position_index].is_open_position()
|| user.perp_positions[position_index].has_open_order()
|| user.perp_positions[position_index].is_lp(),
|| user.perp_positions[position_index].has_open_order(),
ErrorCode::PositionDoesntHaveOpenPositionOrOrders
)?;

Expand Down Expand Up @@ -227,27 +225,7 @@ pub fn liquidate_perp(
drop(market);

// burning lp shares = removing open bids/asks
let lp_shares = user.perp_positions[position_index].lp_shares;
if lp_shares > 0 {
let (position_delta, pnl) = burn_lp_shares(
&mut user.perp_positions[position_index],
perp_market_map.get_ref_mut(&market_index)?.deref_mut(),
lp_shares,
oracle_price,
)?;

// emit LP record for shares removed
emit_stack::<_, { LPRecord::SIZE }>(LPRecord {
ts: now,
action: LPAction::RemoveLiquidity,
user: *user_key,
n_shares: lp_shares,
market_index,
delta_base_asset_amount: position_delta.base_asset_amount,
delta_quote_asset_amount: position_delta.quote_asset_amount,
pnl,
})?;
}
let lp_shares = 0;

// check if user exited liquidation territory
let intermediate_margin_calculation = if !canceled_order_ids.is_empty() || lp_shares > 0 {
Expand Down Expand Up @@ -832,8 +810,7 @@ pub fn liquidate_perp_with_fill(
let position_index = get_position_index(&user.perp_positions, market_index)?;
validate!(
user.perp_positions[position_index].is_open_position()
|| user.perp_positions[position_index].has_open_order()
|| user.perp_positions[position_index].is_lp(),
|| user.perp_positions[position_index].has_open_order(),
ErrorCode::PositionDoesntHaveOpenPositionOrOrders
)?;

Expand Down Expand Up @@ -878,27 +855,7 @@ pub fn liquidate_perp_with_fill(
drop(market);

// burning lp shares = removing open bids/asks
let lp_shares = user.perp_positions[position_index].lp_shares;
if lp_shares > 0 {
let (position_delta, pnl) = burn_lp_shares(
&mut user.perp_positions[position_index],
perp_market_map.get_ref_mut(&market_index)?.deref_mut(),
lp_shares,
oracle_price,
)?;

// emit LP record for shares removed
emit_stack::<_, { LPRecord::SIZE }>(LPRecord {
ts: now,
action: LPAction::RemoveLiquidity,
user: *user_key,
n_shares: lp_shares,
market_index,
delta_base_asset_amount: position_delta.base_asset_amount,
delta_quote_asset_amount: position_delta.quote_asset_amount,
pnl,
})?;
}
let lp_shares = 0;

// check if user exited liquidation territory
let intermediate_margin_calculation = if !canceled_order_ids.is_empty() || lp_shares > 0 {
Expand Down Expand Up @@ -2448,12 +2405,6 @@ pub fn liquidate_borrow_for_perp_pnl(
base_asset_amount
)?;

validate!(
!user_position.is_lp(),
ErrorCode::InvalidPerpPositionToLiquidate,
"user is an lp. must call liquidate_perp first"
)?;

let pnl = user_position.quote_asset_amount.cast::<i128>()?;

validate!(
Expand Down Expand Up @@ -2983,12 +2934,6 @@ pub fn liquidate_perp_pnl_for_deposit(
base_asset_amount
)?;

validate!(
!user_position.is_lp(),
ErrorCode::InvalidPerpPositionToLiquidate,
"user is an lp. must call liquidate_perp first"
)?;

let unsettled_pnl = user_position.quote_asset_amount.cast::<i128>()?;

validate!(
Expand Down
Loading
Loading