Skip to content

feat: add binary prediction example#102

Open
dhruvja wants to merge 3 commits into
mainfrom
dhruvja/feat/binary-prediction-example
Open

feat: add binary prediction example#102
dhruvja wants to merge 3 commits into
mainfrom
dhruvja/feat/binary-prediction-example

Conversation

@dhruvja

@dhruvja dhruvja commented Jul 1, 2026

Copy link
Copy Markdown

Closes: #101

Summary

  • Add a binary up/down prediction Anchor example using MagicBlock Ephemeral Rollups, session keys, and an ER price oracle.
  • Keep pool config on the base layer while delegating token custody through EATA accounts for ER-side betting and settlement.
  • Add deterministic local smoke coverage, example docs, root README listing, and local runner support for Anchor.toml genesis fixtures.

Testing

  • NO_DNA=1 cargo fmt
  • NO_DNA=1 yarn build
  • NO_DNA=1 yarn lint
  • NO_DNA=1 npx tsc --noEmit
  • NO_DNA=1 ./scripts/test-locally.sh binary-prediction

Breaking Changes

None.

Summary by CodeRabbit

  • New Features

    • Added a new Binary Prediction example to the app’s examples grid.
    • Introduced a full end-to-end binary prediction flow, including pool setup, betting, settlement, and fund withdrawal.
    • Added local test support and configuration for running the example consistently.
  • Documentation

    • Added a detailed guide for setting up and using the Binary Prediction example.
  • Tests

    • Added integration coverage for the complete example lifecycle.

@vercel

vercel Bot commented Jul 1, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
counter-session-keys Ready Ready Preview, Comment Jul 1, 2026 3:25pm
er-rolldice Ready Ready Preview, Comment Jul 1, 2026 3:25pm
magicblock-counter-example Ready Ready Preview, Comment Jul 1, 2026 3:25pm
magicblock-engine-examples Ready Ready Preview, Comment Jul 1, 2026 3:25pm
magicblock-rewards-dashboard Ready Ready Preview, Comment Jul 1, 2026 3:25pm
rps-example Ready Ready Preview, Comment Jul 1, 2026 3:25pm
spl-tokens Ready Ready Preview, Comment Jul 1, 2026 3:25pm

Request Review

@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@dhruvja, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 34 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: cf580165-703d-410c-b409-3bc4fea6a9ef

📥 Commits

Reviewing files that changed from the base of the PR and between 9da0ac6 and 50a6c5e.

⛔ Files ignored due to path filters (1)
  • binary-prediction/yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (7)
  • binary-prediction/README.md
  • binary-prediction/package.json
  • binary-prediction/programs/binary-prediction/src/error.rs
  • binary-prediction/programs/binary-prediction/src/lib.rs
  • binary-prediction/programs/binary-prediction/src/state.rs
  • binary-prediction/programs/binary-prediction/src/utils.rs
  • binary-prediction/tests/binary-prediction.ts

Walkthrough

This PR adds a new "Binary Prediction" example dApp: an Anchor Solana program implementing an up/down price prediction market using an in-program LP pool, ephemeral-rollups delegation, oracle prices, and session keys. It includes program source, tests, docs, config, and CI script updates.

Changes

Binary Prediction Example

Layer / File(s) Summary
Program state and errors
binary-prediction/programs/binary-prediction/src/state.rs, binary-prediction/programs/binary-prediction/src/error.rs
Defines `Pool`, `Bet` account structs with `LEN` constants, the `Direction` enum, and the `ErrorCode` error enum.
Utility helpers
binary-prediction/programs/binary-prediction/src/utils.rs
Adds price reading, payout/outcome calculation, and CPI helpers for token transfer, ATA/vault init, delegation, and PDA derivation.
Program instructions and contexts
binary-prediction/programs/binary-prediction/src/lib.rs
Implements `initialize`, `initialize_bet`, `delegate_bet`, `undelegate_bet`, `place_bet`, `settle` instructions with their Anchor account contexts.
Crate and workspace config
binary-prediction/programs/binary-prediction/Cargo.toml, binary-prediction/Cargo.toml
Adds crate dependencies/features and workspace/release build profile settings.
Anchor and package config
binary-prediction/Anchor.toml, binary-prediction/package.json, binary-prediction/tsconfig.json
Configures Anchor program addresses, registry/provider, genesis fixtures, test script, and TypeScript tooling.
Integration tests
binary-prediction/tests/binary-prediction.ts
Adds end-to-end test covering oracle setup, pool init, delegation, session keys, bet placement/settlement, and withdrawal.
Documentation
binary-prediction/README.md, README.md
Adds example README documenting flow/lifecycle/instructions, and links a new example card in the root README.
CI and local test scripts
scripts/projects.sh, scripts/test-locally.sh
Registers the project as regular, adds genesis fixture preloading, storage cleanup, and explicit success exit code.

