Skip to content

fix: make bet placement atomic across fee and state writes - #1431

Merged
greatest0fallt1me merged 1 commit into
Predictify-org:masterfrom
Hakimziyech18:fix/1395-atomic-bet-placement
Aug 30, 2026
Merged

fix: make bet placement atomic across fee and state writes#1431
greatest0fallt1me merged 1 commit into
Predictify-org:masterfrom
Hakimziyech18:fix/1395-atomic-bet-placement

Conversation

@Hakimziyech18

@Hakimziyech18 Hakimziyech18 commented Aug 30, 2026

Copy link
Copy Markdown

Summary

Makes single bet placement atomic across fee validation, fund locking, and bet-related state writes.

The placement flow now completes all predictable validation and fallible arithmetic before transferring user funds. Once funds are locked, the contract only commits state values that were already prepared successfully.

Changes

  • Resolve and validate the applicable fee before fund transfer.
  • Remove duplicate fee validation from the single-bet placement path.
  • Precompute the user's updated cumulative stake before locking funds.
  • Precompute market bet statistics before locking funds.
  • Use checked arithmetic for market total stake updates.
  • Commit the prepared bet, user stake, statistics, and market state only after the fund transfer succeeds.
  • Keep event emission after successful state commit.
  • Preserve the existing place_bet public interface and existing duplicate-bet protection.

Atomicity / failure handling

The single-bet flow is now ordered as:

  1. authorization and existing validation
  2. fee/slippage validation
  3. duplicate-bet validation
  4. precompute all stake/statistics/market state changes
  5. lock funds
  6. commit prepared state
  7. emit the bet placement event

Arithmetic overflow and other predictable validation failures therefore occur before funds are moved.

Soroban transaction semantics continue to provide transaction-level rollback for host-level failures during the commit phase.

Retries after a successful placement remain deterministic because the existing one-bet-per-user-per-market invariant returns AlreadyBet instead of applying the bet again.

Compatibility

No public contract interface or persisted data type was changed.

Existing callers of place_bet remain compatible.

No dependency changes are required.

Tests

Added focused regression coverage for:

  • preparing market statistics without mutating persisted state
  • market statistics overflow rejection before commit
  • preparing cumulative user stake without mutating persisted state
  • cumulative user stake overflow rejection before commit

Closes #1395

Validation commands:

cargo test --manifest-path contracts/predictify-hybrid/Cargo.toml atomic_
cargo test --manifest-path contracts/predictify-hybrid/Cargo.toml bets::tests

@drips-wave

drips-wave Bot commented Aug 30, 2026

Copy link
Copy Markdown

@Hakimziyech18 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me
greatest0fallt1me merged commit 30062eb into Predictify-org:master Aug 30, 2026
2 of 3 checks passed
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.

[Quality-2][Medium] Make bet placement atomic across fee and state writes

2 participants