A decentralized exchange (DEX) implementation built with Solidity and Foundry, featuring an automated market maker (AMM) protocol with TWAP oracle integration.
This project implements a production-ready AMM protocol inspired by Uniswap V2, providing trustless token swapping and liquidity provision on the Ethereum blockchain.
- Constant product formula (x * y = k) for price discovery
- Deterministic pricing based on liquidity pool reserves
- Slippage protection with minimum output guarantees
- Deterministic pair creation using CREATE2
- One pair per token combination
- Permissionless pair deployment
- Add liquidity to trading pairs
- Remove liquidity with LP tokens
- Optimal liquidity calculation
- Minimum liquidity lock for initial providers
- Exact input token swaps
- Multi-hop routing support
- Deadline protection for time-sensitive transactions
- Path-based routing for optimal trade execution
- Time-Weighted Average Price calculations
- Manipulation-resistant price feeds
- Cumulative price tracking
- Configurable observation periods
- Reentrancy protection
- Deadline checks for transaction expiration
- Safe math operations with Solmate
- Integer overflow protection
- Solidity ^0.8.0 - Smart contract development
- Foundry - Development framework and testing
- Solmate - Gas-optimized contract libraries
- OpenZeppelin - Security standards
# Clone the repository
git clone <repository-url>
cd swap
# Install dependencies
forge install
# Build contracts
forge build# Run all tests
forge test
# Run tests with verbosity
forge test -vvv
# Run specific test file
forge test --match-path test/Swap.t.sol
# Run fuzz tests
forge test --match-path test/SwapFuzz.t.sol
# Run with gas reporting
forge test --gas-report# Deploy to local network
forge script script/Deploy.s.sol --fork-url http://localhost:8545 --broadcast
# Deploy to testnet
forge script script/Deploy.s.sol --rpc-url <RPC_URL> --private-key <PRIVATE_KEY> --broadcast --verify