Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Miniswap — Uniswap V2 AMM DEX

A from-scratch implementation of Uniswap V2 core mechanics, built for learning and demonstration purposes.

Overview

This project re-implements the core Uniswap V2 contracts without relying on the original codebase, demonstrating deep understanding of AMM design, EVM storage optimization, and DeFi security patterns.

Contracts

Contract Description
MiniswapERC20 LP token base contract (ERC20 from scratch)
MiniswapFactory Pair registry and creation
MiniswapPair Core AMM — mint / burn / swap / TWAP oracle
MiniswapRouter User-facing router with multi-hop routing

Key Concepts Implemented

  • Constant product formula x * y = k
  • Storage slot packingreserve0 / reserve1 / blockTimestampLast packed into a single 32-byte slot (saves 2/3 SLOAD cost)
  • TWAP price oracle — UQ112x112 fixed-point cumulative price
  • Reentrancy guard — uint256 lock pattern
  • Flash loans — optimistic transfer pattern in swap()
  • 0.3% swap fee — verified via adjusted k check
  • Safe transfer — low-level call to handle non-standard ERC20s

Test Coverage

forge test -vv
Ran 6 tests
[PASS] test_CreatePair
[PASS] test_AddLiquidity
[PASS] test_Swap              (amountIn: 1000 TKA → amountOut: 987 TKB)
[PASS] test_RemoveLiquidity
[PASS] test_ReentrancyProtection
[PASS] testFuzz_GetAmountOut  (256 runs)

Getting Started

# Install Foundry
curl -L https://foundry.paradigm.xyz | bash && foundryup

# Clone and test
git clone https://github.com/tusugit/miniswap.git
cd miniswap
forge test -vv

Architecture

User
 │
 ▼
MiniswapRouter   ← single entry point, calculates amounts, routes swaps
 │
 ├── MiniswapFactory  ← deploys and tracks Pair contracts
 │
 └── MiniswapPair     ← holds liquidity, executes swaps, mints LP tokens
          │
          └── MiniswapERC20  ← LP token (inherited)

References

About

Uniswap V2 AMM DEX implementation from scratch — Solidity, Foundry

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages