Demo: https://drive.google.com/file/d/17s2WLCbmhP7QQ0JMdvCDkJOdwza5ZJFW/view?usp=sharing
Make sure to also look at my solidity repo to deploy the DEX smart contract: https://github.com/wiledw/67dex
A decentralized exchange (DEX) built with Next.js and Thirdweb that enables seamless swapping between Ethereum (ETH) and a custom ERC-20 token. Oniswap implements an Automated Market Maker (AMM) model similar to Uniswap, providing real-time price calculations and secure on-chain swaps.
- 🔄 Token Swapping: Swap between ETH and custom ERC-20 tokens
- 💰 Real-time Pricing: Instant price calculations based on current liquidity reserves
- 🔐 Wallet Integration: Connect with MetaMask or any Web3 wallet via Thirdweb
- 📊 Live Balances: Real-time balance updates for both ETH and tokens
- ⚡ Gas Optimized: Efficient contract calls and minimal re-renders
- 🎨 Modern UI: Clean, responsive interface with smooth user experience
- 🔍 Transaction Transparency: All transactions are verifiable on Etherscan
Oniswap interacts with two deployed smart contracts on Ethereum:
-
Token Contract:
0x2FC834EefF1F7553a491ba223FAfC6597Ccb4628- ERC-20 token with symbol "67"
- Handles token transfers and approvals
-
DEX Contract:
0x5514Dc256AD47Db3f9989a07fFCF79bba0E762B3- Manages liquidity pools
- Executes swaps using AMM formula
- Functions:
swapEthTotoken(),swapTokenToEth(),getAmountOfTokens()
- Framework: Next.js 13
- Blockchain SDK: Thirdweb v5
- Language: TypeScript
- Styling: CSS Modules
- Ethereum Provider: ethers.js v5
- Node.js 16+ and npm/yarn
- A Web3 wallet (MetaMask recommended)
- Thirdweb Client ID (optional but recommended)
- Clone the repository:
git clone <repository-url>
cd Oniswap- Install dependencies:
npm install
# or
yarn install- Create a
.env.localfile in the root directory:
NEXT_PUBLIC_TEMPLATE_CLIENT_ID=your_thirdweb_client_idYou can get a free Thirdweb Client ID from thirdweb.com/dashboard
- Run the development server:
npm run dev
# or
yarn dev- Open http://localhost:3000 in your browser
-
Connect Wallet: Click the "Connect Wallet" button in the top right corner and connect your MetaMask or other Web3 wallet
-
Swap ETH to Token:
- Enter the amount of ETH you want to swap
- The app automatically calculates how many tokens you'll receive
- Click "Swap" and confirm the transaction in your wallet
-
Swap Token to ETH:
- Click the toggle button (↕) to switch swap direction
- Enter the amount of tokens you want to swap
- Click "Swap" - you'll need to approve the transaction first, then confirm the swap
-
View Balances: Your ETH and token balances are displayed at the top of each input field and update automatically after transactions
The app reads from the token contract using Thirdweb hooks:
useReadContractto fetch token metadata (symbol(),decimals())useWalletBalanceto display user token balancesapprove()function call before token-to-ETH swaps to grant spending allowance
The DEX contract manages liquidity and executes swaps:
- Reads ETH and token reserves to calculate exchange rates
- Calls
getAmountOfTokens()with current reserves for real-time pricing - Executes swaps:
swapEthTotoken()for ETH→Token orswapTokenToEth()for Token→ETH - Contract balances refresh every 10 seconds to maintain accurate pricing
- Reading Data: Uses Thirdweb hooks (
useReadContract,useWalletBalance) for view calls (no gas required) - Writing Data: Uses
useSendTransactionwithprepareContractCallto format and send transactions - Wallet Connection: Thirdweb's
ConnectButtonintegrates with Web3 providers - Balance Fetching: Uses ethers.js directly to bypass RPC limitations when needed
Oniswap/
├── components/
│ ├── Navbar.tsx # Navigation bar with wallet connection
│ ├── SwapInput.tsx # Input component for swap amounts
│ └── Toast.tsx # Notification component
├── pages/
│ ├── _app.tsx # App wrapper with ThirdwebProvider
│ └── index.tsx # Main swap interface
├── styles/
│ ├── globals.css # Global styles
│ ├── Home.module.css # Main page styles
│ ├── Navbar.module.css # Navbar styles
│ └── Toast.module.css # Toast notification styles
└── public/
└── images/ # Token logos and assets
npm run dev- Start development servernpm run build- Build for productionnpm run start- Start production servernpm run lint- Run ESLintnpm run deploy- Build and deploy to IPFS via Thirdweb
The app uses the AMM formula to calculate swap amounts:
outputAmount = (inputAmount * outputReserve) / (inputReserve + inputAmount)
This calculation happens instantly as you type, using the current reserves from the DEX contract.
The app properly handles token decimals (defaults to 18) by converting between human-readable amounts and blockchain units (wei for ETH, smallest unit for tokens).
- User enters swap amount
- App calculates output using
getAmountOfTokens() - User clicks "Swap"
- For token swaps:
approve()transaction first - Swap transaction sent via
useSendTransaction - User confirms in wallet
- Transaction processed on blockchain
- UI updates automatically
| Variable | Description | Required |
|---|---|---|
NEXT_PUBLIC_TEMPLATE_CLIENT_ID |
Thirdweb Client ID | Optional (recommended) |
- Wallet not connecting: Make sure MetaMask or another Web3 wallet is installed
- Transactions failing: Ensure you have sufficient ETH for gas fees
- Balance not updating: Refresh the page or wait a few seconds for automatic updates
- Price calculation errors: Check that the DEX contract has sufficient liquidity
- Thirdweb React Documentation
- Thirdweb TypeScript Documentation
- Next.js Documentation
- Ethereum Documentation
See LICENSE.md for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Built with ❤️ using Next.js and Thirdweb