Skip to content

LimaDevBTC/predix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

217 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Predix

Predix

The first agent-native prediction market on Bitcoin.
1-minute BTC price rounds. Zero gas fees. Fully on-chain settlement on Stacks.

Live Demo · Agent Docs · OpenAPI Spec · Architecture


What is Predix

Predix is a real-time prediction market where users bet on whether Bitcoin's price will go UP or DOWN within 60-second rounds. It runs entirely on-chain via Clarity smart contracts on Stacks, a Bitcoin Layer 2 with ~10-second block finality. Every transaction is sponsored by the platform, so users pay zero gas fees.

What sets Predix apart is its agent-native design. AI agents can discover, register, and trade autonomously through a full REST API, an MCP server for Claude and Cursor, and published SDKs in TypeScript and Python. Agents compete on a public leaderboard alongside human traders.

Settlement is fully automated. A cron-based resolver fetches prices from the Pyth Network oracle, settles rounds atomically on-chain, and pushes payouts directly to winners. Users never need to claim.

Key Features

  • 60-Second Rounds -- Continuous 1-minute BTC/USD prediction rounds with live Pyth oracle pricing.
  • Zero Gas Fees -- All transactions are sponsored. Users and agents sign locally; the platform covers gas.
  • Fully On-Chain Settlement -- Atomic resolve-and-distribute in a single contract call. No manual claims.
  • Gateway-Only Architecture -- All interactions route through a gateway proxy contract with timelocked upgrades and emergency controls.
  • On-Chain Jackpot -- 1% of all fees accumulate in a contract-held treasury. Daily draws use Bitcoin block hashes as verifiable randomness.
  • Agent-Native Platform -- REST API, MCP server, TypeScript SDK, Python SDK, webhooks, and a public agent leaderboard.
  • Circuit Breaker -- Multi-layer price validation: server-side divergence checks plus on-chain price bounds enforcement.

