EWMA Gas Price Penalty
DeepBook implements an Exponentially Weighted Moving Average (EWMA) system to dynamically adjust taker fees based on network gas prices. This feature helps prevent toxic takers from using high gas prices to prioritize their transactions and pick off stale maker orders.
Overviewβ
The system tracks gas prices over time and applies a penalty fee to takers when the current gas price is unusually high compared to recent history. This protects makers from having their orders filled during periods of abnormal network activity where toxic takers might try to front-run or take advantage of stale orders.
How it worksβ
Core conceptβ
The EWMA system calculates a smoothed average and variance of recent gas prices, then compares the current gas price against this historical baseline. When the current gas price is significantly elevated (beyond a threshold measured in standard deviations), an additional taker fee penalty is applied.
Key componentsβ
- Mean (ΞΌ): Smoothed average of recent gas prices
- Variance (ΟΒ²): Measure of gas price volatility
- Standard Deviation (Ο): Square root of variance, used for z-score calculation
- Z-Score: How many standard deviations the current gas price is from the mean
- Z-Score Threshold: Trigger point for applying the penalty
The formulaβ
z_score = (current_gas_price - mean) / standard_deviation
if z_score > z_score_threshold:
apply additional_taker_fee
Configuration parametersβ
The following table shows the default configuration parameters for the EWMA system:
| Parameter | Value | Meaning |
|---|---|---|
| Alpha | 0.1 (100000000) | 10% weight on new data, 90% on history |
| Mean | 1,478 | Average gas price (smoothed) |
| Variance | 43,270,831 | Volatility measure |
| Std Deviation | 6,578 | Calculated: βvariance |
| Z-Score Threshold | 3.0 | Trigger: 3 standard deviations |
| Additional Taker Fee | 0.1% (1000000) | Penalty fee added |
When does the penalty apply?β
The penalty is applied only when ALL of the following conditions are met:
- EWMA is enabled for the pool
- Current gas price β₯ Mean (must be above average)
- Z-Score > Threshold (must exceed 3.0 standard deviations)
Penalty threshold calculationβ
Penalty Threshold = Mean + (Z-Score Threshold Γ Std Dev)
Penalty Threshold = 1,478 + (3.0 Γ 6,578)
Penalty Threshold = 1,478 + 19,734
Penalty Threshold β 21,212
Practical examplesβ
Example 1: Normal conditions (current)β
- Gas Price: 1,000
- Calculation: (1,000 - 1,478) / 6,578 = -0.073
- Z-Score: Negative, below mean
- Result: No penalty - Base taker fee only
Example 2: Slightly elevatedβ
- Gas Price: 5,000
- Calculation: (5,000 - 1,478) / 6,578 = 0.54
- Z-Score: 0.54
- Result: No penalty - Below 3.0 threshold
Example 3: High spikeβ
- Gas Price: 15,000
- Calculation: (15,000 - 1,478) / 6,578 = 2.06
- Z-Score: 2.06
- Result: No penalty - Still below 3.0 threshold
Example 4: Extreme spike (penalty triggered)β
- Gas Price: 25,000
- Calculation: (25,000 - 1,478) / 6,578 = 3.58
- Z-Score: 3.58
- Result: PENALTY APPLIED - Additional 0.1% fee added
Benefitsβ
- Maker Protection: Discourages takers from picking off stale maker orders during network congestion
- Dynamic Adjustment: Automatically adapts to changing gas price patterns over time
- Fair Pricing: Only penalizes during extreme conditions (3 standard deviations)
- Statistical Rigor: Uses well-established statistical methods to identify outliers
Technical notesβ
- Update Frequency: EWMA updates when an order is submitted. Only counts if the timestamp is different (multiple orders within the same transaction will only be counted once)
- Alpha Value: 0.1 means the system adapts relatively quickly to new price levels while maintaining historical context
- 3-Sigma Threshold: Statistically, only ~0.3% of observations exceed 3 standard deviations in a normal distribution (~0.15% above 3 standard deviations)
- Pool-Specific: Each pool has its own EWMA state tracked independently
APIβ
Following are the EWMA-related functions that DeepBook exposes.
Enable EWMA stateβ
Enable or disable the EWMA state for a pool. This allows the pool to use the EWMA state for volatility calculations and additional taker fees. Only admin can call this function.
packages/deepbook/sources/pool.move. You probably need to run `pnpm prebuild` and restart the site.Set EWMA parametersβ
Configure the EWMA parameters for a pool. Only admin can set these parameters. The parameters must meet the following constraints:
alpha: Must be β€ maximum EWMA alphaz_score_threshold: Must be β€ maximum z-score thresholdadditional_taker_fee: Must be β€ maximum additional taker fee
packages/deepbook/sources/pool.move. You probably need to run `pnpm prebuild` and restart the site.Related linksβ
The DeepBookV3 repository on GitHub.