Introducing a new way to review your PRs.

Read the announcement

solana-foundation / program-examples

A repository of Solana program examples

View on GitHub

19 documents · Updated Nov 23, 2026

docs / gacha-and-world-cup-mechanics

Gacha and World Cup game mechanics

The Gacha and World Cup examples show two game models on Solana. Gacha turns a paid pull into a verifiable random prize. World Cup turns a paid bracket into a ranked competition with one winner. Both programs keep the game rules, money flow, and final result on-chain; see Game programs for the wider program context.

Game models

Each game uses persistent program state to control who can act and when. Gacha waits for a random reveal for each pull. World Cup waits for a complete set of tournament results before it ranks entries.

GameParticipant actionOutcome sourceReward
GachaPay to open a pullVerifiable random function (VRF) outputOne Token-2022 NFT with a rarity label
World CupPay to submit one full bracket and a goal guessAdmin-posted tournament resultsThe pot goes to the unique top-ranked bracket

Gacha mechanics

Gacha models a fixed-price pool with fixed reward odds. A buyer commits private input when opening a pull. A registered operator later reveals the random result, and the buyer receives the prize only after that result is settled.

Pool and pull state

The pool is the game configuration and the accounting boundary for all pulls. Its admin sets the entry fee, active reward tiers, tier weights, registered operator, and settlement deadline. These values stay fixed for the life of the pool, so the odds do not change as more users play. games/…/pool.rs• L9-15 games/…/pool.rs• L27-49

Each pull belongs to one buyer and one pool. It records the buyer's random seed, the derived VRF input, the revealed output, the chosen tier, and its current status. The status moves from Pending to Settled to Claimed; a pending pull is a liability that the pool must be able to refund. games/…/pull.rs• L15-24 games/…/pull.rs• L31-58

References: games/…/pool.rs• L9-15 games/…/pull.rs• L15-58 games/…/vault.rs• L5-12

Random draw

The pull uses a commit-reveal flow. The program derives alpha by hashing the pull address with the buyer's 32-byte seed. The operator then produces the VRF output beta for that input. Because the buyer seed is part of the input, the operator cannot prepare a preferred result before the purchase is recorded. games/…/gacha.rs• L31-41 games/…/settle_pull.rs• L144-168

Settlement anchors the reveal in the cc-vrf registry and records the output and selected tier. The registry commit is tied to the pull and the pool's registered operator. Its Light validity proof also binds the operator authority record, including its key, label, and frozen, unrevoked status. The program maps the output into the pool's fixed weighted tiers. Anyone can repeat this calculation from the stored pull and pool data. games/…/ccvrf.rs• L1-16 games/…/ccvrf.rs• L81-87 games/…/settle_pull.rs• L116-125 games/…/settle_pull.rs• L166-209 games/…/gacha.rs• L43-75

The on-chain trust model does not verify the full ECVRF proof. It accepts the registered operator's reveal and makes dishonest output detectable through the recorded proof and registry evidence. games/…/lib.rs• L3-18 games/…/gacha_settle.rs• L1-9

References: games/…/gacha.rs• L31-75 games/…/ccvrf.rs• L1-16 games/…/ccvrf.rs• L81-87 games/…/settle_pull.rs• L116-125 games/…/settle_pull.rs• L166-209 games/…/lib.rs• L3-18

Prize and refund paths

After settlement, a claim creates one Token-2022 NFT for the buyer. The NFT stores the rarity label in its own metadata, and its mint is tied to the pull so one pull can produce only one prize. Any funded signer can perform the claim, but the prize always goes to the pull's buyer. games/…/claim_prize.rs• L76-83 games/…/prize_nft.rs• L52-66 games/…/prize_mint.rs• L5-12

An unsettled pull has a liveness escape path. After its deadline, the buyer can reclaim the entry fee and close the pull. The admin can withdraw only the vault balance that remains after the rent floor and all pending-pull liabilities. This protects buyer funds while allowing settled fees to become revenue. games/…/refund_pull.rs• L48-55 games/…/refund_pull.rs• L74-109 games/…/withdraw_fees.rs• L66-110

The Gacha lifecycle connects one paid action to either a prize or a refund.

flowchart LR
    A[Admin configures pool] --> B[Buyer opens pull and pays fee]
    B --> C[Pending pull records buyer input]
    C --> D[Registered operator reveals VRF output]
    D --> E[Program selects fixed-weight tier]
    E --> F[Anyone claims one NFT for buyer]
    C --> G{Deadline passes without reveal?}
    G -->|Yes| H[Buyer refunds fee]

References: games/…/init_pool.rs• L82-112 games/…/buy_pull.rs• L81-88 games/…/claim_prize.rs• L76-83 games/…/refund_pull.rs• L48-56 games/…/withdraw_fees.rs• L66-110

World Cup mechanics

World Cup models a fixed tournament prediction contest. An entrant submits one complete bracket before the lock time. An admin records the real results, and the program uses those results to rank every submitted bracket.

Tournament and bracket state

