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/update-amm-last-update-slot-neg-tfmq #292

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

### Fixes

- program: disable lower bound check for update amm once it's already been breached ([#292](https://github.com/drift-labs/protocol-v2/pull/292))
- ts-sdk: fix DLOB.updateOrder ([#290](https://github.com/drift-labs/protocol-v2/pull/290))
- ts-sdk: make calculateClaimablePnl mirror on-chain logic ([#291](https://github.com/drift-labs/protocol-v2/pull/291))
- ts-sdk: add margin trading toggle field to user accounts, update toggle margin trading function to add ability to toggle for any subaccount rather than just the active ([#285](https://github.com/drift-labs/protocol-v2/pull/285))
Expand Down
12 changes: 6 additions & 6 deletions programs/drift/src/controller/repeg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,16 +503,16 @@ pub fn update_amm_larg_conf_w_neg_tfmd_test() {
};

let cost_of_update = _update_amm(&mut market, &oracle_price_data, &state, now, slot).unwrap();
assert_eq!(cost_of_update, 0);
assert_eq!(market.amm.long_spread, 377);
assert_eq!(market.amm.short_spread, 975 - 377);
assert_eq!(cost_of_update, 299407);
assert_eq!(market.amm.long_spread, 378);
assert_eq!(market.amm.short_spread, 975 - 378);

let mrk = market.amm.reserve_price().unwrap();
let (bid, ask) = market.amm.bid_ask_price(mrk).unwrap();

assert_eq!(bid, 18817611205);
assert_eq!(mrk, 18828870870);
assert_eq!(ask, 18835969354);
assert_eq!(bid, 18817335418);
assert_eq!(mrk, 18828576078);
assert_eq!(ask, 18835693279);
assert_eq!((oracle_price_data.price as u64) > bid, true);
assert_eq!((oracle_price_data.price as u64) < ask, true);
}
4 changes: 4 additions & 0 deletions programs/drift/src/math/repeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ pub fn calculate_optimal_peg_and_budget(

fee_budget = calculate_repeg_cost(&market.amm, optimal_peg)?.cast::<u128>()?;

check_lower_bound = false;
} else if market.amm.total_fee_minus_distributions
< get_total_fee_lower_bound(market)?.cast()?
{
check_lower_bound = false;
}
}
Expand Down
72 changes: 72 additions & 0 deletions programs/drift/src/math/repeg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::math::constants::{
QUOTE_PRECISION,
};
use crate::math::repeg::*;
use crate::state::oracle::HistoricalOracleData;
use crate::state::state::{PriceDivergenceGuardRails, State, ValidityGuardRails};

#[test]
fn calc_peg_tests() {
Expand Down Expand Up @@ -158,6 +160,76 @@ fn calculate_optimal_peg_and_budget_test() {
assert!(!check_lb);
}

#[test]
fn calculate_optimal_peg_and_budget_2_test() {
let mut market = PerpMarket {
amm: AMM {
base_asset_reserve: 2270516211133,
quote_asset_reserve: 2270925669621,
terminal_quote_asset_reserve: 2270688451627,
sqrt_k: 2270720931148,
peg_multiplier: 17723081263,
base_asset_amount_with_amm: 237200000,
mark_std: 43112524,
last_mark_price_twap_ts: 0,
base_spread: 250,
curve_update_intensity: 100,
max_spread: 500 * 100,
total_exchange_fee: 298628987,
total_fee_minus_distributions: -242668966,
total_fee_withdrawn: 124247717,
concentration_coef: 1020710,
historical_oracle_data: HistoricalOracleData {
last_oracle_price_twap: 17765940050,
last_oracle_price_twap_5min: 17763317077,
..HistoricalOracleData::default()
},
..AMM::default()
},
margin_ratio_initial: 500,

..PerpMarket::default()
};

let oracle_price_data = OraclePriceData {
price: (17_800 * PRICE_PRECISION) as i64,
confidence: 10233,
delay: 2,
has_sufficient_number_of_data_points: true,
};

let (optimal_peg, budget, check_lb) =
calculate_optimal_peg_and_budget(&market, &oracle_price_data).unwrap();

assert_eq!(optimal_peg, 17796790576);
assert_eq!(optimal_peg > oracle_price_data.price as u128, false);
assert_eq!(budget, 0);
assert_eq!(check_lb, false); // because market.amm.total_fee_minus_distributions < get_total_fee_lower_bound(market)?.cast()
use crate::controller::repeg::*;

let state = State {
oracle_guard_rails: OracleGuardRails {
price_divergence: PriceDivergenceGuardRails {
mark_oracle_divergence_numerator: 1,
mark_oracle_divergence_denominator: 10,
},
validity: ValidityGuardRails {
slots_before_stale_for_amm: 10, // 5s
slots_before_stale_for_margin: 120, // 60s
confidence_interval_max_size: 1000,
too_volatile_ratio: 5,
},
},
..State::default()
};

// test amm update
assert_eq!(market.amm.last_update_slot, 0);
let c = _update_amm(&mut market, &oracle_price_data, &state, 1, 1337).unwrap();
assert_eq!(c, 424);
assert_eq!(market.amm.last_update_slot, 1337);
}

#[test]
fn calc_adjust_amm_tests_repeg_in_favour() {
// btc-esque market
Expand Down