Skip to content

Commit

Permalink
program: new ExcludePreviousFill flag for modify_order (#1357)
Browse files Browse the repository at this point in the history
* program: new ExcludePreviousFill flag for modify_order

* skip placing order instead of erroring out if base asset amount is 0

* CHANGELOG
  • Loading branch information
crispheaney authored Dec 5, 2024
1 parent d3134a6 commit e17ae06
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 46 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- program: make ModifyOrderParams a bit flag and add ExcludePreviousFill ([#1357](https://github.com/drift-labs/protocol-v2/pull/1357))

- program: fix force delete user for token 2022 ([#1358](https://github.com/drift-labs/protocol-v2/pull/1358))

### Breaking
Expand Down
77 changes: 45 additions & 32 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ pub fn modify_order(
Ok(order_index) => order_index,
Err(e) => {
msg!("User order id {} not found", user_order_id);
if modify_order_params.policy == Some(ModifyOrderPolicy::MustModify) {
if modify_order_params.must_modify() {
return Err(e);
} else {
return Ok(());
Expand All @@ -916,7 +916,7 @@ pub fn modify_order(
Ok(order_index) => order_index,
Err(e) => {
msg!("Order id {} not found", order_id);
if modify_order_params.policy == Some(ModifyOrderPolicy::MustModify) {
if modify_order_params.must_modify() {
return Err(e);
} else {
return Ok(());
Expand Down Expand Up @@ -947,30 +947,32 @@ pub fn modify_order(
let order_params =
merge_modify_order_params_with_existing_order(&existing_order, &modify_order_params)?;

if order_params.market_type == MarketType::Perp {
place_perp_order(
state,
&mut user,
user_key,
perp_market_map,
spot_market_map,
oracle_map,
clock,
order_params,
PlaceOrderOptions::default(),
)?;
} else {
place_spot_order(
state,
&mut user,
user_key,
perp_market_map,
spot_market_map,
oracle_map,
clock,
order_params,
PlaceOrderOptions::default(),
)?;
if let Some(order_params) = order_params {
if order_params.market_type == MarketType::Perp {
place_perp_order(
state,
&mut user,
user_key,
perp_market_map,
spot_market_map,
oracle_map,
clock,
order_params,
PlaceOrderOptions::default(),
)?;
} else {
place_spot_order(
state,
&mut user,
user_key,
perp_market_map,
spot_market_map,
oracle_map,
clock,
order_params,
PlaceOrderOptions::default(),
)?;
}
}

Ok(())
Expand All @@ -979,16 +981,27 @@ pub fn modify_order(
fn merge_modify_order_params_with_existing_order(
existing_order: &Order,
modify_order_params: &ModifyOrderParams,
) -> DriftResult<OrderParams> {
) -> DriftResult<Option<OrderParams>> {
let order_type = existing_order.order_type;
let market_type = existing_order.market_type;
let direction = modify_order_params
.direction
.unwrap_or(existing_order.direction);
let user_order_id = existing_order.user_order_id;
let base_asset_amount = modify_order_params
.base_asset_amount
.unwrap_or(existing_order.get_base_asset_amount_unfilled(None)?);
let base_asset_amount = match modify_order_params.base_asset_amount {
Some(base_asset_amount) if modify_order_params.exclude_previous_fill() => {
let base_asset_amount =
base_asset_amount.saturating_sub(existing_order.base_asset_amount_filled);

if base_asset_amount == 0 {
return Ok(None);
}

base_asset_amount
}
Some(base_asset_amount) => base_asset_amount,
None => existing_order.get_base_asset_amount_unfilled(None)?,
};
let price = modify_order_params.price.unwrap_or(existing_order.price);
let market_index = existing_order.market_index;
let reduce_only = modify_order_params
Expand Down Expand Up @@ -1034,7 +1047,7 @@ fn merge_modify_order_params_with_existing_order(
(None, None, None)
};

Ok(OrderParams {
Ok(Some(OrderParams {
order_type,
market_type,
direction,
Expand All @@ -1052,7 +1065,7 @@ fn merge_modify_order_params_with_existing_order(
auction_duration,
auction_start_price,
auction_end_price,
})
}))
}

pub fn fill_perp_order(
Expand Down
22 changes: 13 additions & 9 deletions programs/drift/src/state/order_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,20 +796,24 @@ pub struct ModifyOrderParams {
pub auction_duration: Option<u8>,
pub auction_start_price: Option<i64>,
pub auction_end_price: Option<i64>,
pub policy: Option<ModifyOrderPolicy>,
pub policy: Option<u8>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Eq, PartialEq)]
pub enum ModifyOrderPolicy {
TryModify,
MustModify,
}
impl ModifyOrderParams {
pub fn must_modify(&self) -> bool {
self.policy.unwrap_or(0) & ModifyOrderPolicy::MustModify as u8 != 0
}

impl Default for ModifyOrderPolicy {
fn default() -> Self {
Self::TryModify
pub fn exclude_previous_fill(&self) -> bool {
self.policy.unwrap_or(0) & ModifyOrderPolicy::ExcludePreviousFill as u8 != 0
}
}

pub enum ModifyOrderPolicy {
MustModify = 1,
ExcludePreviousFill = 2,
}

#[derive(Clone)]
pub struct PlaceOrderOptions {
pub swift_taker_order_slot: Option<u64>,
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6477,7 +6477,7 @@ export class DriftClient {
postOnly?: boolean;
immediateOrCancel?: boolean;
maxTs?: BN;
policy?: ModifyOrderPolicy;
policy?: number;
},
txParams?: TxParams,
subAccountId?: number
Expand Down Expand Up @@ -6525,7 +6525,7 @@ export class DriftClient {
postOnly?: boolean;
immediateOrCancel?: boolean;
maxTs?: BN;
policy?: ModifyOrderPolicy;
policy?: number;
},
subAccountId?: number
): Promise<TransactionInstruction> {
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,9 +1077,9 @@ export type ModifyOrderParams = {
[Property in keyof OrderParams]?: OrderParams[Property] | null;
} & { policy?: ModifyOrderPolicy };

export class ModifyOrderPolicy {
static readonly MUST_MODIFY = { mustModify: {} };
static readonly TRY_MODIFY = { tryModify: {} };
export enum ModifyOrderPolicy {
MustModify = 1,
ExcludePreviousFill = 2,
}

export const DefaultOrderParams: OrderParams = {
Expand Down

0 comments on commit e17ae06

Please sign in to comment.