|
| 1 | +# Betting Market |
| 2 | + |
| 3 | +A parimutuel (pooled) betting market. An admin opens an **event**, adds the possible |
| 4 | +**outcomes**, and bettors stake a token on the outcome they think will win. Every stake across |
| 5 | +every outcome goes into one pool. When the admin settles the event to the winning outcome, the |
| 6 | +losing stakes — minus a protocol fee — are split among the winners in proportion to their stake. |
| 7 | + |
| 8 | +This is the pooled model used by Solana prediction-market platforms such as Hedgehog Markets, |
| 9 | +where odds are set by the crowd's stakes rather than by an order book or a fixed-odds bookmaker. |
| 10 | + |
| 11 | +## Purpose |
| 12 | + |
| 13 | +It solves the core problem of trustless betting: collecting stakes from many bettors, holding them |
| 14 | +in one place no single bettor controls, and paying winners by a fixed, public formula. The pool is |
| 15 | +a token account owned by the event's PDA, so payouts are signed by the program with the event's |
| 16 | +seeds — there is no admin key that can move bettors' stakes out of the pool. The admin's only |
| 17 | +powers are creating events/outcomes and choosing the winning outcome (or cancelling). |
| 18 | + |
| 19 | +## Major Concepts |
| 20 | + |
| 21 | +### Accounts |
| 22 | + |
| 23 | +- **Config** (`seeds = [b"config"]`) — one per deployment. Holds the `admin` (the only key that can |
| 24 | + create events/outcomes, settle, and cancel), the `token_mint` every market accepts, the |
| 25 | + `fee_recipient`, and the `fee_bps`. |
| 26 | +- **Event** (`seeds = [b"event", event_id]`) — one betting market. Tracks `total_pool`, `status` |
| 27 | + (`Open` / `Settled` / `Cancelled`), and — once settled — the `winning_outcome_index`, |
| 28 | + `winning_pool`, and `distributable_losing_pool` that the payout formula reads. The `fee_bps` is |
| 29 | + snapshotted at creation so later Config changes can't alter a market bettors have already joined. |
| 30 | +- **Outcome** (`seeds = [b"outcome", event, index]`) — one possible result. Its `total_amount` is |
| 31 | + the outcome's share of the pool and the denominator for pro-rata payouts when it wins. |
| 32 | +- **Bet** (`seeds = [b"bet", outcome, bettor]`) — a bettor's total stake on one outcome. Re-betting |
| 33 | + the same outcome adds to the existing Bet, so there is exactly one per (outcome, bettor). |
| 34 | +- **User** (`seeds = [b"user", wallet]`) — a per-wallet index listing the bettor's Bet addresses, so |
| 35 | + a client can find someone's positions without scanning every Bet on the program. The list is |
| 36 | + capped (see `MAX_BETS_PER_USER`) to keep the account a fixed size; the Bet accounts are the |
| 37 | + authoritative stake record. |
| 38 | + |
| 39 | +### The vault |
| 40 | + |
| 41 | +Each event owns a single vault token account — the associated token account of the Event PDA for |
| 42 | +`config.token_mint`. `place_bet` moves the stake from the bettor's token account into this vault. |
| 43 | +`settle_event`, `claim_winnings`, and `claim_refund` move tokens back out, with the program signing |
| 44 | +as the Event PDA (`seeds = [b"event", event_id, bump]`). |
| 45 | + |
| 46 | +### Payout formula |
| 47 | + |
| 48 | +When an event settles to a winning outcome: |
| 49 | + |
| 50 | +``` |
| 51 | +losing_pool = total_pool - winning_pool |
| 52 | +fee = losing_pool * fee_bps / 10000 // charged only on the losing side |
| 53 | +distributable_losing = losing_pool - fee |
| 54 | +``` |
| 55 | + |
| 56 | +Each winning bet then claims: |
| 57 | + |
| 58 | +``` |
| 59 | +payout = stake + stake * distributable_losing / winning_pool |
| 60 | +``` |
| 61 | + |
| 62 | +A winner always gets their own stake back; the fee is only ever taken from losing stakes. Integer |
| 63 | +division floors each share, leaving at most a few base units of dust in the vault. |
| 64 | + |
| 65 | +**Worked example:** Outcome A pool 100, Outcome B pool 50, `fee_bps = 200` (2%). A wins. |
| 66 | +`losing_pool = 50`, `fee = 1`, `distributable_losing = 49`. A bettor who staked 40 claims |
| 67 | +`40 + 40 * 49 / 100 = 59`. |
| 68 | + |
| 69 | +### Instruction handlers |
| 70 | + |
| 71 | +| Handler | Who | What it does | |
| 72 | +| --- | --- | --- | |
| 73 | +| `initialize_config` | anyone (becomes admin) | One-time setup: sets admin, stake token, fee, fee recipient. | |
| 74 | +| `create_event` | admin | Opens a market and creates its vault. | |
| 75 | +| `add_outcome` | admin | Adds a possible result. Only before any bet is placed. | |
| 76 | +| `place_bet` | bettor | Stakes tokens on one outcome; updates the pools and the user's index. | |
| 77 | +| `settle_event` | admin | Resolves to a winning outcome, takes the fee, records the payout figures. | |
| 78 | +| `claim_winnings` | winning bettor | Withdraws stake plus pro-rata share of the losing pool. | |
| 79 | +| `cancel_event` | admin | Voids an unresolved market. | |
| 80 | +| `claim_refund` | bettor | After a cancellation, reclaims the exact stake. | |
| 81 | + |
| 82 | +`add_outcome` is locked once betting starts, so the field of choices can't change under existing |
| 83 | +bettors. `settle_event` rejects a winning outcome with no bets — use `cancel_event` to unwind an |
| 84 | +event that can't be resolved fairly. |
| 85 | + |
| 86 | +## Setup |
| 87 | + |
| 88 | +Install the [Solana CLI](https://docs.anza.xyz/cli/install) (provides `cargo-build-sbf`) and |
| 89 | +[Anchor](https://www.anchor-lang.com/docs/installation). Build the program so the test binary |
| 90 | +exists on disk: |
| 91 | + |
| 92 | +```sh |
| 93 | +anchor build |
| 94 | +``` |
| 95 | + |
| 96 | +## Testing |
| 97 | + |
| 98 | +Tests are Rust integration tests running against [LiteSVM](https://www.anchor-lang.com/docs/testing/litesvm) |
| 99 | +with [solana-kite](https://crates.io/crates/solana-kite) helpers. They cover the full lifecycle |
| 100 | +(bet → settle → claim with exact payout and fee assertions), admin authorization, the |
| 101 | +bet-after-settle and double-claim guards, settling an outcome with no bets, and the cancel/refund |
| 102 | +path. |
| 103 | + |
| 104 | +```sh |
| 105 | +anchor test |
| 106 | +``` |
| 107 | + |
| 108 | +(`Anchor.toml` sets `test = "cargo test"`, so `cargo test` works too.) |
0 commit comments