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: vamm gives maker rebate #653

Merged
merged 20 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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
85 changes: 52 additions & 33 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::math::constants::{
BASE_PRECISION_U64, FEE_POOL_TO_REVENUE_POOL_THRESHOLD, FIVE_MINUTE, ONE_HOUR, PERP_DECIMALS,
QUOTE_SPOT_MARKET_INDEX,
};
use crate::math::fees::{ExternalFillFees, FillFees};
use crate::math::fees::{determine_user_fee_tier, ExternalFillFees, FillFees};
use crate::math::fulfillment::{
determine_perp_fulfillment_methods, determine_spot_fulfillment_methods,
};
Expand All @@ -57,6 +57,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 @@ -852,6 +853,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 @@ -1091,6 +1093,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 @@ -1475,12 +1478,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 @@ -1491,6 +1502,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 @@ -1530,14 +1542,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 @@ -1578,6 +1590,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 @@ -1704,14 +1717,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 @@ -1721,24 +1734,18 @@ 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 fee_tier = determine_user_fee_tier(user_stats, fee_structure, &MarketType::Perp)?;
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,
fee_tier,
)?;

let fill_price = if user.orders[order_index].post_only {
Expand Down Expand Up @@ -1813,7 +1820,7 @@ pub fn fulfill_perp_order_with_amm(
referee_discount,
referrer_reward,
fee_to_market_for_lp,
..
maker_rebate,
} = fees::calculate_fee_for_fulfillment_with_amm(
user_stats,
quote_asset_amount,
Expand Down Expand Up @@ -1872,6 +1879,7 @@ pub fn fulfill_perp_order_with_amm(

// Increment the user's total fee variables
user_stats.increment_total_fees(user_fee)?;
user_stats.increment_total_rebate(maker_rebate)?;
user_stats.increment_total_referee_discount(referee_discount)?;

if let (Some(referrer), Some(referrer_stats)) = (referrer.as_mut(), referrer_stats.as_mut()) {
Expand All @@ -1885,11 +1893,21 @@ pub fn fulfill_perp_order_with_amm(

let position_index = get_position_index(&user.perp_positions, market.market_index)?;

controller::position::update_quote_asset_and_break_even_amount(
&mut user.perp_positions[position_index],
market,
-user_fee.cast()?,
)?;
if user_fee != 0 {
controller::position::update_quote_asset_and_break_even_amount(
&mut user.perp_positions[position_index],
market,
-user_fee.cast()?,
)?;
}

if maker_rebate != 0 {
controller::position::update_quote_asset_and_break_even_amount(
&mut user.perp_positions[position_index],
market,
maker_rebate.cast()?,
)?;
}

if order_post_only {
user_stats.update_maker_volume_30d(quote_asset_amount, now)?;
Expand Down Expand Up @@ -1987,6 +2005,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 @@ -2003,20 +2022,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 @@ -2094,14 +2113,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
Loading