Cryptocurrency Exchange Platform
A comprehensive cryptocurrency exchange server built with Node.js, TypeScript, and Express. This platform supports trading of Bitcoin, Ethereum, and Tron cryptocurrencies with Iranian Rial (IRR) support.
Features
- Multi-Cryptocurrency Support: Bitcoin (BTC), Ethereum (ETH), and Tron (TRX)
- User Authentication: Email and phone verification, session management
- Wallet Management: Secure wallet operations with support for multiple cryptocurrencies
- Trading System: Buy/sell orders, offer management, and order matching
- Price Statistics: Real-time and historical price data (hourly, daily, weekly, monthly, yearly)
- Real-time Updates: WebSocket integration via Socket.io for live updates
- Admin Panel: Administrative tools for managing the exchange
- Support Tickets: Integrated ticketing system for customer support
- Automated Backups: Daily database backups
- Rate Limiting: Brute force protection and rate limiting
Tech Stack
- Runtime: Node.js
- Language: TypeScript
- Framework: Express.js
- Database: MongoDB (Mongoose)
- Cache: Redis
- Message Queue: AMQP (RabbitMQ)
- Real-time: Socket.io
- Blockchain Libraries:
bitcoin-corefor Bitcoinweb3andethereumjs-txfor Ethereumtronwebfor Tron
- Other: Winston (logging), Nodemailer (emails), bcrypt (hashing)
Prerequisites
- Node.js (v14 or higher)
- MongoDB
- Redis
- RabbitMQ (AMQP)
- TypeScript
Installation
- Clone the repository:
git clone <repository-url>
cd Exchange
- Navigate to the server directory:
cd server
- Install dependencies:
npm install
- Create a
.envfile in the server directory with the following variables:
# Database
MONGO_DATABASE=mongodb://localhost:27017/exchange
MONGO_DATABASE_NAME_TICKETS=trudesk
# Session
SESSION_SECRET=your-session-secret-key
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
# REDIS_PASS=your-redis-password
# AMQP (RabbitMQ)
AMQP_URL=amqp://localhost
# Email (Nodemailer)
NODEMAILER_HOST=smtp.gmail.com
NODEMAILER_PORT=587
NODEMAILER_USER=your-email@gmail.com
NODEMAILER_PASS=your-email-password
SENDER_ADDRESS=your-email@gmail.com
# API
API=http://localhost:3001
# External APIs
CURRENCY_API_KEY=your-nomics-api-key
SMS_API_ACCESS_KEY=your-sms-api-key
SMS_API_PHONE_PATTERN_CODE=your-sms-pattern-code
SMS_API_DEFINITE_SENDER_NUMBER=your-sms-sender-number
# Ticket System
TICKET_START_CONVERSATION=your-ticket-api-url
TICKET_GET_MESSAGES_URL=your-ticket-api-url
TICKET_CREATE_TICKET_URL=your-ticket-api-url
TICKET_ADD_COMMENT_URL=your-ticket-api-url
TICKET_SEND_MESSAGE_URL=your-ticket-api-url
SUPPORT_ROLE_ID=your-support-role-id
USER_ROLE_ID=your-user-role-id
ACCESS_TOKEN=your-access-token
# Crypto
CRYPTO_SECRET=your-crypto-secret
SALT_I=10
OBJECTID_RIAL=your-rial-currency-object-id
# Feature Flags
BUYFROMOFFERS=true
# Chart
CHART_LIMIT=20
# Test
TEST_API_URL=http://localhost:3001
NODE_ENV=development
- Compile TypeScript (if needed):
npx tsc
Running the Application
Development Mode
npm start
The server will start on http://localhost:3001
Test Mode
npm run test-env
Running Tests
npm test
Project Structure
server/
├── api/ # API utilities and integrations
│ ├── walletApi/ # Blockchain wallet APIs (Bitcoin, Ethereum, Tron)
│ ├── amqp.ts # AMQP message queue configuration
│ ├── logger.ts # Winston logger configuration
│ ├── redis.ts # Redis client configuration
│ └── socket.ts # Socket.io configuration
├── db/ # Database models and schemas
│ ├── user.ts # User model
│ ├── currencies.ts # Currency model
│ ├── activeOffers.ts # Active trading offers
│ ├── acceptedOffers.ts # Accepted offers
│ └── ... # Other models
├── middlewares/ # Express middlewares
│ ├── auth.ts # Authentication middleware
│ ├── validation.ts # Request validation
│ ├── errorHandler.ts # Error handling
│ └── preventBruteForce.ts # Rate limiting
├── routes/ # API routes
│ ├── auth.ts # Authentication routes
│ ├── user.ts # User routes
│ ├── wallet.ts # Wallet routes
│ ├── admin.ts # Admin routes
│ ├── service.ts # Service routes
│ └── tickets.ts # Support ticket routes
├── scripts/ # Utility scripts
│ ├── priceStats.ts # Price statistics
│ ├── currenciesadder.ts # Currency management
│ └── localPriceScript.ts # Local price tracking
└── test/ # Test files
API Endpoints
Authentication (/auth)
GET /auth- Check authentication statusPOST /register- User registrationPOST /login- User loginGET /logout- User logoutPOST /verify- Verify email/phone
User (/user)
GET /getUserWallet- Get user wallet balanceGET /getUserOffers- Get user's trading offersPOST /createOffer- Create a new trading offerGET /getUserTransactions- Get user transaction history
Wallet (/wallet)
GET /getEtheriumNonce- Get Ethereum noncePOST /transferToExchange- Transfer cryptocurrency to exchangePOST /transferFromExchange- Transfer cryptocurrency from exchangePOST /transferToExchangeById- Transfer by transaction ID
Service (/service)
GET /getDeafultAcceptedOffers- Get default accepted offersGET /getPriceChart- Get price chart dataGET /getCurrencies- Get supported currencies
Admin (/admin)
- Admin-specific endpoints for managing the exchange
Tickets (/tickets)
- Support ticket management endpoints
Key Features
Automated Tasks
- Daily database backups (runs at 11:59 PM)
- Continuous price statistics updates
- Local price tracking (hourly, daily, weekly, monthly, yearly)
- Dollar to Rial price updates
Security
- Session-based authentication
- CSRF protection
- Rate limiting and brute force protection
- Password hashing with bcrypt
- Input validation
Real-time Features
- Socket.io for real-time updates
- Online user tracking
- Live price updates
Logging
The application uses Winston for logging. Logs are written to:
combined.log- All logserror.log- Error logsexceptions.log- Uncaught exceptions
Database
The application uses MongoDB with the following main databases:
exchange- Main application datatrudesk- Support ticket system
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Write tests if applicable
- Submit a pull request
License
MIT
Notes
- Make sure MongoDB, Redis, and RabbitMQ are running before starting the server
- Configure all environment variables in the
.envfile - The application includes automated backup scripts that run daily
- Price statistics are updated continuously for real-time trading data
Description
Languages
JavaScript
60.8%
TypeScript
39.2%