A beginner-friendly decentralized exchange for swapping tokens on the blockchain.
This is a simple DEX where users can swap tokens directly from their crypto wallets. No sign-up needed - just connect your wallet and start trading
- Swap Tokens: Exchange one token for another instantly
- Connect Wallet: Works with MetaMask
- See Prices: View current token prices before swapping
- Transaction History: See your past swaps
- Add Liquidity: Provide tokens to earn trading fees
- View Pool Stats: Check liquidity and trading volume
- Solidity: Smart contract language
- Hardhat: For writing and testing smart contracts
- OpenZeppelin: Pre-built secure contract code
- React: Building the user interface
- Node.js + Express: Backend API server
- MongoDB: Store transaction history and pool data
- Web3.js: Connect frontend to blockchain
- MetaMask: Wallet connection
- Infura/Alchemy: Connect to Ethereum network
dex-project/
├── contracts/ # Smart contracts
│ ├── DEX.sol # Main DEX contract
│ └── Token.sol # ERC20 token contract
├── scripts/ # Deployment scripts
├── test/ # Smart contract tests
├── backend/ # Express server
│ ├── models/ # MongoDB models
│ ├── routes/ # API routes
│ └── server.js # Main server file
└── frontend/ # React app
├── src/
│ ├── components/ # React components
│ ├── pages/ # Pages
│ └── utils/ # Web3 helper functions
└── public/
- DEX.sol: Main contract that handles token swaps
- Users deposit tokens into liquidity pools
- Contract calculates exchange rates using AMM formula
- Performs the swap and sends tokens back
- Distributes trading fees to liquidity providers
- Stores user transaction history
- Caches pool data and token prices
- Provides REST API for frontend
- Tracks liquidity provider positions
- Connect wallet button (MetaMask)
- Swap interface (select tokens, enter amounts)
- Shows estimated price and fees
- Transaction confirmation and status
- Pool stats dashboard
Hardhat: Easy to learn, great testing tools, perfect for beginners.
MERN Stack: JavaScript everywhere - one language for everything.
Web3.js: Simple library to connect React to blockchain.
MongoDB: Fast and flexible for storing transaction data.
Using a basic constant product formula: x * y = k
- Two tokens in a liquidity pool (like ETH and USDC)
- When you swap, the ratio changes but product stays constant
- Prices are determined by the ratio of tokens in the pool
- Simple to understand and implement
- Slippage Protection: User sets max price change they accept
- Reentrancy Guard: Prevents hacking attacks
- Access Control: Owner-only functions for critical operations
- Input Validation: All user inputs are checked
- Add/remove liquidity from pools
- Swap tokens with slippage protection
- Calculate exchange rates automatically
- Distribute trading fees to liquidity providers
- Store and retrieve transaction history
- Cache token prices and pool data
- Provide statistics and analytics
- Wallet Connection: Connect MetaMask
- Swap Interface: Select tokens and enter amounts
- Price Display: Shows current rates and estimated output
- Transaction Status: Real-time updates
- Pool Management: Add/remove liquidity
- History Dashboard: View past transactions
-
Smart Contract Layer
- Write core DEX logic with swap and liquidity functions
- Test thoroughly with Hardhat
- Deploy to testnet
-
Backend Layer
- Set up Express API server
- Connect to MongoDB for data storage
- Build REST API endpoints
-
Frontend Layer
- Create React UI components
- Integrate Web3.js for blockchain interaction
- Connect to backend API for data
-
Integration & Testing
- Connect all layers together
- Test complete user flows
- Optimize and fix issues
Uses constant product formula: x * y = k
- Pool maintains two tokens (like ETH and USDC)
- Price is determined by the ratio of tokens
- Trading fee: 0.3% per swap
- Liquidity providers earn fees proportional to their share
- Transactions: User swaps and liquidity actions
- Pools: Token reserves and trading volume
- User Stats: Portfolio and earnings tracking
- Support for multiple token pairs
- Price charts and historical data
- Limit orders
- Mobile app version
- Token farming/staking
- Gasless transactions (meta-transactions)
##Contract details:
contract address: 0x6d1Ff0C5A7fbcf65A85016CF5F1d7eCC21261193
MIT License