Skip to content

Commit

Permalink
program: fix amm-jit erroring out when bids/asks are zero (#279)
Browse files Browse the repository at this point in the history
* init

* update changelog
  • Loading branch information
0xNineteen committed Dec 7, 2022
1 parent 0fc4025 commit f7d44a3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- program: fix amm-jit erroring out when bids/asks are zero ([#279](https://github.com/drift-labs/protocol-v2/pull/279))

### Breaking

## [2.2.0] - 2022-12-06
Expand Down
69 changes: 69 additions & 0 deletions programs/drift/src/controller/orders/amm_jit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,75 @@ pub mod amm_jit {

use super::*;

#[test]
fn zero_asks_with_amm_jit_taker_long() {
let oracle_price_key =
Pubkey::from_str("J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix").unwrap();

let mut market = PerpMarket {
amm: AMM {
base_asset_reserve: 100 * AMM_RESERVE_PRECISION,
quote_asset_reserve: 100 * AMM_RESERVE_PRECISION,
base_asset_amount_with_amm: (AMM_RESERVE_PRECISION / 2) as i128,
base_asset_amount_long: (AMM_RESERVE_PRECISION / 2) as i128,
sqrt_k: 100 * AMM_RESERVE_PRECISION,
peg_multiplier: 100 * PEG_PRECISION,
max_slippage_ratio: 50,
max_fill_reserve_fraction: 100,
order_step_size: 1000,
order_tick_size: 1,
oracle: oracle_price_key,
amm_jit_intensity: 100,
base_spread: 20000,
long_spread: 20000,
short_spread: 20000,
historical_oracle_data: HistoricalOracleData {
last_oracle_price: (100 * PRICE_PRECISION) as i64,
last_oracle_price_twap: (100 * PRICE_PRECISION) as i64,
last_oracle_price_twap_5min: (100 * PRICE_PRECISION) as i64,

..HistoricalOracleData::default()
},

..AMM::default()
},
margin_ratio_initial: 1000,
margin_ratio_maintenance: 500,
status: MarketStatus::Initialized,
..PerpMarket::default_test()
};
market.amm.base_asset_reserve = 0;
market.amm.max_base_asset_reserve = u64::MAX as u128;
market.amm.min_base_asset_reserve = 0;

let (new_ask_base_asset_reserve, new_ask_quote_asset_reserve) =
crate::math::amm_spread::calculate_spread_reserves(
&market.amm,
PositionDirection::Long,
)
.unwrap();
let (new_bid_base_asset_reserve, new_bid_quote_asset_reserve) =
crate::math::amm_spread::calculate_spread_reserves(
&market.amm,
PositionDirection::Short,
)
.unwrap();
market.amm.ask_base_asset_reserve = new_ask_base_asset_reserve;
market.amm.bid_base_asset_reserve = new_bid_base_asset_reserve;
market.amm.ask_quote_asset_reserve = new_ask_quote_asset_reserve;
market.amm.bid_quote_asset_reserve = new_bid_quote_asset_reserve;

// shouldnt throw an error when bids/asks are zero
crate::math::amm_jit::calculate_jit_base_asset_amount(
&market,
BASE_PRECISION_U64,
PRICE_PRECISION_U64,
Some(PRICE_PRECISION_I64),
PositionDirection::Long,
)
.unwrap();
}

#[test]
fn no_fulfill_with_amm_jit_taker_long() {
let now = 0_i64;
Expand Down
3 changes: 2 additions & 1 deletion programs/drift/src/math/amm_jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ pub fn calculate_jit_base_asset_amount(
let denominator = max_bids.min(max_asks);
let ratio = numerator
.safe_mul(AMM_RESERVE_PRECISION)?
.safe_div(denominator)?;
.safe_div(denominator)
.unwrap_or(u128::MAX);

let imbalanced_bound = 15_u128.safe_mul(AMM_RESERVE_PRECISION.safe_div(10)?)?;

Expand Down

0 comments on commit f7d44a3

Please sign in to comment.