Switchboard Documentation
  • Switchboard On Demand
  • Understanding Switchboard
    • Introduction
      • Why Switchboard Oracles?
      • Vision & mission
      • Brief History and Key Achievements to Date
      • Switchboard’s Architecture, Tech Stack and Security
        • Trusted Execution Environments (TEEs)
        • Oracle Queues
        • Node Architecture
  • Product Documentation
    • Data Feeds
      • Getting Started with Switchboard Data Feeds
      • Solana / SVM
        • Part 1: Designing and Simulating Your Feed
          • Option 1: Drag-and-Drop Feed Builder
          • Option 2: Designing a Feed in Typescript
        • Part 2: Deploying your Feed On-Chain
        • Part 3: Integrating your Feed
          • Integrating your Feed On-Chain
          • Integrating into Frontends
        • Costs
        • Integrating on Eclipse
      • EVM
        • Part 1: Prerequisites and Quick Start Guide
        • Part 2: Designing and Creating Your Feed
          • Option 1: Drag-and-Drop Feed Builder
          • Option 2: Designing a Feed in Typescript
        • Part 3: Integrating your Feed
          • Integrating your Feed On-Chain
          • Integrating your Feed with Typescript
          • Integrating into Frontends (EVM)
      • Aptos
      • Sui
      • Movement
      • Starknet
      • Optional Features
        • Switchboard Secrets
      • Task Types
    • Aggregator
      • How to use the Switchboard Oracle Aggregator
    • Randomness
      • Why Randomness is important?
      • Switchboard's Approach to Verifiable Randomness
      • Tutorials
        • Solana / SVM
        • EVM
  • Tooling and Resources
    • Crossbar
      • Run Crossbar with Docker Compose
    • Switchboard Command Line Interface
    • Technical Resources and Documentation
      • SDKs and Documentation
      • Solana Accounts
      • EVM Identifiers
      • Code Examples (Github)
  • Switchboard Protocol
    • (Re)staking
      • What is (re)staking?
      • What are Node Consensus Networks (NCNs)?
      • What are Vault Receipt Tokens (VRTs)?
      • The Node Partner Program
      • The Switchboard NCN
    • Running a Switchboard Oracle
      • Prerequisites
        • Knowledge about Linux, containers and Self-Hosting
        • Hardware Requirements and AMD SEV SNP
        • Software Requirements
        • Network Requirements
      • Hardware: tested providers and setup
        • OVH
      • Platform: Kubernetes + AMD SEV SNP
        • Bare Metal with Kubernetes (K3s)
      • The Git Repo: Clone Our Code
        • Repo Structure
      • Configuration: Tweaking Configurations
        • cfg/00-common-vars.cfg
        • cfg/00-devnet-vars.cfg and cfg/00-mainnet-vars.cfg
      • Installation: Setup Via Scripts
        • Bare Metal with Kubernetes (K3s) + AMD SEV SNP
  • Frequently Asked Questions and Glossary
    • FAQ
    • Glossary
Powered by GitBook
On this page
  1. Product Documentation
  2. Data Feeds
  3. EVM
  4. Part 3: Integrating your Feed

Integrating your Feed with Typescript

PreviousIntegrating your Feed On-ChainNextIntegrating into Frontends (EVM)

Last updated 1 month ago

After the feed has been initialized from the last step of , we can now request price signatures from oracles!

So now that we have the contract ready to read and use Switchboard update data, we need a way to fetch these encoded values. Using Crossbar, we can get an encoded feed update with just a fetch. For simplicity, we'll demonstrate a fetch using both.

We'll be working from the Typescript portion of :

Adding Imports

bun add ethers

index.ts

import { CrossbarClient } from "@switchboard-xyz/common";
import * as ethers from "ethers";
  1. We'll be using to write updates to the example contract. Add it to the project and import the Switchboard EVM call.

Setting up the call

// for initial testing and development, you can use the rate-limited 
// <https://crossbar.switchboard.xyz> instance of crossbar
const crossbar = CrossbarClient.default();

// Get the latest update data for the feed
const { encoded } = await crossbar.fetchEVMResults({
  aggregatorIds: ["0x0eae481a0c635fdfa18ccdccc0f62dfc34b6ef2951f239d4de4acfab0bcdca71"],
  chainId: 1115, // 1115 here is the chainId for Core Testnet
});
  1. Here we're getting the results for the aggregatorId from Switchboard using the default crossbar deployment.

Creating contract bindings

// Target contract address
const exampleAddress = process.env.CONTRACT_ADDRESS as string;

// (this is the readable ABI format)
const abi = ["function getFeedData(bytes[] calldata updates) public payable"];

// ... Setup ethers provider ...

// The Contract object
const exampleContract = new ethers.Contract(exampleAddress, abi, provider);
  1. Pass the encoded updates bytes[] calldata into the getFeedData call. This will send the transaction over the wire.

  2. In order to submit transactions on the target chain, you need to plug in the right RPC and private key. The signerWithProvider will be what we pass into the contract.

Getting the provider

// Pull the private key from the environment 0x..
const pk = process.env.PRIVATE_KEY;
if (!pk) {
  throw new Error("Missing PRIVATE_KEY environment variable.");
}

// Provider 
const provider = new ethers.JsonRpcProvider(
  "https://ethereum.rpc.example"
);
const signerWithProvider = new ethers.Wallet(pk, provider);
  1. Add the example contract binding with the getFeedData call in the ABI.

Adding the call

// Update feeds
await exampleContract.getFeedData(encoded);

Putting it together

Here we're connecting all of these components. We're compiling all of calls into a system where we can pull the encoded updates, and calling the contract.

import { CrossbarClient } from "@switchboard-xyz/common";
import * as ethers from "ethers";

// ... simulation logic ...

// Create a Switchboard On-Demand job
const chainId = 1115; // Core Devnet (as an example)

// 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 here is the chainId for Core Testnet
});

// Target contract address
const exampleAddress = "0xc65f0acf9df6b4312d3f3ce42a778767b3e66b8a";

// The Human Readable contract ABI
const abi = ["function getFeedData(bytes[] calldata updates) public payable"];

// ... Setup ethers provider ...

// The Contract object
const exampleContract = new ethers.Contract(exampleAddress, abi, provider);

// Update feeds
await exampleContract.getFeedData(encoded);
Integrating your Feed On-Chain
Designing a Feed (EVM)
ethers