Skip to content

Commit

Permalink
program: derive auction for crossing limit with no duration (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney authored Dec 29, 2023
1 parent 7f2aa51 commit e7c1801
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
5 changes: 4 additions & 1 deletion programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn place_perp_order(
spot_market_map: &SpotMarketMap,
oracle_map: &mut OracleMap,
clock: &Clock,
params: OrderParams,
mut params: OrderParams,
mut options: PlaceOrderOptions,
) -> DriftResult {
let now = clock.unix_timestamp;
Expand Down Expand Up @@ -219,6 +219,9 @@ pub fn place_perp_order(
(existing_position_direction, base_asset_amount)
};

// updates auction params for crossing limit orders w/out auction duration
params.update_perp_auction_params(market)?;

let oracle_price_data = oracle_map.get_price_data(&market.amm.oracle)?;
let (auction_start_price, auction_end_price, auction_duration) = get_auction_params(
&params,
Expand Down
57 changes: 57 additions & 0 deletions programs/drift/src/state/order_params.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::controller::position::PositionDirection;
use crate::error::DriftResult;
use crate::state::perp_market::PerpMarket;
use crate::state::user::{MarketType, OrderTriggerCondition, OrderType};
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
Expand All @@ -24,6 +26,61 @@ pub struct OrderParams {
pub auction_end_price: Option<i64>,
}

impl OrderParams {
pub fn update_perp_auction_params(&mut self, perp_market: &PerpMarket) -> DriftResult {
if self.order_type != OrderType::Limit {
return Ok(());
}

if self.auction_duration.is_some() {
return Ok(());
}

if self.post_only != PostOnlyParam::None {
return Ok(());
}

if self.immediate_or_cancel {
return Ok(());
}

if self.oracle_price_offset.is_some() {
return Ok(());
}

match self.direction {
PositionDirection::Long => {
let reserve_price = perp_market.amm.reserve_price()?;
let ask_price = perp_market.amm.ask_price(reserve_price)?;
if self.price > ask_price {
let auction_duration = 60;
let auction_start_price = ask_price as i64;
let auction_end_price = self.price as i64;
msg!("derived auction params for limit order. duration = {} start_price = {} end_price = {}", auction_duration, auction_start_price, auction_end_price);
self.auction_duration = Some(auction_duration);
self.auction_start_price = Some(auction_start_price);
self.auction_end_price = Some(auction_end_price);
}
}
PositionDirection::Short => {
let reserve_price = perp_market.amm.reserve_price()?;
let bid_price = perp_market.amm.bid_price(reserve_price)?;
if self.price < bid_price {
let auction_duration = 60;
let auction_start_price = bid_price as i64;
let auction_end_price = self.price as i64;
msg!("derived auction params for limit order. duration = {} start_price = {} end_price = {}", auction_duration, auction_start_price, auction_end_price);
self.auction_duration = Some(auction_duration);
self.auction_start_price = Some(auction_start_price);
self.auction_end_price = Some(auction_end_price);
}
}
}

Ok(())
}
}

#[derive(Clone, Copy, BorshSerialize, BorshDeserialize, PartialEq, Debug, Eq)]
pub enum PostOnlyParam {
None,
Expand Down

0 comments on commit e7c1801

Please sign in to comment.