In this project, I created my own Token, DEXLiquidity Pool.

Warning: This code is not audited. Use at your own risk.\
A decentralized exchange (DEX) smart contract built on Ethereum using Solidity.
This DEX allows users to:
- Swap ETH ↔ Tokens — Trade ETH for tokens and vice versa
- Add Liquidity — Provide ETH + tokens to the pool and earn LP tokens
- Remove Liquidity — Burn LP tokens to withdraw your share of the pool
The DEX uses an Automated Market Maker (AMM) with the constant product formula:
x × y = k
x = ETH reserve
y = Token reserve
k = constant (stays the same)
| Function | Description |
|---|---|
addLiquidity() |
Add ETH + tokens to the pool, receive LP tokens |
removeLiquidity() |
Burn LP tokens, receive ETH + tokens back |
swapEthToToken() |
Swap ETH for tokens |
swapTokenToEth() |
Swap tokens for ETH |
getAmountOfTokens() |
Calculate output amount for a swap |
getTokensInContract() |
Get token balance in the pool |
- Node.js (v16+)
- Yarn or npm
# Install dependencies
yarn install
# or
npm installCompile the contract:
npx thirdweb buildDeploy the contract to any EVM chain:
npx thirdweb deployThis will open a browser where you can select the network and configure deployment.
- Go to thirdweb.com/explore
- Search for "Token" or "TokenERC20"
- Click Deploy Now
- Fill in the details:
- Name: Your token name (e.g., "67")
- Symbol: Token symbol (e.g., "67")
- Admin: Your wallet address
- Select your network (e.g., Ethereum, zkSync)
- Click Deploy and confirm in MetaMask
- Save your token contract address! (e.g.,
0x1234...abcd)
- Go to your token contract on thirdweb dashboard
- Click "Tokens" in the sidebar
- Click "+ Mint"
- Enter the amount (e.g.,
1000000for 1 million tokens) - Click Mint and confirm in MetaMask
- You now have tokens in your wallet!
- In your project folder, run:
npx thirdweb build
npx thirdweb deploy-
A browser window opens. Fill in the constructor parameters:
- _token: Your token contract address from Step 1 (e.g.,
0x1234...abcd) - _defaultAdmin: Your wallet address
- _name: LP token name (e.g., "67 DEX LP")
- _symbol: LP token symbol (e.g., "67LP")
- _token: Your token contract address from Step 1 (e.g.,
-
Select your network (same as your token!)
-
Click Deploy and confirm in MetaMask
-
Save your DEX contract address! (e.g.,
0xDEX...5678)
Before adding liquidity, you must allow the DEX to transfer your tokens.
- Go to your TOKEN contract on thirdweb dashboard
- Click "Write" tab
- Find
approvefunction - Enter:
- spender: Your DEX contract address (from Step 3)
- amount:
1000000000000000000000000(1 million tokens with 18 decimals)
- Click Execute and confirm in MetaMask
Now add your tokens + ETH to create the liquidity pool.
- Go to your DEX contract on thirdweb dashboard
- Click "Write" tab
- Find
addLiquidityfunction - Enter:
- Amount: Number of tokens (with 18 decimals)
- Example:
100000000000000000000000= 100,000 tokens
- Example:
- Native Token Value: ETH to add
- Example:
0.1= 0.1 ETH
- Example:
- Amount: Number of tokens (with 18 decimals)
- Click Execute and confirm in MetaMask
- Go to your DEX contract → "Read" tab
- Call
balanceOfwith your wallet address- Should show your LP tokens (not 0!)
- Call
getTokensInContract- Should show tokens in the pool
- Check the contract's ETH balance on block explorer
Step 1: Create token on thirdweb → Get TOKEN_ADDRESS
Step 2: Mint tokens to your wallet → You have tokens
Step 3: Deploy DEX with npx thirdweb deploy → Get DEX_ADDRESS
Step 4: Approve DEX to spend tokens → TOKEN.approve(DEX_ADDRESS, amount)
Step 5: Add liquidity → DEX.addLiquidity(tokens) + send ETH
Step 6: Verify LP balance > 0 → Success!
| Problem | Solution |
|---|---|
| LP balance is 0 | You didn't send ETH (Native Token Value was 0) |
| Transaction failed | You didn't approve the DEX first |
| Wrong network | Make sure token and DEX are on the same network |
| Not enough gas | Get more ETH for gas fees |
dexcontract/
├── contracts/
│ └── Contract.sol # Main DEX contract
├── scripts/
│ └── verify/ # Verification scripts
├── hardhat.config.js # Hardhat configuration
└── package.json
When you add liquidity, you receive LP (Liquidity Provider) tokens. These represent your share of the pool.
Larger trades have more price impact. The AMM formula ensures prices adjust based on supply and demand.
The pool holds both ETH and tokens. The ratio between them determines the price.