-
Notifications
You must be signed in to change notification settings - Fork 124
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -771,6 +771,45 @@ | |
} | ||
|
||
impl AMM { | ||
pub fn get_fallback_price(self, | ||
direction: &PositionDirection, | ||
amm_available_liquidity: u64, | ||
oracle_price: i64, | ||
seconds_til_order_expiry: i64, | ||
) -> DriftResult<i64> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
)?.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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)? | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs tests