🧩 Project Overview
A complete educational DEX stack demonstrating core decentralized finance principles.
This Automated Market Maker (AMM) implements the Uniswap v1 constant-product model (x × y = k)
with ERC-20 token pairs, liquidity provisioning, and on-chain price discovery.
It includes a full-stack workflow using Solidity, Hardhat, and Ethers.js on the backend —
paired with a React + Redux frontend for live swap and liquidity management.
Designed for clarity, modularity, and hands-on blockchain developer training.
A minimalist Automated Market Maker (AMM) implementation modeled after Uniswap v1 — built end-to-end with Solidity, Hardhat, Ethers.js, and a React + Redux frontend.
This repository demonstrates how decentralized liquidity pools, swaps, and LP share mechanics work under the hood, complete with a deployable test environment and modular scripts for both backend and UI layers.
- Constant product AMM (
x × y = k) — live pricing & liquidity mechanics - ERC-20 pair (DAPP + USD) — full swap, deposit, and withdrawal flows
- Hardhat testing + deploy scripts — local & Sepolia-ready
- React + Redux frontend — wallet connection, balance tracking, live charts
- Read-only analytics script (
poolInfo.ts) — fetches reserves and fees without a wallet
This project was extended beyond the original tutorial to better reflect real-world AMM behavior and development practices:
- Removed stored invariant (
K) and compute it dynamically during swaps to reduce redundant state - Implemented a 0.3% swap fee (Uniswap-style), retained in the pool to benefit liquidity providers
- Refactored swap calculations for both token directions
- Updated test suite to handle precision, rounding, and dynamic outputs
- Introduced proper Git workflow (feature branching, pull requests, version tagging)
These changes improve correctness, reduce state redundancy, and align the AMM more closely with production-grade DeFi protocols.
🧩 Educational project designed for full-stack blockchain dev training. Built to be cloned, extended, or integrated into larger DeFi prototypes.
- How AMMs work using the constant product formula:
x * y = k - How to write and test smart contracts in Solidity
- How to deploy contracts using Hardhat
- How to connect a React frontend to smart contracts using Ethers.js
- How to manage blockchain state using Redux
- ERC-20 token pair (DAPP + USD)
- Liquidity provision and LP share management
- Token swaps with price adjustment
- Share-based withdrawals
- Slippage protection
- Connect wallet (MetaMask)
- Switch between networks (Sepolia or Localhost)
- Swap, Deposit, Withdraw, and Charts tabs
- Live account + balance tracking
- Identicon display with react-blockies
amm-tutorial/
├── contracts/
│ ├── Token.sol
│ └── AMM.sol
├── test/
│ └── AMM.test.js
├── scripts/
│ ├── deploy.js
│ └── seed.js
├── frontend/
│ ├── src/
│ │ ├── App.js
│ │ ├── config.json
│ │ ├── logo.png
│ │ ├── store/
│ │ │ ├── interactions.js
│ │ │ └── reducers/
│ │ │ ├── provider.js
│ │ │ ├── tokens.js
│ │ │ └── amm.js
│ │ ├── components/
│ │ │ ├── Navigation.jsx
│ │ │ ├── Tabs.jsx
│ │ │ ├── Swap.jsx
│ │ │ ├── Deposit.jsx
│ │ │ ├── Withdraw.jsx
│ │ │ └── Charts.jsx
├── hardhat.config.js
└── README.md
git clone https://github.com/your-username/amm-tutorial.git
cd amm-tutorialnpm install
npx hardhat compilenpx hardhat testnpx hardhat nodenpx hardhat run scripts/deploy.js --network localhostThis populates the pool and simulates some swaps.
npx hardhat run scripts/seed.js --network localhostInvestor accounts will receive DAPP and USD tokens and perform swaps automatically.
cd frontend
npm install
npm start- provider
- connection, chainId, account
- tokens
- contracts, symbols, balances
- amm
- contract, shares, swaps (optional)
All state is synced through store/interactions.js.
Contracts are loaded dynamically using config.json per chain:
{
"31337": {
"dapp": { "address": "0x..." },
"usd": { "address": "0x..." },
"amm": { "address": "0x..." }
},
"11155111": {
"dapp": { "address": "0x..." },
"usd": { "address": "0x..." },
"amm": { "address": "0x..." }
}
}The core logic for token swaps and LP share accounting.
| Function | Description |
|---|---|
addLiquidity() |
Deposit both tokens to earn LP shares |
removeLiquidity() |
Redeem LP shares for tokens |
swapToken1() / swapToken2() |
Perform token swaps |
calculateToken1Swap() / ...Swap() |
Estimate swap output |
calculateToken1Deposit() / ...() |
Maintain deposit ratio |
shares(address) |
Returns LP shares for a user |
totalShares() |
Total LP shares in existence |
- Swap — Emitted after each token swap with full metadata.
MIT — use freely, fork, or build on top.
🙏 Acknowledgments: Based on tutorials from Dapp University, with added full-stack integration and improvements.
#solidity #hardhat #defi #amm #dex
#ethereum #react #redux #vite #ethersjs
#typescript #web3 #fullstack #tutorial #education