Using Price Feeds

Get all supported price feeds and their addresses

import PublicPriceOracle from 0xAddress

// oracleAddress => oracleTag
pub fun main(): {Address: String} {
    return PublicPriceOracle.getAllSupportedOracles()
}

Sample result:

{
    0xf5d12412c09d2470	:    "USDC/USD"
    0xe385412159992e11	:    "FLOW/USD"
    0x1a9caf561de25a86	:    "FUSD/USD"
    0x031dabc5ba1d2932	:    "stFlow/USD"
    0x11b69dcfd16724af	:    "BLT/USD"
    0x07e2f8fc48632ece	:    "USDT/USD"
}

Get latest result and update time

import PublicPriceOracle from 0xAddress

pub fun main(oracle: Address): [UFix64] {
    let lastResult = PublicPriceOracle.getLatestPrice(oracleAddr: oracle)
    let lastBlockNum = PublicPriceOracle.getLatestBlockHeight(oracleAddr: oracle)
    // Get block timestamp does not always work: the # of recent blocks cached and servicable are depending on execution nodes. 
    let lastTimestamp = getBlock(at: lastBlockNum)?.timestamp
    return [
        lastResult,
        UFix64(lastBlockNum),
        lastTimestamp ?? 0.0
    ]
}

Sample result for "Flow/USD" price feed:

[0.58717715, 57835057.00000000, 1690794621.00000000]

Note that:

  • The data is updated whichever condition is met first:

    • Deviation is beyond certain threshold (by default 0.5%)

    • A fixed window of time has passed (by default 2000 blocks)

  • Users of the PublicPriceOracle need to check the latest update time and handle the extreme condition if this data is too old.

  • We recommend to use blockHeight, as the block timestamp is not accurate or not always retrievable: the number of recent blocks cached and servicable are depending on execution nodes.

DEX-based TWAP Price Oracle

Check examples here.

Last updated