The tournament has three states: Registration, Locked, and Finalized. Registration accepts one paid bracket per wallet. The locked state accepts result updates and score refreshes. Finalization records the winner before the pot can be claimed. games/…/common.rs• L53-63 games/…/config.rs• L12-50

The shared configuration tracks the entry fee, entrant count, refresh progress, current best ranking key, and winner. The ranking key is meaningful only after the oracle is complete and every bracket has been folded into the tally. A separate bracket records each entrant's 32 picks, goal guess, score, and submission order. The oracle is the shared result record for the 32 games and the Round-of-32 goal total. games/…/config.rs• L12-50 games/…/bracket.rs• L9-37 games/…/oracle.rs• L13-35

References: games/…/common.rs• L53-63 games/…/config.rs• L12-50 games/…/bracket.rs• L9-37 games/…/oracle.rs• L13-35 games/…/vault.rs• L5-12

Bracket rules

The bracket uses positional team identities rather than display names. It has 32 game picks, including the knockout rounds and the third-place game. Each pick must come from the teams that could reach that game through the entrant's own earlier picks. games/…/tournament.rs• L1-15 games/…/tournament.rs• L63-108

The same dependency rule applies to posted results. The admin must record feeder games before later games, and each result is written once. This keeps both entrant brackets and the shared tournament record internally consistent. games/…/tournament.rs• L111-148 games/…/post_result.rs• L65-99

References: games/…/tournament.rs• L1-15 games/…/tournament.rs• L63-148 games/…/post_result.rs• L65-99

Competition scoring

The admin-posted oracle supplies the competition truth. After all 32 winners are recorded, the admin posts the total Round-of-32 goals. The goal total is separate from match winners because it is used only for the tiebreaker. games/…/oracle.rs• L13-35 games/…/post_goals.rs• L63-89

The program gives more points to correct picks in later rounds. It ranks equal scores by the absolute difference between the entrant's goal guess and the actual total. If that is also equal, the earlier submission wins. Anyone can refresh a bracket after the oracle is complete, and each bracket is folded into the global tally once for that result set. The refresh order does not change the best ranking key. games/…/tournament.rs• L38-40 games/…/tournament.rs• L151-165 games/…/refresh_score.rs• L42-107 games/…/config.rs• L14-18 games/…/test_finalize.rs• L93-117

References: games/…/oracle.rs• L13-35 games/…/tournament.rs• L38-40 games/…/tournament.rs• L151-165 games/…/refresh_score.rs• L42-107 games/…/config.rs• L14-18

Winner and payout

Finalization is an admin action that checks three conditions before recording a winner: the oracle is complete, every bracket has been refreshed, and the supplied bracket matches the stored best ranking key. The submission order makes this key total, so exactly one bracket can win. games/…/finalize.rs• L48-53 games/…/finalize.rs• L57-112 games/…/config.rs• L14-18

Only the recorded winner can claim the pot. Bracket accounts can later be closed after finalization, and their returned rent flows into the vault for a later winner claim. games/…/claim.rs• L48-82 games/…/close_bracket.rs• L44-74

The World Cup lifecycle separates entry, result recording, scoring, and payout.

flowchart LR
    A[Admin creates tournament] --> B[Entrants submit valid brackets]
    B --> C[Admin locks registration]
    C --> D[Admin posts ordered game results]
    D --> E[Admin posts Round-of-32 goals]
    E --> F[Anyone refreshes every bracket]
    F --> G[Admin finalizes the best ranking key]
    G --> H[Recorded winner claims pot]

References: games/…/submit_bracket.rs• L80-149 games/…/lock.rs• L42-63 games/…/post_result.rs• L65-102 games/…/post_goals.rs• L63-92 games/…/finalize.rs• L48-115 games/…/claim.rs• L48-82

Client-to-program flows

The client boundary is the Codama contract for each program. The committed IDL describes account state, instruction inputs, and permitted actions. The generated Rust client exposes that contract so a caller can build transactions without moving game rules into a separate client implementation. games/…/lib.rs• L1-9 games/…/lib.rs• L1-9

Gacha client flow

A Gacha caller reads the pool, derives the pull account for the buyer and next index, and sends buy_pull with fresh buyer entropy. Later, the registered operator sends settle_pull with the reveal data and the Light validity context needed to anchor the reveal. A caller then sends claim_prize, or the buyer sends refund_pull after the deadline. The IDL exposes these actions and the Pending, Settled, and Claimed states to clients. games/…/gacha.json• L18-297 games/…/gacha.json• L819-1677 games/…/gacha_settle.rs• L256-317

The program emits pull, settlement, refund, fee, and prize events as the state changes. A client can use those records to show progress and to retain the evidence needed to check a reveal outside the program. Events use a stable tag and discriminator so indexers can identify their type. games/…/event_engine.rs• L66-109 games/…/settle_pull.rs• L201-209 games/…/refund_pull.rs• L102-109 games/…/withdraw_fees.rs• L105-110

References: games/…/lib.rs• L1-9 games/…/gacha.json• L18-297 games/…/gacha.json• L819-1677 games/…/ccvrf.rs• L45-72 games/…/event_engine.rs• L66-109 games/…/gacha_settle.rs• L256-317

