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:
- Sliding window oracle (opens in a new tab) — a sliding window oracle that uses observations collected over a window to provide moving price averages in the past.
- ankrBNB oracle (opens in a new tab) — an oracle that gets information from the sliding window oracle and provides the user ankrBNB (ex-aBNBc) price in BUSD.
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 thegranularity
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 containingtimestamp
,price0Cumulative
, andprice1Cumulative
.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:
- Call the sliding window oracle to first obtain the ankrBNB–BNB price. The sliding window oracle calculates the price:
- Obtain the current
timestamp
,price0Cumulative
, andprice1Cumulative
from PancakeSwap, and the oldest observation from the array of stored observations. In our example, it's the one stored 6 hours ago. - Calculate the TWAP price for the ankrBNB–BNB:
- For token0 in the pair, the TWAP price = (currentPrice0Cumulative - oldestPrice0Cumulative) / (currentTimestamp - oldestTimestamp).
- For token1 the pair, the TWAP price = (currentPrice1Cumulative - oldestPrice1Cumulative) / (currentTimestamp - oldestTimestamp).
- Return the current ankrBNB–BNB price to ankrBNB oracle.
- Obtain the current
- Call the sliding window oracle to second obtain the BNB-BUSD price. The sliding window oracle calculates the price:
- Obtain the current
timestamp
,price0Cumulative
, andprice1Cumulative
from PancakeSwap, and the oldest observation from the array of stored observations. In our example, it's the one stored 6 hours ago. - Calculate the TWAP price for the BNB–BUSD:
- For token0 in the pair, the TWAP price = (currentPrice0Cumulative - oldestPrice0Cumulative) / (currentTimestamp - oldestTimestamp).
- For token1 the pair, the TWAP price = (currentPrice1Cumulative - oldestPrice1Cumulative) / (currentTimestamp - oldestTimestamp).
- Return the current BNB–BUSD price to ankrBNB oracle.
- Obtain the current
- 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: