Surge Tutorial
Prerequisites
Installation
npm install @switchboard-xyz/on-demand
# or
yarn add @switchboard-xyz/on-demandBasic Implementation
import * as sb from "@switchboard-xyz/on-demand";
// Load environment (keypair, connection, queue, etc.)
const { keypair, connection, queue } = await sb.AnchorUtils.loadEnv();
// Initialize Surge client with keypair and connection
const surge = new sb.Surge({ connection, keypair });
// Discover available feeds
const availableFeeds = await surge.getSurgeFeeds();
console.log(`Found ${availableFeeds.length} available feeds`);
// Subscribe to price feeds
await surge.connectAndSubscribe([
{ symbol: 'BTC/USD' },
{ symbol: 'SOL/USD' },
]);
// Handle real-time updates
surge.on('signedPriceUpdate', async (response: sb.SurgeUpdate) => {
const metrics = response.getLatencyMetrics();
// Skip heartbeat messages
if (metrics.isHeartbeat) return;
const prices = response.getFormattedPrices();
metrics.perFeedMetrics.forEach((feed) => {
console.log(`${feed.symbol}: ${prices[feed.feed_hash]}`);
console.log(` Source → Oracle: ${feed.sourceToOracleMs}ms`);
console.log(` Emit Latency: ${feed.emitLatencyMs}ms`);
});
});Converting to Oracle Quotes
Streaming to Frontend with Crossbar
Setting Up Crossbar
React Integration
Use Case Examples
Perpetual Exchange
Arbitrage Bot
Error Handling
Next Steps
Last updated