diff --git a/CHANGELOG.md b/CHANGELOG.md index cc542cdd6..b0d3aaecc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - program: track fuel ([#1048](https://github.com/drift-labs/protocol-v2/pull/1048)) - program: track fuel for if staking ([#1127](https://github.com/drift-labs/protocol-v2/pull/1127)) - program: validate fee structure ([#1075](https://github.com/drift-labs/protocol-v2/pull/1075)) +- program: check 5 min oracle twap divergence in trigger order ([#1116](https://github.com/drift-labs/protocol-v2/pull/1116)) ### Fixes diff --git a/programs/drift/src/controller/orders.rs b/programs/drift/src/controller/orders.rs index ba7844404..0c7b584c0 100644 --- a/programs/drift/src/controller/orders.rs +++ b/programs/drift/src/controller/orders.rs @@ -1058,6 +1058,7 @@ pub fn fill_perp_order( .max_oracle_twap_5min_percent_divergence() .cast()?, )?; + if oracle_too_divergent_with_twap_5min { // update filler last active so tx doesn't revert if let Some(filler) = filler.as_deref_mut() { @@ -2683,6 +2684,24 @@ pub fn trigger_order( let oracle_price = oracle_price_data.price; + let oracle_too_divergent_with_twap_5min = is_oracle_too_divergent_with_twap_5min( + oracle_price_data.price, + perp_market + .amm + .historical_oracle_data + .last_oracle_price_twap_5min, + state + .oracle_guard_rails + .max_oracle_twap_5min_percent_divergence() + .cast()?, + )?; + + validate!( + !oracle_too_divergent_with_twap_5min, + ErrorCode::OrderBreachesOraclePriceLimits, + "oracle price vs twap too divergent" + )?; + let can_trigger = order_satisfies_trigger_condition( &user.orders[order_index], oracle_price.unsigned_abs().cast()?, @@ -4899,6 +4918,23 @@ pub fn trigger_spot_order( let oracle_price = oracle_price_data.price; + let oracle_too_divergent_with_twap_5min = is_oracle_too_divergent_with_twap_5min( + oracle_price_data.price, + spot_market + .historical_oracle_data + .last_oracle_price_twap_5min, + state + .oracle_guard_rails + .max_oracle_twap_5min_percent_divergence() + .cast()?, + )?; + + validate!( + !oracle_too_divergent_with_twap_5min, + ErrorCode::OrderBreachesOraclePriceLimits, + "oracle price vs twap too divergent" + )?; + let can_trigger = order_satisfies_trigger_condition( &user.orders[order_index], oracle_price.unsigned_abs().cast()?,