Common approaches to integrating on-demand into client code.
For most Pull Feed use-cases, frontend development with JavaScript/TypeScript will be the primary method for handling encode updates. Client code can package a feed-update instruction before an instruction that reads the feed, enforcing some constraints around staleness.
Installation
Getting started with Switchboard on-demand on frontends requires the use of the package @switchboard-xyz/on-demand. Install it with the following npm (or bun, or even pnpm!):
npmadd@switchboard-xyz/on-demand# alias for `i --save`
Reading a Feed
Showing a feed's current value can be helpful for demonstrating an estimated execution price. Since oracle nodes are heavily rate limited, it's useful to simulate using a Crossbar Server.
What's a Crossbar Server?
Crossbar is a utility server for interacting with Switchboard which anyone can run, and everybody is encouraged to run their own instance. It can store and pull feeds from IPFS (using centralized providers or your own now), it can get encoded updates, and it can simulate feeds using a local instance of a Task-Runner.
Why run your instance? The public crossbar node is fairly rate-limited, and oracle nodes are heavily rate-limited. In the future, oracle providers may offer API keys you may be able to plug into crossbar for elevated rates. But for now, if you want to hammer a crossbar server with simulates, it's recommended that you run your own instance.
Streaming Simulations
import { CrossbarClient,} from"@switchboard-xyz/on-demand";constcrossbar=newCrossbarClient("http://myCrossbarDeployment.com");/** * Print out the results of a feed simulation to the console and return them * @param feeds - the feed public keys encoded as base58 strings * @returns results - the output of each job in each feed */asyncfunctionprintFeedResults( feeds:string[]):Promise<{feed:string; results:number[]; feedHash:string}[]> {constresults=awaitcrossbar.simulateSolanaFeeds("mainnet",// network "mainnet" | "devnet" feeds // feed pubkeys as base58 );for (let simulation of results) {console.log(`Feed Public Key ${simulation.feed} job outputs: ${simulation.results}`); }return results;}// Periodically do something with feed resultssetInterval(async () => {// drop your solana pull-feed account key hereconstbtcFeed="6qmsMwtMmeqMgZEhyLv1Pe4wcqT5iKwJAWnmzmnKjf83";constsomeOtherFeed="B3ZwcSoNo75VNbABMd8bdjrEaLj87EMQ3TkDWnVrFkcX";constresults=awaitprintFeedResults([btcFeed, someOtherFeed]);// do something with resultsconsole.log(results.length,"results found");},10_000);
In the above code block we're printing the feed values every 10 seconds to the console. Let's break down how it's happening:
In this section we're importing CrossbarClient, instantiating an instance of it pointing to our own crossbar server instance at http://myCrossbarDeployment.com.
/** * Print out the results of a feed simulation to the console and return them * @param feeds - the feed public keys encoded as base58 strings * @returns results - the output of each job in each feed */asyncfunctionprintFeedResults( feeds:string[]):Promise<{feed:string; results:number[]; feedHash:string}[]> {constresults=awaitcrossbar.simulateSolanaFeeds("mainnet",// network "mainnet" | "devnet" feeds // feed pubkeys as base58 );for (let simulation of results) {console.log(`Feed Public Key ${simulation.feed} job outputs: ${simulation.results}`); }return results;}
In this code block we're creating an asynchronous function to send a simulate request for passed-in feeds to crossbar and returning the simulation result after printing them each.
// Periodically do something with feed resultssetInterval(async () => {// drop your solana pull-feed account key hereconstbtcFeed="6qmsMwtMmeqMgZEhyLv1Pe4wcqT5iKwJAWnmzmnKjf83";constsomeOtherFeed="B3ZwcSoNo75VNbABMd8bdjrEaLj87EMQ3TkDWnVrFkcX";constresults=awaitprintFeedResults([btcFeed, someOtherFeed]);// ...},1000*10); // ten seconds (in milliseconds)
Here we're actually calling the simulate every 10 seconds with some different feeds. It's fairly straightforward, but this is the kind of logic that one might port into react code with the relevant hooks.
Updating Feeds on the Frontend
Getting feed updates is a simple process if you have the relevant keys and the @switchboard-xyz/on-demand package installed in your frontend code.
// Load the Switchboard Anchor Programconstwallet=useAnchorWallet();const { connection } =useConnection();constprovider=newAnchorProvider(connection, wallet, {});constidl= (awaitProgram.fetchIdl(programId, provider))!;constswitchboard=newProgram(idl, provider);// Replace with your feed pubkeyconstfeed=newPublicKey("6qmsMwtMmeqMgZEhyLv1Pe4wcqT5iKwJAWnmzmnKjf83");constfeedAccount=newPullFeed(switchboard, feed);
Create the PullFeed account object. This is an off-chain wrapper class with utility functions for interacting with pull feeds. Here useConnection and useAnchorWallet would be coming from Anza's Wallet Adapter, but any anchor provider would work.
// Get anchor programconstdemo=awaitmyAnchorProgram(program.provider, demoPath);// Instruction to example program using the switchboard feedconstmyIx=awaitdemo.methods.test().accounts({ feed }).instruction();
Get the instruction that uses reads from a Switchboard feed (along with your anchor program).
// Get the update instruction for switchboard and lookup tables to make the instruction lighterconst [pullIx,responses,success] =awaitfeedAccount.fetchUpdateIx({ crossbarClient: crossbar });
Get the instruction for updating the desired feed account. This will only work if the feed has been stored and is available on IPFS with Crossbar (as we need to fetch the job definitions to resolve them).
Get the lookup tables for this switchboard feed so that we the transaction can be a bit smaller. Address lookup tables are a useful tool for limiting the size-impact of Solana instructions.
// Set priority fee for that the txconstpriorityFeeIx=web3.ComputeBudgetProgram.setComputeUnitPrice({ microLamports:100_000,});// Get the current context const { context: { slot: minContextSlot }, value: { blockhash,lastValidBlockHeight },} =awaitconnection.getLatestBlockhashAndContext();
These days users can hardly get a transaction through without setting some priority fee. It's useful to set some number > 100. Here we're setting it to 100,000 micro-lamports because we're very determined to get an update through.
// Get Transaction Message constmessage=newweb3.TransactionMessage({ payerKey: publicKey, recentBlockhash: blockhash, instructions: [addPriorityFee, pullIx, myIx],}).compileMessageV0(lookupTables);// Get Versioned Transactionconstvtx=newweb3.VersionedTransaction(message);constsigned=awaitwallet.signTransaction(vtx);// Send the transaction via rpc constsignature=awaitconnection.sendRawTransaction(signed.serialize(), { maxRetries:0, skipPreflight:true,});// Wait for confirmationawaitconnection.confirm({ signature, blockhash, lastValidBlockHeight,});
Here we're simulating and requesting a signature for the transaction using the wallet libraries, which conform to the same API that web3.js does.
Putting it all together
import { web3, AnchorProvider, Program } from"@coral-xyz/anchor";import { PullFeed, loadLookupTables, SB_ON_DEMAND_PID} from"@switchboard-xyz/on-demand";import { fetchMyAnchorProgram, crossbar } from"../"// ...// Load the Switchboard Anchor Programconstwallet=useAnchorWallet();const { connection } =useConnection();constprovider=newAnchorProvider(connection, wallet, {});constidl= (awaitProgram.fetchIdl(programId, provider))!;constswitchboard=newProgram(idl, provider);// Replace with your feed pubkeyconstfeed=newPublicKey("6qmsMwtMmeqMgZEhyLv1Pe4wcqT5iKwJAWnmzmnKjf83");constfeedAccount=newPullFeed(switchboard, feed);// If using a wallet adapter of some sortconst { connection } =useConnection();const { publicKey,sendTransaction } =useWallet();// Write update to programconstupdateFeedAndCallProgram=async () => {// Get my custom anchor programconstdemo=awaitfetchMyAnchorProgram();// Instruction to example program using the switchboard feedconstmyIx=awaitdemo.methods.test().accounts({ feed }).instruction();// Get the update instruction for switchboard and lookup tables to make the instruction lighterconst [pullIx,responses,success] =awaitfeedAccount.fetchUpdateIx({ crossbarClient: crossbar });constlookupTables=awaitloadLookupTables([...responses.map((x) =>x.oracle), feedAccount]);// Set priority fee for that the txconstpriorityFeeIx=web3.ComputeBudgetProgram.setComputeUnitPrice({ microLamports:100_000, });// Get the latest contextconst { context: { slot: minContextSlot }, value: { blockhash,lastValidBlockHeight }, } =awaitconnection.getLatestBlockhashAndContext();// Get Transaction Message constmessage=newweb3.TransactionMessage({ payerKey: publicKey, recentBlockhash: blockhash, instructions: [addPriorityFee, pullIx, myIx], }).compileMessageV0(lookupTables);// Get Versioned Transactionconstvtx=newweb3.VersionedTransaction(message);constsigned=awaitwallet.signTransaction(vtx);// Send the transaction via rpc constsignature=awaitconnection.sendRawTransaction(signed.serialize(), { maxRetries:0, skipPreflight:true, });// Wait for confirmationawaitconnection.confirm({ signature, blockhash, lastValidBlockHeight, });}// ...
Perhaps the best approach to using Switchboard on the frontend is to simulate on a private crossbar server extremely frequently and use the above to pull a signature from oracles when it comes time to make a trade or create some transaction.