Introducing a new way to review your PRs.

Read the announcement

solana-foundation / program-examples

A repository of Solana program examples

View on GitHub

19 documents · Updated Nov 23, 2026

docs / pyth-price-feed-integration

Pyth price-feed integration

The Pyth example shows how a Solana program receives an on-chain price feed as an instruction account, accepts only a valid Pyth price-update account, and reads the reported market value. It exposes the oracle fields for later program policy, but the example does not make a business decision from them. See the parent page, Oracle integration patterns, for the wider oracle model.

Account input

The price feed is a Solana account that carries market data for one asset. A caller selects that account and passes it as an instruction input, so the program reads a value that is already on chain instead of requesting data from an outside service during execution.

The program declares the input as the Pyth PriceUpdateV2 account type. Anchor checks that the account belongs to the expected Pyth receiver and can be decoded as that account type. The account address still matters to the caller because it selects the asset and network feed. The example does not separately restrict the address to one fixed asset.

References: oracles/…/README.md• L3-18 oracles/…/lib.rs• L1-2 oracles/…/lib.rs• L21-24

Price-feed fields

After account validation, the program reads the price message from the decoded account. The message supplies the identity of the feed and the information needed to interpret its reported value.

FieldRole in oracle use
Feed IDIdentifies the source feed for the asset.
PriceHolds the reported value in the feed's raw integer form.
ExponentGives the decimal scale for the raw price. The price and exponent must be used together.
ConfidenceDescribes the oracle's uncertainty range around the reported price.
Publish timeIndicates when the value was published and supports freshness checks.

The example logs each field and returns success. It does not convert the raw price to a display value or enforce limits on confidence or publish time.

References: oracles/…/README.md• L6-18 oracles/…/lib.rs• L10-17

Value consumption boundary

The program decision boundary starts after Pyth has accepted and decoded the account. The current oracle value is available to program logic as the price message, while the application must decide whether that value is for the intended feed, recent enough, precise enough, and scaled correctly.

The example stops at observation. A program that uses the value for a transfer, trade, limit, or other state change must apply those checks before the value affects that action. A valid Pyth account proves that the input has the expected owner and shape. It does not by itself prove that the caller chose the intended asset or that the value is fresh enough for the decision.

This diagram shows the boundary between account validation, field parsing, and application policy.

flowchart LR
    A[Caller chooses price-feed account] --> B[Instruction account input]
    B --> C[Pyth account validation and parsing]
    C --> D[Price message fields]
    D --> E[Feed, scale, confidence, and freshness policy]
    E --> F[Program decision]

References: oracles/…/lib.rs• L6-24 oracles/…/README.md• L12-18

Fixture-backed oracle check

The example verifies the account boundary with a captured SOL/USD price-update account. The Anchor test command creates the fixture directory and obtains the account from the configured network only when the local fixture is missing. The Rust test then installs that account in LiteSVM before it sends the read instruction.

The test checks two system guarantees:

  • A real Pyth price-update account can be passed to the program and produces price data.
  • An account that is not owned by the Pyth receiver is rejected before the program can treat it as oracle data.

The test compares the logged price and exponent with the decoded fixture. This setup checks account decoding and acceptance. It does not prove that a price is fresh at the time of a later transaction because the test uses a captured account and the program has no freshness rule.

References: oracles/…/Anchor.toml• L14-18 oracles/…/test_pyth.rs• L17-38 oracles/…/test_pyth.rs• L40-69

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the instruction account type aligned with the Pyth receiver and the serialized price-update format when the oracle SDK changes.
  • Recheck feed selection rules when callers or the configured SOL/USD source changes, because a valid account can still represent the wrong asset or network.
  • Update the fixture retrieval command and local fixture loading together when the source account or account encoding changes.
  • Preserve the link between raw price values and their exponent when adding decision logic or changing value conversion.
  • Define freshness and confidence rules before allowing oracle data to control state changes.

Important failure modes

TriggerConsequenceGuard
Caller passes an account that is not owned by the Pyth receiverThe instruction fails instead of reading oracle data.Use the typed Pyth price-update account and keep rejection coverage for unrelated accounts.
The SOL/USD fixture is missing before the Rust test runsTest setup stops before the oracle flow is exercised.Run the Anchor test command that retrieves the configured price-update account.
Caller passes a valid feed for the wrong asset or networkThe program can read a real price that is not valid for the intended decision.Bind feed selection to the intended asset and network in caller or application policy.
Decision logic uses the raw price without the exponentThe value has the wrong decimal scale.Treat price and exponent as one scaled value.
Decision logic accepts a value without checking publish timeA stale value can control the program action.Apply an explicit freshness limit before the value reaches business logic.
Decision logic ignores confidenceA wide uncertainty range can produce an unsafe action.Apply a confidence limit that fits the action.

Key files

FileWhy to read it
oracles/…/README.mdGives the high-level Pyth account model and defines the price-feed fields.
oracles/…/lib.rsShows the program boundary where the account is accepted, decoded, and exposed to logic.
oracles/…/test_pyth.rsShows fixture use, logged value checks, and rejection of an account outside the Pyth receiver.
oracles/…/Anchor.tomlShows how the test flow creates and obtains the captured price-update account before running the Rust checks.