Oracle integration patterns
The oracle examples show how a Solana program can use trusted data from an external oracle account. The program receives the account with the instruction, lets the oracle SDK validate and decode it, and then exposes the data to application logic. The main example uses Pyth price-feed data and keeps the sample focused on safe account acceptance and clear value reading.
Oracle data model
An oracle publishes market data in a Solana account called a price feed. A consumer program reads the account as part of an instruction rather than fetching the value from outside the transaction. Pyth data gives the program a feed identifier, a price, a confidence value, an exponent, and a publish time. Together, these fields describe both the reported value and how it should be interpreted.
The price and exponent must be considered as one value. The exponent supplies the decimal scale for the raw price, while confidence shows the width of the oracle's uncertainty range. Publish time lets application logic decide whether the value is recent enough for its use.
References: oracles/…/README.md• L3-18 oracles/…/lib.rs• L10-17
Account acceptance
The instruction accepts a typed Pyth price-update account as a read-only input. This account type ties the input to the Pyth receiver and its expected data shape, so an unrelated Solana account does not become an oracle value by being passed to the instruction.
The account address selects the asset and network feed. Callers must therefore pass the intended feed account for the asset they want to use. The example tests this boundary by sending a normal payer account and requiring the transaction to fail.
References: oracles/…/lib.rs• L1-2 oracles/…/lib.rs• L21-24 oracles/…/test_pyth.rs• L61-69
Price consumption flow
The consumption flow moves from a selected price-feed account to validated fields inside the Solana program. Application logic should apply its own policy after this read, especially for feed identity, freshness, confidence, and decimal scaling.
flowchart LR
A[Caller selects feed account] --> B[Instruction receives read-only account]
B --> C[Pyth account type validates and decodes data]
C --> D[Program reads price fields]
D --> E[Application policy checks value]
E --> F[Business logic uses accepted value]
The example stops at field inspection and logs the decoded values. A production consumer should treat the decoded data as an input to explicit application checks rather than as an automatically safe business result.
References: oracles/…/lib.rs• L6-24 oracles/…/test_pyth.rs• L40-59
Fixture-backed testing
The example tests oracle use with a captured SOL/USD price-update account. The configured test command obtains the account fixture when it is missing, and the Rust test harness installs that account in a local Solana VM before sending the read instruction. This gives the program realistic account data without requiring the test to query the live network.
The test suite covers two key guarantees:
- A valid Pyth account can be read and produces the expected price and exponent logs.
- An account that does not belong to the Pyth receiver is rejected.
References: oracles/…/Anchor.toml• L14-18 oracles/…/test_pyth.rs• L19-38 oracles/…/test_pyth.rs• L51-69
Pyth price-feed integration
Pyth is the concrete oracle integration documented by this example. Use Pyth price-feed integration for the feed-specific account model, setup steps, and consumer guidance.
References: oracles/…/README.md• L1-20 oracles/…/lib.rs• L1-24
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the account type and the oracle receiver aligned when changing the oracle SDK or feed format.
- Recheck every caller when changing which feed account an instruction expects, because the account address selects the asset and network.
- Update the fixture acquisition command and local account setup together when the external account format or source changes.
- Keep application checks separate from account decoding when adding freshness, confidence, or value-scaling rules.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Caller passes an account that is not owned by the Pyth receiver | The read instruction fails instead of producing oracle data | Use the typed price-update account and test rejection of unrelated accounts |
| Caller selects the wrong feed account | The program may read a valid price for the wrong asset or network | Bind the caller to the intended feed address in application policy |
| Application uses the raw price without its exponent | The value has the wrong decimal scale | Consume price and exponent as one scaled value |
| Application ignores confidence or publish time | Business logic may act on uncertain or stale data | Apply explicit confidence and freshness limits before use |
Key files
| File | Why to read it |
|---|---|
| oracles/…/README.md | Defines the oracle account model and the fields exposed by a Pyth price feed. |
| oracles/…/Anchor.toml | Defines the local test entry point and how the price-update fixture is obtained. |
| oracles/…/lib.rs | Shows the end-to-end program boundary for receiving and reading a Pyth account. |
| oracles/…/test_pyth.rs | Shows realistic fixture setup and the safety check for rejecting an unrelated account. |