Smart Contract Documentation

Detailed technical documentation of the Solana prediction market program built with Anchor framework. This section covers account structures, instructions, PDA derivations, and error handling.

Program ID:52eknYvAUeqtTkg9KQHkikSg2SQec2qQLFHmvAjsdF3A

Account Structures

The program uses three main account types to store state on-chain. All accounts are PDAs (Program Derived Addresses) for deterministic addressing.

PredictionMarket

Global state account storing market configuration and counters

Seeds:["market"]
Space:160 bytes
Rust
1#[account]
2pub struct PredictionMarket {
3 /// Admin wallet - can update config and withdraw treasury
4 pub authority: Pubkey,
5 /// Oracle wallet - can create and resolve predictions
6 pub oracle_authority: Pubkey,
7 /// SPL token mint used for staking
8 pub token_mint: Pubkey,
9 /// Treasury token account (receives creator fees)
10 pub treasury: Pubkey,
11 /// Counter for prediction IDs
12 pub total_predictions: u64,
13 /// Creator fee in basis points (max 2000 = 20%)
14 pub creator_fee_bps: u16,
15 /// Minimum stake amount in token base units
16 pub min_stake_amount: u64,
17 /// Voting duration in seconds
18 pub voting_duration: i64,
19 /// Minimum votes required for quorum
20 pub quorum_min_votes: u32,
21 /// PDA bump seed
22 pub bump: u8,
23 /// Treasury PDA bump seed
24 pub treasury_bump: u8,
25}

Prediction

Individual prediction with voting data and resolution status

Seeds:["prediction", market_pubkey, prediction_id]
Space:343 bytes
Rust
1#[account]
2pub struct Prediction {
3 /// Unique prediction ID
4 pub id: u64,
5 /// Reference to parent market
6 pub market: Pubkey,
7 /// Token being predicted (the memecoin)
8 pub token_mint: Pubkey,
9 /// Type of prediction (Volume/Price/MarketCap)
10 pub prediction_type: PredictionType,
11 /// Target value for the prediction
12 pub target_value: u64,
13 /// Unix timestamp of creation
14 pub created_at: i64,
15 /// Unix timestamp when voting ends
16 pub end_timestamp: i64,
17 /// Current status: Active, Resolved, or Cancelled
18 pub status: PredictionStatus,
19 /// Winning side after resolution
20 pub winning_side: Option<VoteSide>,
21 /// Total tokens staked on VALID
22 pub valid_stake_total: u64,
23 /// Total tokens staked on FUD
24 pub fud_stake_total: u64,
25 /// Number of unique VALID voters
26 pub valid_vote_count: u32,
27 /// Number of unique FUD voters
28 pub fud_vote_count: u32,
29 /// AI confidence score (0-100)
30 pub ai_confidence: u8,
31 /// IPFS URI for additional metadata
32 pub metadata_uri: String, // max 200 chars
33 /// PDA bump seeds
34 pub bump: u8,
35 pub reward_pool_bump: u8,
36}

UserStake

Tracks individual user's stake on a specific prediction

Seeds:["stake", prediction_pubkey, user_pubkey]
Space:91 bytes
Rust
1#[account]
2pub struct UserStake {
3 /// User wallet address
4 pub user: Pubkey,
5 /// Reference to prediction
6 pub prediction: Pubkey,
7 /// Which side the user voted for
8 pub vote_side: VoteSide,
9 /// Amount of tokens staked (after fee deduction)
10 pub amount: u64,
11 /// Whether rewards/refund has been claimed
12 pub claimed: bool,
13 /// Unix timestamp when stake was made
14 pub timestamp: i64,
15 /// PDA bump seed
16 pub bump: u8,
17}

Enum Types

PredictionType

  • VolumeThreshold

    Volume exceeds threshold in timeframe

  • PriceIncrease

    Price increases by percentage in timeframe

  • MarketCapTarget

    Market cap reaches target value

PredictionStatus

  • Active

    Voting is open (default state)

  • Resolved

    Prediction resolved with a winner

  • Cancelled

    Cancelled due to insufficient quorum

VoteSide

  • Valid

    Prediction will come true

  • Fud

    Prediction is FUD / won't happen

Instructions

The program exposes eight instructions for interacting with the prediction market. Each instruction has specific access control requirements.

InstructionAccessDescriptionParameters
initialize_marketAdminInitialize the global prediction market with configurationcreator_fee_bps, min_stake_amount, voting_duration, quorum_min_votes
create_predictionOracleCreate a new prediction for users to vote ontoken_mint, prediction_type, target_value, ai_confidence, metadata_uri
stake_voteUserStake tokens and vote on an active predictionamount, vote_side
resolve_predictionOracleResolve a prediction after voting period endswinning_side
claim_rewardsUserClaim rewards after being on the winning side
cancel_predictionOracleCancel prediction when quorum is not reached
refund_stakeUserClaim refund from a cancelled prediction
withdraw_treasuryAdminWithdraw accumulated fees from treasuryamount

PDA Derivation

All accounts are Program Derived Addresses (PDAs) with deterministic seeds. This enables trustless account verification and discovery.

AccountSeeds
Market["market"]
Treasury["treasury", market_pubkey]
Prediction["prediction", market_pubkey, prediction_id_le_bytes]
Reward Pool["reward_pool", prediction_pubkey]
User Stake["stake", prediction_pubkey, user_pubkey]

Error Codes

Custom error codes returned by the program for various failure conditions. Handle these in your client implementation.

CodeNameDescription
6000UnauthorizedNot authorized for this action
6001PredictionNotActivePrediction is not in active state
6002VotingEndedVoting period has ended
6003VotingNotEndedVoting period has not ended yet
6004InsufficientStakeStake amount below minimum
6005CannotChangeVoteSideCannot switch vote sides after initial vote
6006AlreadyClaimedRewards/refund already claimed
6007NotWinnerDid not vote on the winning side
6008QuorumNotReachedMinimum votes not reached
6013PredictionNotResolvedPrediction not yet resolved
6014PredictionNotCancelledPrediction not cancelled
6015InsufficientTreasuryBalanceTreasury balance too low
6022NoWinnersNo stakes on winning side
6023QuorumReachedCannot cancel - quorum was met