Deft Trade - DeFi Trading Simulation Platform Backend

This is a secure, admin-controlled backend for a decentralized finance (DeFi) trading simulation platform called Deft Trade. The platform simulates trading bots based on admin-configured logic without actual blockchain integration.

Features

  • Authentication System: JWT-based authentication with optional 2FA and email verification
  • Wallet System: Automatic creation of Spot and Trading wallets for users
  • Manual USDT Deposits: Admin approval workflow for deposits
  • Manual Withdrawals: Admin review and processing of withdrawals
  • Wallet Transfers: Users can transfer between Spot and Trading wallets
  • Bot Marketplace: Admin-controlled trading bots with configurable parameters
  • Bot Purchase & Simulation: Simulated bot trading with automatic ROI distribution
  • KYC System: Document upload and admin verification
  • Admin Dashboard: Comprehensive admin control panel

Technology Stack

  • Framework: FastAPI (Python)
  • Database: SQLite with SQLAlchemy ORM
  • Authentication: JWT with optional TOTP-based 2FA
  • Migrations: Alembic
  • File Storage: Local file system
  • Email: SMTP integration (optional)

Setup and Installation

Prerequisites

  • Python 3.8+
  • SQLite

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd defitradingsimulationplatformbackend
    
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows, use venv\Scripts\activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. Create a .env file based on .env.example:

    cp .env.example .env
    

    Edit the .env file to set your configuration values, especially:

    • SECRET_KEY and JWT_SECRET_KEY (use secure random strings)
    • ADMIN_EMAIL and ADMIN_PASSWORD (for the default admin user)
    • Email settings if you want to enable email notifications
  5. Run the database migrations:

    alembic upgrade head
    
  6. Run the application:

    uvicorn main:app --reload
    
  7. Access the API documentation at: http://localhost:8000/docs

Directory Structure

.
├── alembic.ini                # Alembic configuration
├── migrations/                # Database migrations
├── app/                       # Main application package
│   ├── api/                   # API endpoints
│   │   └── v1/                # API version 1
│   │       └── endpoints/     # API endpoint implementations
│   ├── core/                  # Core functionality
│   ├── crud/                  # CRUD operations
│   ├── db/                    # Database session and models
│   ├── models/                # SQLAlchemy models
│   ├── schemas/               # Pydantic schemas
│   ├── services/              # Business logic services
│   └── storage/               # File storage directories
├── main.py                    # Application entry point
└── requirements.txt           # Project dependencies

Environment Variables

Create a .env file in the root directory with the following variables:

Variable Description Default Value
PROJECT_NAME Application name "Deft Trade"
DEBUG Debug mode True
SECRET_KEY Secret key for general app encryption Auto-generated
JWT_SECRET_KEY Secret key for JWT tokens Auto-generated
ACCESS_TOKEN_EXPIRE_MINUTES JWT access token expiration time 30
REFRESH_TOKEN_EXPIRE_DAYS JWT refresh token expiration time 7
ALGORITHM JWT algorithm "HS256"
BACKEND_CORS_ORIGINS CORS origins ["*"]
EMAILS_ENABLED Enable email sending False
SMTP_TLS Use TLS for SMTP True
SMTP_PORT SMTP port 587
SMTP_HOST SMTP host None
SMTP_USER SMTP username None
SMTP_PASSWORD SMTP password None
EMAILS_FROM_EMAIL Sender email None
EMAILS_FROM_NAME Sender name None
ADMIN_EMAIL Default admin email "admin@defttrade.com"
ADMIN_PASSWORD Default admin password "change-me-please"
TWO_FACTOR_REQUIRED Require 2FA for all users False
BOT_SIMULATION_INTERVAL Bot simulation check interval (seconds) 60
MIN_DEPOSIT_AMOUNT Minimum deposit amount 10.0
MIN_WITHDRAWAL_AMOUNT Minimum withdrawal amount 10.0
WITHDRAWAL_FEE_PERCENTAGE Withdrawal fee percentage 1.0
MAX_UPLOAD_SIZE Maximum upload size in bytes 5242880 (5MB)

API Endpoints

Authentication

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - User login
  • POST /api/v1/auth/refresh-token - Refresh JWT token
  • POST /api/v1/auth/request-password-reset - Request password reset
  • POST /api/v1/auth/reset-password - Reset password
  • POST /api/v1/auth/enable-2fa - Enable 2FA
  • POST /api/v1/auth/verify-2fa - Verify 2FA token
  • GET /api/v1/auth/me - Get current user info

Wallets

  • GET /api/v1/wallets - Get user wallets
  • POST /api/v1/wallets/transfer - Transfer between wallets

Deposits

  • POST /api/v1/deposits/request - Create deposit request
  • GET /api/v1/deposits - Get user deposits
  • GET /api/v1/admin/deposits/pending - Get all pending deposits (admin)
  • PUT /api/v1/admin/{deposit_id}/approve - Approve deposit (admin)
  • PUT /api/v1/admin/{deposit_id}/reject - Reject deposit (admin)

Withdrawals

  • POST /api/v1/withdrawals/request - Create withdrawal request
  • GET /api/v1/withdrawals - Get user withdrawals
  • GET /api/v1/admin/withdrawals/pending - Get all pending withdrawals (admin)
  • PUT /api/v1/admin/{withdrawal_id}/approve - Approve withdrawal (admin)
  • PUT /api/v1/admin/{withdrawal_id}/reject - Reject withdrawal (admin)

Bots

  • GET /api/v1/bots - Get available bots
  • POST /api/v1/bots/{id}/purchase - Purchase bot
  • GET /api/v1/bots/purchased - Get purchased bots
  • POST /api/v1/admin/bots - Create bot (admin)
  • PUT /api/v1/admin/bots/{id} - Update bot (admin)
  • DELETE /api/v1/admin/bots/{id} - Delete bot (admin)

KYC

  • POST /api/v1/kyc/upload - Upload KYC documents
  • GET /api/v1/kyc/status - Get KYC status
  • GET /api/v1/admin/kyc/pending - Get all pending KYC submissions (admin)
  • PUT /api/v1/admin/kyc/{id}/approve - Approve KYC (admin)
  • PUT /api/v1/admin/kyc/{id}/reject - Reject KYC (admin)

Admin Dashboard

  • GET /api/v1/admin/users - Get all users
  • GET /api/v1/admin/statistics - Get platform statistics
  • GET /api/v1/admin/transactions - Get all transactions

Health Check

  • GET /health - Application health check

Development

Running Tests

pytest

Adding Migrations

If you need to modify the database schema:

  1. Make changes to the SQLAlchemy models in app/models/
  2. Create a new migration:
    alembic revision --autogenerate -m "description of changes"
    
  3. Apply the migration:
    alembic upgrade head
    

Running with Docker

Build and run the Docker image:

docker build -t deft-trade-backend .
docker run -p 8000:8000 deft-trade-backend

License

This project is proprietary and confidential.

Description
Project: DeFi Trading Simulation Platform Backend
Readme 88 KiB
Languages
Python 99.7%
Mako 0.3%