Pyth: How to Use Real-Time Data in Solana Programs
This guide explains how to use real-time Pyth data in SVM applications.
Install Pyth SDKs
Pyth provides two SDKs for SVM applications to cover the on- and off-chain portions of the integration:
Rust SDK
The pyth-solana-receiver-sdk crate(opens in a new tab) can be used to consume Pyth prices inside Solana programs written in Rust. Add this crate to the dependencies section of your Cargo.toml file:
[dependencies]pyth-solana-receiver-sdk ="0.1.0"Typescript SDK
Pyth provides two Typescript packages, @pythnetwork/price-service-client(opens in a new tab) and @pythnetwork/pyth-solana-receiver(opens in a new tab), for fetching Pyth prices and submitting them to the blockchain respectively. Add these packages to your off-chain dependencies:
npm install --save @pythnetwork/price-service-client @pythnetwork/pyth-solana-receiverWrite Contract Code
Add code to your Solana program to read the Pyth price. Pyth prices are posted to price update accounts that can be passed to any instruction that needs price data. If you are using Anchor, you can simply add an Account<'info, PriceUpdateV2> field to your Context struct:
use pyth_solana_receiver_sdk::price_update::{PriceUpdateV2};
#[derive(Accounts)]
#[instruction()]
pub struct Sample<'info> {
#[account(mut)]
pub payer: Signer<'info>,
// Add this account to any instruction Context that needs price data.
pub price_update: Account<'info, PriceUpdateV2>,
}Warning: users must ensure that the account passed to their instruction is owned by the Pyth pull oracle program. Using Anchor with the Account<'info, PriceUpdateV2> type will automatically perform this check. However, if you are not using Anchor, it is your responsibility to perform this check.
Next, update the instruction logic to read the price from the price update account:
Warning: it is your responsibility to validate that the provided price update is for the appropriate price feed and timestamp. PriceUpdateV2 guarantees that the account contains a verified price for some price feed at some point in time. There are various methods on this struct (such as get_price_no_older_than) that you can use to implement the necessary checks. Note: if you choose the price feed account integration (see below), you can use an account address check to validate the price feed id.
Write Frontend Code
There are two different paths to the frontend integration of Pyth prices on Solana. Developers can choose to use two different types of accounts:
Price feed accounts hold a sequence of prices for a specific price feed id that always moves forward in time. These accounts have a fixed address that your program can depend on. The Pyth Data Association maintains a set of price feed accounts that are continuously updated. Such accounts are a good fit for applications that always want to consume the most recent price.
Price update accounts are ephemeral accounts that anyone can create, overwrite, and close. These accounts are a good fit for applications that want to consume prices for a specific timestamp.
Both price feed accounts and price update accounts work identically from the perspective of the on-chain program. However, the frontend integration differs slightly between the two. Both options are explained in the sections below, and developers should pick the one that is best suited for their use case.
Price Feed Accounts
If you are using price feed accounts, your frontend code simply needs to pass the relevant price feed account address to the transaction. Price feed accounts are program-derived addresses and thus the account ID for any price feed can be derived automatically. The PythSolanaReceiver class provides a method for deriving this information:
The price feed account integration assumes that an off-chain process is continuously updating each price feed. The Pyth Data Association sponsors price updates for a subset of commonly-used price feeds on shard 0. Please see Sponsored Feeds for a list of sponsored feeds and their account addresses. However, updating a price feed is a permissionless operation, and anyone can run this process. Please see Using Scheduler for more information. Running the scheduler can help with reliability and to update feed/shard pairs that are not part of the default schedule.
Price Update Accounts
If you are using price update accounts, your frontend code needs to perform two different tasks:
Fetch price updates from Hermes
Post the price updates to Solana and invoke your application logic
Fetch price updates
Use PriceServiceConnection from @pythnetwork/price-service-client to fetch Pyth price updates from Hermes:
Post price updates
Finally, post the price update to the Pyth program on Solana. This step will create the price update account that your application reads from. Applications typically combine posting the price update and invoking their application into a sequence of transactions. The PythSolanaReceiver class in @pythnetwork/pyth-solana-receiver provides a convenient transaction builder to help with this process:
The SDK documentation(opens in a new tab) contains more information about interacting with the Pyth solana receiver contract, including working examples.
Was this helpful?