Saving a reference to the Switchboard contract is quite easy, all you have to do is keep a reference to the Switchboard deployment address (which you can find here in the docs). Migrating to using Pyth through Switchboard is simple:
functionexampleMethod(bytes[] calldata priceUpdate) publicpayable {uint256 fees = switchboard.getFee(updates); switchboard.updateFeeds{ value: fees }(updates);bytes32 aggregatorId ="0xfd2b067707a96e5b67a7500e56706a39193f956a02e9c0a744bf212b19c7246c"; Structs.Update memory latestUpdate = switchboard.latestUpdate(aggregatorId);// Get the latest feed result (int128)// This is encoded as decimal * 10^18 result = latestUpdate.result;}
The important thing to note about switchboard results is that they'll be stored as a decimal with a scale factor of 18.
Updating Off-Chain Code
Step 1: Update Imports
Pyth provides an SDK for connecting to Hermes for price updates. Switchboard has the on-demand ts-sdk for interacting with oracles and updating feeds.
The next step is connecting the Crossbar Client. Crossbar is a utility server that allows us to easily simulate feeds (thereby getting an estimated result), query oracles for prices, and a few other utility functions used to make using Switchboard easier. The Pyth Hermes server has a similar function of brokering data from pythnet.
// for initial testing and development, you can use the rate-limited // https://crossbar.switchboard.xyz instance of crossbarconstcrossbar=newCrossbarClient("https://crossbar.switchboard.xyz");// Get the latest update data for the feedconst { encoded } =awaitcrossbar.fetchEVMResults({ aggregatorIds: ["0x0eae481a0c635fdfa18ccdccc0f62dfc34b6ef2951f239d4de4acfab0bcdca71"], chainId:1115,// Replace this with the chainId for the target EVM network });
Step 3: Submitting the Updates
Submitting updates with Switchboard from here should be pretty similar to how you'd do them with Pyth. Here's an example of what that might look like:
Pyth and Switchboard
// Target contract addressconstexampleAddress="0xc65f0acf9df6b4312d3f3ce42a778767b3e66b8a";// The Human Readable contract ABIconstabi= ["function exampleMethod(bytes[] calldata updates) public payable"];// ... Setup ethers provider ...// The Contract objectconstexampleContract=newethers.Contract(exampleAddress, abi, provider);// Update feedsawaitexampleContract.exampleMethod(encoded);