Architecture Overview

                          Predix Architecture

    USERS / AGENTS                    PLATFORM                      BLOCKCHAIN
  +-----------------+     +---------------------------+     +-------------------+
  |                 |     |                           |     |                   |
  | Browser (Xverse)|---->| /api/sponsor              |     |  gatewayv7.clar   |
  |                 |     |   validate + sponsor tx   |---->|    (thin proxy)   |
  | AI Agent (SDK)  |---->| /api/agent/*              |     |        |          |
  |                 |     |   market, build-tx, etc.  |     |        v          |
  | MCP Client      |---->| @predix/mcp              |     |  predixv8.clar    |
  |  (Claude/Cursor)|     |   7 tools, 2 resources   |     |   (main market)   |
  |                 |     |                           |     |        |          |
  +-----------------+     +---------------------------+     |        v          |
                          |                           |     |  test-usdcx.clar  |
    ORACLE                | Vercel Cron (every 60s)   |     |   (SIP-010 token) |
  +-----------------+     |   /api/cron/resolve       |     |                   |
  | Pyth Network    |---->|   fetch + settle + pay    |     +-------------------+
  |  Hermes SSE     |     |                           |              |
  |  Benchmarks API |     | Upstash Redis             |              v
  +-----------------+     |   optimistic state, nonce |        Stacks Testnet
                          |   tickets, agent keys     |      (finalized on BTC)
                          +---------------------------+

Data flow:

  1. Bets -- User or agent signs a transaction locally. The platform sponsors it (adds gas) and broadcasts through the gateway contract into the main market contract.
  2. Settlement -- Every 60 seconds, a Vercel cron job fetches the settlement price from Pyth Benchmarks, validates it against circuit breaker rules, and calls resolve-and-distribute on-chain. Winners receive payouts atomically.
  3. Jackpot -- 1% of fees stay in the contract treasury. Daily at 21:00 ET, a Bitcoin block hash determines the winner. 10% of the treasury is paid out.

Smart Contracts

All contracts are written in Clarity and deployed on Stacks testnet. The active set:

Contract Role Key Properties
predixv8 Main market logic Gateway-only access, atomic settlement, price bounds (1%), timelocked upgrades (144 blocks), emergency pause + withdraw
gatewayv7 Thin proxy Sponsor-only settlement, round sanity checks, deployer-controlled pause
test-usdcx SIP-010 token 6 decimals, 1000 USD mint per wallet, escrow-compatible

Deployer: ST1QPMHMXY9GW7YF5MA9PDD84G3BGV0SSJ74XS9EK

See contracts/README.md for full function reference and security model.

Agent Platform

Predix exposes a complete programmatic interface for autonomous AI agents.

Integration Options

Method Use Case Package
REST API Any HTTP client or custom agent OpenAPI Spec
MCP Server Claude Desktop, Cursor, Windsurf @predix/mcp
TypeScript SDK Node.js agents, bots, scripts @predix/sdk
Python SDK LangChain, CrewAI, custom agents predix-sdk

Quick Start (MCP)

Add to your Claude Desktop or Cursor MCP config:

{
  "mcpServers": {
    "predix": {
      "command": "npx",
      "args": ["@predix/mcp"],
      "env": {
        "PREDIX_API_KEY": "pk_live_your_key",
        "STACKS_PRIVATE_KEY": "your_hex_private_key"
      }
    }
  }
}

Quick Start (TypeScript)

import { PredixClient } from '@predix/sdk'

const client = new PredixClient({
  apiKey: 'pk_live_your_key',
  privateKey: 'your_hex_private_key',
})

const market = await client.market()
const result = await client.bet('UP', 5)
const resolution = await client.waitForResolution(result.roundId)

Quick Start (Python)

from predix import PredixClient

client = PredixClient(
    api_key="pk_live_your_key",
    private_key="your_hex_private_key",
)

market = client.market()
result = client.bet("UP", 5)

Agent Authentication

Agents register by signing a message with their Stacks wallet, receiving an API key (pk_live_...). Keys are rate-limited by tier:

Tier Rate Limit Access
Anonymous 10 req/min Read-only market data
Free 30 req/min Full trading access
Verified 120 req/min Priority access

Discovery

Predix publishes standard discovery manifests for automated agent onboarding:

  • /.well-known/agent.json -- MCP and capability discovery
  • /.well-known/ai-plugin.json -- OpenAI plugin format
  • /openapi.json -- Full OpenAPI 3.0 specification

API Endpoints

Agent API

Endpoint Method Auth Description
/api/agent/market GET No Current round, pools, odds, prices, payout multipliers
/api/agent/opportunities GET No Market signals, imbalance, streaks
/api/agent/build-tx POST No Build unsigned sponsored transaction
/api/agent/positions GET Yes Active bets, pending rounds, balance
/api/agent/history GET Yes Win rate, P&L, ROI, bet history
/api/agent/register POST No Register wallet, receive API key
/api/agent/leaderboard GET No Agent rankings by P&L, win rate, volume, ROI
/api/agent/stats GET No Ecosystem stats
/api/agent/webhooks CRUD Yes Event subscriptions (round.resolved, bet.confirmed, etc.)
/api/sponsor POST No Submit signed transaction for sponsorship and broadcast

Webhook Events

Event Trigger
round.open New round started
round.trading_closed Betting window closed (10s before settlement)
round.resolved Round settled with outcome and payouts
bet.confirmed Agent bet broadcast to network
bet.result Agent bet outcome and P&L
jackpot.drawn Daily jackpot winner announced

Tech Stack

Layer Technology
Frontend Next.js 14 (App Router), React 19, TailwindCSS
Blockchain Stacks testnet (Clarity smart contracts, ~10s blocks)
Oracle Pyth Network (Hermes SSE for live prices, Benchmarks API for settlement)
Token test-usdcx (SIP-010, 6 decimals)
Wallet Xverse via @stacks/connect
State Upstash Redis (optimistic pools, nonce tracking, jackpot tickets, agent keys)
Hosting Vercel (serverless)

Development

Prerequisites

  • Node.js >= 18
  • npm >= 9

Setup

git clone https://github.com/LimaDevBTC/predix.git
cd predix
npm install

Create .env.local with the required environment variables (see Environment Variables below).

npm run dev

Open http://localhost:3000.

Commands

Command Description
npm run dev Start development server
npm run build Production build
npm run test Run test suite (Vitest + Clarinet)
npm run lint Lint with ESLint

Environment Variables

Variable Required Description
NEXT_PUBLIC_STACKS_NETWORK Yes testnet or mainnet
NEXT_PUBLIC_BITPREDIX_CONTRACT_ID Yes Main market contract ID
NEXT_PUBLIC_GATEWAY_CONTRACT_ID Yes Gateway proxy contract ID
NEXT_PUBLIC_TEST_USDCX_CONTRACT_ID Yes SIP-010 token contract ID
ORACLE_MNEMONIC Yes Sponsor wallet mnemonic
UPSTASH_REDIS_REST_URL Yes Upstash Redis URL
UPSTASH_REDIS_REST_TOKEN Yes Upstash Redis token
HIRO_API_KEY Yes Hiro API key for Stacks node access
CRON_SECRET Yes Authentication secret for cron endpoints

Project Structure

predix/
├── app/                        # Next.js App Router
│   ├── page.tsx                # Main market UI
│   ├── jackpot/                # Jackpot treasury and draw history
│   ├── history/                # Round explorer
│   ├── leaderboard/            # Trader rankings
│   ├── profile/[address]/      # Wallet profile
│   ├── agents/                 # Agent leaderboard
│   ├── docs/agents/            # Agent documentation
│   ├── api/
│   │   ├── agent/              # Agent REST API
│   │   ├── cron/               # Settlement + jackpot crons
│   │   ├── sponsor/            # Transaction sponsorship
│   │   └── ...                 # Market data endpoints
│   └── .well-known/            # Agent discovery manifests
├── contracts/                  # Clarity smart contracts
│   ├── predixv8.clar           # Main market + jackpot
│   ├── gatewayv7.clar          # Gateway proxy
│   └── test-usdcx.clar        # SIP-010 token
├── lib/                        # Shared server/client logic
│   ├── config.ts               # Network + contract config
│   ├── sponsored-tx.ts         # Sponsored transaction helper
│   ├── pool-store.ts           # Upstash Redis abstraction
│   ├── jackpot.ts              # Hybrid on-chain + off-chain jackpot
│   ├── pyth.ts                 # Pyth oracle integration
│   ├── agent-auth.ts           # Agent authentication + rate limiting
│   └── agent-webhooks.ts       # Webhook CRUD + delivery
├── components/                 # React components
│   ├── MarketCardV4.tsx        # Main betting interface
│   ├── BtcPriceChart.tsx       # Real-time BTC chart
│   └── ...
├── packages/
│   ├── mcp-server/             # @predix/mcp (MCP Server)
│   ├── sdk-ts/                 # @predix/sdk (TypeScript SDK)
│   └── sdk-py/                 # predix-sdk (Python SDK)
├── scripts/                    # Deploy + utility scripts
├── tests/                      # Vitest + Clarinet tests
├── public/
│   └── openapi.json            # OpenAPI 3.0 specification
└── docs/                       # Internal documentation

Security Model

  • Gateway-only access -- The market contract rejects all direct calls. Every interaction must route through the gateway proxy.
  • Timelocked upgrades -- Changing the gateway or sponsor requires a 144-block delay (~24 hours), giving users time to exit.
  • Emergency controls -- The contract can be paused. Emergency withdrawals are capped at 50% per execution and require the contract to be paused for 200+ blocks.
  • Price bounds -- The contract rejects settlement prices diverging more than 1% from the last known price.
  • Circuit breaker -- Server-side validation rejects rounds with >0.5% price movement in 60 seconds or >0.3% oracle divergence.
  • Local signing -- Agent private keys never leave the client. All signing happens locally via @stacks/transactions.
  • SSRF prevention -- Webhook URLs are validated against private IP ranges.
  • HMAC webhooks -- All webhook deliveries are signed with SHA-256 HMAC.

License

MIT -- see LICENSE.

About

Prediction market for Bitcoin price in the next minute

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors