-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(predict): optimistic position updates for buy/sell/claim #22493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… system Update existing tests to work with the new optimistic position updates system that replaced the old recentlySoldPositions tracking: - Update confirmClaim tests to track by outcomeTokenId - Update getPositions filtering tests for optimistic removal system - Change timeout from 5 minutes to 1 minute per new implementation - Update placeOrder tests to verify optimistic position creation on BUY - Update all mocked positions to include outcomeTokenId field - Update assertions to match new optimistic update behavior All 149 tests now passing.
Add first batch of tests for optimistic position creation during BUY orders: - Test creating optimistic position when buying new shares - Test helper function validation for optimistic flag These tests use the new helper functions (createOptimisticPosition, setupOptimisticUpdateTest, mockMarketDetailsForOptimistic) and validate the basic optimistic position creation flow. Tests: 151 passing (+2 from baseline)
Add comprehensive tests for optimistic position creation during BUY orders: - Calculate initial values correctly (amount, initialValue, avgPrice, size) - Set expected size for API validation - Fetch market details for complete position data - Handle market details fetch failures gracefully - Block orders on claimable positions (defensive check) Tests validate that optimistic positions are created with correct values using raw USDC amounts, and handle edge cases like API failures. Tests: 156 passing (+5 from previous commit)
Add tests for updating existing optimistic positions when buying more shares: - Update existing position when buying more shares - Accumulate amount and initialValue (tests actual behavior) - Recalculate avgPrice after update - Preserve existing position data (marketId, outcomeId, title) Tests validate that subsequent BUY orders on same outcomeTokenId create new optimistic positions (since optimistic positions aren't returned from API). Tests: 160 passing (+4 from previous commit)
Add tests for PredictPosition and PredictPositionDetail components: PredictPosition (4 tests): - Hide current value when optimistic - Hide percent PnL when optimistic - Show actual values when not optimistic - Show initial value line when optimistic PredictPositionDetail (3 tests): - Hide current value when optimistic and market open - Hide percent PnL when optimistic and market open - Show initial value and outcome when optimistic Tests validate that skeleton loaders replace values for optimistic positions. Tests: 20 passing across both components
Add end-to-end integration tests for optimistic position flows: - Create optimistic position on BUY, remove when API confirms - Clean up after 1-minute timeout if API never confirms (using fake timers) - Handle BUY followed by SELL on same position Tests validate complete workflows from order placement through API confirmation or timeout cleanup. Tests: 163 passing (+3 integration tests) Coverage: 91.53% statements, 91.75% lines (exceeds 80% target)
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
| state.balances[providerId] = state.balances[providerId] || {}; | ||
| state.balances[providerId][signer.address] = { | ||
| balance: cachedBalance - realAmountUsd, | ||
| balance: cachedBalance - (realAmountUsd + totalFee), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a quick fix to the optimistic balance updates, which were missing the fees.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #22493 +/- ##
==========================================
+ Coverage 77.88% 77.90% +0.01%
==========================================
Files 3841 3841
Lines 98201 98294 +93
Branches 19290 19316 +26
==========================================
+ Hits 76488 76579 +91
Misses 16473 16473
- Partials 5240 5242 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Replace raw React Native components with design system equivalents to comply with UI development guidelines: - Replace TouchableOpacity → ButtonBase - Replace View → Box with twClassName props - Replace StyleSheet.create() → Tailwind classes - Delete PredictPosition.styles.ts file - Use useTailwind() hook for inline styles Tailwind class mappings: - positionContainer → flex-row items-start py-2 gap-4 w-full - positionImageContainer → pt-1 - positionImage → w-10 h-10 rounded-full - positionDetails → flex-1 gap-0.5 - positionPnl → gap-2 items-end - skeletonSpacing → mb-1 Fixes CODEBOT violations: - Component Hierarchy (STRICT ORDER) - now uses design system first - Styling Rules (ENFORCE STRICTLY) - removed StyleSheet.create() All tests passing (22/22).
…ents" This reverts commit 05db268.
|
caieu
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
kevinbluer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the earlier comment it LGTM 👍




Description
This PR implements optimistic updates for Predict positions to provide immediate user feedback when placing BUY/SELL orders, significantly improving the user experience by showing skeleton loaders instead of stale data while waiting for API confirmation.
What is the reason for the change?
Previously, when users placed orders in the Predict feature, they had to wait for the API to confirm the transaction before seeing their position update. This created a poor UX where:
What is the improvement/solution?
Implemented a comprehensive optimistic updates system:
Core Features
Implementation Details
PolymarketProvider.ts (~450 lines added):
OptimisticUpdateTypeenum (CREATE, UPDATE, REMOVE)OptimisticPositionUpdateinterface with type, position data, and expected size#optimisticPositionUpdatesByAddressMap to track updates per user addresscreateOrUpdateOptimisticPosition()for BUY ordersremoveOptimisticPosition()for SELL/CLAIM ordersapplyOptimisticPositionUpdates()to merge optimistic data with API responsesisApiPositionUpdated()with 0.1% tolerance for size validationUI Components:
optimistic: trueoptimistic?: booleanflag toPredictPositiontypeController:
Comprehensive Test Coverage (~1,830 lines of tests):
Changelog
CHANGELOG entry: null
Related issues
Fixes: PRED-294
Manual testing steps
Screenshots/Recordings
Before
After placing an order, users saw stale position data with no indication that an order was processing.
After
After placing an order, users immediately see:
Screen.Recording.2025-11-10.at.9.27.17.PM.mov
Pre-merge author checklist
Pre-merge reviewer checklist
Technical Implementation Summary
Files Changed
PolymarketProvider.ts: +450 lines (optimistic update system)PredictPosition.tsx: +29 lines (skeleton loaders)PredictPositionDetail.tsx: +27 lines (skeleton loaders, disabled button)PredictController.ts: +3 lines (fee calculation)types/index.ts: +1 line (optimistic?: booleanflag)Optimistic Update Flow
BUY Order:
placeOrder()calledsubmitClobOrder()createOrUpdateOptimisticPosition()calledoptimistic: trueflaggetPositions()merges optimistic position with API dataSELL/CLAIM Order:
placeOrder()orconfirmClaim()calledremoveOptimisticPosition()marks position for removalgetPositions()filters out the positionTest Coverage
Total: 21 new tests added
PolymarketProvider (14 tests):
UI Components (7 tests):
Coverage Achieved:
All metrics exceed the 80% target.
Note
Adds full optimistic position create/update/remove flow with UI skeletons, disables actions during optimism, adjusts balance for fees, and expands tests.
CREATE/UPDATE/REMOVE) via in-memory map keyed by address andoutcomeTokenId.getPositionswith timeout cleanup (1 min) and API-size validation tolerance.outcomeIdingetPositions.PredictPositionandPredictPositionDetail: show skeletons for value/PnL whenposition.optimisticis true; disable "Cash out" for optimistic positions.optimistic?: booleantoPredictPosition; addoutcomeIdtoGetPositionsParamsand wire through provider method.usePredictPrices.Written by Cursor Bugbot for commit 144511a. This will update automatically on new commits. Configure here.