@@ -219,15 +219,40 @@ mod test {
219219
220220 let reserve_price = 4216 * 10000 ;
221221
222- let max_ref_offset = market. amm . get_max_reference_price_offset ( ) . unwrap ( ) ;
223- assert_eq ! ( max_ref_offset, 10000 ) ;
224- market. amm . base_asset_amount_with_amm = ( AMM_RESERVE_PRECISION * 3 / 20 ) as i128 ;
222+ market. amm . base_asset_amount_with_amm = ( AMM_RESERVE_PRECISION * 7 / 20 ) as i128 ;
223+ let inventory_ratio = calculate_inventory_liquidity_ratio_for_reference_price_offset (
224+ market. amm . base_asset_amount_with_amm ,
225+ market. amm . base_asset_reserve ,
226+ market. amm . min_base_asset_reserve ,
227+ market. amm . max_base_asset_reserve ,
228+ )
229+ . unwrap ( ) ;
230+ assert_eq ! ( inventory_ratio, 100000 ) ; // 10%
231+
232+ market. amm . reference_price_offset_deadband_pct = 10 ; // 10%
233+
234+ // If inventory exceeds threshold positive ref price offset
235+ market. amm . base_asset_amount_with_amm = ( AMM_RESERVE_PRECISION * 8 / 20 ) as i128 ;
236+ let ( _l, _s) = update_spreads ( & mut market, reserve_price as u64 , None ) . unwrap ( ) ;
237+ assert ! ( market. amm. reference_price_offset > 0 ) ;
238+
239+ // If inventory is small, goes to 0
240+ market. amm . base_asset_amount_with_amm = ( AMM_RESERVE_PRECISION * 6 / 20 ) as i128 ;
241+ let ( _l, _s) = update_spreads ( & mut market, reserve_price as u64 , None ) . unwrap ( ) ;
242+ assert_eq ! ( market. amm. reference_price_offset, 0 ) ;
243+
244+ // Same for short pos
245+ // Make sure that the premium is also short
246+ market. amm . last_24h_avg_funding_rate = -1 ;
247+ market. amm . last_mark_price_twap_5min = 4216 * 10000 - 2 * 10000 ;
248+ market. amm . last_mark_price_twap = 4216 * 10000 - 2 * 10000 ;
249+ market. amm . base_asset_amount_with_amm = ( AMM_RESERVE_PRECISION * 8 / 20 ) as i128 * -1 ;
225250 let ( _l, _s) = update_spreads ( & mut market, reserve_price as u64 , None ) . unwrap ( ) ;
226- assert_eq ! ( market. amm. reference_price_offset, 10 ) ;
251+ println ! ( "ref offset: {}" , market. amm. reference_price_offset) ;
252+ assert ! ( market. amm. reference_price_offset < 0 ) ;
227253
228- // If base asset amount with amm is small, reference price offset is 0
229- market. amm . reference_price_offset_deadband_pct = 10 ;
230- market. amm . base_asset_amount_with_amm = ( AMM_RESERVE_PRECISION * 3 / 20 ) as i128 ;
254+ // Same for short pos
255+ market. amm . base_asset_amount_with_amm = ( AMM_RESERVE_PRECISION * 6 / 20 ) as i128 * -1 ;
231256 let ( _l, _s) = update_spreads ( & mut market, reserve_price as u64 , None ) . unwrap ( ) ;
232257 assert_eq ! ( market. amm. reference_price_offset, 0 ) ;
233258 }
0 commit comments