Context:
When users swipe right, the backend must execute trades automatically via Soroban contracts with risk controls (position limits, stop-loss).
Problem:
Build a trade execution service that validates trades, invokes Soroban contracts, and tracks execution status.
What Done Looks Like:
- POST
/trades/execute endpoint
- Trade validation against risk limits
- Soroban contract invocation
- Transaction status tracking
- Success/failure notifications
Folder Structure:
src/
├── trades/
│ ├── trades.service.ts
│ ├── trades.controller.ts
│ ├── trades.module.ts
│ ├── entities/
│ │ └── trade.entity.ts
│ ├── services/
│ │ ├── trade-executor.service.ts
│ │ └── risk-manager.service.ts
│ └── dto/
│ ├── execute-trade.dto.ts
│ └── trade-result.dto.ts
Implementation Guidelines:
- Validate user has sufficient balance
- Check position limits (max 10 open positions per user)
- Verify signal still active and not expired
- Invoke Soroban trade executor contract
- Store trade record in database
- Track transaction hash and status
- Emit event for notifications
- Target <5s execution time
Trade Validation:
- User authenticated
- Signal exists and active
- Sufficient balance for trade + fees
- Within position limits
- Risk parameters valid
Trade Entity Fields:
- id, user_id, signal_id, status
- entry_price, amount, side (BUY/SELL)
- tx_hash, executed_at
- pnl, closed_at
Edge Cases:
- Insufficient balance mid-execution
- Signal expired during execution
- Network congestion causing timeout
- Duplicate trade prevention
Validation:
- Valid trade executes successfully
- Invalid trade rejected with clear error
- Transaction hash stored
- Status updates correctly
Context:
When users swipe right, the backend must execute trades automatically via Soroban contracts with risk controls (position limits, stop-loss).
Problem:
Build a trade execution service that validates trades, invokes Soroban contracts, and tracks execution status.
What Done Looks Like:
/trades/executeendpointFolder Structure:
Implementation Guidelines:
Trade Validation:
Trade Entity Fields:
Edge Cases:
Validation: