This is a Decentralized Exchange (DEX) capable of doing the following:
- Users can deposit tokens (like Token A and Token B)
- Users can create buy/sell orders (e.g., "I want to buy 10 Token A for 2 Token B each")
- The system matches compatible orders and executes trades automatically
- Everything is stored on the blockchain (transparent and secure)
decentralized-exchange/
├── contracts/ # Solidity smart contracts
│ ├── AssetToken.sol # ERC-20 token contract
│ └── DEX.sol # Main DEX contract
├── frontend/ # React + Vite frontend
│ ├── src/
│ │ ├── hooks/
│ │ │ └── useDEX.js # Custom hook for DEX interactions
│ │ ├── utils/
│ │ │ └── contractConfig.js # Contract addresses and ABIs
│ │ └── App.jsx # Main React component
│ └── package.json
├── scripts/
│ └── deploy.js # Deployment script
└── hardhat.config.cjs # Hardhat configuration
1. AssetToken.sol - Your Cryptocurrency Token
- This creates a basic token (like Ethereum's ERC-20 standard)
- You can mint tokens, transfer them, etc.
- Think of it as "coins" people can trade
2. DEX.sol - The Exchange Logic
- This is the brain of your exchange
- Handles deposits, withdrawals, order creation, and order matching
- Stores all orders in an "order book" (like a stock exchange board)
Key Functions:
deposit()- Put tokens into the exchangewithdraw()- Take tokens outcreateBuyOrder()- "I want to buy X tokens"createSellOrder()- "I want to sell X tokens"matchOrders()- Automatically matches buy/sell orders
What it does:
- Beautiful web interface to interact with your smart contracts
- Users can connect their MetaMask wallet
- View balances, create orders, see the order book
- Everything happens through a web browser
Key Files:
App.jsx- Main UI componenthooks/useDEX.js- Connects frontend to blockchainutils/contractConfig.js- Contract addresses and ABIs (the "interface")
deploy.js - Deploys your contracts to the blockchain
- Creates Token A and Token B
- Deploys the DEX contract
- Gives you addresses to use in the frontend
hardhat.config.cjs - Configures Hardhat (development tool)
- Sets Solidity version
- Configures networks (local testnet, Sepolia testnet, etc.)
- Most important config file!
User Opens Browser
↓
Connects MetaMask Wallet
↓
Frontend (React) loads contract addresses
↓
User clicks "Deposit" or "Create Order"
↓
Frontend calls smart contract function via ethers.js
↓
Smart contract executes on blockchain
↓
Transaction confirmed → Frontend updates UI
Example Flow:
- User wants to buy 10 Token A for 2 Token B each
- User fills form in frontend → clicks "Create Buy Order"
- Frontend sends transaction to
DEX.sol→createBuyOrder() - Smart contract checks: Does user have enough Token B? (they need 20 Token B)
- If yes: Creates order, deducts 20 Token B from user's balance
- Order appears in order book
- Another user can create a matching sell order
matchOrders()function executes the trade automatically
# in root
npm install
# cd to the frontend folder
cd frontend
npm install
cd ..Open Terminal 1:
# in root
npx hardhat nodeThis creates the local Ethereum network
Open Terminal 2 (new terminal window):
# in root
npx hardhat run scripts/deploy.js --network localhostCopy the contract addresses it gives you (Token A, Token B, DEX address)
Update frontend/src/utils/contractConfig.js:
- Replace
DEX_CONTRACT_ADDRESSwith the address from Step 4
Update frontend/src/App.jsx:
- Replace the token addresses in the
useEffectwith addresses from Step 4
-
Open MetaMask browser extension
-
Click network dropdown → "Add Network" → "Add a network manually"
-
Fill in:
- Network Name: Hardhat Local
- RPC URL: http://127.0.0.1:8545
- Chain ID: 31337
- Currency Symbol: ETH
-
Click "Save"
-
Import a test account:
- Click your account icon → “Import Account”
- Import Address 1 or Address 2 from your
npx hardhat nodeoutput - These are the two funded accounts used by the deploy script for Token A and Token B
- You may import one or both of them into MetaMask to test deposits, orders, and trades
Open Terminal 3:
cd frontend
npm run dev- Open browser to localhost
- Click "Connect Wallet" in MetaMask
- You should see your balances
- Try depositing some tokens
- Create a buy or sell order