Smart contracts for M2, a free-to-play sports prediction game, on Injective EVM (chainId 1439
testnet, 1776 mainnet). Off-chain code (PWA, admin, API, worker) lives in injective-m2-frontend.
| Contract | Role |
|---|---|
ResultsRegistry |
Per-game fixtures and results. Results come from LocalTruth attestations (CosmWasm); sourceHash is the attested file's SHA-256. The steward can correct a result once, within correctionWindow. |
Predictions |
Leagues, rounds (matchdays), users' signed predictions, round settlement, on-chain layer-0 scoring, final standings root (proposed, anchored once 48 h later). |
operator: registerGames ─▶ createLeague ─▶ scheduleRound(leagueId, matchday, opensAt, lockTime, gameIds)
user: "Confirm" in the app ─▶ worker signs EIP-712 Submission with the user's custodial key
submitter: submitForBatch(roundId, items) (≤ 100 per tx, ~40k gas each; we pay the gas)
relayer: recordResults(...) (after the start and the LocalTruth attestation)
steward: correctResult(result, reason) (once per game, within correctionWindow, 24 h,
of its result; restarts the round's delay)
anyone: finalizeRound(roundId) (finalizationDelay, 24 h, after the round's last
result and once none can be corrected:
snapshots outcomes)
anyone: scoreOf / leagueStatsOf (layer-0 score recomputed on-chain)
steward: proposeStandings(root, ...) (Merkle root of the final standings, replaceable;
the league takes no new round from then on)
anyone: checkStanding(leaf, proof) (a row against the root and against the chain)
steward: finalizeLeague(leagueId) (48 h after the proposal, write-once)
Key rules:
-
One submission per user per round, immutable, accepted only while the round is open.
submitFor/submitForBatchare restricted toSUBMITTER_ROLE: users never send transactions, and a user who exported their key cannot bypass the app's lock. -
Picks schema 1: one byte per game in round order,
1home /2away /3draw, all games required. Stored packed (2 bits/game), in clear: scores are verifiable by anyone. A draw pick is accepted on any game but can only score where the registry allows draws (the app offers it only there). -
picksHash = keccak256(abi.encode(roundId, gamesHash, keccak256(picks)))is recomputed by the contract; it is the receipt the app shows. -
confirmedAt(signed by the user) must be in[opensAt, lockTime)and at mostMAX_CLOCK_SKEW(60 s) ahead of the block time.submitForreverts on an invalid item (SubmissionRejected);submitForBatchskips it (SubmissionSkippedevent plus a status per item), so a retried batch is harmless. The signature is checked withECDSA.tryRecover(no malleable signatures). -
Limits: 32 games per round (2-bit packing in a
uint64), 64 rounds per league, 100 items per batch (registerGames,recordResults,submitForBatch). A round's game list is immutable and its id is never reused, so a signature can never be replayed onto other games. -
A game counts in a round only if it was
Final/Forfeitand did not start before the lock.registerGamestakes only future start times (StartTimeInPast), andrescheduleGameonly moves a game that has not started yet, to a future time: once its start time has passed it never moves again (GameAlreadyStarted), and from that same instant it takes a result. So at the lock the games that can count in a round are fixed: one that started before the lock never comes back (picks confirmed after it was played would score on it), one that starts after can only move later. Only a void removes a game afterwards. -
Postponements. Known before the start:
rescheduleGameto the new date. Known after it: the start time stays; the replay's result is recorded on the same game id and counts in the round, with the same score as if the date had moved. Until then nothing on-chain refuses an early result for that game, so the worker must not record avoidfor a game that is only postponed: pastcorrectableUntilthe replay could no longer be recorded. A replay that should count in a later round needs a new game id (a started game cannot be scheduled again). -
Forfeits decided before the start. The operator moves the game's start to the announcement (
rescheduleGameto a few minutes from now), then the relayer records theForfeitonce that start has passed. The game counts only in rounds locked before the announcement, whose picks were all made without knowing it; in rounds still open it counts for no one. Do not move a round's lock to keep the game. A round that locks between the announcement and the reschedule stays exposed: cancel it before it settles. -
updateRoundLockonly moves a lock to at leastMIN_LOCK_NOTICE(15 min) from now, so the submitter has time to land the picks already confirmed for the round. The trade-off: a game moved to start sooner than that cannot keep its round's lock before it, so it does not count there. Moving a lock earlier ignores the round's games whose start has already passed (they stay excluded), so such a game does not block keeping another game brought forward. -
Home/away teams are registered with the fixture;
recordResultsrejects a swapped file. -
Results can be corrected once, by the steward. They come from an accredited institutional source (attested on LocalTruth), so there is no challenge period: the relayer writes the first attested result. No result is accepted before the start,
Voidincluded. The risk is a relayer bug (e.g. a row matched to the wrong game, home and away scores swapped) that the on-chain checks do not catch, or a wrong attestation. The remedy iscorrectResult:- once per game, within
correctionWindow(24 h, admin-settable between 1 h and 7 days) of its result; the deadline is fixed when the result is written, so a later change of the window never reopens it; - same checks as
recordResults; the source may be the same attested file (parser bug) or a new one; it must change the result or itssourceHash, otherwise it reverts (UnchangedResult) and the correction is not used up;ResultCorrectedcarries both the result and the steward's reason; - it also turns a void (by the relayer or
voidGame) into a result; - it moves
recordedAtto now, so every round of the game gives the corrected result a fullfinalizationDelay.
A round settles only
finalizationDelay(24 h, admin-settable between 1 h and 7 days) after its last result and once none of its results can be corrected (correctableUntil), whatever the two windows are set to: a settled round never misses a correction. If the correction is wrong too, or the window has closed, the steward can still cancel the round before it settles (see "Pause and incidents"). - once per game, within
-
A game with no result
postponeWindow(7 days, admin-settable between 1 and 30 days) after its start can be voided by the steward, so a round is never blocked by a game that is never played nor uploaded. The start is fixed once it has passed, but the void date uses the currentpostponeWindow: an admin change of the window moves it for every game still without a result (later if increased, earlier if decreased). The steward can void only whenblock.timestamp > startTime + postponeWindow. It is a minimum, not a deadline: until the steward voids it, a late replay is still recorded and counts. -
Final standings in two steps. The steward proposes the root once every round of the league is finalized or cancelled (at least one finalized); a new proposal replaces it and restarts the 48 h review. During the review
checkStandingverifies any row against the proposed root and againstleagueStatsOf, so a wrong row is provable on-chain. From the first proposal on,scheduleRoundrefuses new rounds for the league (StandingsAlreadyProposed): the season is over, and the operator key cannot hold upfinalizeLeague. The review starts only when the steward proposes; the app's worker prepares the standings once the league'sendsAthas passed and every round is settled. A proposal made too early (e.g. a wrong end date) would end the season: only the admin can undo it,withdrawStandings(leagueId), and the league then takes rounds again. -
Ranking policy (off-chain, from
leagueStatsOf; referencerankPlayersinscripts/lib/m2.ts):- Players: every account with
roundsPlayed ≥ 1, i.e. an entry in at least one finalized round with at least one counted game. Zero points still ranks. An account with no entry, or whose entries are only in cancelled rounds or in rounds where none of its games counted, is not ranked.totalPlayersis the number of ranked accounts (the leaves of the tree). - Order: points ↓ → rounds played ↓ → average lead time before the lock ↓
(
sumLeadTime / roundsPlayed, compared exactly, not rounded; it is compared only between players with the same rounds played, so this is the order ofsumLeadTime). A round's lead time is itslockTimewhen finalized minus the entry'sconfirmedAt. - Ties: players equal on all three share the rank ("1224").
- Draw: only for players sharing a rank across a prize cutoff. Seed = hash of the block that
contains the league's
LeagueFinalizedevent; the tied players are ordered bykeccak256(abi.encodePacked(seed, account))ascending, and the first take the remaining places.
- Players: every account with
homeScore/awayScore hold the unit that decides the winner (e.g. sets, points or goals,
depending on the sport). Partial scores stay in the attested file that sourceHash points to. Each
game is registered with its format, and recordResults checks every result against it. The format
is per game, so any head-to-head sport needs no contract change, only its format (set in the app's
CMS):
| Format | targetScore |
drawAllowed |
A valid Final |
|---|---|---|---|
| Race to N (e.g. best of 5: N = 3) | N | no | the winner has exactly N: 3-0, 3-1, 3-2 |
| Open score | 0 | no | any score, the side ahead wins |
| Open score with draws | 0 | yes | any score; a level score is Draw |
- With a target there are no
extraPeriodsand no draws (registering both is refused). extraPeriodscounts periods past regulation (e.g. overtime), for sports that have them.Forfeit: the winner isHomeorAway, the score is 0-0 or the winner is ahead, never past the target.Side.Drawis 3, so it fits the 2 bits per game used for picks and outcomes.- Not supported: a game given up by the side that leads (the forfeit check refuses it) and a level score decided by a shootout (a level score is only a draw).
| Role | Holder | Can |
|---|---|---|
DEFAULT_ADMIN_ROLE |
Safe multisig, 2-of-3 (2-step transfer with delay) | roles, unpause, setPostponeWindow, setCorrectionWindow, setFinalizationDelay, withdrawStandings |
STEWARD_ROLE |
Safe multisig, 2-of-3 (the app's inbox prepares the calls) | pause, voidGame, correctResult, cancelRound, proposeStandings, finalizeLeague |
OPERATOR_ROLE |
worker key | fixtures, leagues, rounds, lock updates |
RESULTS_RELAYER_ROLE |
worker key | recordResults |
SUBMITTER_ROLE |
worker key | submitFor, submitForBatch |
Use one key per queue to avoid nonce contention. Every holder is a constructor argument: the deploying account gets no role.
The steward (Safe multisig, 2-of-3) pauses both contracts in minutes; only the admin (Safe
multisig, 2-of-3) unpauses. While paused every write stops except correctResult, cancelRound,
the admin functions (including grantRole/revokeRole) and the views. A pause near a lock loses
that round's late submissions: cancel it.
- Wrong result recorded: within
correctableUntilof the game (the worker's inbox shows it), the steward callscorrectResultwith the right result. No pause is needed: the correction restarts the delay of every round of the game. If the relayer is at fault, the steward also pauses the registry to stop more writes (correcting works while paused); the admin revokes the faulty key if needed and unpauses. - Wrong result past its correction window, or a wrong correction: before the round settles,
the steward pauses Predictions (so nobody calls
finalizeRoundmeanwhile) and cancels the round; the admin unpauses. - Deadline: the round's
finalizableAt, read on-chain, not the end of the correction window. With equal windows (24 h / 24 h) a round can be finalized at the very moment its last correction window closes, so there is no guaranteed time after it: pause Predictions or cancel the round beforefinalizableAt. After it, cancelling works only while the round is not finalized, and unless Predictions is paused anyone can finalize it first, a compromised or revoked worker included. - Worker key compromised: the steward pauses both contracts. Pausing Predictions is what
stops
finalizeRound: pausing only the registry, or revoking the worker's roles, does not. The steward then cancels every unsettled round that has a result (or a reschedule) written by the compromised key and not confirmed by an authentic attested result; the admin revokes the key, grants a new one and unpauses. Rounds settled in the meantime are not affected and cannot be repaired: revoking the key does not undo a finalized score.
The worker keys (operator, relayer, submitter, custodial signing) are trusted for fixtures and results. The contracts check a result against the start time the operator stored, not against the real fixture, and do not verify attestations on-chain. So, if the worker is compromised, a result can be recorded before the real game is played. Likewise, every lock check uses the stored start times and lock: if they are false or stale, a pick made after the real game can score, or a game can be excluded from a round once its outcome is known. These are accepted residual risks:
- Detection must not depend on the worker: fixtures and results are compared, from the on-chain events, with the official calendar and with their LocalTruth attestation.
- The worker keeps fixtures in sync with the official calendar. A change it missed emits no event, so while a round's schedule is in doubt its submissions are suspended until it is reconciled.
- The steward must act before each affected round's
finalizableAt, after which anyone can settle it. Within the correction window it cancorrectResult(e.g. toVoid); otherwise it pauses Predictions and cancels the round, which does not require knowing the real outcome. - Cancelling discards the round: its games cannot join another round, so a replacement needs new game ids and fresh entries, and the round still counts towards the league's 64.
scripts/lib/m2.ts is the reference implementation of every encoding: ids, team codes, picks,
picksHash, EIP-712 types, sourceHash, scoring, standings leaves and ranking.
test/vectors/m2-vectors.json pins the values. Both the Solidity tests (Vectors.t.sol) and
the TypeScript tests check them, so the app can rely on them.
gameId = keccak256(bytes("mvl-2026-27-000123")),leagueId = keccak256(bytes(slug))roundId = keccak256(abi.encode(leagueId, uint16 matchday)), never reused- team code = bytes32 short string (
bytes32("UB_BRONCOS")) - EIP-712 domain
("Predictions", "1", chainId, predictions), typeSubmission(address user,bytes32 roundId,bytes32 picksHash,uint64 confirmedAt) - standings leaf (OZ
StandardMerkleTree,IPredictions.StandingLeaf):(bytes32 leagueId, address account, uint32 rank, uint32 points, uint32 correct, uint32 counted, uint16 roundsPlayed, uint64 sumLeadTime)
Requires Node ≥ 22.10 and pnpm 10.
pnpm install
pnpm build # compile
pnpm test # Solidity (unit, fuzz, invariants, vectors) + TypeScript (season flow, gas)
pnpm test:sol # Solidity tests only
pnpm test:ts # TypeScript tests only
pnpm coverage
pnpm typecheck
pnpm vectors # regenerate test/vectors/m2-vectors.json (CI checks it is up to date)
pnpm export-abi # ABIs, addresses, encoding lib and vectors -> ../injective-m2-frontend/packages/chain/src/generated
# (override the target with M2_EXPORT_DIR)| Test file | What it covers |
|---|---|
test/ResultsRegistry.t.sol |
fixtures, rescheduling, results and formats, voids, corrections, pause, admin |
test/Predictions.t.sol |
rounds, submissions, lock moves, settlement, postponements, standings, fuzzing |
test/Guards.t.sol |
constructor, state-machine and pause guards |
test/PredictionsInvariant.t.sol |
invariants over random submissions, moves, results, corrections and settlement |
test/Vectors.t.sol |
Solidity encodings against test/vectors/m2-vectors.json |
test/SeasonFlow.ts |
a full season with viem (custodial keys, EIP-712, corrections, standings), gas |
CI (.github/workflows/ci.yml) runs the build, the typecheck, the tests with coverage, the vectors
check and Slither (slither contracts --config-file slither.config.json, fails on medium or higher findings).
pnpm node # Hardhat node, chainId 31337
pnpm deploy:localhost # ignition/parameters/localhost.json (Hardhat default accounts)
pnpm check-deploy:localhost| Network | ResultsRegistry |
Predictions |
|---|---|---|
Injective EVM testnet (1439) |
0x5E482d982f71158cBe1C811cD6086865bDF5D678 |
0xe6189297DF3B228248491c6D7AaDA78f90e09c51 |
Testnet parameters: ignition/parameters/testnet.json (1 h correction window and finalization
delay, no admin delay). Not deployed on mainnet yet.
Contracts are compiled with solc 0.8.28 (cancun, optimizer 200 runs); the production build
profile used by the deploy scripts is identical to the one the tests run against.
cp .env.example .env # RPC + testnet deployer key
# admin + role holders in ignition/parameters/testnet.json (template: testnet.example.json)
pnpm deploy:testnet
pnpm check-deploy:testnet # admin, roles, parameters
pnpm hardhat ignition verify chain-1439 # Blockscout
pnpm export-abiUse the sentry RPC endpoints. The testnet k8s.* endpoint returns null receipts, and Ignition
then reports deploys as dropped.
The admin and every role holder are constructor arguments, so the account that deploys gets no role and only pays the gas. Anyone can deploy: us, or an account Injective authorized.
- Ask Injective whether deployment is permissioned (EVM
authorized_deployers): either our deployer gets authorized, or an authorized account deploys for us. - Fill
ignition/parameters/mainnet.jsonfrommainnet.example.json: admin = the admin Safe (2-of-3),adminDelay= 2 days, the worker keys, steward = the steward Safe (2-of-3). - Deploy from a tagged commit:
- we deploy:
pnpm hardhat keystore set INJECTIVE_MAINNET_DEPLOYER_KEY, thenpnpm deploy:mainnet; - a third party deploys: they run
pnpm deploy:mainnetfrom the same commit with ourmainnet.jsonand their key, and send us the addresses, their deployer address and the deployment block.
- we deploy:
- Check it:
pnpm check-deploy:mainnet(for a third-party deploy also setM2_REGISTRY,M2_PREDICTIONS,M2_DEPLOYER,M2_FROM_BLOCK). It fails if the admin, its delay, any role holder or parameter differs, if an admin transfer is pending, if a contract is paused, or if any other address (the deployer included) holds a role. - Verify the sources on Blockscout (
pnpm hardhat ignition verify chain-1776, or by address if the journal is elsewhere): verification does not require being the deployer. - Fund the worker keys with INJ, run
pnpm export-abi, commitignition/deployments/chain-1776when we hold the journal.
Players' custodial wallets never need INJ (our relayers pay every transaction). If one ever does, send it manually from our treasury wallet; there is no faucet.