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/calculate_long_short_vol_spread-math-in-u128 #352

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- program: fix overflow in calculate_long_short_vol_spread ([#352](https://github.com/drift-labs/protocol-v2/pull/352))
- program: dont let users disable margin trading if they have margin orders open
- program: tweaks to fix max leverage order param flag with imf factor ([#351](https://github.com/drift-labs/protocol-v2/pull/351))

Expand Down
49 changes: 28 additions & 21 deletions programs/drift/src/math/amm_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::math::constants::{
AMM_TO_QUOTE_PRECISION_RATIO_I128, BID_ASK_SPREAD_PRECISION, BID_ASK_SPREAD_PRECISION_I128,
BID_ASK_SPREAD_PRECISION_U128, DEFAULT_LARGE_BID_ASK_FACTOR,
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT, MAX_BID_ASK_INVENTORY_SKEW_FACTOR,
PEG_PRECISION, PERCENTAGE_PRECISION_U64, PRICE_PRECISION, PRICE_PRECISION_I128,
PEG_PRECISION, PERCENTAGE_PRECISION, PRICE_PRECISION, PRICE_PRECISION_I128,
};
use crate::math::safe_math::SafeMath;

Expand Down Expand Up @@ -105,43 +105,50 @@ pub fn calculate_long_short_vol_spread(
reserve_price: u64,
mark_std: u64,
oracle_std: u64,
long_intensity: u64,
short_intensity: u64,
long_intensity_volume: u64,
short_intensity_volume: u64,
volume_24h: u64,
) -> DriftResult<(u64, u64)> {
// 1.6 * std
let market_avg_std_pct = oracle_std
let market_avg_std_pct: u128 = oracle_std
.safe_add(mark_std)?
.safe_mul(PERCENTAGE_PRECISION_U64)?
.safe_div(reserve_price)?
.cast::<u128>()?
.safe_mul(PERCENTAGE_PRECISION)?
.safe_div(reserve_price.cast::<u128>()?)?
.safe_div(2)?;

let vol_spread: u64 = last_oracle_conf_pct.max(market_avg_std_pct.safe_div(2)?);
let vol_spread: u128 = last_oracle_conf_pct
.cast::<u128>()?
.max(market_avg_std_pct.safe_div(2)?);

let factor_clamp_min = PERCENTAGE_PRECISION_U64 / 100; // .01
let factor_clamp_max = 16 * PERCENTAGE_PRECISION_U64 / 10; // 1.6
let factor_clamp_min: u128 = PERCENTAGE_PRECISION / 100; // .01
let factor_clamp_max: u128 = 16 * PERCENTAGE_PRECISION / 10; // 1.6

let long_vol_spread_factor: u64 = long_intensity
.safe_mul(PERCENTAGE_PRECISION_U64)?
.safe_div(max(volume_24h, 1))?
let long_vol_spread_factor: u128 = long_intensity_volume
.cast::<u128>()?
.safe_mul(PERCENTAGE_PRECISION)?
.safe_div(max(volume_24h.cast::<u128>()?, 1))?
.clamp(factor_clamp_min, factor_clamp_max);
let short_vol_spread_factor: u64 = short_intensity
.safe_mul(PERCENTAGE_PRECISION_U64)?
.safe_div(max(volume_24h, 1))?
let short_vol_spread_factor: u128 = short_intensity_volume
.cast::<u128>()?
.safe_mul(PERCENTAGE_PRECISION)?
.safe_div(max(volume_24h.cast::<u128>()?, 1))?
.clamp(factor_clamp_min, factor_clamp_max);

Ok((
max(
last_oracle_conf_pct,
vol_spread
.safe_mul(long_vol_spread_factor)?
.safe_div(PERCENTAGE_PRECISION_U64)?,
.safe_div(PERCENTAGE_PRECISION)?
.cast::<u64>()?,
),
max(
last_oracle_conf_pct,
vol_spread
.safe_mul(short_vol_spread_factor)?
.safe_div(PERCENTAGE_PRECISION_U64)?,
.safe_div(PERCENTAGE_PRECISION)?
.cast::<u64>()?,
),
))
}
Expand Down Expand Up @@ -274,17 +281,17 @@ pub fn calculate_spread(
max_base_asset_reserve: u128,
mark_std: u64,
oracle_std: u64,
long_intensity: u64,
short_intensity: u64,
long_intensity_volume: u64,
short_intensity_volume: u64,
volume_24h: u64,
) -> DriftResult<(u32, u32)> {
let (long_vol_spread, short_vol_spread) = calculate_long_short_vol_spread(
last_oracle_conf_pct,
reserve_price,
mark_std,
oracle_std,
long_intensity,
short_intensity,
long_intensity_volume,
short_intensity_volume,
volume_24h,
)?;

Expand Down