# 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-core` for Bitcoin - `web3` and `ethereumjs-tx` for Ethereum - `tronweb` for Tron - **Other**: Winston (logging), Nodemailer (emails), bcrypt (hashing) ## Prerequisites - Node.js (v14 or higher) - MongoDB - Redis - RabbitMQ (AMQP) - TypeScript ## Installation 1. Clone the repository: ```bash git clone cd Exchange ``` 2. Navigate to the server directory: ```bash cd server ``` 3. Install dependencies: ```bash npm install ``` 4. Create a `.env` file in the server directory with the following variables: ```env # 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 ``` 5. Compile TypeScript (if needed): ```bash npx tsc ``` ## Running the Application ### Development Mode ```bash npm start ``` The server will start on `http://localhost:3001` ### Test Mode ```bash npm run test-env ``` ### Running Tests ```bash 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 status - `POST /register` - User registration - `POST /login` - User login - `GET /logout` - User logout - `POST /verify` - Verify email/phone ### User (`/user`) - `GET /getUserWallet` - Get user wallet balance - `GET /getUserOffers` - Get user's trading offers - `POST /createOffer` - Create a new trading offer - `GET /getUserTransactions` - Get user transaction history ### Wallet (`/wallet`) - `GET /getEtheriumNonce` - Get Ethereum nonce - `POST /transferToExchange` - Transfer cryptocurrency to exchange - `POST /transferFromExchange` - Transfer cryptocurrency from exchange - `POST /transferToExchangeById` - Transfer by transaction ID ### Service (`/service`) - `GET /getDeafultAcceptedOffers` - Get default accepted offers - `GET /getPriceChart` - Get price chart data - `GET /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 logs - `error.log` - Error logs - `exceptions.log` - Uncaught exceptions ## Database The application uses MongoDB with the following main databases: - `exchange` - Main application data - `trudesk` - Support ticket system ## Contributing 1. Fork the repository 2. Create a feature branch 3. Make your changes 4. Write tests if applicable 5. 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 `.env` file - The application includes automated backup scripts that run daily - Price statistics are updated continuously for real-time trading data