Prediction Market Tutorial

Example Code: The complete working example for this tutorial is available at sb-on-demand-examples/solana/prediction-marketarrow-up-right

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:

  1. Create a similar-looking oracle feed with different (manipulated) data sources

  2. Pass that feed to your program

  3. 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 ID

By 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:

  1. Receives oracle data for a Kalshi prediction market order

  2. Recreates the expected feed configuration on-chain

  3. 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:

  1. Constructing an OracleFeed protobuf message

  2. Encoding it as length-delimited bytes

  3. 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

  1. Generate API credentials in your account settings

  2. 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

Last updated