Skip to content

Commit

Permalink
jit: dont let maker fill at worse price than vamm (#31)
Browse files Browse the repository at this point in the history
* jit: dont let maker fill at worse price than vamm

* fix build
  • Loading branch information
crispheaney authored Jan 26, 2024
1 parent dba2f79 commit 1b70ce8
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions programs/jit-proxy/src/instructions/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,34 @@ pub fn jit<'info>(ctx: Context<'_, '_, '_, 'info, Jit<'info>>, params: JitParams
}
}
}
let maker_price = taker_price;

let maker_price = if market_type == DriftMarketType::Perp {
let perp_market = perp_market_map.get_ref(&market_index)?;
let reserve_price = perp_market.amm.reserve_price()?;

let mut maker_price = taker_price;
match maker_direction {
PositionDirection::Long => {
let amm_bid_price = perp_market.amm.bid_price(reserve_price)?;

// if amm price is better than maker, use amm price to ensure fill
if taker_price <= amm_bid_price && amm_bid_price < maker_worst_price {
maker_price = amm_bid_price;
}
}
PositionDirection::Short => {
let amm_ask_price = perp_market.amm.ask_price(reserve_price)?;

if taker_price >= amm_ask_price && amm_ask_price > maker_worst_price {
maker_price = amm_ask_price;
}
}
}

maker_price
} else {
taker_price
};

let taker_base_asset_amount_unfilled = taker_order
.get_base_asset_amount_unfilled(None)?
Expand Down Expand Up @@ -202,8 +229,9 @@ pub fn jit<'info>(ctx: Context<'_, '_, '_, 'info, Jit<'info>>, params: JitParams
if taker_base_asset_amount_unfilled_after == taker_base_asset_amount_unfilled {
// taker order failed to fill
msg!(
"taker price = {} oracle price = {}",
"taker price = {} maker price = {} oracle price = {}",
taker_price,
maker_price,
oracle_price
);
msg!("jit params {:?}", params);
Expand Down

0 comments on commit 1b70ce8

Please sign in to comment.