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

Bigz/perp lp jit #448

Merged
merged 52 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
7f865ef
bigz/perp-lp-jit
0xbigz Apr 28, 2023
752f56e
sdk: add updatePerpMarketTargetBaseAssetAmountPerLP
0xbigz Apr 28, 2023
b013675
init tests (wip)
0xbigz Apr 28, 2023
3aa9a51
todo: start split_with_lps tests
0xbigz Apr 28, 2023
c41bd08
fix test
0xbigz Apr 28, 2023
65e6deb
controller/orders.rs: split out to calculate_amm_jit_liquidity
0xbigz May 1, 2023
e53ab10
split out with split_with_lps param in calculate_jit_amount
0xbigz May 1, 2023
f24cd31
fix idl
0xbigz May 2, 2023
c8bf3be
Merge branch 'master' into bigz/perp-lp-jit
0xbigz May 8, 2023
cbe7eff
update idl
0xbigz May 8, 2023
546bafa
fix greater/less than on target lp amount
0xbigz May 8, 2023
9c3c12e
wip test
0xbigz May 8, 2023
aaace61
working basic tests
0xbigz May 9, 2023
c585830
wip, working typescript tests and functions
0xbigz May 9, 2023
a743abb
fix tests/perpLpJit.ts
0xbigz May 9, 2023
18fe355
fix test
0xbigz May 10, 2023
fb56aab
merge master
0xbigz May 10, 2023
3bc6dbd
add LP split OrderActionExplanation, linter
0xbigz May 10, 2023
91fbec4
fix prettier
0xbigz May 10, 2023
a27eb14
address feedback p1
0xbigz May 11, 2023
fc073b1
add amm_lp_wants_to_jit_make functions
0xbigz May 11, 2023
a16c934
cleanup logs, add intuition comment
0xbigz May 11, 2023
dacf592
improve amm_lp_allowed_to_jit_make compute
0xbigz May 11, 2023
791c6fb
cargo fmt --
0xbigz May 11, 2023
3c44caa
revert split_with_lps fee_to_market
0xbigz May 11, 2023
156e8b0
use AMMLiquiditySplit
0xbigz May 12, 2023
84bf634
use protocol_owned_min_side_liquidity for amm_lp_allowed_to_jit_make
0xbigz May 12, 2023
9a2d562
wip lp-owned fill
0xbigz May 12, 2023
505ac06
cargo fmt --, fix test
0xbigz May 14, 2023
9c6e842
data check inside update_lp_market_position
0xbigz May 15, 2023
c29e80e
update_lp_market_position: comb early return
0xbigz May 15, 2023
668b7c4
incorp most of feedback-p2
0xbigz May 16, 2023
0dcf0d2
working LPOwned fills
0xbigz May 16, 2023
eb7b054
fix comment
0xbigz May 16, 2023
5a7dcb7
merge master
0xbigz May 16, 2023
123b734
add some perp_market precisions
0xbigz May 16, 2023
3060374
tests/perpLpJit.ts: add asserts
0xbigz May 17, 2023
749dcaa
tests/perpLpJit.ts: add btc test
0xbigz May 17, 2023
1900c8f
wip more asserts for tests/perpLpJit.ts
0xbigz May 18, 2023
1f0a105
tests/perpLpJit.ts: expanded
0xbigz May 18, 2023
04475a7
tests: remove assert from spotSwap test causing race conditions
crispheaney May 17, 2023
4f838b6
sdk: update spl-token dependency to 0.3.7 (#465)
crispheaney May 17, 2023
5aa89b8
sdk: tweaks to make driftClient.swap work
crispheaney May 17, 2023
69a5fbe
sdk: add ability to pass in route to driftClient.swap
crispheaney May 17, 2023
4342ebf
sdk: supporting changes for jupiterClient (#466)
wphan May 18, 2023
84419ba
program: add swap reduce only (#468)
crispheaney May 18, 2023
6c93e7e
init msol config (#467)
0xbigz May 18, 2023
4a7ffbb
sdk: spotBalance.ts docs
wphan May 18, 2023
42a9907
sdk: make ts BN for swaprecords
lowkeynicc May 18, 2023
d2057ac
v2.30.0
crispheaney May 18, 2023
64015e6
Merge branch 'master' into bigz/perp-lp-jit
0xbigz May 19, 2023
b2993db
update changelog
0xbigz May 19, 2023
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
88 changes: 47 additions & 41 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::error::ErrorCode;
use crate::get_then_update_id;
use crate::instructions::OrderParams;
use crate::load_mut;
use crate::math::amm_jit::calculate_amm_jit_liquidity;
use crate::math::auction::calculate_auction_prices;
use crate::math::casting::Cast;
use crate::math::constants::{
Expand Down Expand Up @@ -79,6 +80,8 @@ mod tests;

#[cfg(test)]
mod amm_jit_tests;
#[cfg(test)]
mod amm_lp_jit_tests;

pub fn place_perp_order(
state: &State,
Expand Down Expand Up @@ -1781,7 +1784,7 @@ pub fn fulfill_perp_order_with_amm(

let FillFees {
user_fee,
fee_to_market,
mut fee_to_market,
filler_reward,
referee_discount,
referrer_reward,
Expand All @@ -1803,8 +1806,13 @@ pub fn fulfill_perp_order_with_amm(
let user_position_delta =
get_position_delta_for_fill(base_asset_amount, quote_asset_amount, order_direction)?;

crate::dlog!(split_with_lps);
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
if split_with_lps {
update_lp_market_position(market, &user_position_delta, fee_to_market_for_lp.cast()?)?;
} else {
msg!("HIHIHI {:?} {:?}", fee_to_market, fee_to_market_for_lp);
fee_to_market = fee_to_market.safe_add(fee_to_market_for_lp)?;
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
msg!("HIHIHI {:?} {:?}", fee_to_market, fee_to_market_for_lp);
}

if market.amm.user_lp_shares > 0 {
Expand Down Expand Up @@ -1901,7 +1909,11 @@ pub fn fulfill_perp_order_with_amm(
let fill_record_id = get_then_update_id!(market, next_fill_record_id);
let order_action_explanation =
if override_base_asset_amount.is_some() && override_fill_price.is_some() {
OrderActionExplanation::OrderFilledWithAMMJit
if split_with_lps {
OrderActionExplanation::OrderFilledWithAMMJitLPSplit
} else {
OrderActionExplanation::OrderFilledWithAMMJit
}
} else {
OrderActionExplanation::OrderFilledWithAMM
};
Expand Down Expand Up @@ -2037,51 +2049,45 @@ pub fn fulfill_perp_order_with_match(
sanitize_clamp_denominator,
)?;

let amm_wants_to_make = match taker_direction {
PositionDirection::Long => market.amm.base_asset_amount_with_amm < 0,
PositionDirection::Short => market.amm.base_asset_amount_with_amm > 0,
} && market.amm.amm_jit_is_active();
let mut total_quote_asset_amount = 0_u64;

// taker has_limit_price = false means (limit price = 0 AND auction is complete) so
// market order will always land and fill on amm next round
let amm_will_fill_next_round = !taker.orders[taker_order_index].has_limit_price(slot)?
&& maker_base_asset_amount < taker_base_asset_amount;
let (jit_base_asset_amount, split_with_lps) = calculate_amm_jit_liquidity(
market,
taker_direction,
maker_price,
valid_oracle_price,
base_asset_amount,
taker_base_asset_amount,
maker_base_asset_amount,
taker.orders[taker_order_index].has_limit_price(slot)?,
)?;
crate::dlog!(jit_base_asset_amount);
0xbigz marked this conversation as resolved.
Show resolved Hide resolved

let mut total_quote_asset_amount = 0_u64;
if amm_wants_to_make && !amm_will_fill_next_round {
let jit_base_asset_amount = crate::math::amm_jit::calculate_jit_base_asset_amount(
if jit_base_asset_amount > 0 {
let (_, quote_asset_amount_filled_by_amm) = fulfill_perp_order_with_amm(
taker,
taker_stats,
taker_order_index,
market,
base_asset_amount,
maker_price,
oracle_map,
reserve_price_before,
now,
slot,
valid_oracle_price,
taker_direction,
taker_key,
filler_key,
filler,
filler_stats,
&mut None,
&mut None,
fee_structure,
Some(jit_base_asset_amount),
Some(maker_price), // match the makers price
split_with_lps,
)?;

if jit_base_asset_amount > 0 {
let (_, quote_asset_amount_filled_by_amm) = fulfill_perp_order_with_amm(
taker,
taker_stats,
taker_order_index,
market,
oracle_map,
reserve_price_before,
now,
slot,
valid_oracle_price,
taker_key,
filler_key,
filler,
filler_stats,
&mut None,
&mut None,
fee_structure,
Some(jit_base_asset_amount),
Some(maker_price), // match the makers price
false, // dont split with the lps
)?;
total_quote_asset_amount = quote_asset_amount_filled_by_amm;
};
};
total_quote_asset_amount = quote_asset_amount_filled_by_amm
}

let taker_existing_position = taker
.get_perp_position(market.market_index)?
Expand Down
Loading