Skip to content

A Solidity-based AMM for ETH/USDC swaps. Users can swap tokens, provide liquidity, and earn fees. Fully tested with Hardhat.

Notifications You must be signed in to change notification settings

simob75/simple-amm-v1

Repository files navigation

🔄 Simple AMM (Automated Market Maker) - V1

A Solidity-based constant product AMM (like Uniswap V2) that allows ETH/USDC swaps, liquidity provision, and fee distribution.

Hardhat Tests
Solidity
DeFi


🚀 Features

✔️ Swap ETH for USDC & USDC for ETH
✔️ Provide liquidity & earn trading fees
✔️ LPs can claim accumulated fees
✔️ Fully tested with Hardhat
✔️ Deployable on local & testnet networks


🛠️ Tech Stack

  • Solidity 0.8.20 (Smart Contracts)
  • Hardhat (Testing & Deployment)
  • Chai & Mocha (Unit Tests)
  • OpenZeppelin (ERC20 standard)

📂 Project Structure

📦 simple-amm-v1 ┣ 📂 contracts ┃ ┣ 📜 AMMSimulation.sol → (AMM logic) ┃ ┣ 📜 MockERC20.sol → (Mock USDC for testing) ┣ 📂 test ┃ ┣ 📜 AMMSimulation.test.js → (Full test coverage) ┣ 📂 scripts ┃ ┣ 📜 deploy.js → (Hardhat deploy script) ┣ 📜 README.md → (This file) ┣ 📜 hardhat.config.js → (Hardhat setup) ┣ 📜 package.json → (Dependencies) ┣ 📜 .gitignore → (Ignored files)


🛠️ Getting Started

1️⃣ Clone the Repository

git clone https://github.com/YOUR_GITHUB/simple-amm-v1.git
cd simple-amm-v1
2️⃣ Install Dependencies
npm install
3️⃣ Run Tests
npx hardhat test
📌 Expected Output:

  AMMSimulation
    ✔ Should deploy with correct initial reserves
    ✔ Should allow a user to add liquidity
    ✔ Should allow a user to remove liquidity
    ✔ Should swap ETH for USDC and adjust reserves
    ✔ Should swap USDC for ETH and adjust reserves
    ✔ Should distribute trading fees to LPs
    ✔ Should allow LPs to claim fees

  7 passing (2s)
🚀 Deploying Locally with Hardhat

1️⃣ Start a Local Hardhat Node
npx hardhat node
2️⃣ Deploy the Contracts
npx hardhat run scripts/deploy.js --network localhost
3️⃣ Interact with the Contract via Hardhat Console
npx hardhat console --network localhost
📌 Example Commands
const [owner, addr1] = await ethers.getSigners();
const AMMSimulation = await ethers.getContractFactory("AMMSimulation");
const amm = await AMMSimulation.attach("DEPLOYED_AMM_CONTRACT_ADDRESS");

// Check ETH reserve
(await amm.ethReserve()).toString();

// Add liquidity (2 ETH, 2000 USDC)
await amm.connect(addr1).addLiquidity(
    ethers.parseUnits("2", 18),
    ethers.parseUnits("2000", 18),
    { value: ethers.parseUnits("2", 18) }
);
📜 Smart Contracts
🔄 AMMSimulation.sol (Automated Market Maker)
Implements a constant product formula (x * y = k) for ETH/USDC swaps.

Users can swap tokens using ETH as the base currency.
Liquidity Providers earn 0.3% trading fees.
LPs can remove liquidity and claim fees.
💰 MockERC20.sol (Mock USDC)
A basic ERC20 token used for testing USDC swaps.

Can mint & transfer tokens for local testing.
🔐 Security Considerations
⚠️ This contract is not audited and should be used for educational purposes only.
Potential security risks:

Front-running attacks (no slippage protection).
No external oracles (price manipulation risk).
No flash loan protection (could be vulnerable to price manipulation).
For production, consider using Chainlink Oracles, Uniswap V3-style liquidity, and MEV protection.

---

📌 Future Improvements (V2)
✅ Multi-Token Support (ETH/DAI, ETH/WBTC).
✅ Slippage Protection (Prevent price manipulation).
✅ TWAP Oracle Integration (Resist flash loans).
✅ MEV Protection (Anti-front-running techniques).
✅ Custom Fees Per LP (Uniswap V3-style incentives).

---

👨‍💻 Author
**Bruna Simonian**  
[LinkedIn](https://www.linkedin.com/in/bruna-simonian-779a4818/)

---

🌟 Contributing
Want to improve this AMM?

Fork the repository
Create a feature branch (git checkout -b feature-name)
Commit your changes (git commit -m "Added new feature")
Push to your branch (git push origin feature-name)
Open a Pull Request 🚀
📜 License
This project is licensed under the MIT License.

📌 Feel free to use, modify, and contribute!

About

A Solidity-based AMM for ETH/USDC swaps. Users can swap tokens, provide liquidity, and earn fees. Fully tested with Hardhat.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published