import {ISwitchboard} from "@switchboard-xyz/on-demand-solidity/ISwitchboard.sol";
import {Structs} from "@switchboard-xyz/on-demand-solidity/Structs.sol";
2. Creating the Interface
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:
function exampleMethod(bytes[] calldata priceUpdate) public payable {
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.
Pyth
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
import * as ethers from "ethers";
Switchboard
import {
CrossbarClient,
} from "@switchboard-xyz/on-demand";
import * as ethers from "ethers";
Step 2: Connect the Client
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 crossbar
const crossbar = new CrossbarClient("https://crossbar.switchboard.xyz");
// Get the latest update data for the feed
const { encoded } = await crossbar.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 address
const exampleAddress = "0xc65f0acf9df6b4312d3f3ce42a778767b3e66b8a";
// The Human Readable contract ABI
const abi = ["function exampleMethod(bytes[] calldata updates) public payable"];
// ... Setup ethers provider ...
// The Contract object
const exampleContract = new ethers.Contract(exampleAddress, abi, provider);
// Update feeds
await exampleContract.exampleMethod(encoded);