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: improve-perp-validate-check-ref-price-offset #1563

Merged
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
12 changes: 7 additions & 5 deletions programs/drift/src/controller/position/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,8 @@ fn amm_perp_ref_offset() {
max_ref_offset,
)
.unwrap();
assert_eq!(res, 18000);
assert_eq!(perp_market.amm.reference_price_offset, 18000);
assert_eq!(res, 45000);
assert_eq!(perp_market.amm.reference_price_offset, 18000); // not updated vs market account

let now = 1741207620 + 1;
let clock_slot = 324817761 + 1; // todo
Expand Down Expand Up @@ -696,12 +696,14 @@ fn amm_perp_ref_offset() {
perp_market.amm.historical_oracle_data.last_oracle_price,
7101600
);
assert_eq!(perp_market.amm.reference_price_offset, 18000);
assert_eq!(perp_market.amm.reference_price_offset, 45000);
assert_eq!(perp_market.amm.max_spread, 90000);

assert_eq!(r, 7101599);
assert_eq!(perp_market.amm.bid_base_asset_reserve, 4633657972174584);
assert_eq!(perp_market.amm.ask_base_asset_reserve, 4631420570932586);
assert_eq!(perp_market.amm.bid_base_asset_reserve, 4570430670410018);
assert_eq!(perp_market.amm.ask_base_asset_reserve, 4568069910766211);

