Repository navigation
Labels
Stellar WaveStellar Wave Program issuebetspriority:highHigh implementation difficultysecuritySecurity hardeningsmart-contractSoroban smart-contract work
Description
Summary
The slim build exports two ways to put tokens into a market: vote and place_bet. They write overlapping state (market.votes, market.stakes and market.total_staked) but check different things.
- A user who votes and then bets has their first stake silently overwritten. Those tokens stay in the contract, but no claim path can ever reach them.
votealso skips the circuit breaker, stake validation, min/max bet limits, the max-participants accounting used by bets, and the per-ledger cap.
This issue makes the two paths share one validated stake ledger.
Background and evidence
vote(lib.rsaround line 1051, exported in the slim build):- There is no
CircuitBreaker::require_write_allowedcall.place_betcalls it with"betting", sovotekeeps accepting funds while betting is paused. - There is no validation that
stake > 0, and noBetValidator::validate_bet_parameters(min/max limits, per-market cap). A zero stake registers a vote and takes amax_participantsslot. market.total_staked += stakeis unchecked. Withoverflow-checks = truein the release profile this traps instead of returning a typed error.- It rejects a repeat only via
market.votes(AlreadyVoted), and never looks atBetStorage.
- There is no
BetManager::place_bet_inner(bets.rsaround line 387):- It rejects a repeat only via
BetStorage(AlreadyBet), and never looks atmarket.votes. - It then writes
market.votes.set(user, outcome)andmarket.stakes.set(user, amount), overwriting any earlier vote and stake, whiletotal_stakedadds both.
- It rejects a repeat only via
- Where the first stake goes.
claim_winnings(lib.rsaround line 1857) anddistribute_payoutspay frommarket.stakes[user], so only the second amount is ever paid out.BetManager::cancel_bet(bets.rsaround line 997) refundsbet.amountand then removesmarket.votes[user]andmarket.stakes[user]. The first stake is then permanently orphaned, andtotal_stakedstays inflated by it.
- Totals disagree.
get_market_bet_stats(outcome_totals) counts only bets, whileResolutionOutcomeCacheuses market totals. The odds that integrators show therefore ignore vote stakes.
Scope
In scope
- One stake-entry function.
- Extract an internal
fn record_stake(env, user, market_id, outcome, amount, source: StakeSource) -> Result<(), Error>. - It runs, in order: circuit breaker, market active and before the deadline, outcome valid, amount bounds via
BetValidator, the max-participants check, a duplicate check across bothmarket.votesandBetStorage, a checkedtotal_staked,lock_funds, then the state writes. - Both
voteandplace_betcall it.user.require_auth()stays once, in the entrypoint or inplace_bet_inner, never in both.
- Extract an internal
- No overwrites. A user who has voted cannot then
place_beton the same market, and vice versa. Return a single error (AlreadyBet, or a newAlreadyStaked) and document it inerr.rs. - Typed errors in
vote. Use checked arithmetic and return typed errors instead of panicking on overflow. - Consistent cancellation.
cancel_betmust only remove ledger entries that it created. - Docs. Document the unified rules in
docs/bet/(or thebets.rsmodule docs) and list the new error code.
Out of scope
- Removing
votefrom the API (a separate deprecation decision). - Changing payout math.
Design constraints
- Error precedence, the same for both entrypoints: circuit breaker, then market state, then deadline, then outcome, then amount, then duplicate, then capacity, then overflow. Pin this with a test.
- Atomicity. No state is written before
lock_fundssucceeds, matching the existing "atomic commit" contract inplace_bet_inner. - Storage compatibility. No layout change to
Market. Existing markets where both entries already exist must still be claimable: document how they are handled, and test it. - WASM size. The slim build stays at or below 131,072 bytes. Report the delta.
Test plan
Add contracts/predictify-hybrid/src/stake_ledger_unification_tests.rs.
| # | Scenario | Expected |
|---|---|---|
| 1 | vote(S1) then place_bet(S2) by the same user |
the second call fails; stakes[user] == S1; total_staked == S1 |
| 2 | place_bet(S2) then vote(S1) |
the second call fails with the same error |
| 3 | vote while the circuit breaker has betting paused |
CBOpen (or the same error place_bet returns); no token transfer |
| 4 | vote with stake 0 / negative / below min / above the market cap |
the matching typed error, identical to place_bet |
| 5 | vote filling max_participants, then place_bet from a new user |
MaxParticipantsReached |
| 6 | total_staked near i128::MAX |
typed overflow error, no trap |
| 7 | cancel_bet after a bet-only stake |
refund equals the amount; votes and stakes are cleared; total_staked restored |
| 8 | Conservation | after mixed vote and bet activity, resolution and claims: the contract token balance minus fees equals the outstanding claimable amount |
| 9 | Error precedence | inputs violating several rules return the first error in the documented order, for both entrypoints |
Regression proof. Row 1 succeeds on current master, which overwrites the stake and strands S1. Show the stranded amount in the PR.
Acceptance criteria
-
voteandplace_betboth delegate to one validated stake function. -
grep -n 'total_staked +=' contracts/predictify-hybrid/src/lib.rsreturns nothing. -
cargo test -p predictify-hybrid --libpasses, and every row above is covered. -
cargo check -p predictify-hybrid --no-default-featurespasses; slim WASM size reported. - Coverage is at least 95% on
vote,place_bet_innerand the new function.
Getting started
git checkout -b fix/unify-stake-ledger
cargo test -p predictify-hybrid --lib bets
cargo test -p predictify-hybrid --lib votingRead these first:
lib.rs:vote,claim_winningsbets.rs:place_bet_inner,cancel_bet,BetValidator::validate_bet_parameters,BetUtils::lock_funds
PR requirements
- Link this issue (
Closes #<n>). - Explain how markets that already have both a vote and a bet for a user are handled.
- Paste the test output, the coverage and the WASM size delta.
- Example commit:
fix(bets): route vote and place_bet through one validated stake ledger.
Priority rationale
High. Users can lose funds through normal use of two exported entrypoints, and vote bypasses the emergency pause. A correct fix touches the betting, voting, cancellation and claim paths, and needs careful thought about atomicity and backwards compatibility.
Metadata
Metadata
Assignees
Labels
Stellar WaveStellar Wave Program issuebetspriority:highHigh implementation difficultysecuritySecurity hardeningsmart-contractSoroban smart-contract work
Activity
AnnieIj commentedon Oct 18, 2026
@AnnieIj has applied to work on this issue as part of the Stellar Wave Program's 10th wave.
ℹ️ Repo Maintainers: To accept this application, review their application or assign @AnnieIj to this issue.
drips-wave commentedon Oct 19, 2026
Congratulations, @AnnieIj! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on October 22, 2026.
🧑💻 @AnnieIj: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.
Warning
When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.
🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.
🌊 Happy Wave 🌊
drips-wave commentedon Oct 24, 2026
This issue has been completed by @AnnieIj as part of the Stellar Wave Program's 10th Wave 🥳
😎 @AnnieIj: You earned 100 Points for completing this issue! Once the Wave's rewards are finalized, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.
🧑💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.