Skip to content

Implement Trade Execution Service #14

@Mathews-25

Description

@Mathews-25

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:

  1. User authenticated
  2. Signal exists and active
  3. Sufficient balance for trade + fees
  4. Within position limits
  5. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions