Skip to content

Commit

Permalink
add get_lower_bound_sqrt_k functor
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbigz committed Jan 26, 2024
1 parent 0960a14 commit 0508beb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
15 changes: 14 additions & 1 deletion programs/drift/src/controller/position/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,12 @@ fn recenter_amm_2() {
let current_k = perp_market.amm.sqrt_k;
let _current_peg = perp_market.amm.peg_multiplier;
let new_k = current_k * 2;

// refusal to decrease further
assert_eq!(current_k, current_k);
assert_eq!(perp_market.amm.user_lp_shares, current_k - 1);
assert_eq!(perp_market.amm.get_lower_bound_sqrt_k().unwrap(), current_k);

recenter_perp_market_amm(&mut perp_market.amm, oracle_price_data.price as u128, new_k).unwrap();

assert_eq!(perp_market.amm.sqrt_k, new_k);
Expand Down Expand Up @@ -1611,8 +1617,15 @@ fn recenter_amm_2() {
assert_eq!(adjustment_cost, 0);

update_k(&mut perp_market, &update_k_result).unwrap();
assert_eq!(perp_market.amm.sqrt_k, new_sqrt_k);

// higher lower bound now
assert_eq!(perp_market.amm.sqrt_k, new_sqrt_k);
assert_eq!(perp_market.amm.user_lp_shares, current_k - 1);
assert!(perp_market.amm.get_lower_bound_sqrt_k().unwrap() > current_k);
assert_eq!(
perp_market.amm.get_lower_bound_sqrt_k().unwrap(),
140766081456000000
);
// assert_eq!(perp_market.amm.peg_multiplier, current_peg);
}

Expand Down
15 changes: 1 addition & 14 deletions programs/drift/src/math/repeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,7 @@ pub fn adjust_amm(
// TODO can be off by 1?

// always let protocol-owned sqrt_k be either least .1% of lps or the base amount / min order
let new_sqrt_k_lower_bound = market.amm.sqrt_k.min(
market
.amm
.user_lp_shares
.safe_add(market.amm.user_lp_shares.safe_div(1000)?)?
.max(market.amm.min_order_size.cast()?)
.max(
market
.amm
.base_asset_amount_with_amm
.unsigned_abs()
.cast()?,
),
);
let new_sqrt_k_lower_bound = market.amm.get_lower_bound_sqrt_k()?;

let new_sqrt_k = market
.amm
Expand Down
9 changes: 9 additions & 0 deletions programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,15 @@ impl AMM {
}
}

pub fn get_lower_bound_sqrt_k(self) -> DriftResult<u128> {
Ok(self.sqrt_k.min(
self.user_lp_shares
.safe_add(self.user_lp_shares.safe_div(1000)?)?
.max(self.min_order_size.cast()?)
.max(self.base_asset_amount_with_amm.unsigned_abs().cast()?),
))
}

pub fn get_protocol_owned_position(self) -> DriftResult<i64> {
self.base_asset_amount_with_amm
.safe_add(self.base_asset_amount_with_unsettled_lp)?
Expand Down

0 comments on commit 0508beb

Please sign in to comment.