For Developers
Oracles
PancakeSwap Price Oracle

Ankr PancakeSwap price oracle

Ankr PancakeSwap price oracle is a TWAP oracle that obtains time-weighted average price (TWAP) for a pair of tokens from PancakeSwap (opens in a new tab).

TWAP is a compound price that is calculated, using data from a specific period of time.

Smart contracts

Smart contracts involved in PancakeSwap oracle are:

Workflow

To explain how the oracle works, let's show what happens when we request price of ankrBNB in USD (BUSD).

Prerequisite knowledge

Before we dive into the example, you should know two things:

  • Since there is no direct pair ankrBNB-BUSD on PancakeSwap, the price is calculated via 2 pairs — ankrBNB-BNB and BNB-BUSD — which is done automatically, without additional user interaction. The user just asks the oracle for the price of ankrBNB in BUSD.
  • The oracle iteratively collects price for a pair every periodSize and stores it in an array of the granularity length. periodSize = windowSize / granularity.
    • periodSize — frequency, which oracle queries PancakeSwap for the price of a pair with.
    • windowSize — desired amount of time, which the moving average should be computed over.
    • granularity — number of observations stored for each pair, i.e., how many price observations are stored in the window.

If granularity = 6 and windowSize = 6h, then periodSize = windowSize / granularity = 1h.

Each periodSize the price of a pair of tokens is updated and stored in an array of Observations. In our example, the length of the array is 6 items.

  • An Observation is a structure containing timestamp, price0Cumulative, and price1Cumulative.
    • timestamp — block timestamp.
    • price0Cumulative — cumulative price of any asset in a pair.
    • price1Cumulative — cumulative price of the other asset in a pair.

Actual workflow

To get the ankrBNB–BUSD TWAP price, the user calls the peek() method of ankrBNB oracle. The ankrBNB oracle runs the following logic inside:

  1. Call the sliding window oracle to first obtain the ankrBNB–BNB price. The sliding window oracle calculates the price:
    1. Obtain the current timestamp, price0Cumulative, and price1Cumulative from PancakeSwap, and the oldest observation from the array of stored observations. In our example, it's the one stored 6 hours ago.
    2. Calculate the TWAP price for the ankrBNB–BNB:
      1. For token0 in the pair, the TWAP price = (currentPrice0Cumulative - oldestPrice0Cumulative) / (currentTimestamp - oldestTimestamp).
      2. For token1 the pair, the TWAP price = (currentPrice1Cumulative - oldestPrice1Cumulative) / (currentTimestamp - oldestTimestamp).
    3. Return the current ankrBNB–BNB price to ankrBNB oracle.
  2. Call the sliding window oracle to second obtain the BNB-BUSD price. The sliding window oracle calculates the price:
    1. Obtain the current timestamp, price0Cumulative, and price1Cumulative from PancakeSwap, and the oldest observation from the array of stored observations. In our example, it's the one stored 6 hours ago.
    2. Calculate the TWAP price for the BNB–BUSD:
      1. For token0 in the pair, the TWAP price = (currentPrice0Cumulative - oldestPrice0Cumulative) / (currentTimestamp - oldestTimestamp).
      2. For token1 the pair, the TWAP price = (currentPrice1Cumulative - oldestPrice1Cumulative) / (currentTimestamp - oldestTimestamp).
    3. Return the current BNB–BUSD price to ankrBNB oracle.
  3. Return the current ankrBNB–BUSD price to the user.

If you need more details on cumulative prices and TWAP oracles, refer to the Uniswap oracles documentation (opens in a new tab).

Now that you know the flow, you can integrate with the Ankr PancakeSwap price oracle, using the functions below.

Smart Contract API

peek()

Gets time-weighted average price of a pair of tokens from PancakeSwap; a view function.

Parameters

The function returns two parameters:

  • price (bytes32) — time-weighted average price of ankrBNB in BUSD from PancakeSwap, up to 18 decimals.
  • query status (bool) — status of the attempt to get the price (success/failure).

Smart contracts

Examples

You can query for a price on the contract page (opens in a new tab), anytime.

As an example, see the response for a price query in the picture below:

response for a price query