Integrating On-Chain (SVM)
Adding Switchboard to your Solana Program
Switchboard feed updates are instructions that should usually come first in a Solana transaction. This lets users get the most accurate (recent) data possible on-chain. Integrating on-chain only requires a few steps:
Step 1: Getting a Solana Feed Public Key
Browse our awesome Explorer for a feed you like and save the feeds public key! You can also create a feed in our On-Demand Builder, which is a tool for building and deploying feeds directly from the a user interface. You can use it to pull data from almost anywhere. See the Designing a Feed section for more on configuring oracle jobs.
Step 2: Using Switchboard On-Chain
Add the latest switchboard-on-demand following to Cargo.toml
by running:
Then within your Solana Program, you can add the Pull Feed Account into an instruction for use within the program. For example:
To conveniently read the feed value using all samples within a staleness range, we provide the following method on Solana via our SDK here. Once your feed account is loaded, you may take the lower bound median of the accrued samples to use in your program using get_value
.
Using On-Demand Feeds with Typescript
After the feed has been initialized, we can now request price signatures from oracles!
Whats happening here?
In this instance, we're fetching the latest update with signatures from the oracle network, then assembling the transaction and submitting it on-chain. It's fairly straightforward, but some troubleshooting can be necessary:
Why Pre-Flight Commitment is Necessary
Off-chain, Switchboard selects the most recent slothash
to minimize the risk of choosing a forked slot, proving the oracle data was produced after that slot's creation. If transaction simulation uses the confirmed or finalized chain state, the likelihood of the signed slot being already confirmed is slim.
Fetching and Submitting Data
To fetch the latest price update instruction for your feed, while pulling the jobs from crossbar:
Next, submit the pullIx
to the network, securing Switchboard data on
For every oracle submission, we track which oracle submitted the value and at which slot. In this way, we can track the freshest data from each oracle.
To use this data feed, you do not just want to collect the most recent oracle report, but a set of samples within your staleness tolerance.
To conveniently read the feed value using all samples within a staleness range, we provide the following method on Solana via our SDK here.
Lookup Tables
Here we create a set of lookup tables to be passed into our transaction. This can be a big space-saver when packaging a Switchboard instruction alongside some business logic for your protocol (atomic updates).
Check out our examples repository for more on backend integration! https://github.com/switchboard-xyz/sb-on-demand-examples/
Last updated