crate::validation::perp_market::validate_perp_market(&perp_market).unwrap();
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions programs/drift/src/math/amm_spread/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ mod test {
let max_ref_offset = market.amm.get_max_reference_price_offset().unwrap();
assert_eq!(max_ref_offset, 10000); // 100 bps

market.amm.max_spread = 10000 * 10; // 10%
market.amm.max_spread = 10000 * 5; // 5%
let max_ref_offset = market.amm.get_max_reference_price_offset().unwrap();
assert_eq!(max_ref_offset, 20000); // 200 bps (5% of max spread)
assert_eq!(max_ref_offset, 25000); // 250 bps (5% of max spread)

let orig_price = calculate_price(
amm.quote_asset_reserve,
Expand Down
3 changes: 2 additions & 1 deletion programs/drift/src/math/cp_curve/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ fn calculate_k_tests_with_spread() {
market.amm.bid_base_asset_reserve >= market.amm.base_asset_reserve
&& market.amm.bid_quote_asset_reserve <= market.amm.quote_asset_reserve,
ErrorCode::InvalidAmmDetected,
"bid reserves out of wack: {} -> {}, quote: {} -> {}",
"market index {} amm bid reserves invalid: {} -> {}, quote: {} -> {}",
market.market_index,
market.amm.bid_base_asset_reserve,
market.amm.base_asset_reserve,
market.amm.bid_quote_asset_reserve,
Expand Down
181 changes: 181 additions & 0 deletions programs/drift/src/state/lp_pool/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
use super::*;
use crate::math::constants::PERCENTAGE_PRECISION_U64;
use crate::state::oracle::OracleSource;

fn weight_datum(data: u64, last_slot: u64) -> WeightDatum {
WeightDatum { data, last_slot }
}

fn dummy_constituent(index: u16) -> Constituent {
Constituent {
pubkey: Pubkey::default(),
constituent_index: index,
oracle: Pubkey::default(),
oracle_source: OracleSource::Pyth,
max_weight_deviation: 0,
swap_fee_min: 0,
max_fee_premium: 0,
spot_market_index: index,
last_oracle_price: 0,
last_oracle_price_ts: 0,
spot_balance: BLPosition {
scaled_balance: 0,
cumulative_deposits: 0,
market_index: index,
balance_type: SpotBalanceType::Deposit,
padding: [0; 4],
},
}
}

#[test]
fn test_single_zero_weight() {
let mut mapping = AmmConstituentMapping {
num_rows: 1,
num_cols: 1,
data: vec![weight_datum(0, 0)],
};

let amm_inventory = vec![1_000_000u64]; // 1 unit
let prices = vec![1_000_000u64]; // price = 1.0
let constituents = vec![dummy_constituent(0)];
let aum = 1_000_000; // 1 USD
let now_ts = 1000;

let mut target = ConstituentTargetWeights {
num_rows: 0,
num_cols: 0,
oldest_weight_ts: 0,
data: vec![],
};

target.update_target_weights(
&mapping,
&amm_inventory,
&constituents,
&prices,
aum,
now_ts,
);

assert_eq!(target.data.len(), 1);
assert_eq!(target.data[0].data, 0);
assert_eq!(target.data[0].last_slot, now_ts);
}

#[test]
fn test_single_full_weight() {
let mut mapping = AmmConstituentMapping {
num_rows: 1,
num_cols: 1,
data: vec![weight_datum(PERCENTAGE_PRECISION_U64, 0)],
};

let amm_inventory = vec![1_000_000];
let prices = vec![1_000_000]; // price = 1.0
let constituents = vec![dummy_constituent(0)];
let aum = 1_000_000; // 1 USD
let now_ts = 1234;

let mut target = ConstituentTargetWeights::default();
target.update_target_weights(
&mapping,
&amm_inventory,
&constituents,
&prices,
aum,
now_ts,
);

assert_eq!(target.data.len(), 1);
assert_eq!(target.data[0].data, PERCENTAGE_PRECISION_U64); // 100%
assert_eq!(target.data[0].last_slot, now_ts);
}

#[test]
fn test_multiple_constituents_partial_weights() {
let mut mapping = AmmConstituentMapping {
num_rows: 1,
num_cols: 2,
data: vec![
weight_datum(PERCENTAGE_PRECISION_U64 / 2, 0),
weight_datum(PERCENTAGE_PRECISION_U64 / 2, 0),
],
};

let amm_inventory = vec![1_000_000];
let prices = vec![1_000_000, 1_000_000];
let constituents = vec![dummy_constituent(0), dummy_constituent(1)];
let aum = 1_000_000;
let now_ts = 999;

let mut target = ConstituentTargetWeights::default();
target.update_target_weights(
&mapping,
&amm_inventory,
&constituents,
&prices,
aum,
now_ts,
);

assert_eq!(target.data.len(), 2);
assert_eq!(target.data[0].data, PERCENTAGE_PRECISION_U64 / 2);
assert_eq!(target.data[1].data, PERCENTAGE_PRECISION_U64 / 2);
}

#[test]
fn test_zero_aum_safe() {
let mut mapping = AmmConstituentMapping {
num_rows: 1,
num_cols: 1,
data: vec![weight_datum(PERCENTAGE_PRECISION_U64, 0)],
};

let amm_inventory = vec![1_000_000];
let prices = vec![1_000_000];
let constituents = vec![dummy_constituent(0)];
let aum = 0;
let now_ts = 111;

let mut target = ConstituentTargetWeights::default();
target.update_target_weights(
&mapping,
&amm_inventory,
&constituents,
&prices,
aum,
now_ts,
);

assert_eq!(target.data.len(), 2);
assert_eq!(target.data[0].data, 0); // No division by zero panic
}

#[test]
fn test_overflow_protection() {
let mut mapping = AmmConstituentMapping {
num_rows: 1,
num_cols: 1,
data: vec![weight_datum(u64::MAX, 0)],
};

let amm_inventory = vec![u64::MAX];
let prices = vec![u64::MAX];
let constituents = vec![dummy_constituent(0)];
let aum = 1; // smallest possible AUM to maximize weight
let now_ts = 222;

let mut target = ConstituentTargetWeights::default();
target.update_target_weights(
&mapping,
&amm_inventory,
&constituents,
&prices,
aum,
now_ts,
);

assert_eq!(target.data.len(), 2);
assert!(target.data[0].data <= PERCENTAGE_PRECISION_U64); // cap at max
}
4 changes: 2 additions & 2 deletions programs/drift/src/state/perp_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,10 +1210,10 @@ impl AMM {
let lower_bound_multiplier: i64 =
self.curve_update_intensity.safe_sub(100)?.cast::<i64>()?;

// always allow 1-100 bps of price offset, up to a fifth of the market's max_spread
// always allow 1-100 bps of price offset, up to half of the market's max_spread
let lb_bps =
(PERCENTAGE_PRECISION.cast::<i64>()? / 10000).safe_mul(lower_bound_multiplier)?;
let max_offset = (self.max_spread.cast::<i64>()? / 5).max(lb_bps);
let max_offset = (self.max_spread.cast::<i64>()? / 2).max(lb_bps);

Ok(max_offset)
}
Expand Down
Loading