Oracle-Based Pricing System for Perpetuals
Perpetual DEX without correct oracle pricing — minefield. GMX, dYdX, Synthetix — all used oracle price for mark price contracts, fundamentally different from spot order book pricing. Correct oracle mechanism determines if protocol can be manipulated and fairness of liquidations.
Mark Price vs Index Price vs Oracle Price
Index Price: aggregated price from major centralized exchanges (Binance, OKX, Coinbase). Represents "fair" market price.
Mark Price: price used for unrealized PnL calculation and liquidation determination. On perp DEX usually = Oracle Price. Can't deviate significantly from Index Price (if does — funding rate corrects).
Oracle Price: concrete smart contract from which price read.
Consequences of wrong oracle: if mark price deviates from fair price — unfair liquidations (liquidate healthy position on oracle spike) or manipulation attacks possible.
Oracle Choice for Perpetual DEX
GMX v2 Approach: Chainlink Low-Latency
GMX v2 integrated Chainlink Data Streams — off-chain price reports signed by Chainlink nodes, verifiable on-chain. Sub-second freshness with cryptographic reliability.
Protection Against Oracle Manipulation
Spread-Based Protection
On large oracle deviation from previous price — activate protection:
function updateOraclePrice(bytes32 assetId, uint256 newPrice) internal {
uint256 lastPrice = lastOraclePrice[assetId];
if (lastPrice > 0) {
uint256 deviation = newPrice > lastPrice
? (newPrice - lastPrice) * 10000 / lastPrice
: (lastPrice - newPrice) * 10000 / lastPrice;
// Circuit breaker on too sharp movement
if (deviation > MAX_PRICE_DEVIATION) {
newPrice = lastPrice * (10000 + MAX_PRICE_DEVIATION) / 10000;
}
}
lastOraclePrice[assetId] = newPrice;
}
Multi-Source Validation
Read prices from multiple sources (Chainlink + Pyth + on-chain TWAP), take median.
TWAP for Liquidations
For liquidation threshold — use TWAP (Time-Weighted Average Price) over last 15-30 minutes, not spot price. Flash crash doesn't trigger mass liquidations.
| Operation | Recommended Oracle |
|---|---|
| Open/close position | Spot oracle (Chainlink/Pyth) |
| Unrealized PnL | Mark price (oracle) |
| Liquidation check | TWAP (15-30 min) |
| Funding rate | Index price (CEX aggregate) |
Development of oracle system for perp DEX — 4-8 weeks. Critical component: most major perp DEX attacks (GMX, Synthetix) were oracle manipulation attacks.







