Surge Tutorial
Example Code: The complete working example for this tutorial is available at sb-on-demand-examples/evm/price-feeds (see
scripts/surgeToEvmConversion.ts)
This tutorial walks you through converting Switchboard Surge real-time price updates into EVM-compatible format for use with your smart contracts.
What You'll Build
A TypeScript script that:
Receives Surge real-time price updates
Converts them to EVM-encoded format
Submits the data to your smart contracts
Prerequisites
Bun or Node.js 18+
Basic understanding of hexadecimal encoding
The Conversion Flow
Surge WebSocket → SurgeRawGatewayResponse → EVMUtils.convertSurgeUpdateToEvmFormat() → bytes → Smart ContractThe SurgeRawGatewayResponse Structure
When you receive a Surge update, it has this structure:
The Conversion Script
Here's a complete example that converts Surge updates to EVM format:
The convertSurgeUpdateToEvmFormat Function
This function takes the raw Surge response and returns a 0x-prefixed hex string that your smart contract can parse.
EVM Data Structure
The encoded data follows this binary format:
Slot
8 bytes
Solana slot number
Timestamp
8 bytes
Unix timestamp
Number of Feeds
1 byte
Count of feeds in update
Number of Signatures
1 byte
Count of oracle signatures
Feed Data
Variable
Per-feed data (see below)
Signature Data
Variable
Per-signature data (see below)
Feed Data (per feed)
Feed Hash
32 bytes
Feed identifier
Value
16 bytes
Price value (int128)
Min Samples
1 byte
Minimum oracle samples
Signature Data (per signature)
Signature
64 bytes
ECDSA signature (r, s)
Recovery ID
1 byte
ECDSA recovery ID (v)
Parsing the Encoded Data
To understand what the encoded data contains, you can parse it:
Using with Smart Contracts
Once you have the encoded data, submit it to Switchboard:
TypeScript Integration
Full Integration Pattern
Here's how to combine Surge WebSocket streaming with EVM submission:
Running the Example
1. Clone the Examples Repository
2. Install Dependencies
3. Run the Conversion Example
Expected Output
Troubleshooting
Invalid surge data
Ensure the input matches SurgeRawGatewayResponse structure
Missing signature
The oracle_response.signature field must be a valid base64 string
Invalid recovery_id
Must be 0 or 1
Fee errors on-chain
Query switchboard.getFee() with your encoded data
Next Steps
Learn about on-demand price feeds for pull-based updates
Explore randomness integration for gaming and NFTs
Check out the Sui Surge tutorial for comparison
Last updated