A proof of concept implementation for a trading exchange backend settlement system.
Component | Technology |
---|---|
Language | Python 3.11+ (FastAPI, aiokafka, psycopg2) |
Event Streaming | Apache Kafka (with Confluent Kafka for Python) |
Liquidity Engine | Blockchain / API |
Databases | PostgreSQL (event store, user balances) |
Auth | AWS Cognito (OAuth 2.0/JWT) |
Infrastructure | Docker (local PoC), AWS ECS/EKS (production) |
Monitoring | Prometheus + Grafana (metrics), OpenTelemetry (tracing) |
- Docker installed on your system
- docker-compose installed
- uvicorn installed
- Python 3.11 or higher (tested version)
First, start the supporting services:
docker-compose up -d
This starts the required services:
- Kafka (Zookeeper and Broker)
- PostgreSQL database
- API Server:
uvicorn api.main:app --reload --port 8000
- Wallet Manager:
python -m wallet.manager
- Liquidity Engine:
python -m engine.liquidity_engine
curl -X POST "http://localhost:8000/wallet/deposit" \
-H "Content-Type: application/json" \
-d '{
"user_id": "user123",
"currency": "ETH",
"amount": 1.5,
"transaction_id": "769f1e96-da1b-4baa-94df-8495b613e772"
}'
curl -X POST "http://localhost:8000/wallet/deposit/confirm" \
-H "Content-Type: application/json" \
-d '{
"user_id": "user123",
"transaction_id": "769f1e96-da1b-4baa-94df-8495b613e772"
}'
Place a BUY order for 0.03 BTC at a price of 33.33 ETH per BTC:
curl -X POST "http://localhost:8000/orders" \
-H "Content-Type: application/json" \
-d '{
"user_id": "user123",
"symbol": "BTC/ETH",
"side": "buy",
"order_type": "limit",
"price": 33.33,
"quantity": 1.0
}'
Confirm a BUY order for 0.03 BTC at a price of 33.33 ETH per BTC:
curl -X POST "http://localhost:8000/orders/confirm" \
-H "Content-Type: application/json" \
-d '{
"user_id": "user123",
"transaction_id": "order123",
"confirmation_id": "confirmation123"
}'
- FastAPI Endpoints:
POST /wallet/deposit
: AcceptsDepositFundsCommand
to initiate depositsPOST /wallet/deposit/confirm
: AcceptsDepositConfirmationCommand
to confirm depositsPOST /wallet/withdraw
: AcceptsWithdrawFundsCommand
to initiate withdrawalsGET /wallet/{user_id}
: Retrieves current balance (currently returns placeholder data)
- Kafka Integration:
- Consumes
deposit_funds
,deposit_confirmation
, andwithdraw_funds
commands - Produces wallet events to event store
- Consumes
- Event Sourcing:
- Maintains wallet state through
deposit_initiated
,deposit_confirmed
,withdrawal_requested
events - Stores events in PostgreSQL with transaction history
- Maintains wallet state through
- Core Operations:
- Deposit initiation and confirmation
- Withdrawal processing
- Balance snapshot maintenance
- Transaction verification
- Error Handling:
- Validates sufficient funds for withdrawals
- Prevents duplicate transaction confirmations
- Maintains transaction consistency through event sourcing
- FastAPI Endpoints:
POST /orders
: AcceptsPlaceOrderCommand
to initiate new ordersPOST /orders/confirm
: AcceptsOrderConfirmationCommand
to confirm order execution
- Kafka Integration:
- Consumes
place_order
andorder_confirmation
commands - Produces order events to event store
- Consumes
- Event Sourcing:
- Maintains order state through
order_placed
,liquidity_confirmed
, andliquidity_failed
events - Stores events in PostgreSQL with transaction history
- Maintains order state through
- Core Operations:
- Token transfers to exchange (blockchain integration)
- Merchant swap initiation
- Order confirmation handling
- Error recovery through event replay
- Error Handling:
- Creates
liquidity_failed
andliquidity_confirmation_failed
events for error cases - Maintains transaction consistency through event sourcing
- Creates
- Event store ingests events such as:
order_placed
,liquidity_failed
,liquidity_confirmed
,liquidity_confirmation_failed
,order_confirmed
- Update snapshots of user balances (currently in-memory snapshots, could be postgres in production)
- Real-time updates via WebSocket (not implemented in this PoC)
The system uses:
- Event sourcing for transaction processing
- Kafka for command processing
- PostgreSQL for event store and user balances
The wallet manager maintains user balances and processes transactions asynchronously.