API & Backend Overview
Beforehand's backend consists of Supabase (PostgreSQL database, Edge Functions, and Realtime) plus smart contract interactions.
Architecture
Supabase Components
- PostgreSQL Database: Stores all off-chain data
- Edge Functions: Serverless functions for complex operations
- Realtime: WebSocket subscriptions for live updates
- Storage: File storage (if needed for images)
Smart Contract Integration
- Viem: Ethereum interaction library
- Public Client: Read contract state
- Wallet Client: Sign and send transactions
- Event Listening: Monitor contract events
API Patterns
Database Queries
Most data comes from Supabase:
- Market listings
- User profiles
- Community data
- Voting records
Contract Reads
On-chain data read via Viem:
- Token balances (positions)
- Pool sizes
- Market status
- Resolution state
Contract Writes
User transactions via Privy wallet:
- Creating markets
- Placing trades
- Redeeming tokens
Data Synchronization
The system keeps on-chain and off-chain data in sync:
- Market creation: Contract address stored in database
- Trading: Database updated from contract events
- Resolution: Database updated when contract resolves
- Real-time: Frontend queries both and merges results