Estimated code review effort: 4 (Complex) | ~60 minutes

Suggested reviewers: GabrielePicco, jonasXchen, taco-paco

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding a binary prediction example.
Linked Issues check ✅ Passed The PR adds the binary up/down prediction example with ER price oracle, session keys, LP pool, and settlement flow, matching #101.
Out of Scope Changes check ✅ Passed All added docs, scripts, configs, program code, and tests support the binary prediction example, with no obvious unrelated changes.
Docstring Coverage ✅ Passed Docstring coverage is 84.21% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dhruvja/feat/binary-prediction-example

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@binary-prediction/package.json`:
- Around line 17-23: The test setup in binary-prediction currently mixes a
CommonJS TypeScript runtime with chai@5, which is ESM-only and will break when
binary-prediction/tests/binary-prediction.ts imports it. Fix this by either
pinning chai to the last CommonJS-compatible major in package.json or updating
the binary-prediction test harness/tsconfig to run under ESM so the existing
chai import works; use the chai dependency and the ts-mocha-based test path to
locate the change.

In `@binary-prediction/programs/binary-prediction/src/lib.rs`:
- Around line 315-322: The settle flow currently uses pool_signed_transfer with
authority = pool, but initialize only creates pool_token_account and never sets
pool.key() as that ATA’s SPL delegate or verifies it in the account constraints.
Update the setup in initialize and the relevant account validation around
pool_token_account/pool_authority so the pool ATA is actually delegated to the
Pool PDA before settlement, or fail early if the delegate is missing. Make the
same fix wherever the transfer paths rely on pool_signed_transfer so winners can
be paid from a newly created pool ATA.
- Around line 216-223: The liquidity check in the bet entry flow is using only
ctx.accounts.pool_token_account.amount, which can overbook the pool and also
rejects valid bets by ignoring the incoming stake. Update the Pool state to
track reserved exposure for open bets, then change the liquidity gate in the
bet-opening logic to compare required payout plus new reservation against free
liquidity instead of raw token balance. Make sure the check uses the existing
bet creation path around checked_payout and ErrorCode::InsufficientLiquidity so
settle remains fully covered.
- Around line 146-152: The initialize_bet handler currently overwrites the bet
PDA state unconditionally, which lets a caller re-run it and wipe an existing
active bet. Update initialize_bet in lib.rs to first verify the bet account is
truly uninitialized before setting fields, or remove init_if_needed in the
InitializeBet account flow so an existing PDA cannot be reset; preserve the
existing account data for any already-open bet and only perform initialization
once.
- Around line 67-73: The CPI setup in the transfer flow is using the wrong token
program handle, so update the `CpiContext::new` call in the function that builds
`SplTransfer` to pass `ctx.accounts.token_program.to_account_info()` instead of
`ctx.accounts.token_program.key()`. Keep the existing `token::transfer` call and
account wiring unchanged, but make sure the `token_program` account is provided
as `AccountInfo` so the CPI compiles correctly.

In `@binary-prediction/programs/binary-prediction/src/utils.rs`:
- Around line 19-27: The feed ID used in read_price is incorrect because
price_update_account.key().to_bytes() returns the account address instead of the
Pyth feed ID stored in PriceUpdateV2. Update read_price to accept or derive the
configured feed ID from the caller, and pass that value into
get_price_no_older_than using the existing PriceUpdateV2/Clock flow so real
receiver accounts can read prices correctly.

In `@binary-prediction/tests/binary-prediction.ts`:
- Around line 230-243: The oracle update helper in updateData is using
Date.now() for a field named timestampNs, which is milliseconds instead of
nanoseconds. Update the temporalNumericValue.timestampNs assignment to use a
nanosecond-resolution timestamp so the feed looks fresh to read_price. Keep the
fix localized to updateData in binary-prediction/tests/binary-prediction.ts and
preserve the rest of the update payload unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: b7fbbe0f-ea0d-4f0e-b154-213d9c567325

📥 Commits

Reviewing files that changed from the base of the PR and between 988fe00 and 9da0ac6.

⛔ Files ignored due to path filters (5)
  • binary-prediction/Cargo.lock is excluded by !**/*.lock
  • binary-prediction/tests/fixtures/dlp.so is excluded by !**/*.so
  • binary-prediction/tests/fixtures/ephemeral_oracle.so is excluded by !**/*.so
  • binary-prediction/tests/fixtures/ephemeral_token_program.so is excluded by !**/*.so
  • binary-prediction/yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (14)
  • README.md
  • binary-prediction/Anchor.toml
  • binary-prediction/Cargo.toml
  • binary-prediction/README.md
  • binary-prediction/package.json
  • binary-prediction/programs/binary-prediction/Cargo.toml
  • binary-prediction/programs/binary-prediction/src/error.rs
  • binary-prediction/programs/binary-prediction/src/lib.rs
  • binary-prediction/programs/binary-prediction/src/state.rs
  • binary-prediction/programs/binary-prediction/src/utils.rs
  • binary-prediction/tests/binary-prediction.ts
  • binary-prediction/tsconfig.json
  • scripts/projects.sh
  • scripts/test-locally.sh

Comment thread binary-prediction/package.json
Comment on lines +67 to +73
let cpi_accounts = SplTransfer {
from: ctx.accounts.admin_token_account.to_account_info(),
to: ctx.accounts.pool_token_account.to_account_info(),
authority: ctx.accounts.admin.to_account_info(),
};
let cpi_ctx = CpiContext::new(ctx.accounts.token_program.key(), cpi_accounts);
token::transfer(cpi_ctx, seed_amount)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

ast-grep outline binary-prediction/programs/binary-prediction/src/lib.rs --view expanded

Repository: magicblock-labs/magicblock-engine-examples

Length of output: 3528


🏁 Script executed:

sed -n '60,85p; 1,40p' binary-prediction/programs/binary-prediction/src/lib.rs

Repository: magicblock-labs/magicblock-engine-examples

Length of output: 2791


🏁 Script executed:

sed -n '60,85p;1,40p' binary-prediction/programs/binary-prediction/src/lib.rs

Repository: magicblock-labs/magicblock-engine-examples

Length of output: 2791


Pass ctx.accounts.token_program.to_account_info() to CpiContext::new. ctx.accounts.token_program.key() is a Pubkey, so this CPI setup will not compile as written.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@binary-prediction/programs/binary-prediction/src/lib.rs` around lines 67 -
73, The CPI setup in the transfer flow is using the wrong token program handle,
so update the `CpiContext::new` call in the function that builds `SplTransfer`
to pass `ctx.accounts.token_program.to_account_info()` instead of
`ctx.accounts.token_program.key()`. Keep the existing `token::transfer` call and
account wiring unchanged, but make sure the `token_program` account is provided
as `AccountInfo` so the CPI compiles correctly.

