This document provides a high-level overview of how FlowFi's components interact and how the system processes on-chain events.
FlowFi consists of three main components:
- Soroban Smart Contracts - On-chain logic for payment streams
- Backend API - Indexing, API endpoints, and real-time event streaming
- Frontend - User interface built with Next.js
┌─────────────┐
│ Frontend │ (Next.js + React)
│ (Port 3000)│
└──────┬──────┘
│ HTTP/REST
│ SSE (Server-Sent Events)
▼
┌─────────────┐
│ Backend │ (Express.js + TypeScript)
│ (Port 3001)│
└──────┬──────┘
│
│ Indexes Events
│ Queries State
▼
┌─────────────┐
│ Stellar │
│ Network │
└──────┬──────┘
│
│ Smart Contract
│ Events & State
▼
┌─────────────┐
│ Soroban │
│ Contracts │ (Rust)
└─────────────┘
Location: contracts/stream_contract/
The smart contract handles all on-chain logic for payment streams:
- Stream Creation: Users create streams by depositing tokens
- Withdrawals: Recipients can withdraw available funds
- Top-ups: Senders can add more funds to active streams
- Cancellation: Senders can cancel streams and receive refunds
Key Contract Functions:
create_stream()- Creates a new payment streamwithdraw()- Withdraws available funds from a streamtop_up_stream()- Adds funds to an existing streamcancel_stream()- Cancels a stream and refunds remaining balanceget_stream()- Reads stream state
Events Emitted: The contract emits events for all state changes:
stream_created- When a new stream is createdtokens_withdrawn- When funds are withdrawnstream_topped_up- When additional funds are addedstream_cancelled- When a stream is cancelled
Location: backend/
The backend serves multiple purposes:
Where Indexing Happens:
The backend indexes on-chain events from Soroban contracts. The indexing process:
- Event Detection: The backend listens to Stellar network events (via Stellar Horizon API or similar)
- Event Processing: When contract events are detected, they are processed and stored
- Database Storage: Events are stored in PostgreSQL using Prisma ORM
Database Models:
Stream- Mirrors on-chain stream state for fast queryingStreamEvent- Stores all on-chain events (CREATED, TOPPED_UP, WITHDRAWN, CANCELLED, COMPLETED)User- Tracks Stellar wallet addresses
Indexing Implementation:
The indexing logic is designed to be integrated with a Stellar event listener. See:
backend/src/services/indexer-integration.example.ts- Example integration patternbackend/prisma/schema.prisma- Database schema for indexed data
Event Types Indexed:
CREATED- Stream creation eventsTOPPED_UP- Additional funds addedWITHDRAWN- Funds withdrawn by recipientCANCELLED- Stream cancellationCOMPLETED- Stream completion (all funds withdrawn)
The backend provides REST endpoints for:
- Stream Management: Query stream state, create streams (via contract interaction)
- User Data: Get user streams, balances, history
- Health Checks: API status and metrics
API Documentation:
- Swagger UI:
http://localhost:3001/api-docs - OpenAPI Spec:
http://localhost:3001/api-docs.json
Server-Sent Events (SSE):
The backend provides SSE endpoints for real-time updates:
- Endpoint:
GET /events/subscribe - Purpose: Push real-time stream updates to frontend clients
- Event Types:
stream.created,stream.topped_up,stream.withdrawn,stream.cancelled,stream.completed
How It Works:
- Frontend connects to SSE endpoint
- Backend maintains connection and broadcasts events
- When on-chain events are indexed, they trigger SSE broadcasts
- Frontend receives real-time updates without polling
See backend/docs/SSE_ARCHITECTURE.md for detailed SSE implementation.
Location: frontend/
The frontend is a Next.js application that:
- Displays Streams: Shows active streams, incoming/outgoing payments
- Wallet Integration: Connects to Stellar wallets (Freighter, etc.)
- Real-Time Updates: Subscribes to SSE events for live stream updates
- Stream Management: UI for creating, viewing, and managing streams
Key Features:
- Dashboard with stream overview
- Incoming/outgoing stream lists
- Real-time balance updates via SSE
- Wallet connection and transaction signing
- User Action: User fills out stream creation form in frontend
- Frontend: Prepares transaction and prompts wallet for signature
- Stellar Network: Transaction is submitted and processed
- Contract:
create_stream()executes, emitsstream_createdevent - Backend Indexer: Detects event, stores in database
- Backend SSE: Broadcasts event to subscribed clients
- Frontend: Receives SSE update, UI updates automatically
- User Action: Recipient clicks withdraw in frontend
- Frontend: Prepares withdrawal transaction, prompts wallet
- Stellar Network: Transaction submitted
- Contract:
withdraw()executes, emitstokens_withdrawnevent - Backend Indexer: Detects event, updates database
- Backend SSE: Broadcasts withdrawal event
- Frontend: Updates balance and stream state
- User Action: User navigates to stream details page
- Frontend: Makes REST API call to backend
- Backend: Queries indexed database (fast, no on-chain call needed)
- Backend: Returns stream data
- Frontend: Displays stream information
For real-time accuracy, the frontend can also:
- Subscribe to SSE events for that specific stream
- Receive updates immediately when on-chain events occur
- Language: Rust
- Framework: Soroban SDK
- Build Target:
wasm32-unknown-unknown
- Runtime: Node.js
- Framework: Express.js
- Language: TypeScript
- Database: PostgreSQL
- ORM: Prisma
- Real-Time: Server-Sent Events (SSE)
- Framework: Next.js 16
- Language: TypeScript
- Styling: Tailwind CSS
- State Management: React Context
- Wallet Integration: Stellar SDK
- Start Infrastructure:
docker compose up(PostgreSQL) - Start Backend:
cd backend && npm run dev - Start Frontend:
cd frontend && npm run dev - Deploy Contracts: Build and deploy to Stellar testnet
- Contracts: Rust unit tests in contract source files
- Backend: Vitest for API and service tests
- Frontend: Next.js testing utilities
- Rate Limiting: Backend implements rate limiting on all endpoints
- Input Validation: Zod schemas validate all API inputs
- Authentication: Wallet-based authentication via Stellar signatures
- Event Verification: Indexed events are verified against on-chain state
Potential areas for improvement:
- Indexer Service: Dedicated microservice for event indexing
- Caching Layer: Redis for frequently accessed stream data
- WebSocket Support: Alternative to SSE for bidirectional communication
- GraphQL API: More flexible querying for complex frontend needs
- SSE Architecture - Detailed SSE implementation
- Backend README - Backend-specific documentation
- Contributing Guide - Development setup and workflows