TWAP
What is TWAP?
TWAP (Time-Weighted Average Price) is a pricing algorithm used to calculate the average price of an asset over a defined time period. The value derived from TWAP is used as the reference price for any token with a liquidity pool deployed on GnoSwap, serving as a minimal and permissionless price oracle.
Advantages of TWAP
Flexibility: TWAP is highly customizable and flexible due to its simple implementation. A shorter observation window can be used for freshness and responsiveness, while a longer observation window can be used for stability against abnormal price spikes.
Manipulation Resistance: Manipulating a TWAP-based token price forces the attacker to maintain an unfavorable exchange rate for a liquidity pool for an extended period of time while exposed to arbitrage. The cost to attack increases linearly with the length of the observation window.
Scalability: TWAP can be used on any liquidity pool on GnoSwap to provide a reference price permissionlessly, without needing to integrate a 3rd-party oracle provider.
TWAP Formula
The following formula is used to calculate the TWAP of a token:
N
: Current blockM
: The number of blocks in the observation windowpriceCum
: Cumulative sum ofprice * block duration
over the observation window
For tokens with multiple pools, the price history of the most liquid pool is selected.
Price Grades
Token prices are classified into 2 tiers: oracle-grade and informational.
Oracle-grade refers to prices of tokens that pass strict criteria described below.
Informational refers to prices of tokens that do not meet the criteria. Token prices under this category should only be used for informational purposes.
The criteria evaluate pools containing a token, using the following variables:
Root Pool: A stricter requirement is enforced to pools which consists of two Root Tokens (e.g., USDC/GNOT, GNS/GNOT).
TVL Threshold: A minimum Total Value Locked (TVL) to ensure sufficient liquidity.
SqrtPrice Threshold: Ensures both tokens are present in the pool to prevent single-sided liquidity manipulations.
Block Age Threshold: Pools must have enough price history to calculate a reliable TWAP.
Observation Window: Pools with higher liquidity can tolerate shorter observation windows, as the cost of price manipulation scales with liquidity around market price.
TWAP API Usage Guide
The following API endpoints can be used to fetch the TWAP of any token traded on GnoSwap. You can filter by tiers or set a custom observation window.
1. Oracle-Grade Prices
Use this endpoint to fetch the prices of oracle-grade tier tokens.
GET
https://api.gnoswap.io/v1/tokens/prices?grade=ORACLE
2. Informational Prices
Use this endpoint to fetch the prices of informational tier tokens.
GET
https://api.gnoswap.io/v1/tokens/prices?grade=INFORMATIONAL
3. No Price Available
No TWAP price is provided for tokens with negligible liquidity or an age of 11 blocks or less.
GET
https://api.gnoswap.io/v1/tokens/prices?grade=NONE
4. Custom Observation Window
You can customize TWAP queries by specifying the observation window used to calculate the price. Longer observation windows can be used for instances that require higher stability. Shorter observation windows can be used for instances that require responsiveness and freshness of price data.
GET
https://api.gnoswap.io/v1/tokens/prices?window=600
Examples:
window=600
→ 10-minute TWAPwindow=1800
→ 30-minute TWAP
Last updated
Was this helpful?