Skip to content

Commit

Permalink
program: check if user has room for open order before placing order t… (
Browse files Browse the repository at this point in the history
#899)

* program: check if user has room for open order before placing order to derisk lp position

* add CHANGELOG.md

---------

Co-authored-by: 0xbigz <[email protected]>
  • Loading branch information
crispheaney and 0xbigz committed Feb 23, 2024
1 parent ad56b09 commit 6cdca50
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- program: handle derisk lp when orders array full ([#899](https://github.com/drift-labs/protocol-v2/pull/899))
- program: invalid borrow in get_referrer_info when maker is refferer ([#900](https://github.com/drift-labs/protocol-v2/pull/900))

### Breaking
Expand Down
24 changes: 13 additions & 11 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2960,17 +2960,19 @@ pub fn burn_user_lp_shares_for_risk_reduction(

drop(market);

controller::orders::place_perp_order(
state,
user,
user_key,
perp_market_map,
spot_market_map,
oracle_map,
clock,
params,
PlaceOrderOptions::default().explanation(OrderActionExplanation::DeriskLp),
)?;
if user.has_room_for_new_order() {
controller::orders::place_perp_order(
state,
user,
user_key,
perp_market_map,
spot_market_map,
oracle_map,
clock,
params,
PlaceOrderOptions::default().explanation(OrderActionExplanation::DeriskLp),
)?;
}

Ok(())
}
Expand Down
10 changes: 10 additions & 0 deletions programs/drift/src/state/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ impl User {

Ok(())
}

pub fn has_room_for_new_order(&self) -> bool {
for order in self.orders.iter() {
if order.status == OrderStatus::Init {
return true;
}
}

false
}
}

#[zero_copy(unsafe)]
Expand Down

0 comments on commit 6cdca50

Please sign in to comment.