uDEX Docs
  • 👋Welcome to uDEX
  • Feature Description
    • 🎁uDEX Luck
      • Getting Started
      • How to Send a Giftbox?
      • How to Share and Claim uDEX Luck Gifts
      • Customize Giftbox
    • 🔫uGun
      • Introduction
      • Features
      • How to Quickly Buy and Sell
      • How to Manage Assets
      • How to Copy Trade
      • Trading Settings Guide
      • Language Support
    • 💫Swap Trading
    • ✨Leverage Trading
    • 🌈uBridge
    • 🎭Pools
    • 💵Liquidation
    • 💹Oracle
  • 📒Tutorial
    • Preparation for Trading: Getting Started
    • How to Start a Market Swap?
    • How to Start a Limit Swap?
    • How to Add Trading Tokens?
    • How to Start Futures Trading?
    • How to Trade After Cross-Chain?
  • ❓FAQ
    • What should I do if the uDEX Telegram Bot is unresponsive?
    • 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?
  • 🏅Referral Program
  • 💌Contact Us
  • 🥷uDEX Privacy Policy
Powered by GitBook
On this page
  • Oracle Application and Protection
  • Protection for Uniswap V2 and Its Forks
  • Additional Price Storage
  • Price Verification Measures
  • Oracle Update Mechanism
  • Protection for Uniswap 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 Uniswap, 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 Uniswap V2 and Its Forks

Uniswap 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 Uniswap V2.

struct V2PriceOracle {    
    uint32 blockTimestampLast;  // BlockTime of last update
    uint price0;                // TWAP for token0
    uint price1;                // TWAP for token1
    uint price0CumulativeLast;  // Cumulative TWAP for token0
    uint price1CumulativeLast;  // Cumulative TWAP for token1
}

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 Uniswap V3

Uniswap 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 Uniswap V3 do not require individual Oracle updates.

PreviousLiquidationNextTutorial

Last updated 1 year ago

💹