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/improve-perp-match-get_fallback_price #797

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 5 additions & 8 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2089,25 +2089,22 @@ pub fn fulfill_perp_order_with_match(
return Ok((0_u64, 0_u64, 0_u64));
}

let (bid_price, ask_price) = market.amm.bid_ask_price(market.amm.reserve_price()?)?;

let oracle_price = oracle_map.get_price_data(&market.amm.oracle)?.price;
let taker_direction = taker.orders[taker_order_index].direction;
let taker_direction: PositionDirection = taker.orders[taker_order_index].direction;

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(
market.amm.get_fallback_price(
&taker_direction,
bid_price,
ask_price,
amm_available_liquidity,
oracle_price,
)?
taker.orders[taker_order_index].seconds_til_expiry(now)
)?.cast()?
};

let taker_existing_position = taker
.get_perp_position(market.market_index)?
.base_asset_amount;
Expand Down
20 changes: 0 additions & 20 deletions programs/drift/src/math/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,26 +693,6 @@ pub fn calculate_fill_price(
.cast::<u64>()
}

pub fn get_fallback_price(
direction: &PositionDirection,
bid_price: u64,
ask_price: u64,
amm_available_liquidity: u64,
oracle_price: i64,
) -> DriftResult<u64> {
let oracle_price = oracle_price.unsigned_abs();
match direction {
PositionDirection::Long if amm_available_liquidity > 0 => {
ask_price.safe_add(ask_price / 200)
}
PositionDirection::Long => oracle_price.safe_add(oracle_price / 20),
PositionDirection::Short if amm_available_liquidity > 0 => {
bid_price.safe_sub(bid_price / 200)
}
PositionDirection::Short => oracle_price.safe_sub(oracle_price / 20),
}
}

pub fn get_max_fill_amounts(
user: &User,
user_order_index: usize,
Expand Down
39 changes: 39 additions & 0 deletions programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,45 @@
}

impl AMM {
pub fn get_fallback_price(self,
Copy link
Member

Choose a reason for hiding this comment

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

needs tests

direction: &PositionDirection,
amm_available_liquidity: u64,
oracle_price: i64,
seconds_til_order_expiry: i64,
) -> DriftResult<i64> {
Copy link
Member

Choose a reason for hiding this comment

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

should return u64 probably

// PRICE_PRECISION
if direction.eq(&PositionDirection::Long) {
// pick amm ask + buffer if theres liquidity
// otherwise be aggressive vs oracle + 1hr premium
if amm_available_liquidity >= self.min_order_size {
let reserve_price = self.reserve_price()?;
let amm_ask_price: i64 = self.ask_price(reserve_price)?.cast()?;
amm_ask_price
.safe_add(amm_ask_price / (seconds_til_order_expiry * 20).clamp(100, 200))
} else {
oracle_price.safe_add(self.last_ask_price_twap.cast::<i64>()?
.safe_sub(self.historical_oracle_data.last_oracle_price_twap)?
.max(0)

Check warning on line 792 in programs/drift/src/state/perp_market.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/perp_market.rs#L790-L792

Added lines #L790 - L792 were not covered by tests
)?.safe_add(oracle_price / (seconds_til_order_expiry * 2).clamp(10, 50))
}
} else {
// pick amm bid - buffer if theres liquidity
// otherwise be aggressive vs oracle + 1hr bid premium
if amm_available_liquidity >= self.min_order_size {
let reserve_price = self.reserve_price()?;
let amm_bid_price: i64 = self.bid_price(reserve_price)?.cast()?;
amm_bid_price
.safe_sub(amm_bid_price / (seconds_til_order_expiry * 20).clamp(100, 200))
} else {
oracle_price.safe_add(self.last_bid_price_twap.cast::<i64>()?
.safe_sub(self.historical_oracle_data.last_oracle_price_twap)?
.min(0)

Check warning on line 806 in programs/drift/src/state/perp_market.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/perp_market.rs#L804-L806

Added lines #L804 - L806 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

think it'd be better to min 0 with (last bid price twap - last_oracle_price_twap) so this is never 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

think thats what its doing already

)?.safe_add(oracle_price / (seconds_til_order_expiry * 2).clamp(10, 50))
}
}

}

pub fn get_protocol_owned_position(self) -> DriftResult<i64> {
self.base_asset_amount_with_amm
.safe_add(self.base_asset_amount_with_unsettled_lp)?
Expand Down
4 changes: 4 additions & 0 deletions programs/drift/src/state/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,10 @@ pub enum AssetType {
}

impl Order {
pub fn seconds_til_expiry(self, now: i64) -> i64 {
self.max_ts - now
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn has_oracle_price_offset(self) -> bool {
self.oracle_price_offset != 0
}
Expand Down
Loading