Prediction Market Tutorial
Example Code: The complete working example for this tutorial is available at sb-on-demand-examples/solana/prediction-market
This tutorial demonstrates how to verify oracle feed configurations on-chain using Kalshi prediction market data. You'll learn a critical security pattern that prevents oracle substitution attacks.
The Problem: Oracle Substitution Attacks
When using oracle data in your program, how do you know the oracle is fetching data from the sources you expect? A malicious actor could:
Create a similar-looking oracle feed with different (manipulated) data sources
Pass that feed to your program
Exploit your program with incorrect data
Example Attack:
Your program expects BTC price from Binance + Coinbase
Attacker creates a feed that looks similar but fetches from a manipulated source
Your liquidation logic uses the wrong price
The Solution: Feed ID Verification
Switchboard feed IDs are deterministic SHA-256 hashes of the feed's protobuf definition:
Feed Definition → Protobuf Encoding → SHA-256 Hash → Feed IDBy recreating the expected feed configuration on-chain and comparing its hash to the oracle's feed ID, you cryptographically prove the oracle uses exactly the data sources you expect.
What You'll Build
A Solana program that:
Receives oracle data for a Kalshi prediction market order
Recreates the expected feed configuration on-chain
Verifies the feed ID matches before trusting the data
Prerequisites
Rust and Cargo installed
Anchor framework familiarity
Solana CLI installed and configured
Kalshi API credentials (API key ID and private key)
Key Concepts
Feed ID Derivation
Feed IDs are derived by:
Constructing an
OracleFeedprotobuf messageEncoding it as length-delimited bytes
Computing SHA-256 hash
QuoteVerifier
The QuoteVerifier uses a builder pattern to verify Ed25519 signatures from oracle operators:
Variable Overrides
Kalshi requires authentication. Variables like ${KALSHI_API_KEY_ID} are placeholders that get replaced at runtime when fetching the quote:
The On-Chain Program
Dependencies
Program Structure
Feed ID Recreation
The critical function that recreates the expected feed configuration:
Account Context
The TypeScript Client
Kalshi Authentication
Kalshi uses RSA-PSS-SHA256 signatures for API authentication:
Complete Client Flow
Running the Example
1. Clone the Examples Repository
2. Install Dependencies
3. Build and Deploy the Program
4. Get Kalshi API Credentials
Sign up at Kalshi
Generate API credentials in your account settings
Download your private key PEM file
5. Run the Verification
Expected Output
Use Cases
1. Prediction Market Settlement
Before settling prediction market positions, verify the oracle is using the correct data source:
2. Conditional Payments
Release funds only when verified oracle data meets conditions:
3. DeFi Protocol Integration
Verify oracle configuration before using prices for:
Liquidations
Collateral calculations
Interest rate adjustments
4. Compliance & Audit Trails
Prove on-chain that specific data sources were used:
Security Best Practices
Always Verify Feed Configuration
Validate Queue Account
Use QuoteVerifier
Extending the Pattern
Generic HTTP APIs
Polymarket Integration
Multi-Source Validation
Verify multiple feeds use approved sources:
Next Steps
Price Feeds: Learn basic oracle integration in Basic Price Feed
Custom Feeds: Create your own feed definitions in Custom Feeds
Randomness: Explore verifiable randomness in Randomness
Last updated