How to Use the Alcor Price Oracle
Reading TWAP prices from Alcor Swap pools in your own contract.
The interface and an example contract are in alcorexchange/alcor-oracle-price on GitHub.
-
Copy the
includefolder of the repository into your contract project. It reads thepoolsandobservationstables ofswap.alcordirectly, so no call to Alcor is needed. -
Include
alcorpriceoracle_interface.hppand call:uint128_t priceX64 = AlcorPriceOracle::getPriceTwapX64(poolId, twapInterval);poolId: the Alcor Swap pool to read.twapInterval: the averaging window in seconds.0returns the current spot price.- The result is the price of the pool's
tokenAintokenB, in raw token units (without decimals), as a Q64.64 fixed-point number: divide by 2^64 to get the ratio.
-
Make sure the pool keeps enough history for your window.
AlcorPriceOracle::getOldestObservationSecondsAgo(poolId)returns the age of the oldest stored observation. If it is shorter than your window, buy more observations.
The interface also has consult(poolId, secondsAgo), which returns the tick change and the harmonic mean liquidity over a window.
Choosing the window
Many protocols use a 30-minute TWAP. A longer window is harder to manipulate but follows the market more slowly during volatility. A shorter window tracks the market more closely but is cheaper to manipulate. Choose according to your protocol's risks, and use a pool with deep liquidity.
Questions: Alcor Telegram.