Comment thread binary-prediction/programs/binary-prediction/src/lib.rs
Comment on lines +216 to +223
let open_price = read_price(&ctx.accounts.price_update)?;
let now = Clock::get()?.unix_timestamp;
let required_payout = checked_payout(stake, ctx.accounts.pool.payout_bps)?;
let pool_balance = ctx.accounts.pool_token_account.amount;
require!(
pool_balance >= required_payout,
ErrorCode::InsufficientLiquidity
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

This liquidity gate can still overbook the pool.

pool_token_account.amount is only the raw balance. It does not subtract liabilities from already-open bets, so the same LP capital can be reused to admit many concurrent bets and leave settle short later. It also ignores the incoming stake, which means some solvent bets are rejected today. Track reserved exposure in Pool and compare against free liquidity, not the current token balance.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@binary-prediction/programs/binary-prediction/src/lib.rs` around lines 216 -
223, The liquidity check in the bet entry flow is using only
ctx.accounts.pool_token_account.amount, which can overbook the pool and also
rejects valid bets by ignoring the incoming stake. Update the Pool state to
track reserved exposure for open bets, then change the liquidity gate in the
bet-opening logic to compare required payout plus new reservation against free
liquidity instead of raw token balance. Make sure the check uses the existing
bet creation path around checked_payout and ErrorCode::InsufficientLiquidity so
settle remains fully covered.

Comment thread binary-prediction/programs/binary-prediction/src/lib.rs
Comment thread binary-prediction/programs/binary-prediction/src/utils.rs Outdated
Comment thread binary-prediction/tests/binary-prediction.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: binary up/down prediction example

1 participant