Surge Tutorial

Example Code: The complete working example for this tutorial is available at sb-on-demand-examples/sui/surge/basicarrow-up-right

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.

What You'll Build

A TypeScript application that:

  • Connects to Switchboard Surge for real-time price streaming via WebSocket

  • Receives signed price updates with sub-second latency

  • Submits price updates to the Sui blockchain

  • Tracks latency statistics and oracle performance

Prerequisites

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 and Switchboard clients. For testnet, use https://fullnode.testnet.sui.io:443.

Creating Surge Connection

  • connection: Your SuiClient instance

  • keypair: Your Sui keypair (must have an active subscription)

  • signatureScheme: Use 'secp256k1' for Sui

  • 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

  • getFormattedPrices(): Returns human-readable prices

Submitting to Sui

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 keypair in ~/.sui/sui_config/sui.keystore must have an active Surge subscription. Subscribe at explorer.switchboardlabs.xyz/subscriptionsarrow-up-right.

4. Run the Examples

Expected Output

Adding to Your Project

Dependencies

Minimal Integration

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 keypair in ~/.sui/sui_config/sui.keystore

  • Run sui client new-address ed25519 to create one

"Subscription not found" or connection rejected

"Oracle ID not found for key"

  • The oracle mapping might be stale

  • Force refresh by clearing oracleMapping and calling fetchOracleMappings()

  • Check you're using the correct network (mainnet vs testnet)

Transaction Failures

  • Ensure your wallet has sufficient SUI for gas

  • Check network connectivity

  • Verify you're on the correct network

High Latency

  • Check your network connection

  • Consider using a dedicated RPC endpoint

  • Reduce logging if running in production

Next Steps

  • Quote Verifier Pattern: See the Price Feeds tutorial for verified on-chain price storage

  • Multiple Feeds: Subscribe to multiple feeds for portfolio tracking

  • Custom Integration: Use the price data to trigger your own Move contract logic

Last updated