XEI Docs
  • 👋Welcome to XEI
  • Feature Description
    • 💫Swap Trading
    • ✨Leverage Trading
    • 💰Liquidity Pools
    • 🎭Lending Pools
    • 💵Liquidation
    • 💹Oracle
  • PRODUCT GUIDES
    • 📒Tutorial
      • Getting Started on XEI
      • How to Start a Market Swap
      • How to Add Trading Tokens
      • How to Start Futures Trading
      • How to Add Liquidity
    • ❓FAQ
      • What should I do if I can't find the cryptocurrency I want to trade?
      • What are market orders and limit orders?
      • What fees are associated with Swap transactions?
      • What are fee tiers, and how do you choose the right fee tier?
      • What happens if the current price exceeds the price range set for my liquidity position?
      • Are fee rewards automatically compounded?
      • What factors affect LP APR?
      • What is Lending Pools?
      • How is the reward period calculated after participating in Lending Pool?
      • Which cryptocurrencies are supported by Lending Pool?
      • How long does it take for redemption to be credited to the account?
  • OTHER
    • 💌Contact Us
Powered by GitBook
On this page
  • Oracle Application and Protection
  • Protection for XEI V3 and Its Forks
  • Additional Price Storage
  • Price Verification Measures
  • Oracle Update Mechanism
  • Protection for XEI V3
  1. Feature Description

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.

PreviousLiquidationNextTutorial

Last updated 11 months ago

💹