Price Feeds Tutorial

Example Code: The complete working example for this tutorial is available at sb-on-demand-examples/evm/price-feedsarrow-up-right

This tutorial walks you through integrating Switchboard oracle price feeds into your EVM smart contracts. You'll learn how to fetch oracle data, submit updates to your contract, and read verified prices.

What You'll Build

A Solidity smart contract that:

  • Receives and stores verified oracle price updates

  • Validates price freshness and deviation

  • Provides helper functions for DeFi use cases (collateral ratios, liquidations)

Plus a TypeScript client that fetches oracle data and submits it to your contract.

Prerequisites

  • Foundry for Solidity development (forge, cast)

  • Bun or Node.js 18+

  • Native tokens for gas (MON, ETH, etc.)

  • Basic understanding of Solidity and ethers.js

Key Concepts

How Switchboard On-Demand Works on EVM

Switchboard uses an on-demand model where:

  1. Your client fetches signed price data from Crossbar (Switchboard's gateway)

  2. Your contract submits the signed data to the Switchboard contract for verification

  3. Switchboard verifies the oracle signatures and stores the data

  4. Your contract reads the verified data via latestUpdate()

This pattern ensures prices are cryptographically verified on-chain while keeping gas costs low.

The CrossbarClient

The CrossbarClient from @switchboard-xyz/common is your interface to fetch oracle data:

Fee Handling

Some networks require a fee for oracle updates. Always check before submitting:

The Smart Contract

Here's a complete example contract that integrates Switchboard price feeds:

Contract Walkthrough

State Variables

  • switchboard - Reference to the deployed Switchboard contract

  • prices - Maps feed IDs to their latest price data

  • maxPriceAge - Maximum acceptable age for price data (5 minutes default)

  • maxDeviationBps - Maximum price change allowed (10% default, prevents manipulation)

The updatePrices Function

This is the main entry point for updating prices:

  1. Check fee - Ensure caller sent enough to cover the oracle update fee

  2. Submit to Switchboard - Call updateFeeds() which verifies oracle signatures

  3. Read verified data - Call latestUpdate() to get the verified price

  4. Store locally - Save the price in your contract's storage

  5. Refund excess - Return any overpayment to the caller

Reading Prices

Always check freshness before using a price:

The TypeScript Client

Here's a complete client that fetches oracle data and submits it to your contract:

Client Walkthrough

Step 1: Fetch Oracle Data

The fetchEVMResults call returns encoded oracle data signed by Switchboard oracles. This data includes:

  • The price value

  • Timestamp

  • Oracle signatures

Step 2: Submit to Contract

Your contract receives the encoded data, submits it to Switchboard for verification, then stores the result.

Step 3-5: Confirm and Read

After confirmation, you can:

  • Parse PriceUpdated events from the receipt

  • Read the stored price directly from your contract

Deployment

Using Foundry

Create a deploy script script/Deploy.s.sol:

Deploy Commands

Running the Example

1. Clone the Examples Repository

2. Install Dependencies

3. Configure Environment

Security: Never use export PRIVATE_KEY=...—it appears in shell history. Use a .env file instead.

Create a .env file (add to .gitignore):

4. Run the Example

Expected Output

Adding to Your Project

1. Install the Switchboard Interfaces

Copy the interface files from the examples repo:

Or install via npm:

2. Import and Use

Example: DeFi Business Logic

The example contract includes helper functions for common DeFi patterns:

Calculate Collateral Ratio

Check Liquidation

Switchboard Contract Addresses

Network
Chain ID
Switchboard Contract

Monad Testnet

10143

0xD3860E2C66cBd5c969Fa7343e6912Eff0416bA33

Monad Mainnet

143

0xB7F03eee7B9F56347e32cC71DaD65B303D5a0E67

HyperEVM Mainnet

999

0xcDb299Cb902D1E39F83F54c7725f54eDDa7F3347

Arbitrum One

42161

0xAd9b8604b6B97187CDe9E826cDeB7033C8C37198

Arbitrum Sepolia

421614

0xA2a0425fA3C5669d384f4e6c8068dfCf64485b3b

Core Mainnet

1116

0x33A5066f65f66161bEb3f827A3e40fce7d7A2e6C

Available Feeds

Find available price feeds at the Switchboard Explorerarrow-up-right.

Popular feeds include:

  • BTC/USD: 0x4cd1cad962425681af07b9254b7d804de3ca3446fbfd1371bb258d2c75059812

  • ETH/USD: 0xa0950ee5ee117b2e2c30f154a69e17bfb489a7610c508dc5f67eb2a14616d8ea

  • SOL/USD: 0x822512ee9add93518eca1c105a38422841a76c590db079eebb283deb2c14caa9

Troubleshooting

Error
Solution

InsufficientFee

Query switchboard.getFee(updates) and send that amount as msg.value

PriceDeviationTooHigh

Normal during high volatility; adjust maxDeviationBps if needed

PriceTooOld

Fetch fresh data from Crossbar; adjust maxPriceAge if needed

InvalidFeedId

Ensure the feed ID exists and has been updated at least once

Build errors

Run forge clean && forge build

Next Steps

Last updated