All projects

cre-prediction-markets

AI-powered prediction markets with on-chain settlement via CRE and Gemini oracle

Prediction Markets

What it is

CRE Prediction Markets is a demo for decentralized prediction market platform built on the Chainlink Runtime Environment (CRE)
that uses AI to autonomously monitor and settle real-world event outcomes.

What it is: Users create prediction markets on-chain (e.g., "Will BTC exceed 100k by 2027?"), stake ETH on YES/NO
outcomes, and an AI oracle (Google Gemini) evaluates evidence to settle markets and track probability over time. A web
dashboard displays on-chain markets alongside real-world markets from Polymarket and Kalshi for comparison.

How it works: The system runs three CRE workflow triggers on Chainlink's Decentralized Oracle Network:

  • HTTP trigger — accepts requests to create new prediction markets on a Sepolia smart contract
  • Cron trigger — runs every 10 minutes to monitor all active markets, querying Gemini for AI-estimated probabilities,
    auto-settling expired markets, and writing probability updates on-chain
  • Log trigger — listens for on-chain SettlementRequested events and asks Gemini for a final YES/NO verdict with a
    confidence score to settle the market

The smart contract handles betting pools, CRE report validation via Chainlink's KeystoneForwarder, and proportional
payout distribution to winners. The web dashboard reads on-chain state and external market APIs, displaying AI vs.
market probability charts over time.

What problem it solves: Traditional prediction markets rely on centralized resolution or manual oracle intervention.
This project replaces that with a decentralized, AI-powered oracle running on Chainlink's DON — markets are monitored
continuously, settled autonomously based on AI analysis, and all operations are verifiable on-chain. By comparing AI
probabilities against external market data from Polymarket and Kalshi, it also provides a transparency layer showing how
AI assessment aligns with real market sentiment.

How it Works

The project is built with three layers: a Solidity smart contract, a CRE TypeScript workflow, and a web dashboard.

Smart Contract (Solidity + Foundry): The PredictionMarket.sol contract is deployed on Ethereum Sepolia and extends
Chainlink's ReceiverTemplate to validate incoming CRE reports via the KeystoneForwarder. It uses a prefix-based routing
scheme in onReport — no prefix for market creation, 0x01 for settlement, 0x02 for monitor updates — so a single contract
entry point handles all workflow actions. Users interact directly via predict() to stake ETH and claim() to collect
winnings after settlement.

CRE Workflow (TypeScript + @chainlink/cre-sdk): The workflow runs on Chainlink's DON and registers three trigger
handlers in main.ts:

  • HTTP trigger — accepts {question, endTime} payloads to create new markets, encoding the data as an ABI-packed report
    written on-chain via EVMClient.writeReport()
  • Log trigger — listens for SettlementRequested events from the contract, reads market data via
    EVMClient.callContract(), calls Google Gemini (with grounded Google Search) for a YES/NO verdict, and writes the
    settlement report on-chain
  • Cron trigger — runs on a configurable schedule (default every 10 minutes), iterates over all unsettled markets,
    auto-settles expired ones, queries Gemini for AI probability estimates on active markets, computes market probability
    from on-chain YES/NO pool ratios, and writes MonitorUpdate events on-chain. It also fetches external markets from
    Polymarket (Gamma API) and Kalshi (REST API) via CRE's HTTPClient for off-chain comparison logging.

AI calls use consensusIdenticalAggregation to ensure DON nodes reach consensus on the Gemini response. All on-chain
writes go through runtime.report() to produce signed reports that the KeystoneForwarder can verify. ABI
encoding/decoding is handled by viem.

Web Dashboard (Bun + Hono): A lightweight server reads on-chain market state and MonitorUpdate events via Sepolia RPC
using viem, proxies Polymarket and Kalshi APIs, and serves an HTML dashboard with Chart.js time-series graphs showing AI
probability vs. market probability over time across all three market sources.

Runtime & Tooling: Bun is the JS runtime, Foundry (forge/cast) handles contract compilation and deployment, and the CRE
CLI manages workflow simulation and deployment.

Links

Created by

  • Bin Deng