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: oracle-normalise-on-safe-contract-tiers #1100

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
7 changes: 6 additions & 1 deletion programs/drift/src/math/amm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,12 @@ pub fn update_oracle_price_twap(
None => amm.reserve_price()?,
};

let oracle_price = normalise_oracle_price(amm, oracle_price_data, Some(reserve_price))?;
// only normalise high confidence interval oracle if contract tier is A or B
let oracle_price = if sanitize_clamp.unwrap_or(0) > 2 {
normalise_oracle_price(amm, oracle_price_data, Some(reserve_price))?
} else {
oracle_price_data.price
};

let capped_oracle_update_price = sanitize_new_price(
oracle_price,
Expand Down
13 changes: 7 additions & 6 deletions programs/drift/src/math/amm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::math::constants::{
PRICE_PRECISION_U64, QUOTE_PRECISION,
};
use crate::state::oracle::HistoricalOracleData;
use crate::state::perp_market::PerpMarket;
use crate::state::perp_market::{ContractTier, PerpMarket};
use crate::state::user::PerpPosition;

#[test]
Expand All @@ -15,6 +15,7 @@ fn calculate_amm_available_guards() {
// let px = 32 * PRICE_PRECISION;

let mut market: PerpMarket = PerpMarket::default_btc_test();
market.contract_tier = ContractTier::HighlySpeculative;

let _oracle_price_data = OraclePriceData {
price: (34 * PRICE_PRECISION) as i64,
Expand Down Expand Up @@ -656,7 +657,7 @@ fn update_mark_twap_tests() {

while now < 3600 {
now += 1;
update_oracle_price_twap(&mut amm, now, &oracle_price_data, None, None).unwrap();
update_oracle_price_twap(&mut amm, now, &oracle_price_data, None, Some(3)).unwrap();
update_mark_twap_from_estimates(
&mut amm,
now,
Expand All @@ -675,12 +676,12 @@ fn update_mark_twap_tests() {
assert!(new_bid_twap <= new_ask_twap);
assert_eq!((new_bid_twap + new_ask_twap) / 2, new_mark_twap);
assert!((new_oracle_twap as u64) < new_mark_twap); // funding in favor of maker?
assert_eq!(new_oracle_twap, 40008161);
assert_eq!(new_oracle_twap, 40008162);
assert_eq!(new_bid_twap, 40014547);
assert_eq!(new_mark_twap, 40024054); // ~ 2 cents above oracle twap
assert_eq!(new_ask_twap, 40033561);
assert_eq!(amm.mark_std, 27230);
assert_eq!(amm.oracle_std, 3119);
assert_eq!(amm.oracle_std, 3118);

let trade_price_2 = 39_971_280 * PRICE_PRECISION_U64 / 1_000_000;
let trade_direction_2 = PositionDirection::Short;
Expand Down Expand Up @@ -754,7 +755,7 @@ fn calc_oracle_twap_tests() {
};

let _new_oracle_twap =
update_oracle_price_twap(&mut amm, now, &oracle_price_data, None, None).unwrap();
update_oracle_price_twap(&mut amm, now, &oracle_price_data, None, Some(3)).unwrap();
assert_eq!(
amm.historical_oracle_data.last_oracle_price_twap,
(34 * PRICE_PRECISION - PRICE_PRECISION / 100) as i64
Expand Down Expand Up @@ -880,7 +881,7 @@ fn calc_oracle_twap_clamp_update_tests() {
assert_eq!(amm.last_oracle_normalised_price, 33_760_245);

while now < prev + 3600 * 10 {
update_oracle_price_twap(&mut amm, now, &oracle_price_data, None, None).unwrap();
update_oracle_price_twap(&mut amm, now, &oracle_price_data, None, Some(3)).unwrap();
now += 1;
}

Expand Down
2 changes: 1 addition & 1 deletion programs/drift/src/math/oracle/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn calculate_oracle_valid() {
assert!(!oracle_status.mark_too_divergent);

let _new_oracle_twap =
update_oracle_price_twap(&mut market.amm, now, &oracle_price_data, None, None).unwrap();
update_oracle_price_twap(&mut market.amm, now, &oracle_price_data, None, Some(3)).unwrap();
assert_eq!(
market.amm.historical_oracle_data.last_oracle_price_twap,
(34 * PRICE_PRECISION - PRICE_PRECISION / 100) as i64
Expand Down
1 change: 1 addition & 0 deletions programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ impl PerpMarket {
margin_ratio_initial: 1000, // 10x
margin_ratio_maintenance: 500, // 5x
status: MarketStatus::Initialized,
contract_tier: ContractTier::A,
..PerpMarket::default()
}
}
Expand Down
Loading