
- Replace EmailStr with custom email validation using regex
- Remove pydantic[email] dependency to prevent import errors
- Update config to use dynamic database path with proper directory creation
- Improve database session configuration with connection pooling
- Fix application startup failures caused by missing email-validator package
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
Crypto P2P Trading Platform
A peer-to-peer cryptocurrency trading platform built with FastAPI and SQLite, similar to Binance P2P. This platform allows users to create buy/sell advertisements, place orders, and automatically handle payments and crypto transfers through a payment provider integration.
Features
- User Authentication: JWT-based authentication with user registration and login
- Wallet Management: Multi-cryptocurrency wallet system with balance tracking and fund locking
- Advertisement System: Users can create buy/sell ads with customizable terms and conditions
- Order Management: Secure order placement with automatic fund locking and crypto release
- Payment Integration: Integration with payment providers for automatic account generation
- Auto-Release: Automatic crypto release to buyers after payment confirmation
- Real-time Status Tracking: Track order status from pending to completion
- Health Monitoring: Built-in health check endpoint for system monitoring
Tech Stack
- Backend: FastAPI (Python)
- Database: SQLite with SQLAlchemy ORM
- Authentication: JWT tokens with bcrypt password hashing
- Migrations: Alembic for database migrations
- API Documentation: Auto-generated OpenAPI/Swagger docs
- HTTP Client: httpx for payment provider integration
Project Structure
.
├── main.py # FastAPI application entry point
├── requirements.txt # Python dependencies
├── alembic.ini # Alembic configuration
├── alembic/ # Database migrations
│ ├── env.py
│ └── versions/
├── app/
│ ├── api/v1/ # API routes
│ │ ├── api.py # Main API router
│ │ └── endpoints/ # Individual endpoint modules
│ ├── core/ # Core utilities
│ │ ├── config.py # Application configuration
│ │ ├── deps.py # Dependencies
│ │ └── security.py # Security utilities
│ ├── db/ # Database configuration
│ │ ├── base.py # SQLAlchemy base
│ │ └── session.py # Database session
│ ├── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic schemas
│ └── services/ # Business logic services
└── storage/ # Application storage
└── db/ # SQLite database files
Installation
- Clone the repository:
git clone <repository-url>
cd cryptop2ptradingplatform-f8a0bc
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables (optional):
# Create .env file with the following variables (all optional):
SECRET_KEY=your-secret-key-here
SERVER_HOST=http://localhost:8000
PAYMENT_PROVIDER_API_URL=https://api.your-payment-provider.com
PAYMENT_PROVIDER_API_KEY=your-payment-provider-api-key
- Run database migrations:
alembic upgrade head
- Start the application:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
Environment Variables
The following environment variables can be configured:
SECRET_KEY
: Secret key for JWT token generation (default: auto-generated)SERVER_HOST
: Server host URL for API documentation links (default: http://localhost:8000)PAYMENT_PROVIDER_API_URL
: Payment provider API base URL (optional)PAYMENT_PROVIDER_API_KEY
: Payment provider API key (optional)
API Documentation
Once the application is running, you can access:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
API Endpoints
Authentication
POST /api/v1/auth/register
- Register a new userPOST /api/v1/auth/login
- Login and get access token
Users
GET /api/v1/users/me
- Get current user profilePUT /api/v1/users/me
- Update current user profile
Cryptocurrencies
GET /api/v1/cryptocurrencies/
- List supported cryptocurrenciesGET /api/v1/cryptocurrencies/{id}
- Get cryptocurrency details
Wallets
GET /api/v1/wallets/
- Get user's walletsGET /api/v1/wallets/{cryptocurrency_id}
- Get specific walletPOST /api/v1/wallets/{cryptocurrency_id}/lock
- Lock fundsPOST /api/v1/wallets/{cryptocurrency_id}/unlock
- Unlock funds
Advertisements
GET /api/v1/advertisements/
- List all advertisementsGET /api/v1/advertisements/my
- Get user's advertisementsPOST /api/v1/advertisements/
- Create new advertisementGET /api/v1/advertisements/{id}
- Get advertisement detailsPUT /api/v1/advertisements/{id}
- Update advertisementDELETE /api/v1/advertisements/{id}
- Cancel advertisement
Orders
GET /api/v1/orders/
- Get user's ordersPOST /api/v1/orders/
- Create new orderGET /api/v1/orders/{id}
- Get order detailsPUT /api/v1/orders/{id}
- Update orderPOST /api/v1/orders/{id}/confirm-payment
- Confirm payment (buyer)POST /api/v1/orders/{id}/release
- Release crypto (seller)POST /api/v1/orders/{id}/cancel
- Cancel order
System
GET /health
- Health check endpoint
How It Works
Trading Flow
-
Advertisement Creation: Users create buy/sell advertisements specifying:
- Cryptocurrency type and amount
- Price per unit
- Minimum/maximum order amounts
- Accepted payment methods
- Terms and conditions
-
Order Placement: When a buyer places an order:
- Seller's crypto is automatically locked
- Payment account details are generated via payment provider API
- Order expires in 30 minutes if not completed
-
Payment Process:
- Buyer receives payment account details (account number, bank name, reference)
- Buyer makes payment to the provided account
- System tracks payment status via payment provider API
-
Automatic Release:
- Once payment is confirmed, crypto is automatically released to buyer's wallet
- Seller's locked funds are transferred to buyer
- Order status is updated to completed
Security Features
- JWT-based authentication
- Automatic fund locking prevents double-spending
- Payment verification through external provider
- Order expiration prevents indefinite locks
- Comprehensive audit trail
Database Models
- Users: User accounts with authentication
- Cryptocurrencies: Supported crypto types
- Wallets: User balances per cryptocurrency
- Advertisements: Buy/sell listings
- Orders: Trade orders with status tracking
- Payments: Payment details and status
Development
Running in Development Mode
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
Database Migrations
Create a new migration:
alembic revision --autogenerate -m "Description of changes"
Apply migrations:
alembic upgrade head
Code Linting
ruff check .
ruff format .
Payment Provider Integration
The platform integrates with payment providers to:
- Generate temporary payment accounts for each order
- Verify payment status automatically
- Handle payment notifications via webhooks
If no payment provider is configured, the system uses mock data for demonstration purposes.
Health Monitoring
The /health
endpoint provides system status information:
{
"status": "healthy",
"service": "crypto-p2p-platform",
"version": "1.0.0"
}
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the MIT License.