Skip to content

Commit 34f832e

Browse files
authored
program: relax fill validation for spot/swap (#1796)
* program: relax fill validation for spot/swap * account for external fills * CHANGELOG
1 parent 92b6c59 commit 34f832e

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Fixes
1313

14+
- program: less aggressive fill bands for spot swaps ([#1796](https://github.com/drift-labs/protocol-v2/pull/1796))
15+
1416
### Breaking
1517

1618
## [2.130.0] - 2025-07-29

programs/drift/src/controller/orders.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ pub fn fill_perp_order(
13361336
.oracle_guard_rails
13371337
.max_oracle_twap_5min_percent_divergence(),
13381338
perp_market.is_prediction_market(),
1339+
None,
13391340
)?;
13401341

13411342
perp_market.last_fill_price = fill_price;
@@ -4102,6 +4103,11 @@ pub fn fill_spot_order(
41024103
.oracle_guard_rails
41034104
.max_oracle_twap_5min_percent_divergence(),
41044105
false,
4106+
if fulfillment_params.is_external() {
4107+
Some(order_direction)
4108+
} else {
4109+
None
4110+
},
41054111
)?;
41064112
}
41074113

programs/drift/src/math/orders.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ pub fn validate_fill_price_within_price_bands(
491491
margin_ratio_initial: u32,
492492
oracle_twap_5min_percent_divergence: u64,
493493
is_prediction_market: bool,
494+
direction: Option<PositionDirection>,
494495
) -> DriftResult {
495496
if is_prediction_market {
496497
validate!(
@@ -504,6 +505,22 @@ pub fn validate_fill_price_within_price_bands(
504505
return Ok(());
505506
}
506507

508+
if let Some(direction) = direction {
509+
if direction == PositionDirection::Long {
510+
if fill_price < oracle_price.cast::<u64>()?
511+
&& fill_price < oracle_twap_5min.cast::<u64>()?
512+
{
513+
return Ok(());
514+
}
515+
} else {
516+
if fill_price > oracle_price.cast::<u64>()?
517+
&& fill_price > oracle_twap_5min.cast::<u64>()?
518+
{
519+
return Ok(());
520+
}
521+
}
522+
}
523+
507524
let max_oracle_diff = margin_ratio_initial.cast::<u128>()?;
508525
let max_oracle_twap_diff = oracle_twap_5min_percent_divergence.cast::<u128>()?; // 50%
509526

programs/drift/src/math/orders/tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,6 +3177,7 @@ pub mod validate_fill_price_within_price_bands {
31773177
margin_ratio_initial,
31783178
(PERCENTAGE_PRECISION / 2) as u64,
31793179
false,
3180+
None,
31803181
)
31813182
.is_ok())
31823183
}
@@ -3195,6 +3196,7 @@ pub mod validate_fill_price_within_price_bands {
31953196
margin_ratio_initial,
31963197
(PERCENTAGE_PRECISION / 2) as u64,
31973198
false,
3199+
None,
31983200
)
31993201
.is_ok())
32003202
}
@@ -3214,6 +3216,7 @@ pub mod validate_fill_price_within_price_bands {
32143216
margin_ratio_initial,
32153217
(PERCENTAGE_PRECISION / 2) as u64,
32163218
false,
3219+
None,
32173220
)
32183221
.is_err())
32193222
}
@@ -3233,6 +3236,7 @@ pub mod validate_fill_price_within_price_bands {
32333236
margin_ratio_initial,
32343237
(PERCENTAGE_PRECISION / 2) as u64,
32353238
false,
3239+
None,
32363240
)
32373241
.is_err())
32383242
}
@@ -3252,6 +3256,7 @@ pub mod validate_fill_price_within_price_bands {
32523256
margin_ratio_initial,
32533257
(PERCENTAGE_PRECISION / 2) as u64,
32543258
false,
3259+
None,
32553260
)
32563261
.is_err())
32573262
}
@@ -3271,6 +3276,7 @@ pub mod validate_fill_price_within_price_bands {
32713276
margin_ratio_initial,
32723277
(PERCENTAGE_PRECISION / 2) as u64,
32733278
false,
3279+
None,
32743280
)
32753281
.is_err())
32763282
}
@@ -3290,6 +3296,7 @@ pub mod validate_fill_price_within_price_bands {
32903296
margin_ratio_initial,
32913297
(PERCENTAGE_PRECISION / 2) as u64,
32923298
false,
3299+
None,
32933300
)
32943301
.is_err())
32953302
}
@@ -3309,6 +3316,7 @@ pub mod validate_fill_price_within_price_bands {
33093316
margin_ratio_initial,
33103317
(PERCENTAGE_PRECISION / 2) as u64,
33113318
false,
3319+
None,
33123320
)
33133321
.is_err())
33143322
}

programs/drift/src/math/spot_swap.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn validate_price_bands_for_swap(
100100
out_price: i64,
101101
oracle_twap_5min_percent_divergence: u64,
102102
) -> DriftResult {
103-
let (fill_price, oracle_price, oracle_twap_5min, margin_ratio) = {
103+
let (fill_price, direction, oracle_price, oracle_twap_5min, margin_ratio) = {
104104
let in_market_margin_ratio = in_market.get_margin_ratio(&MarginRequirementType::Initial)?;
105105

106106
if in_market_margin_ratio != 0 {
@@ -113,6 +113,7 @@ pub fn validate_price_bands_for_swap(
113113

114114
(
115115
fill_price,
116+
PositionDirection::Short,
116117
in_price,
117118
in_market.historical_oracle_data.last_oracle_price_twap_5min,
118119
in_market_margin_ratio,
@@ -123,6 +124,7 @@ pub fn validate_price_bands_for_swap(
123124

124125
(
125126
fill_price,
127+
PositionDirection::Long,
126128
out_price,
127129
out_market
128130
.historical_oracle_data
@@ -139,6 +141,7 @@ pub fn validate_price_bands_for_swap(
139141
margin_ratio,
140142
oracle_twap_5min_percent_divergence,
141143
false,
144+
Some(direction),
142145
)?;
143146

144147
Ok(())

0 commit comments

Comments
 (0)