💹Oracle

Oracle Application and Protection

In developing a permissionless margin trading market, we have opted not to rely on external oracles to convey off-chain price data for risk calculation purposes. Instead, we utilize the Time-Weighted Average Price (TWAP) provided by XEI, including its forks, to build an Oracle that updates as needed. This approach aims to prevent price manipulation in permissionless markets and allows for real-time risk calculations based on on-chain prices.

Protection for XEI V3 and Its Forks

XEI offers cumulative price data updated every second. To ensure the accuracy of the TWAP, we check the cumulative total at the start and end of each period.

Additional Price Storage

We record the cumulative total and the latest prices, representing the most recent TWAP for currency pairs on XEI V3.

Price Verification Measures

We calculate different margin ratios by comparing three reference prices to protect against price manipulation. These prices include the last recorded TWAP, the most recent TWAP, and the latest spot price. The margin ratio for each new position must exceed all three calculated ratios. Conversely, for a position to be liquidated, its margin ratio must fall below these three ratios. This approach mitigates the risk of flash loan attacks, which can only manipulate spot prices within a single block, not the TWAP. Large trade manipulations are theoretically possible but come with high risk and cost.

Oracle Update Mechanism

The Oracle updates only if at least 60 seconds have passed since the last update and the current TWAP differs from the last recorded TWAP by more than 5%. If the current and last recorded TWAPs differ significantly, new positions and liquidations might be paused until a manual update is done. However, closing healthy positions doesn't require an update. Successful trades also trigger an automatic update of the recorded price. Therefore, in active markets with relatively stable price fluctuations, the recorded TWAP is continuously updated, eliminating the need for frequent Oracle updates.

Protection for XEI V3

XEI V3 has significantly improved its TWAP oracle, allowing for the calculation of the latest TWAP over approximately the past nine days with a single on-chain call. This enables us to calculate the TWAP based on the previous two blocks. We also ensure that any new positions or liquidations meet the margin ratio requirements calculated based on these three prices: the TWAP between T-2 and T-1 blocks, the TWAP from T-1 to the present, and the spot price. Trading pairs on XEI V3 do not require individual Oracle updates.

Last updated