Skip to content

Commit

Permalink
program: can-lower-k-improvement (#1338)
Browse files Browse the repository at this point in the history
* program: can-lower-k-improvement

* check min_order_size_u128

* CHANGELOG

---------

Co-authored-by: Chris Heaney <[email protected]>
  • Loading branch information
0xbigz and crispheaney authored Nov 21, 2024
1 parent 44dd77e commit 7b1d901
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- program: can update k looks at min order size ([#1338](https://github.com/drift-labs/protocol-v2/pull/1338))
- program: skip validate_post_only_order if amm paused ([#1202](https://github.com/drift-labs/protocol-v2/pull/1202))

### Breaking
Expand Down
15 changes: 11 additions & 4 deletions programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,10 +1398,17 @@ impl AMM {

pub fn can_lower_k(&self) -> DriftResult<bool> {
let (max_bids, max_asks) = amm::calculate_market_open_bids_asks(self)?;
let can_lower = self.base_asset_amount_with_amm.unsigned_abs()
< max_bids.unsigned_abs().min(max_asks.unsigned_abs())
&& self.base_asset_amount_with_amm.unsigned_abs()
< self.sqrt_k.safe_sub(self.user_lp_shares)?;
let min_order_size_u128 = self.min_order_size.cast::<u128>()?;

let can_lower = (self.base_asset_amount_with_amm.unsigned_abs()
< max_bids.unsigned_abs().min(max_asks.unsigned_abs()))
&& (self
.base_asset_amount_with_amm
.unsigned_abs()
.max(min_order_size_u128)
< self.sqrt_k.safe_sub(self.user_lp_shares)?)
&& (min_order_size_u128 < max_bids.unsigned_abs().max(max_asks.unsigned_abs()));

Ok(can_lower)
}

Expand Down

0 comments on commit 7b1d901

Please sign in to comment.