World Cup client flow

A World Cup caller reads the configuration and oracle to determine the current phase. During registration, an entrant sends the 32 picks and goal guess in submit_bracket. After the lock, the admin sends ordered result and goal updates. Once the oracle is complete, any caller can refresh brackets, then the admin finalizes the best bracket and the winner claims the pot. games/…/world_cup.json• L18-317 games/…/world_cup.json• L681-1701 games/…/test_helpers.rs• L96-239

The client must treat program state as the source of truth for phase, score, and winner. Display names and other presentation data can explain positional team identities, but they do not replace the bracket and oracle values that the program checks. Refresh transactions may be sent by different callers and in any order. games/…/tournament.rs• L1-7 games/…/config.rs• L12-50 games/…/test_finalize.rs• L93-117

References: games/…/lib.rs• L1-9 games/…/world_cup.json• L18-317 games/…/world_cup.json• L681-1701 games/…/tournament.rs• L1-7 games/…/test_helpers.rs• L193-239

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to Gacha tier count, weights, or rarity labels must keep pool validation, tier selection, prize metadata, and the IDL contract aligned.
  • Changes to Gacha pull status or deadline rules must preserve the relationship between pending liabilities, settlement, refund, and fee withdrawal.
  • Changes to Gacha VRF input or reveal data must preserve byte-level agreement between the program, the cc-vrf registry record, emitted evidence, and client verification.
  • Changes to the Gacha prize mint must preserve the one-pull-to-one-NFT rule and delivery to the recorded buyer.
  • Changes to the Gacha operator authority contract must keep pool registration fields aligned with the cc-vrf authority record and its Light validity proof.
  • Changes to World Cup game indexes or bracket topology must update bracket validation, result ordering, scoring weights, and client team mapping together.
  • Changes to World Cup ranking fields must keep refresh, finalization, and displayed leaderboards based on the same score, goal distance, and submission order.
  • Changes to World Cup lifecycle states must preserve the order of submission, lock, oracle completion, refresh, finalization, and payout.
  • Changes to either program's instructions or account fields must regenerate and review the matching Codama IDL and Rust client contract.
  • Changes to event tags or discriminators must preserve indexer decoding for both programs.

Important failure modes

TriggerConsequenceGuard
Gacha settlement uses an input that does not match the stored pullThe reveal cannot represent that pull, so settlement fails or the evidence does not verifyDerive the VRF input from the pull address and stored buyer seed
Gacha operator is not registered with the pool or its authority is not frozenThe registry proof cannot match the pool's required operator record, so settlement failsPin the operator and authority label in pool state and require the matching frozen, unrevoked authority
Gacha operator does not reveal before the deadlineThe buyer cannot receive a prize from that pull, but can reclaim the entry feePending status and deadline-gated refund
Gacha pool has no valid active tier weightThe program cannot map a random output to a prize rarityPool initialization rejects zero or invalid tier configuration
Gacha claim targets a non-settled pull or repeats a claimed pullNo prize is minted, or the one-prize rule would be at riskPull status checks and a pull-derived prize mint
World Cup bracket picks a team that cannot reach its gameThe entrant cannot submit a coherent tournament pathBracket validation checks each feeder relationship
World Cup posts a result before its feeder gamesThe oracle could contain a result for an unknown matchupResult validation requires feeder games first
World Cup finalizes before all results, goals, or brackets are foldedThe recorded best bracket may not represent the full contestFinalization requires a complete oracle and refresh count equal to entrant count
World Cup payout is requested by a non-winnerFunds stay in the vault and the claim failsClaim checks the recorded winner and finalized state

Key files

FileWhy to read it
games/…/README.mdProduct goal and randomness trust model
games/…/lib.rsProduct-level Gacha flow and trust boundary
games/…/gacha.rsShared random-input and weighted-tier rules
games/…/ccvrf.rsRegistry boundary for operator authority and one reveal per pull
games/…/pool.rsPool odds, fees, operator, and pending-liability state
games/…/pull.rsPull state from buyer commitment through claim or refund
games/…/settle_pull.rsReveal, registry evidence, and settlement boundary
games/…/refund_pull.rsDeadline refund and pending-liability release
games/…/withdraw_fees.rsRevenue withdrawal after pending liabilities are reserved
games/…/prize_nft.rsOne-prize NFT and rarity metadata behavior
games/…/gacha.jsonClient-facing Gacha accounts and instruction contract
games/…/README.mdProduct goal and tournament lifecycle
games/…/tournament.rsBracket topology, result rules, and scoring model
games/…/config.rsTournament phase and global ranking key
games/…/bracket.rsEntrant picks, tiebreaker, and refresh state
games/…/oracle.rsShared match results and goal total
games/…/refresh_score.rsPermissionless fold into the global ranking
games/…/finalize.rsWinner proof before payout
games/…/close_bracket.rsPost-finalization bracket cleanup and rent return
games/…/world_cup.jsonClient-facing World Cup accounts and instruction contract
games/…/lib.rsGenerated Rust client entry point for Gacha
games/…/lib.rsGenerated Rust client entry point for World Cup