Data Flow
Beforehand uses a hybrid architecture where financial operations happen on-chain while metadata and social features are stored off-chain. This document describes how data flows through the system for key operations.
Overview
Data flows between four main components:
- Frontend: React application that users interact with
- Smart Contracts: On-chain market contracts that handle trades and payouts
- Database: Supabase PostgreSQL storing metadata and social data
- Event Listeners: Background processes that sync on-chain events to the database
The system maintains consistency by treating on-chain data as the source of truth for financial operations, while the database provides rich metadata and social features.
Market Creation Flow
When a user creates a market, the system deploys a new smart contract and stores metadata in the database.
Process
- User fills out market form (question, description, close time, resolution time)
- Frontend validates input and checks user permissions
- Market contract is deployed on-chain via MarketFactory
- Contract address is returned to frontend
- User places initial trade (required to activate market)
- Market metadata is stored in database with contract address
- Market status is set to "pending_match"
- Real-time notification sent to community members
Diagram
Key Points
- Contract deployment happens first to get the address
- Initial trade is required to activate the market
- Database stores metadata but contract is source of truth for financial state
- Real-time notifications alert community members of new markets
Trading Flow
Trading involves on-chain token transactions with off-chain position tracking.
Process
- User selects market and side (YES or NO)
- Frontend calculates current price from pool sizes
- User enters trade amount and confirms
- Frontend requests USDC approval (if needed)
- User approves USDC spending
- Frontend calls contract to buy tokens
- Contract mints position tokens and updates pools
- Contract emits Trade event
- Frontend queries updated token balance
- Database syncs position data from on-chain state
- Real-time updates broadcast to other users
Diagram
Key Points
- USDC approval is a separate transaction (one-time per market)
- Contract automatically calculates token amount based on pool sizes
- Position tokens are ERC20 tokens (yUSDC for YES, nUSDC for NO)
- Event listeners sync on-chain trades to database
- Real-time updates show trades to all users immediately
Resolution Flow
Resolution determines the market outcome and distributes payouts to winners.
Process
- Trading closes at the close time
- Market status updates to "closed" in database
- If community resolution: users vote, votes are weighted by credibility
- Admin reviews votes and evidence in database
- Admin calls resolveMarket() on contract
- Contract sets outcome (YES, NO, or CANCELLED)
- Contract enables redemption for winners
- Contract emits MarketResolved event
- Database updates with outcome
- Winners redeem tokens for USDC payouts
- Real-time notifications sent to all participants
Diagram
Key Points
- Trading closes automatically at the close time
- Community votes are weighted by credibility scores
- Admin has final authority to resolve markets
- Resolution happens on-chain and is immutable
- Winners must redeem tokens to receive payouts
- Redemption can happen anytime after resolution
Data Synchronization Flow
The system continuously syncs on-chain contract state with the off-chain database.
Sync Mechanisms
- Event Listening: Background processes listen to contract events
- Polling: Frontend periodically queries contract state
- Real-time Subscriptions: Supabase Realtime pushes database changes
- Manual Sync: Admin tools can force sync when needed
Diagram
Key Points
- On-chain data is authoritative for financial state
- Database provides metadata and social features
- Frontend merges both sources for display
- Real-time updates keep UI synchronized
- Event listeners ensure database stays current
Event Flow
Contract events trigger database updates and UI refreshes.
Event Types
- MarketCreated: New market deployed
- Trade: User placed a trade
- MarketResolved: Market outcome set
- MarketCanceled: Market canceled and refunded
Diagram
Error Handling
When sync fails or data differs:
- On-chain is authoritative: Financial data always comes from contract
- Database reconciliation: Sync jobs update database to match contract
- User notification: Frontend shows warnings if data differs
- Manual override: Admins can force sync if needed
The system prioritizes on-chain accuracy for financial operations while maintaining database consistency for metadata.