With EVM Networks

Migrating to Switchboard, using Chainlink, on EVM Networks

Switchboard provides two SDKs, one for using the SDK with Typescript, another in Solidity for reading prices on-chain.

Typescript SDK

You can replace these packages with the single Switchboard Typescript SDK, @switchboard-xyz/on-demand with has a similar responsibility.

Get started by installing our Javascript/Typescript SDK via:

# https://switchboard-docs.web.app/
npm i @switchboard-xyz/on-demand

Solidity SDK

You can install the Switchboard On-Demand Solidity SDK by running:

# Add the Switchboard Solidity interfaces
npm add @switchboard-xyz/on-demand-solidity

Forge

If you're using Forge, add following to your remappings.txt file:

@switchboard-xyz/on-demand-solidity/=node_modules/@switchboard-xyz/on-demand-solidity

Approach 1: Implementing Switchboard SDK

Updating On-Chain Code

1. Updating Imports

import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";

Switchboard

import {ISwitchboard} from "@switchboard-xyz/on-demand-solidity/ISwitchboard.sol";
import {Structs} from "@switchboard-xyz/on-demand-solidity/Structs.sol";

2. Creating the Interface

AggregatorV3Interface internal dataFeed;

constructor(address chainlinkFeedaddress) {
  dataFeed = AggregatorV3Interface(chainlinkFeedaddress);
}  

Switchboard

ISwitchboard switchboard

constructor(address switchboardContract) {
  // https://docs.switchboard.xyz/docs/switchboard/readme/on-evm-networks#deployments
  switchboard = ISwitchboard(switchboardContract);
}

Step 3: Updating & Reading the Feed

function exampleMethod() public payable {
  (
    /* uint80 roundID */,
    int answer,
    /*uint startedAt*/,
    /*uint timeStamp*/,
    /*uint80 answeredInRound*/
  ) = dataFeed.latestRoundData();
  result = answer;
}

Switchboard

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.

🚧🔧 Under Construction 🚧🔧

Last updated