EMA-based Mark Price Component Exhibits Excessive Lag Leading to Inaccurate Pricing
The basis spread EMA used in mark price calculation is computed over 900 snapshots instead of the intended 15-minute window, because the code passes 15 minutes as a snapshot count. This causes the mark price to lag behind market conditions for up to 2.25 hours, leading to inaccurate funding rates.
Submitted by BenRai, also found by Egbe, jayx, LeoScant, qpzm, and slowpoke
perps/types/Market.sol #L466
The Exponential Moving Average (EMA) calculation for basis spread in mark price determination uses an overly long time window (2,5 hours), causing the mark price to lag significantly behind actual market conditions and potentially leading to inaccurate pricing for up to 2.25 hours.
The perpetual trading system calculates mark price using a median of three components: funding rate component (p1), basis spread EMA component (p2), and impact price TWAP component (p3). According to the developer team, the setMarkPrice() function will be called every 10 seconds by administrators to update market pricing.
The critical issue lies in the getBasisSpreadEMA() function in Market.sol:
function getBasisSpreadEMA(Market storage self) internal view returns (int256 basisSpreadEMA) {
return StorageLib.loadMarketMetadata(self.asset).basisSpreadHistory.ema(15 minutes);
}
The EMA calculation in PriceHistory.sol intends to calculate the EMA over a 15-minute period, but since 15 minutes (15 * 60 = 900) is later used as the number of snapshots to consider in the EMA calculation. This translates to a period of 900 * 10 seconds = 9000 seconds = 2.5 hours instead of the intended 15 minutes.
The basis spread represents the difference between the order book’s mid-price and the index price (midPrice - indexPrice). When market conditions change rapidly (e.g., a lot of orders being placed or cancelled or the indexPrice changing fast), the current basis spread should immediately reflect these changes. However, calculating the EMA over 2.5 hours totally negates this purpose.
Because the EMA price factors into the markPrice, and the markPrice is used to calculate the funding rate, this issue will lead to an inaccurate funding rate.
Recommended mitigation steps
Implement one of the following solutions:
- Reduce EMA period: Change the number of snapshots used for the calculation of the EMA component to represent the intended calculation period of 15 minutes by only using 15 * 60 / 10 = 90 snapshots.
- Time-weighted EMA: Implement timestamp-based EMA calculation that considers actual time intervals rather than snapshot counts. For this it will be necessary to save the
baseSpreadsnapshots with a time stamp and determine the snapshots to be used for the EMA calculation by this timestamp.
View detailed Proof of Concept
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The code passes a time duration (15 minutes) as the number of snapshots to the EMA function, which expects a count, so the effective window is 900 snapshots instead of 90.
Impact
The mark price could lag significantly, leading to inaccurate funding rates for up to 2.25 hours during rapid market changes.
What the attacker needed
Administrator calls setMarkPrice every 10 seconds
The fix
The EMA period should be corrected to use 90 snapshots (15 minutes at 10-second intervals) or a timestamp-based calculation.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A moving average smooths out price data over time. Here, the code meant to average the last 15 minutes of data but accidentally averaged the last 900 data points, which at one update every 10 seconds covers 2.5 hours. So the price used for funding stays stale and does not react quickly to market changes.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.