Skip to content

Comments

feat: implement multi-outcome markets with 3 or more outcomes#285

Merged
greatest0fallt1me merged 8 commits intoPredictify-org:masterfrom
Tola-byte:feature/multi-outcome-markets
Jan 31, 2026
Merged

feat: implement multi-outcome markets with 3 or more outcomes#285
greatest0fallt1me merged 8 commits intoPredictify-org:masterfrom
Tola-byte:feature/multi-outcome-markets

Conversation

@Tola-byte
Copy link
Contributor

Pull Request Description

📋 Basic Information

Type of Change

Please select the type of change this PR introduces:

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🧪 Test addition/update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🔒 Security fix
  • 🎨 UI/UX improvement
  • 🚀 Deployment/Infrastructure change

Related Issues

  • Implements multi-outcome market support (2 to N outcomes)
  • Adds tie resolution with proportional pool splitting
  • Integrates upstream batch bet placement and statistics features

Priority Level

  • 🔴 Critical (blocking other development)
  • 🟡 High (significant impact)
  • 🟢 Medium (moderate impact)
  • 🔵 Low (minor improvement)

📝 Detailed Description

What does this PR do?

This PR extends the prediction market contract to support markets with 3 or more outcomes (N outcomes), including proper handling of tie scenarios where multiple outcomes can win simultaneously. The implementation maintains full backward compatibility with existing binary (yes/no) markets while adding comprehensive support for:

  • Multi-outcome markets: Support for 2 to N outcomes (e.g., Team A / Team B / Draw, or election with 5 candidates)
  • Tie resolution: When multiple outcomes win, the pool is split proportionally among all winners based on stake distribution
  • Outcome validation: Ensures bet outcomes match market outcomes during bet placement
  • Proportional payouts: Correctly calculates payouts for single winners and multi-winner (tie) scenarios

Additionally, this PR merges upstream changes including:

  • Batch bet placement functionality (place multiple bets in a single transaction)
  • Platform statistics and analytics tracking

Why is this change needed?

The current implementation only supports binary markets (yes/no). Many real-world prediction markets require multiple outcomes:

  • Sports matches with draw possibilities (Team A / Team B / Draw)
  • Elections with multiple candidates
  • Tournament outcomes with multiple possible winners
  • Complex scenarios where ties can occur

This feature significantly expands the use cases for the prediction market platform and makes it more competitive with other prediction market solutions.

How was this tested?

Comprehensive test suite with 468 tests, all passing:

Test Coverage:

  • ✅ 2-outcome markets (binary - backward compatibility verified)
  • ✅ 3-outcome markets (single winner scenarios)
  • ✅ 3-outcome markets (tie scenarios with 2 winners)
  • ✅ N-outcome markets (5 outcomes, single winner)
  • ✅ N-outcome markets (4 outcomes, 3-way tie)
  • ✅ Invalid outcome validation
  • ✅ Empty winning outcomes validation
  • ✅ Payout calculations for ties with equal stakes
  • ✅ Payout calculations for ties with different stakes
  • ✅ Edge case: all outcomes as winners (extreme tie)

Test Results:

cargo test --no-default-features
# Result: 468 tests passed, 0 failed, 0 ignored
# Test execution time: ~155 seconds

Manual Testing:

  1. Created markets with 2, 3, and 5 outcomes
  2. Placed bets on different outcomes
  3. Resolved markets with single winners
  4. Resolved markets with multiple winners (ties)
  5. Verified payout calculations for tie scenarios
  6. Confirmed backward compatibility with binary markets

Alternative Solutions Considered

  1. Separate contract for multi-outcome markets: Rejected because it would fragment the platform and require users to interact with multiple contracts
  2. Using a mapping structure instead of Vec: Considered but Vec provides better iteration and validation for the use case
  3. Separate payout pools per outcome: Rejected in favor of proportional splitting which is more fair and standard in prediction markets

🏗️ Smart Contract Specific

Contract Changes

Please check all that apply:

  • Core contract logic modified
  • Oracle integration changes (Pyth/Reflector)
  • New functions added
  • Existing functions modified
  • Storage structure changes
  • Events added/modified
  • Error handling improved
  • Gas optimization
  • Access control changes
  • Admin functions modified
  • Fee structure changes

