AlcorDocs

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.

  1. Copy the include folder of the repository into your contract project. It reads the pools and observations tables of swap.alcor directly, so no call to Alcor is needed.

  2. Include alcorpriceoracle_interface.hpp and call:

    uint128_t priceX64 = AlcorPriceOracle::getPriceTwapX64(poolId, twapInterval);
    • poolId: the Alcor Swap pool to read.
    • twapInterval: the averaging window in seconds. 0 returns the current spot price.
    • The result is the price of the pool's tokenA in tokenB, in raw token units (without decimals), as a Q64.64 fixed-point number: divide by 2^64 to get the ratio.
  3. 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.

On this page