Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

program: derive auction for crossing limit with no duration #802

Merged
merged 5 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
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 @@
(existing_position_direction, base_asset_amount)
};

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

Check warning on line 223 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L223

Added line #L223 was not covered by tests

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 Expand Up @@ -813,15 +816,19 @@
let oracle_price_offset = modify_order_params
.oracle_price_offset
.or(Some(existing_order.oracle_price_offset));
let auction_duration = modify_order_params
.auction_duration
.or(Some(existing_order.auction_duration));
let auction_start_price = modify_order_params
.auction_start_price
.or(Some(existing_order.auction_start_price));
let auction_end_price = modify_order_params
.auction_end_price
.or(Some(existing_order.auction_end_price));
let (auction_duration, auction_start_price, auction_end_price) =

Check warning on line 819 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L819

Added line #L819 was not covered by tests
if modify_order_params.auction_duration.is_some()
&& modify_order_params.auction_start_price.is_some()
&& modify_order_params.auction_end_price.is_some()

Check warning on line 822 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L821-L822

Added lines #L821 - L822 were not covered by tests
{
(
modify_order_params.auction_duration,
modify_order_params.auction_start_price,
modify_order_params.auction_end_price,

Check warning on line 827 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L825-L827

Added lines #L825 - L827 were not covered by tests
)
} else {
(None, None, None)

Check warning on line 830 in programs/drift/src/controller/orders.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/controller/orders.rs#L830

Added line #L830 was not covered by tests
};

Ok(OrderParams {
order_type,
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 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(());

Check warning on line 32 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L30-L32

Added lines #L30 - L32 were not covered by tests
}

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

Check warning on line 36 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L35-L36

Added lines #L35 - L36 were not covered by tests
}

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

Check warning on line 40 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L39-L40

Added lines #L39 - L40 were not covered by tests
}

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

Check warning on line 44 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L43-L44

Added lines #L43 - L44 were not covered by tests
}

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

Check warning on line 48 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L47-L48

Added lines #L47 - L48 were not covered by tests
}

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);

Check warning on line 62 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L51-L62

Added lines #L51 - L62 were not covered by tests
}
}
PositionDirection::Short => {
let reserve_price = perp_market.amm.reserve_price()?;
let bid_price = perp_market.amm.bid_price(reserve_price)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we know if the amm is recently updated by the time its here?

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);

Check warning on line 75 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L65-L75

Added lines #L65 - L75 were not covered by tests
}
}
}

Ok(())

Check warning on line 80 in programs/drift/src/state/order_params.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/order_params.rs#L80

Added line #L80 was not covered by tests
}
}

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