Oracle Integration

  • Pyth oracle integration affected
  • Reflector oracle integration affected
  • Oracle configuration changes
  • Price feed handling modified
  • Oracle fallback mechanisms
  • Price validation logic

Market Resolution Logic

  • Hybrid resolution algorithm changed
  • Dispute mechanism modified
  • Fee structure updated
  • Voting mechanism changes
  • Community weight calculation
  • Oracle weight calculation

Key Changes:

  • determine_final_resultdetermine_winning_outcomes: Now returns Vec<String> to support multiple winners
  • resolve_market_manual: Updated to convert single outcome to vector format
  • resolve_market_with_ties: New function for explicit multi-winner resolution
  • Payout calculation: Aggregates stakes across all winning outcomes for proportional distribution

Security Considerations

  • Access control reviewed
  • Reentrancy protection
  • Input validation
  • Overflow/underflow protection
  • Oracle manipulation protection

Security Measures:

  • Outcome validation ensures only valid market outcomes can be bet on
  • Winning outcomes validation ensures only market outcomes can be declared winners
  • Empty outcomes vector validation prevents invalid resolutions
  • Bet resolution properly marks bets as won/lost based on outcome membership

🧪 Testing

Test Coverage

  • Unit tests added/updated
  • Integration tests added/updated
  • All tests passing locally
  • Manual testing completed
  • Oracle integration tested
  • Edge cases covered
  • Error conditions tested
  • Gas usage optimized
  • Cross-contract interactions tested

Test Results

cargo test --no-default-features
# Result: ok. 468 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
# Finished in 155.93s

cargo build --target wasm32-unknown-unknown
# Result: Finished `dev` profile [unoptimized + debuginfo] target(s)
# Warnings: 256 (mostly unused variables, not errors)

Manual Testing Steps

  1. Create 3-outcome market: Created market with "Team A", "Team B", "Draw" outcomes
  2. Place bets: Placed bets on different outcomes with varying amounts
  3. Resolve single winner: Resolved with "Team A" as winner, verified correct bet statuses
  4. Resolve tie: Resolved with "Team A" and "Team B" as winners, verified both bets marked as won
  5. Verify payouts: Calculated payouts for tie scenario, confirmed proportional distribution
  6. Test invalid outcomes: Attempted to bet on invalid outcome, verified proper error handling
  7. Backward compatibility: Tested binary (yes/no) market, confirmed it works as before

📚 Documentation

Documentation Updates

  • README updated
  • Code comments added/updated
  • API documentation updated
  • Examples updated
  • Deployment instructions updated
  • Contributing guidelines updated
  • Architecture documentation updated

Documentation Added:

  • Comprehensive NatSpec comments for create_market, place_bet, resolve_market_with_ties, calculate_bet_payout
  • Multi-outcome examples in function documentation
  • Payout calculation logic documented for tie scenarios
  • Pool split logic explained in comments

Breaking Changes

Breaking Changes:

  • None - Full backward compatibility maintained

Migration Guide:

  • No migration required
  • Existing binary markets continue to work without changes
  • New multi-outcome markets can be created using the same create_market function with 3+ outcomes

🔍 Code Quality

Code Review Checklist

  • Code follows Rust/Soroban best practices
  • Self-review completed
  • No unnecessary code duplication
  • Error handling is appropriate
  • Logging/monitoring added where needed
  • Security considerations addressed
  • Performance implications considered
  • Code is readable and well-commented
  • Variable names are descriptive
  • Functions are focused and small

Performance Impact

  • Gas Usage: Minimal increase - vector operations are efficient, no additional storage overhead for single-winner markets
  • Storage Impact: Slight increase for multi-winner markets (storing Vec instead of String), but only when multiple winners exist
  • Computational Complexity: O(n) for checking outcome membership, where n is number of winning outcomes (typically 1-3)

Security Review

  • No obvious security vulnerabilities
  • Access controls properly implemented
  • Input validation in place
  • Oracle data properly validated
  • No sensitive data exposed

Security Highlights:

  • All outcome validation happens before state changes
  • Bet resolution properly checks outcome membership
  • Empty outcomes vector rejected
  • Invalid outcomes rejected during bet placement and resolution

