Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Pyth Price Feeds (Anchor)

Read a Pyth price feed account and log price, confidence, and exponent.

See also: Pyth overview and the repository catalog.

Note

The official pyth-solana-receiver-sdk is not Anchor 1.0 compatible (as of June 2026), so this example vendors the PriceUpdateV2 account type instead of importing it.

The latest pyth-solana-receiver-sdk (1.2.0) builds against anchor-lang 0.32 and pulls pythnet-sdk (2.3.1), which still derives borsh 0.10 on PriceFeedMessage. Anchor 0.32's AnchorSerialize/AnchorDeserialize derives require borsh 1.x, so pyth-solana-receiver-sdk's own PriceUpdateV2 fails to compile:

error[E0277]: the trait bound `pythnet_sdk::messages::PriceFeedMessage: BorshSerialize` is not satisfied

No published pyth-solana-receiver-sdk targets anchor-lang 1.0 (which this repo standardizes on), and no pythnet-sdk release has migrated to borsh 1.x - so the dependency can't simply be upgraded. Tracked upstream at pyth-network/pyth-crosschain#3756.

As a workaround, programs/pythexample/src/lib.rs mirrors the onchain PriceUpdateV2 layout locally (same fields, same 8-byte discriminator, owned by the Pyth Receiver program) so accounts written by Pyth deserialize unchanged. Replace the vendored type with the SDK import once an Anchor 1.0 / borsh 1.x compatible release ships.

Major concepts

  • Oracle price accounts
  • Consuming external onchain data in a program
  • Oracle account validation: Account<PriceUpdateV2> enforces that the price account is owned by the Pyth Receiver program (rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ)
  • Price freshness: read_price rejects updates older than MAXIMUM_PRICE_AGE_SECONDS (compared against publish_time, a unix timestamp in seconds, mirroring the SDK's get_price_no_older_than)

Setup

From this directory (basics/pyth/anchor/):

pnpm install
anchor build

Prerequisites: Agave CLI (version in Anchor.toml [toolchain]), Anchor, and pnpm.

Testing

Tests run in-process with LiteSVM. No local validator.

pnpm test

This runs cargo test as configured in Anchor.toml. Tests call instruction handlers and check onchain state.

Usage

Read the program programs/ source and Anchor.toml for deployed program IDs. For deployment, use anchor build && anchor deploy against your target cluster.