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");// for initial testing and development, you can use the rate-limited // https://crossbar.switchboard.xyz instance of crossbar/** * Print out the results of a feed simulation to the console and return them * @param feeds - the feed aggregator Ids encoded as hex strings * @returns results - the output of each job in each feed */asyncfunctionprintFeedResults( feeds:string[]):Promise<{feed:string; results:number[]; feedHash:string}[]> {constresults=awaitcrossbar.simulateEVMFeeds(1115,// chainId (Core Testnet is 1115) feeds // feed aggregatorIds );for (let simulation of results) {console.log(`Feed Id ${simulation.feed} job outputs: ${simulation.results}`); }return results;}// Periodically do something with feed resultssetInterval(async () => {// drop your evm aggregatorIds hereconstbtcFeed="0x0eae481a0c635fdfa18ccdccc0f62dfc34b6ef2951f239d4de4acfab0bcdca71";constresults=awaitprintFeedResults([btcFeed]);// 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 feeds' aggregatorIds * @returns results - the output of each job in each feed */asyncfunctionprintFeedResults( feeds:string[]):Promise<{feed:string; results:number[]; feedHash:string}[]> {constresults=awaitcrossbar.simulateEvmFeeds(1115,// chainId feeds // aggregatorId (hex string 0x...) );for (let simulation of results) {console.log(`Feed Id ${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 evm aggregatorIds hereconstbtcFeed="0x0eae481a0c635fdfa18ccdccc0f62dfc34b6ef2951f239d4de4acfab0bcdca71";constsomeOtherFeed="0xe2ba292a366ff6138ea8b66b12e49e74243816ad4edd333884acedcd0e0c2e9d";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
When you're ready to call a price update, you can fetch updates through the sdk.
import { CrossbarClient,} from"@switchboard-xyz/on-demand";constcrossbar=newCrossbarClient("http://myCrossbarDeployment.com");/** * Fetch the fresh encoded update to submit as part of your transaction * @param chainId - The EVM chain id, ex: 1115 (core testnet) * @param aggregatorIds - A list of each aggregator's id you want to update in this tx */asyncfunctionfetchEVMResult(chainId:number, aggregatorIds:string[]):Promise<string[]> {constresults=awaitcrossbar.fetchEVMResults({ chainId, aggregatorIds });returnresults.encoded;}asyncfunctioncallSomeEVMFunction(contractInstance:ethers.Contract) {constresults:string[] =awaitfetchEVMResult(1115,"0x0eae481a0c635fdfa18ccdccc0f62dfc34b6ef2951f239d4de4acfab0bcdca71");// call the evm function to update data, alongside whatever business logic uses the data// function updateBusinessLogic(bytes[] calldata encoded) public payable;consttx=awaitcontractInstance.updateBusinessLogic(results);console.log(tx);}