Using Price Feeds


To use price feeds, your smart contract should import OracleInterface.cdc which defines the interfaces of all the price instantiations:
For each price oracle contract, only one type of price is provided. All the deployment addresses of current price oracle can be found at addresses.
In OracleInterface.cdc, two Resources type are defined: PriceReader and PriceFeeder. Holders of the former resource can read oracle prices, while the latter can provide quotes. Currently, both the reader and the feeder need to be in the whitelist. The architecture of the entire price oracle system is shown below.
There is a test oracle on the testnet (Test/USD at 0x24650d6246d4176c), which can be tested without reader whitelist.

How to read price

  1. 1.
    Apply for price reader permission and join your contract address in the whitelist.
  2. 2.
    Mint the PriceReader resource by the specific oracle contract and save it in local storage.
/// testnet orcale usecase
import OracleInterface from 0x2a9b59c3e2b72ee0
import OracleConfig from 0x2a9b59c3e2b72ee0
/// Flow/USD address is 0xcbdb5a7b89c3c844
let oracleAddr: Address = 0xcbdb5a7b89c3c844
/// Oracle contract's interface
let oracleRef = getAccount(oracleAddr).getCapability<&{OracleInterface.OraclePublicInterface_Reader}>(OracleConfig.OraclePublicInterface_ReaderPath).borrow()
?? panic("Lost oracle public capability")
/// mint PriceReader resource
let priceReader <- oracleRef.mintPriceReader()
/// Recommended storage path for PriceReader resource
let priceReaderSuggestedPath = oracleRef.getPriceReaderStoragePath()
/// Save PriceReader resource in local storage<- priceReader, to: priceReaderSuggestedPath)
3. Read the median price of all the quotes via local PriceReader resource in your contract.
/// Recommended storage path for PriceReader resource
let priceReaderSuggestedPath = getAccount(oracleAddr).getCapability<&{OracleInterface.OraclePublicInterface_Reader}>(OracleConfig.OraclePublicInterface_ReaderPath).borrow()!.getPriceReaderStoragePath()
/// local PriceReader reference
let priceReaderRef = self.account.borrow<&OracleInterface.PriceReader>(from: priceReaderSuggestedPath)
?? panic("Lost local price reader")
let price = priceReaderRef.getMedianPrice()
There are some suggested StoragePaths configured in the OracleConfig.cdc contract. If you would like to manage the paths yourself, you could not import it.
Be sure to save PriceStorage in the local Storage, and use reference or capability to reader the price. The PriceOracle contract will check the address of the caller based on resource.owner.