Comment on page
Get Elastic Pool Price
Query Historical Price Data
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.Elastic SDK
KyberSwap has created an Elastic SDK to make interacting with our Elastic smart contracts easier. You can refer to our Elastic SDK Developer Guides for step-by-step walkthroughs on how to achieve various Elastic operations in a TypeScript environment.
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 thetickCumulative
value.
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.Following the query above, we can then extract the average price by using this formula:
- We are querying a
WETH/USDT
pool to get the average price for the pool over the last hour - Using a
secondsAgo
of[0, 3600]
, theobserveFromPool()
function above returns atickCumulatives
value of[500000000, 225000000]
.
Plugging in the values, we get:
Last modified 1mo ago