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/make-auction-start-baseline-more-passive #890

Merged
merged 6 commits into from
Feb 20, 2024
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
83 changes: 53 additions & 30 deletions programs/drift/src/state/order_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,40 @@ impl OrderParams {
perp_market: &PerpMarket,
direction: PositionDirection,
) -> DriftResult<i64> {
// price offsets baselines for perp market auctions
let mark_twap_slow = perp_market
if perp_market
.amm
.last_ask_price_twap
.safe_add(perp_market.amm.last_bid_price_twap)?
.safe_div(2)?
.cast::<i64>()?;
.historical_oracle_data
.last_oracle_price_twap_ts
.safe_sub(perp_market.amm.last_mark_price_twap_ts)?
.abs()
>= 60
{
// if uncertain with timestamp mismatch, enforce within N bps
let price_divisor = if perp_market
.contract_tier
.is_as_safe_as_contract(&ContractTier::B)
{
500
} else {
100
};

return Ok(match direction {
PositionDirection::Long => {
perp_market.amm.last_bid_price_twap.cast::<i64>()? / price_divisor
Copy link
Member

Choose a reason for hiding this comment

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

might be good to add comment that it's starting at bid (beneath oracle) intentionally to get a good price?

}
PositionDirection::Short => {
-(perp_market.amm.last_ask_price_twap.cast::<i64>()? / price_divisor)
Copy link
Member

Choose a reason for hiding this comment

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

should short start above oracle, why is this negative?

Copy link
Member Author

Choose a reason for hiding this comment

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

this is only when *_twap_ts has more than 60 second mismatch (cant be trusted to enforce optimistic starts), so the boundary is not strict at all

}
});
}

// price offsets baselines for perp market auctions
let mark_twap_slow = match direction {
PositionDirection::Long => perp_market.amm.last_bid_price_twap,
PositionDirection::Short => perp_market.amm.last_ask_price_twap,
}
.cast::<i64>()?;

let baseline_start_price_offset_slow = mark_twap_slow.safe_sub(
perp_market
Expand All @@ -402,31 +429,27 @@ impl OrderParams {
.last_oracle_price_twap_5min,
)?;

let frac_of_long_spread_in_price: i64 = perp_market
Copy link
Member

Choose a reason for hiding this comment

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

can save some compute by doing the calculation in the match blocks

Copy link
Member Author

Choose a reason for hiding this comment

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

i need both of them for both though

for the slow, i pushing it up slightly from the opposite side of the book
for the fast, im pulling it away from the mid

.amm
.long_spread
.cast::<i64>()?
.safe_mul(mark_twap_slow)?
.safe_div(PRICE_PRECISION_I64 * 10)?;

let frac_of_short_spread_in_price: i64 = perp_market
.amm
.short_spread
.cast::<i64>()?
.safe_mul(mark_twap_slow)?
.safe_div(PRICE_PRECISION_I64 * 10)?;

let baseline_start_price_offset = match direction {
PositionDirection::Long => {
let frac_of_spread_in_price: i64 = perp_market
.amm
.long_spread
.cast::<i64>()?
.safe_mul(mark_twap_slow)?
.safe_div(PRICE_PRECISION_I64 * 10)?;

baseline_start_price_offset_slow
.max(baseline_start_price_offset_fast)
.safe_add(frac_of_spread_in_price)?
}
PositionDirection::Short => {
let frac_of_spread_in_price: i64 = perp_market
.amm
.short_spread
.cast::<i64>()?
.safe_mul(mark_twap_slow)?
.safe_div(PRICE_PRECISION_I64 * 10)?;

baseline_start_price_offset_slow
.min(baseline_start_price_offset_fast)
.safe_sub(frac_of_spread_in_price)?
}
PositionDirection::Long => baseline_start_price_offset_slow
.safe_add(frac_of_long_spread_in_price)?
.min(baseline_start_price_offset_fast.safe_sub(frac_of_short_spread_in_price)?),
Copy link
Member

Choose a reason for hiding this comment

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

so before we were doing max, which gives the more aggressive, now we're doing min, which is less aggresswive?

Copy link
Member Author

Choose a reason for hiding this comment

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

correct, doing min for long is less aggressive

PositionDirection::Short => baseline_start_price_offset_slow
.safe_sub(frac_of_short_spread_in_price)?
.max(baseline_start_price_offset_fast.safe_add(frac_of_long_spread_in_price)?),
};

Ok(baseline_start_price_offset)
Expand Down
2 changes: 2 additions & 0 deletions programs/drift/src/state/order_params/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,10 @@ mod get_close_perp_params {
let amm = AMM {
last_ask_price_twap: 103 * PRICE_PRECISION_U64,
last_bid_price_twap: 101 * PRICE_PRECISION_U64,
last_mark_price_twap_5min: 102 * PRICE_PRECISION_U64,
historical_oracle_data: HistoricalOracleData {
last_oracle_price_twap: 100 * PRICE_PRECISION_I64,
last_oracle_price_twap_5min: 100 * PRICE_PRECISION_I64,
..HistoricalOracleData::default()
},
mark_std: PRICE_PRECISION_U64,
Expand Down
9 changes: 4 additions & 5 deletions programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,10 @@ impl PerpMarket {

let open_interest = self.get_open_interest();

let depth = self
.amm
.min_order_size
.safe_mul(1000)?
.max(open_interest.safe_div(500)?.cast::<u64>()?);
let depth = (open_interest.safe_div(1000)?.cast::<u64>()?).clamp(
self.amm.min_order_size.safe_mul(100)?,
self.amm.min_order_size.safe_mul(5000)?,
);

Ok(depth)
}
Expand Down
Loading