Get Elastic Pool Price
Query Historical Price Data
Overview
KyberSwap Elastic enables integrators to trustlessly query the time-weighted average price of an Elastic pool via the poolOracle contract. Integrators can then derive the geometric mean to get an accurate representation of the pool's price data. For more information on the variable definitions, concepts, and implementation details, please refer to TWAP Oracle.
Getting the pool price
Retrieve the accumulated tick
To compute the TWAP, we first need to retrieve the tick accumulator for the requested time period. The poolOracle contract exposes an observeFromPool() function which expects:
pool: The address of the pool whose price is being queriedsecondsAgo: A list of the amount of time, in seconds, since the query at which to return thetickCumulativevalue.
Note that a secondsAgo value of 0 or a value later than the last observation will return the latest tickCumulative value. If the target time matches an observation timestamp, the corresponding tickCumulative of the observation will be returned. If the target time is between two observations, the interpolated tickCumulative of the two observations will be returned.
Calculating the geometric mean TWAP
Following the query above, we can then extract the average price by using this formula:
Assumptions
We are querying a
WETH/USDTpool to get the average price for the pool over the last hourUsing a
secondsAgoof[0, 3600], theobserveFromPool()function above returns atickCumulativesvalue of[500000000, 225000000].
Calculation
Plugging in the values, we get:
Last updated
Was this helpful?