Skip to content

Commit

Permalink
program: check 5 min oracle twap divergence in trigger order (#1116)
Browse files Browse the repository at this point in the history
* program: check 5 min oracle twap divergence in trigger order

* CHANGELOG

* fix typo
  • Loading branch information
crispheaney authored Jul 15, 2024
1 parent 20e24ce commit fd85513
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()?,
Expand Down Expand Up @@ -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()?,
Expand Down

0 comments on commit fd85513

Please sign in to comment.