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/fix for inventory scale max values #322

Merged
merged 6 commits into from
Jan 11, 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 @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- program: avoid overflow when calculating overflow ([#322](https://github.com/drift-labs/protocol-v2/pull/322))
- ts-sdk: fix user.getUnrealizedPnl to account for lp position
- program: cancel market order for not satisfying limit price only if there was some base asset amount filled

Expand Down
15 changes: 8 additions & 7 deletions programs/drift/src/math/amm_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,12 @@ pub fn calculate_spread_inventory_scale(

let min_side_liquidity = max_bids.min(max_asks.abs());

// cap so (6e9 * AMM_RESERVE_PRECISION)^2 < 2^127
let amm_inventory_size = base_asset_amount_with_amm.abs().min(6000000000000000000);

// inventory scale
let inventory_scale = base_asset_amount_with_amm
.safe_mul(
base_asset_amount_with_amm
.abs()
.max(AMM_RESERVE_PRECISION_I128),
)?
let inventory_scale = amm_inventory_size
.safe_mul(amm_inventory_size.max(AMM_RESERVE_PRECISION_I128))?
.safe_div(AMM_RESERVE_PRECISION_I128)?
.safe_mul(DEFAULT_LARGE_BID_ASK_FACTOR.cast::<i128>()?)?
.safe_div(min_side_liquidity.max(1))?
Expand All @@ -189,7 +188,9 @@ pub fn calculate_spread_inventory_scale(

let inventory_scale_capped = min(
inventory_scale_max,
BID_ASK_SPREAD_PRECISION.safe_add(inventory_scale.cast()?)?,
BID_ASK_SPREAD_PRECISION
.safe_add(inventory_scale.cast()?)
.unwrap_or(u64::MAX),
);

Ok(inventory_scale_capped)
Expand Down