See the full documentation at Switchboard On-Demand Documentation
Switchboard On-Demand is designed to support high-fidelity financial systems. It allows users to specify how data from both on-chain and off-chain sources is ingested and transformed.
Unlike many pull-based blockchain oracles that manage data consensus on their own Layer 1 (L1) and then propagate it to users—giving oracle operators an advantage—Switchboard Oracles operate inside confidential runtimes. This setup ensures that oracles cannot observe the data they are collecting or the operations they perform, giving the end user a 'first-look' advantage when data is propagated.
Switchboard On-Demand is ideal for blockchain-based financial applications and services, offering a solution that is cost-effective, trustless, and user-friendly.
This library is compatible with both Node.js and browser environments. However, some utility functions require Node.js file system access:
AnchorUtils.initKeypairFromFile()
- Use web3.Keypair.fromSecretKey()
directly in browsersAnchorUtils.initWalletFromFile()
- Use browser wallet adapters (e.g., Phantom, Solflare) insteadAnchorUtils.loadEnv()
- Use loadProgramFromConnection()
with your own connection/walletimport * as sb from '@switchboard-xyz/on-demand';
import { Connection, clusterApiUrl } from '@solana/web3.js';
// Use browser wallet adapter instead of file-based keypair
const connection = new Connection(clusterApiUrl('mainnet-beta'));
const program = await sb.AnchorUtils.loadProgramFromConnection(
connection,
walletAdapter // From @solana/wallet-adapter-react or similar
);
// Rest of your code works identically in browser and Node.js
const [pullIx] = await feedAccount.fetchUpdateIx({ numSignatures: 3 });
To start building your own on-demand oracle with Switchboard, you can refer to the oracle specification in our documentation.
const [pullIx] = await feedAccount.fetchUpdateIx({ numSignatures: 3 });
const tx = await sb.asV0Tx({
connection,
ixs: [pullIx],
signers: [payer],
computeUnitPrice: 200_000,
computeUnitLimitMultiple: 1.3,
});
await program.provider.connection.sendTransaction(tx, {
// preflightCommitment is REQUIRED to be processed or disabled
preflightCommitment: "processed",
});
The SwitchboardSurge
class provides real-time price streaming capabilities through WebSocket connections to Switchboard gateways.
import { SwitchboardSurge } from '@switchboard-xyz/on-demand';
// Initialize and subscribe to price feeds
const surge = new SwitchboardSurge({
apiKey: 'your-api-key',
gatewayUrl: 'http://localhost:8082', // Your gateway URL
});
// Listen for price updates
surge.on('data', (update) => {
console.log('Price update:', update.processed.values);
});
// Subscribe to feeds (validation happens automatically)
await surge.subscribe([
{ symbol: 'BTCUSDT', source: 'BINANCE' },
{ symbol: 'ETHUSDT', source: 'BINANCE' },
]);
surge.on('connected', () => {
console.log('Connected to Switchboard Surge');
});
surge.on('data', (response) => {
// response.processed: Ready for Solana transactions
console.log('Feed values:', response.processed.values);
console.log('Feed hashes:', response.processed.feedHashes);
});
surge.on('error', (error) => {
console.error('Streaming error:', error.message);
});
surge.on('disconnected', (code, reason) => {
console.log('Disconnected:', code, reason);
});
const surge = new SwitchboardSurge({
apiKey: 'your-api-key',
gatewayUrl: 'http://localhost:8082', // Your gateway URL
autoReconnect: true, // Auto-reconnect on disconnect
maxReconnectAttempts: 5, // Max reconnection attempts
reconnectDelay: 1000, // Delay between reconnects (ms)
});
The OracleQuote
class provides utilities for working with oracle quote accounts and verified oracle data.
import { OracleQuote } from '@switchboard-xyz/on-demand';
// Derive the canonical oracle quote account address from feed hashes
const feedHashes = [
'your-feed-hash-1',
'your-feed-hash-2'
];
const [oracleAccount, bump] = OracleQuote.getCanonicalPubkey(
queueKey, // Queue public key for canonical derivation (required)
feedHashes // Uses default program ID
);
console.log('Oracle Quote Account:', oracleAccount.toString());
// Or with a custom program ID:
const [customOracleAccount, customBump] = OracleQuote.getCanonicalPubkey(
queueKey,
feedHashes,
customProgramId
);
The OracleQuote.getCanonicalPubkey()
method:
orac1eFjzWL5R3RbbdMV68K9H6TaCVVcL6LjvQQWAbz
if no program ID is providedFeed hashes must be provided as:
// Valid formats
const feedHashes = [
'0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
'1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
Buffer.from('1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', 'hex')
];