On-chain game programs
The game programs are stateful multiplayer applications on Solana. They let an administrator open a game, let many players submit actions or entries, and record outcomes that control rewards or rankings. The Gacha and World Cup examples use different game rules, but both make the key game state available on-chain so later actions can rely on the same shared result.
Shared game model
The examples divide game state between shared records and player records. Shared records hold the rules and current phase. A player record holds an entry or action that can be checked again when the game produces an outcome.
Game state
Each game keeps a durable record of the facts that affect later actions. Gacha uses a pool for fees and reward odds plus one pull record per purchase. World Cup uses a tournament configuration, one shared result record, and one bracket per entrant. These records allow the program to reject actions that do not match the current phase or the original participant. (games/gacha/pinocchio/program/src/state/pool.rs#L9-L15, games/gacha/pinocchio/program/src/state/pull.rs#L15-L22, games/world-cup/pinocchio/program/src/state/config.rs#L12-L19, games/world-cup/pinocchio/program/src/state/bracket.rs#L9-L16)
The records also preserve ownership and responsibility. A Gacha pool names its administrator and result operator. A Gacha pull names its buyer. A World Cup configuration names its administrator and each bracket names its entrant. This separation lets public actions update shared progress while limiting rule changes and trusted outcome input to the assigned authority.
References: games/…/pool.rs• L22-49 games/…/pull.rs• L31-58 games/…/config.rs• L24-50 games/…/bracket.rs• L21-38
Game lifecycle
Both programs use a staged lifecycle. Setup creates the shared state. Player actions create or update individual state. A later outcome step makes the result final enough for a reward or ranking step.
The two examples express this pattern in different ways:
| Stage | Gacha | World Cup |
|---|---|---|
| Setup | An administrator creates a pool with a fee, reward tiers, and a result operator. | An administrator creates the tournament, result record, and prize vault. |
| Player action | A buyer opens a pull and places the fee in escrow. | An entrant submits one checked bracket and pays the entry fee. |
| Outcome input | The operator reveals a verifiable random output through the cc-vrf registry, or the buyer waits for a refund deadline. | The administrator records game winners and the Round-of-32 goal total. |
| Outcome use | The program selects a reward tier and mints the prize NFT. | The program scores all brackets, selects the best one, and pays the winner. |
The Gacha and World Cup game mechanics page explains the rules inside each of these stages.
References: games/…/init_pool.rs• L82-170 games/…/buy_pull.rs• L81-156 games/…/settle_pull.rs• L116-209 games/…/init_config.rs• L75-127 games/…/submit_bracket.rs• L80-151
The following diagram shows how a shared setup record leads to player state and then to a managed outcome in each example.
flowchart LR
Setup[Game setup] --> Shared[Shared rules and funds]
Shared --> GEntry[Gacha player entry]
GEntry --> GOutcome[Random outcome or refund]
GOutcome --> GReward[Prize claim]
Shared --> WEntry[World Cup bracket entries]
WEntry --> WOutcome[Posted results]
WOutcome --> WScore[Scores and ranking]
WScore --> WPayout[Winner payout]
Initialization and player interaction
Initialization fixes the rules before players act. Player actions then add commitments to those rules instead of changing them, which gives all participants the same basis for later settlement.
Game setup
Game setup creates the shared records and the place where entry funds remain until the outcome is known. Gacha fixes the entry fee, tier weights, settlement deadline, and operator. World Cup fixes the registration deadline and entry fee, then starts with an empty result record. Each program emits an initialization event so clients can discover the new game state.
The setup step is therefore more than account creation. It establishes which actors may manage outcomes and which values must remain stable for every player.
References: games/…/init_pool.rs• L82-118 games/…/pool.rs• L59-87 games/…/init_config.rs• L75-85 games/…/config.rs• L60-80
Player entry
Player entry creates a durable claim on a future result. A Gacha buyer pays the pool fee and supplies client entropy before the pull becomes pending. A World Cup entrant pays the fee and submits a full bracket while registration is open. The program checks the entry against the active rules before it accepts the funds and records the entry.
The entry records also make later processing independent of the client that sent the first transaction. A later operator, administrator, or permissionless caller can read the recorded entry and continue the game without asking the player to repeat it.
References: games/…/buy_pull.rs• L81-87 games/…/buy_pull.rs• L115-154 games/…/pull.rs• L67-105 games/…/submit_bracket.rs• L80-99 games/…/submit_bracket.rs• L114-149
Participation windows
Time and phase rules control when players may act. World Cup accepts brackets only during registration and moves to a locked phase after its configured time. Gacha does not close all entries at one time; each pull stays pending until settlement or until its individual refund deadline passes.
This difference shows two common multiplayer patterns: a shared round with one cutoff, and independent player requests with their own completion window.
References: games/…/lock.rs• L42-64 games/…/submit_bracket.rs• L83-98 games/…/buy_pull.rs• L115-150 games/…/refund_pull.rs• L48-90
Outcome management
Outcome management turns player state into a result that other participants can inspect and use. The programs separate result input from reward or ranking work, so the source of an outcome and the action that applies it remain clear.
Random outcome settlement
Gacha uses buyer entropy and a registered operator to produce each pull result. The program derives the random input from the pull address and the buyer's seed. The operator supplies the random output and proof, then the settlement path commits their hashes and the output to the external cc-vrf registry through a Light Protocol validity check. The program records the selected fixed-weight reward tier after that commit succeeds.
The registry binds the commit to the configured operator authority and to one pull. This prevents a second reveal for the same pull and makes an operator with an unfrozen, revoked, or mismatched registration unable to settle. The program does not verify the full ECVRF proof itself. Clients can verify the proof and output from the settlement event and registry record.
If the operator does not reveal the result in time, the buyer can recover the entry fee and close the pending pull. This refund path preserves player funds without allowing a late operator action to settle the same pull. (games/gacha/pinocchio/README.md#L12-L39, games/gacha/pinocchio/program/src/gacha.rs#L31-L50)
References: games/…/gacha.rs• L31-74 games/…/ccvrf.rs• L1-16 games/…/settle_pull.rs• L116-209 games/…/refund_pull.rs• L48-109 games/…/gacha_settle.rs• L405-494
Competition outcome scoring
World Cup separates result posting from score calculation. After registration closes, the administrator records each game winner in an order that respects the bracket. The program also records the Round-of-32 goal total, which supplies the tiebreaker value.
Once all results are present, anyone can refresh an entrant's score. The shared ranking keeps the highest score, then the closest goal guess, then the earliest submission. The submission order makes the ranking total, so the finalization step can identify one bracket.
References: games/…/tournament.rs• L83-159 games/…/config.rs• L12-19 games/…/post_result.rs• L65-103 games/…/post_goals.rs• L63-94 games/…/refresh_score.rs• L42-111 games/…/test_finalize.rs• L33-118
Rewards and payouts
The final step delivers the value linked to the outcome. In Gacha, a settled pull can be claimed as one Token-2022 prize NFT. The NFT records the selected rarity in its metadata and gives the single token to the buyer recorded in the pull. The mint authority is removed after minting, so the prize cannot be increased.
In World Cup, an administrator finalizes the bracket that matches the on-chain ranking key. The recorded winner can then claim the available pot. This separates proof of who won from the later movement of funds.
References: games/…/prize_mint.rs• L1-12 games/…/prize_nft.rs• L1-6 games/…/prize_nft.rs• L52-109 games/…/finalize.rs• L48-53 games/…/test_finalize.rs• L33-51
Authority and liveness boundaries
Multiplayer game programs must handle both trust and delay. The examples assign narrow authority to setup and result input while leaving safe progress steps open to other callers.
Authority roles
Gacha has an administrator who owns the pool and a registered operator who supplies random reveals. Settlement checks that the caller matches the pool operator and that the cc-vrf record is the configured frozen authority. World Cup has an administrator who controls the tournament setup and posts the official results. Player ownership stays separate from these roles, so an authority cannot redirect a prize to a different buyer or change another entrant's bracket through the normal outcome flow.
The programs also keep outcome evidence in shared state and emitted events. Clients can use those records to display the same result to all participants instead of trusting a private server response.
References: games/…/pool.rs• L27-39 games/…/ccvrf.rs• L81-87 games/…/settle_pull.rs• L130-191 games/…/event_engine.rs• L83-109 games/…/config.rs• L24-50 games/…/post_result.rs• L69-75 games/…/event_engine.rs• L93-127
Permissionless progress
Some actions do not need the original actor. Gacha prize claims can be cranked by any funded signer. World Cup score refreshes are permissionless and idempotent, so a caller can process each bracket after the oracle is complete without gaining control of the prize.
This pattern keeps the game live when the player or administrator is not online at the exact time a later step becomes possible. The program still checks the stored owner, phase, and result before it changes state.
References: games/…/claim_prize.rs• L80-83 games/…/refresh_score.rs• L42-44 games/…/refresh_score.rs• L70-107 games/…/refund_pull.rs• L48-55
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the pull status, pending-pull count, refund deadline, and vault liability in step. A settlement or refund must release exactly one pending fee obligation.
- Keep the Gacha randomness contract aligned across buyer seed handling, tier weights, cc-vrf commit fields, settlement events, and off-chain proof checks. A change to one binding can make old outcomes hard to verify or allow a replay.
- Keep the cc-vrf and Light account order, authority fields, and canonical tree assumptions aligned with the external registry. These values cross the program boundary and can fail without a local state error.
- Keep event discriminators and serialized fields aligned with indexers. Gacha and World Cup clients use the event stream to reconstruct lifecycle progress.
- Keep prize metadata, mint uniqueness, and the buyer recorded in the pull aligned. The caller may pay for a claim, but must not become the prize owner.
- Keep World Cup result topology, score weights, closeness rules, submission order, and the final ranking key aligned. A change in one rule can change which bracket is valid or wins.
- Keep the committed IDL and generated Rust client aligned with each program's instruction and account contract. Regenerate both after a program interface change.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A Gacha operator does not reveal a pending pull before its deadline. | The buyer waits for settlement and may otherwise lose access to the entry fee. | The buyer can refund the fee after the deadline, and the pull is closed. |
| A Gacha reveal uses an unfrozen, revoked, or mismatched cc-vrf authority. | The pull remains pending because the external commit cannot prove the configured operator record. | Settlement binds the registry record to the pool operator and authority label through a Light validity proof. |
| A Gacha reveal is submitted twice for one pull. | A second random result could replace the first result or create another settlement. | The pull status and the registry commit address both allow only one reveal. |
| A Gacha tier configuration has no usable weight. | A pull cannot map its random output to a reward tier. | Pool setup rejects invalid tier configuration, and tier selection rejects a zero total. |
| An admin withdraws funds reserved for pending Gacha pulls. | A later refund may lack its entry fee. | Withdrawal leaves the rent floor and all pending-pull fee liabilities in the vault. |
| A World Cup result is posted for a game before its feeder games are decided. | Later bracket scores could use a result that does not match the tournament tree. | Result validation requires feeder results before dependent games. |
| A World Cup bracket is refreshed before every official result and the goal total are known. | The global ranking could be based on partial results. | Score refresh requires a complete oracle. |
| A World Cup bracket is refreshed twice at the final result mask. | One entrant could count more than once in the global tally. | Each bracket records its tally mask and rejects an already folded bracket. |
| A payout caller does not match the recorded winner or buyer. | Funds or a prize could go to the wrong participant. | Claim paths compare the caller with the stored winner or buyer before delivery. |
| A program change is not reflected in the committed IDL or generated client. | Clients can build invalid instructions or decode state with the wrong contract. | The build flow regenerates the IDL and Rust client from the program contract. |
Key files
| File | Why to read it |
|---|---|
| games/…/README.md | Gives the Gacha product flow and its randomness trust model. |
| games/…/pool.rs | Shows the shared Gacha rules, operator role, fee escrow, and pending-pull accounting. |
| games/…/pull.rs | Shows the player-level Gacha lifecycle from pending pull to settled or claimed result. |
| games/…/gacha.rs | Shows how buyer entropy becomes a reproducible random input and how weighted tiers are selected. |
| games/…/buy_pull.rs | Shows how a player entry commits funds and randomness input. |
| games/…/settle_pull.rs | Shows how an external random result becomes a recorded game outcome. |
| games/…/ccvrf.rs | Shows how settlement binds a reveal to the external VRF registry and Light validity context. |
| games/…/refund_pull.rs | Shows the liveness path for an uncompleted player action. |
| games/…/withdraw_fees.rs | Shows how settled revenue is separated from pending refund liabilities. |
| games/…/claim_prize.rs | Shows how a settled outcome becomes a player-owned prize. |
| games/…/prize_nft.rs | Shows the one-of-one prize NFT and its rarity metadata. |
| games/…/event_engine.rs | Shows the event stream used to expose Gacha lifecycle changes to indexers. |
| games/…/README.md | Summarizes the World Cup lifecycle and its unique-winner rule. |
| games/…/config.rs | Shows tournament-wide phase, entry, tally, and winner state. |
| games/…/bracket.rs | Shows the durable record for one entrant and the score-fold marker. |
| games/…/tournament.rs | Shows the bracket rules, result checks, weighted scoring, and tiebreaker model. |
| games/…/submit_bracket.rs | Shows how a player enters a checked bracket during registration. |
| games/…/refresh_score.rs | Shows permissionless score calculation and one-time tally updates. |
| games/…/finalize.rs | Shows how the program proves the unique winning bracket before payout. |
| games/…/event_engine.rs | Shows the event stream used to expose tournament lifecycle changes to indexers. |
| games/…/world_cup.json | Defines the public World Cup accounts, instructions, states, and errors used by clients. |