Price Feeds
in Aptos Contracts

How to Use Real-Time Data in Aptos Contracts

This guide explains how to use real-time Pyth data in Aptos applications.

Configuring the Move.toml file

Add the Pyth Contract to your project dependencies in the Move.toml file:

[dependencies]
Pyth = { git = "https://github.com/pyth-network/pyth-crosschain.git", subdir = "target_chains/aptos/contracts", rev = "main" }

The named addresses of pyth, wormhole, and deployers must be defined at compile time. These addresses are used to interact with the Pyth contract on Aptos.

[addresses]
pyth = "0x7e783b349d3e89cf5931af376ebeadbfab855b3fa239b7ada8f5a92fbea6b387"
deployer = "0xb31e712b26fd295357355f6845e77c888298636609e93bc9b05f0f604049f434"
wormhole = "0x5bc11445584a763c1fa7ed39081f1b920954da14e04b32440cba863d03e19625"

Consult Aptos Contract Addresses for the complete list of contract addresses on different Aptos networks.

Write Contract Code

The code snippet below provides an example module fetching the BTC/USD price from Pyth price feeds:

module example::example {
    use pyth::pyth;
    use pyth::price::Price;
    use pyth::price_identifier;
    use aptos_framework::coin;
 
    // Add the pyth_price_update argument to any method on your contract that needs to read the Pyth price.
    // See https://docs.pyth.network/price-feeds/fetch-price-updates for more information on how to fetch the pyth_price_update.
    public fun get_btc_usd_price(user: &signer, pyth_price_update: vector<vector<u8>>): Price {
 
        // First update the Pyth price feeds
        let coins = coin::withdraw(user, pyth::get_update_fee(&pyth_price_update));
        pyth::update_price_feeds(pyth_price_update, coins);
 
        // Read the current price from a price feed.
        // Each price feed (e.g., BTC/USD) is identified by a price feed ID.
        // The complete list of feed IDs is available at https://pyth.network/developers/price-feed-ids
        // Note: Aptos uses the Pyth price feed ID without the `0x` prefix.
        let btc_price_identifier = x"e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43";
        let btc_usd_price_id = price_identifier::from_byte_vec(btc_price_identifier);
        pyth::get_price(btc_usd_price_id)
    }
}
 
ℹ️

The pyth_price_update argument contains verified prices from Pyth. Calling pyth::update_price_feeds with this value updates the on-chain Pyth price and ensures your application has recent price data. The pyth_price_update can be fetched from Hermes; Consult Fetch Price Updates for more information on how to fetch the pyth_price_update.

The code snippet above does the following things:

  1. Call pyth::get_update_fee to get the fee required to update the Pyth price feeds.
  2. Call pyth::update_price_feeds and pass pyth_price_update to update the Pyth price feeds.
  3. Call pyth::get_price to read the current price, providing the price feed ID (opens in a new tab) you wish to read.

Additional Resources

You may find these additional resources helpful for developing your Aptos application.

API Reference

The Aptos API reference lets you interactively explore the complete API of the Pyth contract.

Example Applications

Last updated on