Skip to content

feat(#1398): implement event creation idempotency with fee protection - #1420

Merged
greatest0fallt1me merged 1 commit into
Predictify-org:masterfrom
nlstylz:feat/1398-event-creation-idempotency
Aug 29, 2026
Merged

feat(#1398): implement event creation idempotency with fee protection#1420
greatest0fallt1me merged 1 commit into
Predictify-org:masterfrom
nlstylz:feat/1398-event-creation-idempotency

Conversation

@nlstylz

@nlstylz nlstylz commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Closes #1398

Summary

Implement caller-supplied idempotency keys for event creation to prevent duplicate event-creation fee charges on retry scenarios (e.g., after network timeout or partial failure).

Changes

Storage Infrastructure

  • Add CreateEventIdem(Address, BytesN<32>) to DataKey enum in storage.rs
    • Scoped by admin address + caller-supplied 32-byte key
    • TTL: ~7 days (reuses PLACE_BETS_IDEM_TTL_LEDGERS constant)

Core Implementation (lib.rs)

  • Add optional idempotency_key: Option<BytesN<32>> parameter to create_event
  • Idempotency check BEFORE any state mutations:
    • If duplicate key found: return IdempotentBatchAlreadyApplied error
    • If fresh key: proceed with fee collection and event creation
  • Integrate fee collection via process_creation_fee (was previously missing)
  • Store idempotency key with TTL after successful event creation

Behavior

  • First call: Event created, fee charged, key stored
  • Retry (same key, within TTL): IdempotentBatchAlreadyApplied error, NO fee
  • After TTL expiry: Key reusable for new event creation
  • No key provided: Works as before (backward compatible)

Failure Mode Safety

  • Idempotency check fires BEFORE fee collection → no duplicate fees on retry
  • Key scope: admin-specific → different admins independent
  • Deterministic error response → clear signal to clients

Acceptance Criteria

✓ Deterministic behavior for valid/invalid/duplicate/boundary inputs ✓ Authorization & validation still enforced before idempotency check ✓ Retries, partial failures safe from double-charging ✓ Key scope prevents collisions across admins
✓ Backward compatible: optional parameter
✓ Clear error codes and audit trail entries

Testing

Comprehensive test suite covers:

  • Happy path: fresh key creates event + charges fee
  • Duplicate detection: same key rejected, no fee
  • Key scoping: different admins independent
  • TTL expiry: key becomes reusable
  • Fee verification: charged exactly once
  • Regression: existing tests pass

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

Closes #(issue number)
Fixes #(issue number)
Related to #(issue number)

Priority Level

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

📝 Detailed Description

What does this PR do?

Why is this change needed?

How was this tested?

Alternative Solutions Considered


🏗️ 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

Security Considerations

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

🧪 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

# Paste test output here
cargo test
# Expected output: X tests passed, Y tests failed

Manual Testing Steps


📚 Documentation

Documentation Updates

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

Breaking Changes

Breaking Changes:

Migration Guide:


🔍 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:
  • Storage Impact:
  • Computational Complexity:

Security Review

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

🚀 Deployment & Integration

Deployment Notes

  • Network: Testnet/Mainnet
  • Contract Address:
  • Migration Required: Yes/No
  • Special Instructions:

Integration Points

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

📊 Impact Assessment

User Impact

  • End Users:
  • Developers:
  • Admins:

Business Impact

  • Revenue:
  • User Experience:
  • Technical Debt:

✅ 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
  • No merge conflicts

📸 Screenshots (if applicable)

🔗 Additional Resources

  • Design Document:
  • Technical Spec:
  • Related Discussion:
  • External Documentation:

💬 Notes for Reviewers

Please pay special attention to:

Questions for reviewers:


Thank you for your contribution to Predictify! 🚀

…fee protection

Closes Predictify-org#1398

## Summary
Implement caller-supplied idempotency keys for event creation to prevent
duplicate event-creation fee charges on retry scenarios (e.g., after network
timeout or partial failure).

## Changes

### Storage Infrastructure
- Add `CreateEventIdem(Address, BytesN<32>)` to DataKey enum in storage.rs
  - Scoped by admin address + caller-supplied 32-byte key
  - TTL: ~7 days (reuses PLACE_BETS_IDEM_TTL_LEDGERS constant)

### Core Implementation (lib.rs)
- Add optional `idempotency_key: Option<BytesN<32>>` parameter to create_event
- Idempotency check BEFORE any state mutations:
  - If duplicate key found: return IdempotentBatchAlreadyApplied error
  - If fresh key: proceed with fee collection and event creation
- Integrate fee collection via process_creation_fee (was previously missing)
- Store idempotency key with TTL after successful event creation

### Behavior
- **First call**: Event created, fee charged, key stored
- **Retry (same key, within TTL)**: IdempotentBatchAlreadyApplied error, NO fee
- **After TTL expiry**: Key reusable for new event creation
- **No key provided**: Works as before (backward compatible)

## Failure Mode Safety
- Idempotency check fires BEFORE fee collection → no duplicate fees on retry
- Key scope: admin-specific → different admins independent
- Deterministic error response → clear signal to clients

## Acceptance Criteria
✓ Deterministic behavior for valid/invalid/duplicate/boundary inputs
✓ Authorization & validation still enforced before idempotency check
✓ Retries, partial failures safe from double-charging
✓ Key scope prevents collisions across admins
✓ Backward compatible: optional parameter
✓ Clear error codes and audit trail entries

## Testing
Comprehensive test suite covers:
- Happy path: fresh key creates event + charges fee
- Duplicate detection: same key rejected, no fee
- Key scoping: different admins independent
- TTL expiry: key becomes reusable
- Fee verification: charged exactly once
- Regression: existing tests pass
@drips-wave

drips-wave Bot commented Aug 28, 2026

Copy link
Copy Markdown

@nlstylz 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 c1d53ff into Predictify-org:master Aug 29, 2026
2 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][High] Prevent duplicate event-creation fees

2 participants