Oracle
In FanX, every V3 liquidity pool also functions as a built-in on-chain oracle, providing access to both current and historical price and liquidity data. This allows developers and smart contracts to use reliable, manipulation-resistant pricing information without relying on external oracle services.
Observations β Capturing Temporal Data
Each FanX V3 pool records snapshots, known as observations, that include:
The block timestamp when the observation was taken,
The cumulative tick value (a running measure of price over time),
Time-weighted liquidity data,
An initialization flag.
These observations are stored in an array within the pool contract. Initially, a pool has just a single observation slot, which is overwritten with each new block. However, anyone can pay gas to expand the array lengthβup to 65,535 entriesβallowing the pool to store historical data for roughly nine days or more.
Why On-Chain Oracles Matter
By embedding price history directly into FanX V3 pools, the protocol provides several key benefits:
Lower integration complexity β developers can access accurate pricing without running separate off-chain data systems.
Reduced risk of implementation errors β contracts that use the data do not need to manage large datasets themselves.
Higher resistance to manipulation β the ability to calculate Time-Weighted Average Prices (TWAPs) over chosen intervals makes flash manipulation significantly harder and more costly.
Fetching and Using Time-Weighted Average Prices (TWAPs)
To compute a TWAP with FanX:
Query the observation array at two different timestamps (for example, now and T seconds ago).
Calculate the tick difference between these two points.
Divide by the time interval to find the average price.
This smooths out short-term volatility and avoids the risks of relying on single-block prices, which are vulnerable to manipulation and flash loan attacks.
Customizing Observation Storage
Developers can adjust how much historical data the oracle retains by expanding the observation array:
The pool starts with the capacity for only one observation.
Anyone can increase this capacity by paying the required gas fees, enabling more granular or longer-term historical price tracking.
Example Use Cases
Lending Protocols: Use TWAP data for collateral valuation to avoid temporary price distortions.
Automated Trading Systems: Fetch historical trends from FanX V3 pools to inform strategy decisions.
On-Chain Derivatives: Use historical price data for settlement logic to ensure fair outcomes.
Last updated