This tutorial shows you how to stream real-time price data via WebSocket using Switchboard Surge and submit updates to the Sui blockchain. This approach is ideal for applications requiring sub-second price updates.
A Sui keypair with SUI tokens (in your Sui keystore) for signing Sui transactions
A Solana keypair with an active Surge subscription (subscribe here)
Surge subscriptions are currently Solana-only; you cannot subscribe with a Sui keypair yet.
Key Concepts
Surge vs On-Demand Quotes
Feature
On-Demand Quotes
Surge Streaming
Update frequency
Request-based
Continuous (~100ms)
Latency
Higher (HTTP request)
Lower (WebSocket)
Use case
Occasional reads
Real-time apps
Authentication
None required
Subscription required
The emitSurgeQuote Function
Surge provides the emitSurgeQuote() function from @switchboard-xyz/sui-sdk that converts Surge updates into Sui transactions. This handles:
Oracle signature formatting
Transaction building
Quote verification setup
Oracle Mapping
Surge returns oracle public keys, but Sui needs oracle object IDs. The example fetches a mapping from Crossbar to convert between these formats.
Transaction Queue Management
Since Sui transactions are sequential, the example implements a queue to:
Buffer incoming price updates
Process one transaction at a time
Track processing latency
The Streaming Client
Here's the complete mainnet streaming example:
Code Walkthrough
Setup
Initialize the Sui client you will submit transactions through, plus a Solana RPC connection for Surge authentication. For Sui testnet, use https://fullnode.testnet.sui.io:443.
Creating Surge Connection
connection: Your Solana Connection instance
keypair: Your Solana keypair (must have an active Surge subscription)
signatureScheme: Use 'ed25519' for Solana keypairs
connectAndSubscribe(): Connects and subscribes to specified feeds
Handling Updates
The signedPriceUpdate event fires whenever new price data arrives. Key methods:
getRawResponse(): Returns the raw signed data for transaction submission
The emitSurgeQuote() function handles converting the Surge response into a valid Sui transaction.
Mainnet vs Testnet
The mainnet and testnet examples are nearly identical with these differences:
Setting
Mainnet
Testnet
RPC URL
https://fullnode.mainnet.sui.io:443
https://fullnode.testnet.sui.io:443
Oracle Mapping
/oracles/sui
/oracles/sui/testnet
Testnet Configuration
Running the Examples
1. Clone the Repository
2. Install Dependencies
3. Ensure Active Subscription
Your Solana keypair (default ~/.config/solana/id.json or SOLANA_KEYPAIR_PATH) must have an active Surge subscription. The Sui keypair only signs Sui transactions. Subscribe at explorer.switchboardlabs.xyz/subscriptions.
4. Run the Examples
Expected Output
Adding to Your Project
Dependencies
Minimal Integration
This example assumes you already loaded a solanaKeypair (with an active Surge subscription) and a suiKeypair (for signing Sui transactions).
Multiple Feeds
Error Handling
Performance Considerations
Transaction Queue
The example uses a queue because:
Sui transactions are sequential per sender
Surge updates arrive faster than transactions complete
Queuing prevents transaction conflicts
Latency Optimization
Keep your Sui node geographically close
Use dedicated RPC endpoints for production
Consider batching updates if latency isn't critical
Oracle Mapping Cache
The oracle mapping is cached for 10 minutes to avoid repeated API calls. Adjust ORACLE_CACHE_TTL based on your needs.
Troubleshooting
"Keypair not loaded"
Ensure you have a valid Sui keypair in ~/.sui/sui_config/sui.keystore
Run sui client new-address ed25519 to create one
Ensure you have a valid Solana keypair at ~/.config/solana/id.json or SOLANA_KEYPAIR_PATH
Run solana-keygen new to create one
"Subscription not found" or connection rejected
Ensure your Solana keypair has an active Surge subscription