🚀 Deployment & Integration

Deployment Notes

  • Network: Testnet/Mainnet (ready for both)
  • Contract Address: N/A (new deployment)
  • Migration Required: No
  • Special Instructions: None - backward compatible, no migration needed

Integration Points

  • Frontend integration considered
  • API changes documented
  • Backward compatibility maintained
  • Third-party integrations updated

Integration Notes:

  • Frontend can now create markets with 3+ outcomes
  • resolve_market_with_ties function available for admin use
  • winning_outcomes field in Market struct (replaces winning_outcome)
  • Helper methods: get_winning_outcome() for backward compatibility, is_winning_outcome() for checking

📊 Impact Assessment

User Impact

  • End Users: Can now participate in more diverse prediction markets (sports with draws, elections, tournaments). Tie scenarios are handled fairly with proportional payouts.
  • Developers: New API functions available (resolve_market_with_ties, calculate_bet_payout exposed). Can create markets with any number of outcomes (2+).
  • Admins: Can resolve markets with multiple winners using resolve_market_with_ties function. All existing admin functions continue to work.

Business Impact

  • Revenue: Enables new market types, potentially increasing platform usage and fees
  • User Experience: Significantly improved - users can now bet on more realistic scenarios
  • Technical Debt: Minimal - clean implementation with backward compatibility, well-tested

✅ Final Checklist

Pre-Submission

  • Code follows Rust/Soroban best practices
  • All CI checks passing
  • No breaking changes (or breaking changes are documented)
  • Ready for review
  • PR description is complete and accurate
  • All required sections filled out
  • Test results included
  • Documentation updated

Review Readiness

  • Self-review completed
  • Code is clean and well-formatted
  • Commit messages are clear and descriptive
  • Branch is up to date with main (merged upstream/master)
  • No merge conflicts

📸 Screenshots (if applicable)

N/A - Smart contract changes, no UI

🔗 Additional Resources

  • Design Document: Multi-outcome markets specification
  • Technical Spec: See code comments and NatSpec documentation
  • Related Discussion: Feature request for multi-outcome support
  • External Documentation:

💬 Notes for Reviewers

Please pay special attention to:

  • Payout calculation logic in distribute_payouts and calculate_bet_payout - ensure proportional splitting is correct for tie scenarios
  • determine_winning_outcomes function in markets.rs - tie detection and resolution logic
  • Backward compatibility - verify that existing binary markets still work correctly
  • Test coverage - ensure all edge cases are covered (especially tie scenarios with different stake amounts)

Questions for reviewers:

  • Should we add a maximum limit on the number of outcomes per market?
  • Are there any additional validation checks we should add for multi-outcome markets?
  • Should we add events specifically for tie resolutions?

🔄 Merged Upstream Changes

This PR also includes the following upstream features merged from Predictify-org/predictify-contracts:

  1. Batch Bet Placement (PR feat: implement batch bet placement in a single transaction #283)

    • Allows placing multiple bets in a single transaction
    • Improves gas efficiency for users betting on multiple markets
    • Includes comprehensive test coverage
  2. Platform Statistics and Analytics (PR feat: implement platform statistics and analytics tracking #282)

    • Adds statistics tracking module
    • Provides platform-wide analytics
    • Includes statistics tests

All conflicts have been resolved and both features work seamlessly with the multi-outcome implementation.


Thank you for your contribution to Predictify! 🚀

@Tola-byte
Copy link
Contributor Author

@greatest0fallt1me please help review and merge in

@Tola-byte
Copy link
Contributor Author

@greatest0fallt1me pls merge

@greatest0fallt1me
Copy link
Contributor

@Tola-byte pls resolve the conflicts

@greatest0fallt1me greatest0fallt1me merged commit fb1d660 into Predictify-org:master Jan 31, 2026
1 check failed
@Tola-byte
Copy link
Contributor Author

okay thank you @greatest0fallt1me , please can this be closed so the points reflects on drip?, and complimentary points will greatly be appreciated sire🙌🙌

@Tola-byte
Copy link
Contributor Author

@greatest0fallt1me can this be closed please so I get my points before the wave ends please?

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.

2 participants