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

program: bound-quote-reserve-divisor #1371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions programs/drift/src/math/amm_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,14 @@ pub fn calculate_spread_reserves(
};

let quote_asset_reserve_delta = if spread_with_offset.abs() > 1 {
let quote_reserve_divisor =
BID_ASK_SPREAD_PRECISION_I128 / (spread_with_offset / 2).cast::<i128>()?;
let mut quote_reserve_divisor =
(BID_ASK_SPREAD_PRECISION_I128 / (spread_with_offset / 2).cast::<i128>()?);

// ensure the divisor is not zero
if quote_reserve_divisor == 0 {
quote_reserve_divisor = if spread_with_offset > 0 { 1 } else { -1 };
}

market
.amm
.quote_asset_reserve
Expand All @@ -507,6 +513,7 @@ pub fn calculate_spread_reserves(
.amm
.quote_asset_reserve
.safe_sub(quote_asset_reserve_delta.unsigned_abs())?
.max(1)
Copy link
Member

Choose a reason for hiding this comment

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

dont need max/min in the other branch?

Copy link
Member Author

@0xbigz 0xbigz Dec 12, 2024

Choose a reason for hiding this comment

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

i think the way the spread math works, you just need to bound for percentage spread on shorts

};

if market.contract_type == ContractType::Prediction {
Expand Down
30 changes: 30 additions & 0 deletions programs/drift/src/math/amm_spread/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ mod test {
assert_eq!(res, 0);
}


#[test]
fn calculate_extreme_spread_tests() {
let amm = AMM {
base_asset_reserve: 2 * AMM_RESERVE_PRECISION,
quote_asset_reserve: 2 * AMM_RESERVE_PRECISION,
sqrt_k: 2 * AMM_RESERVE_PRECISION,
peg_multiplier: PEG_PRECISION,
long_spread: (BID_ASK_SPREAD_PRECISION as u32) + 1000,
short_spread: (BID_ASK_SPREAD_PRECISION as u32) + 1000,
max_spread: (BID_ASK_SPREAD_PRECISION/2) as u32,
curve_update_intensity: 100,
..AMM::default()
};

let mut market = PerpMarket {
amm,
..PerpMarket::default()
};

let (bar_l, qar_l) = calculate_spread_reserves(&market, PositionDirection::Long).unwrap();
let (bar_s, qar_s) = calculate_spread_reserves(&market, PositionDirection::Short).unwrap();

assert_eq!(bar_s, 4000000000000000000);
assert_eq!(qar_s, 1);

assert_eq!(bar_l, 1000000000);
assert_eq!(qar_l, 4000000000);
}

#[test]
fn calculate_spread_tests() {
let base_spread = 1000; // .1%
Expand Down
Loading