PythPredict - Decentralized Prediction Market on Solana
A decentralized prediction market protocol built on Solana that allows users to bet on price movements of cryptocurrencies using real-time price feeds from Pyth Network.
๐ฏ Overview
PythPredict enables users to create binary prediction markets where participants can bet YES or NO on whether an asset's price will move within a specified timeframe. The protocol uses Pyth Network's high-fidelity price oracles to ensure fair and transparent market resolution.
Key Features
- Real-Time Price Feeds: Integration with Pyth Network for accurate BTC/USD and ETH/USD price data
- Binary Markets: Simple YES/NO betting on price movements
- Automated Resolution: Markets resolve automatically based on oracle prices at settlement time
- Fee System: 1% platform fee on all bets
- Fair Payout Distribution: Winners receive proportional shares of the total pool
- Token Conservation: Guaranteed token conservation with no loss of funds
๐ Prerequisites
- Node.js v16+ and Yarn
- Rust 1.70+
- Solana CLI tools 1.17+
- Anchor Framework 0.31.1
๐ Installation
- Clone the repository
git clone https://github.com/TrebuchetNetwork/PythPredict
cd PythPredict- Install dependencies
- Build the Solana program
- Set up local validator (optional for local testing)
๐งช Running Tests
The project includes a comprehensive test suite (master-test.ts) that validates all core functionality.
Run the full test suite
What the Main Test Suite Does
The master test suite (tests/master-test.ts) performs extensive testing across 5 main sections:
Section 1: Core Functionality (6 tests)
- Creates prediction markets with correct parameters
- Places bets and validates fee calculations (1% fee)
- Updates odds based on bet distribution
- Resolves markets after settlement time
- Distributes winnings proportionally to winners
- Prevents double-claiming of winnings
Section 2: Edge Cases (3 tests)
- Handles minimum bet amounts
- Maintains perfect token conservation (no tokens lost)
- Resolves empty markets with no bets
Section 3: Multi-Market Stress Test (1 test)
- Creates and manages multiple concurrent markets
- Places randomized bets across markets
Section 4: Live BTC Price Integration (5 tests)
- Fetches real BTC price from Pyth Network mainnet
- Creates market at actual BTC price (~$118,829 in example)
- Simulates different trading strategies
- Monitors price movement for 20 seconds
- Resolves based on actual price changes
Test Output Example
๐ TEST SUITE FINAL REPORT
============================================================
๐ Test Results:
Total Tests: 15
Passed: 15 (100.0%)
Failed: 0
๐ฐ Token Conservation:
Initial Supply: 3500.00 tokens
User Balances: 2922.18 tokens
โ
EXCELLENT: All critical tests passed!
๐ป Development Commands
# Build the program anchor build # Run tests anchor test # Deploy to devnet anchor deploy --provider.cluster devnet # Monitor test coverage ./coverage.sh
๐๏ธ Architecture
Program Structure
programs/pythpredict/
โโโ src/
โ โโโ lib.rs # Program entry point
โ โโโ instructions.rs # Core instruction handlers
โ โโโ state.rs # Account structures
โ โโโ errors.rs # Error definitions
Key Components
- Market Account: Stores market parameters, pools, and resolution data
- Position Account: Tracks user bets and payouts
- Vault Accounts: Hold YES/NO pools and fees
- Oracle Integration: Pyth price feeds for resolution
Core Instructions
initialize_market: Create a new prediction marketplace_bet: Place a YES or NO betresolve_market: Resolve using oracle priceresolve_with_external_price: Resolve with manual price (testing)claim_winnings: Claim payouts after resolution
๐ง Configuration
Anchor.toml
- Program ID:
J7TLVPzbd47RpiHV8BBPLQuixU53P5qijkrwkvN4u98W - Default cluster: Localnet
- Test script:
yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/master-test.ts
Test Wallets
Test wallets are stored in .wallets/ directory:
- alice, bob, charlie, dave, eve (test participants)
- Each funded with SOL and test tokens
๐ How It Works
- Market Creation: Creator sets initial price, settlement time, and oracle feed
- Betting Phase: Users bet YES (price will move) or NO (price stays same)
- Price Monitoring: Oracle prices tracked in real-time
- Resolution: At settlement, compare final vs initial price
- Payout: Winners share the total pool proportionally
Payout Calculation
User Payout = (User Stake / Winning Pool) * Total Pool
Example: If YES pool has 100 tokens, NO pool has 50 tokens, and YES wins:
- YES bettors share 150 tokens proportionally
- Each receives their stake + proportional share of losing pool
๐ Security Features
- PDA-based account derivation
- Signer verification for all critical operations
- Overflow protection with checked math
- Time-based settlement enforcement
- Oracle confidence validation
๐ License
MIT
๐ค Contributing
Contributions are welcome! Please read our contributing guidelines before submitting PRs.
๐ Support
For issues, questions, or suggestions, please open an issue on GitHub.