Skip to content

Commit

Permalink
program: place and take uses auction end price for market orders (#650)
Browse files Browse the repository at this point in the history
* init

* fix tests

* add tests

* make determine_perp_fulfillment_methods clearer for amm taking against maker

* CHANGELOG
  • Loading branch information
crispheaney committed Oct 24, 2023
1 parent d0eb6d8 commit 899a363
Show file tree
Hide file tree
Showing 13 changed files with 504 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: place and take uses auction end price for market orders ([#650](https://github.com/drift-labs/protocol-v2/pull/650))
- program: reduce cus for place_spot_order ([#662](https://github.com/drift-labs/protocol-v2/pull/662))
- program: bump max sub accounts to 15k
- program: user custom margin ratio works with spot ([#633](https://github.com/drift-labs/protocol-v2/pull/633))
Expand Down
58 changes: 32 additions & 26 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use crate::math::spot_swap::select_margin_type_for_swap;
use crate::print_error;
use crate::state::events::{emit_stack, get_order_action_record, OrderActionRecord, OrderRecord};
use crate::state::events::{OrderAction, OrderActionExplanation};
use crate::state::fill_mode::FillMode;
use crate::state::fulfillment::{PerpFulfillmentMethod, SpotFulfillmentMethod};
use crate::state::margin_calculation::MarginContext;
use crate::state::oracle::{OraclePriceData, StrictOraclePrice};
Expand Down Expand Up @@ -857,6 +858,7 @@ pub fn fill_perp_order(
makers_and_referrer_stats: &UserStatsMap,
jit_maker_order_id: Option<u32>,
clock: &Clock,
fill_mode: FillMode,
) -> DriftResult<u64> {
let now = clock.unix_timestamp;
let slot = clock.slot;
Expand Down Expand Up @@ -1096,6 +1098,7 @@ pub fn fill_perp_order(
slot,
state.min_perp_auction_duration,
amm_is_available,
fill_mode,
)?;

if base_asset_amount != 0 {
Expand Down Expand Up @@ -1480,12 +1483,20 @@ fn fulfill_perp_order(
slot: u64,
min_auction_duration: u8,
amm_is_available: bool,
fill_mode: FillMode,
) -> DriftResult<(u64, u64)> {
let market_index = user.orders[user_order_index].market_index;

let user_order_position_decreasing =
determine_if_user_order_is_position_decreasing(user, market_index, user_order_index)?;

let limit_price = fill_mode.get_limit_price(
&user.orders[user_order_index],
valid_oracle_price,
slot,
perp_market_map.get_ref(&market_index)?.amm.order_tick_size,
)?;

let fulfillment_methods = {
let market = perp_market_map.get_ref(&market_index)?;
let oracle_price = oracle_map.get_price_data(&market.amm.oracle)?.price;
Expand All @@ -1496,6 +1507,7 @@ fn fulfill_perp_order(
&market.amm,
reserve_price_before,
Some(oracle_price),
limit_price,
amm_is_available,
slot,
min_auction_duration,
Expand Down Expand Up @@ -1535,14 +1547,14 @@ fn fulfill_perp_order(
reserve_price_before,
now,
slot,
valid_oracle_price,
user_key,
filler_key,
filler,
filler_stats,
&mut referrer.as_deref_mut(),
&mut referrer_stats.as_deref_mut(),
fee_structure,
limit_price,
None,
*maker_price,
AMMLiquiditySplit::Shared,
Expand Down Expand Up @@ -1583,6 +1595,7 @@ fn fulfill_perp_order(
&mut referrer_stats.as_deref_mut(),
reserve_price_before,
valid_oracle_price,
limit_price,
now,
slot,
fee_structure,
Expand Down Expand Up @@ -1709,14 +1722,14 @@ pub fn fulfill_perp_order_with_amm(
reserve_price_before: u64,
now: i64,
slot: u64,
valid_oracle_price: Option<i64>,
user_key: &Pubkey,
filler_key: &Pubkey,
filler: &mut Option<&mut User>,
filler_stats: &mut Option<&mut UserStats>,
referrer: &mut Option<&mut User>,
referrer_stats: &mut Option<&mut UserStats>,
fee_structure: &FeeStructure,
limit_price: Option<u64>,
override_base_asset_amount: Option<u64>,
override_fill_price: Option<u64>,
liquidity_split: AMMLiquiditySplit,
Expand All @@ -1726,22 +1739,14 @@ pub fn fulfill_perp_order_with_amm(
// Determine the base asset amount the market can fill
let (base_asset_amount, limit_price, fill_price) = match override_base_asset_amount {
Some(override_base_asset_amount) => {
let limit_price = user.orders[order_index].get_limit_price(
valid_oracle_price,
None,
slot,
market.amm.order_tick_size,
)?;

(override_base_asset_amount, limit_price, override_fill_price)
}
None => {
let existing_base_asset_amount = user.perp_positions[position_index].base_asset_amount;
let (base_asset_amount, limit_price) = calculate_base_asset_amount_for_amm_to_fulfill(
&user.orders[order_index],
market,
valid_oracle_price,
slot,
limit_price,
override_fill_price,
existing_base_asset_amount,
)?;
Expand Down Expand Up @@ -1992,6 +1997,7 @@ pub fn fulfill_perp_order_with_match(
referrer_stats: &mut Option<&mut UserStats>,
reserve_price_before: u64,
valid_oracle_price: Option<i64>,
taker_limit_price: Option<u64>,
now: i64,
slot: u64,
fee_structure: &FeeStructure,
Expand All @@ -2008,20 +2014,20 @@ pub fn fulfill_perp_order_with_match(

let oracle_price = oracle_map.get_price_data(&market.amm.oracle)?.price;
let taker_direction = taker.orders[taker_order_index].direction;
let amm_available_liquidity = calculate_amm_available_liquidity(&market.amm, &taker_direction)?;
let taker_fallback_price = get_fallback_price(
&taker_direction,
bid_price,
ask_price,
amm_available_liquidity,
oracle_price,
)?;
let taker_price = taker.orders[taker_order_index].force_get_limit_price(
Some(oracle_price),
Some(taker_fallback_price),
slot,
market.amm.order_tick_size,
)?;

let taker_price = if let Some(taker_limit_price) = taker_limit_price {
taker_limit_price
} else {
let amm_available_liquidity =
calculate_amm_available_liquidity(&market.amm, &taker_direction)?;
get_fallback_price(
&taker_direction,
bid_price,
ask_price,
amm_available_liquidity,
oracle_price,
)?
};

let taker_existing_position = taker
.get_perp_position(market.market_index)?
Expand Down Expand Up @@ -2099,14 +2105,14 @@ pub fn fulfill_perp_order_with_match(
reserve_price_before,
now,
slot,
valid_oracle_price,
taker_key,
filler_key,
filler,
filler_stats,
&mut None,
&mut None,
fee_structure,
taker_limit_price,
Some(jit_base_asset_amount),
Some(maker_price), // match the makers price
amm_liquidity_split,
Expand Down
15 changes: 15 additions & 0 deletions programs/drift/src/controller/orders/amm_jit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub mod amm_jit {
SPOT_CUMULATIVE_INTEREST_PRECISION, SPOT_WEIGHT_PRECISION,
};
use crate::math::constants::{CONCENTRATION_PRECISION, PRICE_PRECISION_U64};
use crate::state::fill_mode::FillMode;
use crate::state::oracle::{HistoricalOracleData, OracleSource};
use crate::state::perp_market::{AMMLiquiditySplit, MarketStatus, PerpMarket, AMM};
use crate::state::perp_market_map::PerpMarketMap;
Expand Down Expand Up @@ -310,6 +311,7 @@ pub mod amm_jit {
slot,
0,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -498,6 +500,7 @@ pub mod amm_jit {
slot,
10,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -694,6 +697,7 @@ pub mod amm_jit {
slot,
10,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -889,6 +893,7 @@ pub mod amm_jit {
slot,
10,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -1092,6 +1097,7 @@ pub mod amm_jit {
slot,
0,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -1304,6 +1310,7 @@ pub mod amm_jit {
slot,
0,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -1514,6 +1521,7 @@ pub mod amm_jit {
slot,
0,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -1747,6 +1755,7 @@ pub mod amm_jit {
slot,
0,
false,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -1951,6 +1960,7 @@ pub mod amm_jit {
slot,
0,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -2137,6 +2147,7 @@ pub mod amm_jit {
slot,
10,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -2335,6 +2346,7 @@ pub mod amm_jit {
slot,
10,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -2585,6 +2597,7 @@ pub mod amm_jit {
slot,
auction_duration,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -2868,6 +2881,7 @@ pub mod amm_jit {
slot,
10,
true,
FillMode::Fill,
)
.unwrap();

Expand Down Expand Up @@ -3095,6 +3109,7 @@ pub mod amm_jit {
slot,
0,
true,
FillMode::Fill,
)
.unwrap();

Expand Down
Loading

0 comments on commit 899a363

Please sign in to comment.