Integrating your Feed On-Chain
This section guides you through the process of incorporating Switchboard data feeds into your Solana programs. By utilising Switchboard, your programs can access reliable and frequently updated external data. We recommend prioritising the Switchboard feed update instruction within your Solana transactions to guarantee access to the freshest data possible.
Prerequisites:
Completion of Part 2 of the guide, including saving a feed's public key. If you haven't done this, you can:
Find an existing feed.
Integration Steps:
Install the Switchboard On-Demand Crate:
To add the latest Switchboard on-demand library, run the following command in your project's root directory:
This will automatically add the
switchboard-on-demand
crate to yourCargo.toml
file.Implement Switchboard in Your Solana Program (Rust):
Include the pull feed account in an instruction within your program. The following example demonstrates how to access and utilise Switchboard data in your Solana program using Rust:
To conveniently read the feed value, considering samples within a specified staleness range, utilise the
get_value
method provided in our Solana SDK. This method allows you to determine the lower bound median of the accrued samples — a useful and effective statistical approach to valuing data within a time-series.Using On-Demand Feeds with Typescript:
After initialising the feed, you can start requesting price signatures from oracles, leveraging their off-chain infrastructure.
Diving Deeper: Understanding the Process
This example fetches the latest price update with signatures from the oracle network, assembles the transaction, and submits it to the Solana blockchain. While generally straightforward, some troubleshooting may be needed.
Why Pre-Flight Commitment is Necessary: Off-chain, Switchboard selects the most recent slot hash to minimise the risk of choosing a forked slot and to prove the origin of the oracle data. Transaction simulation using confirmed/finalised chain state increases the risk of the signed slot already being confirmed.
Fetching and Submitting Data:
Fetch the latest price update instruction for your feed:
Submit the
pullIx
to the network to secure Switchboard data on-chain.The system tracks each oracle's public key and the slot at which the value was signed to maintain the data's freshness. To effectively utilise the data feed, it is crucial to gather a set of samples that falls within an acceptable staleness tolerance. Again, utilise the 'get_value' method.
Further Resources:
Check out our example repository for more information on backend integration:
Last updated