This AMM DEX Solidity Smart Contract Project uses "OpenZeppelin's" ERC20 standard for tokens and is built on Foundry development framework.
This project is an implementation of a Automated Market Maker (AMM) which is a type of Decentralized Exchange (DEX) protocol. Instead of using a traditional order book to match buyers and sellers, AMMs use smart contracts and liquidity pools to facilitate token swaps automatically. Uniswap is a popular example.
Key aspects of AMM -
- Liquidity Pools: Users (liquidity providers) deposit assets into pools, which are then used for trades.
- Algorithmic Pricing: Prices are determined by a mathematical formula (e.g., (x * y = k) rather than an order book.
- Permissionless Trading: Users trade directly from their wallets without intermediaries.
- 24/7 Availability: Automated, trustless trading.
The "Tokens.sol" contract is a really simple ERC-20 Token that will be used to create the trading pool on our exchange. The "Exchange.sol" i.e the AMM smart contract allows swapping between ETH (sepolia) <-> TKN. It charges 1% fee on swaps, when users add liquidity they get lpETH (liquidity provider token) which represents their share of the pool. The liquidity providers are able to burn their lpETH to get back their ETH & TKN.
Tokens -
constructor()
- mints 1,000,000 * 10**decimals() TKN to msg.sender and sets token name/symbol.
Exchange -
constructor(address _tokenAddr)
- sets the AMM’s token address (i_tknAddress) and token name/symbol for LP tokens.
getTknReserve() public view returns (uint256 reserve)
- returns the contract’s current TKN balance (TKN reserve).
addLiquidity(uint256 _tknAmount) public payable returns (uint256)
- user supplies ETH (via msg.value) and TKN; mints LP tokens proportionally. If pool empty, accepts any initial amounts and mints LP = msg.value. Enforces correct token/ETH ratio otherwise.
getLiquidity(uint256 _lpTokens) public returns (uint256, uint256)
- burns _lpTokens from caller and returns corresponding ETH and TKN amounts (pro rata of reserves).
ethTotknSwap(uint256 minTknToReceive) public payable
- swaps incoming ETH for TKN using AMM formula; reverts if received TKN < minTknToReceive.
tknToEthSwap(uint256 tknToSwap, uint256 minEthToReceive) public
- swaps tknToSwap TKN for ETH using AMM formula; transfers TKN from caller, sends ETH to caller, reverts if ETH < minEthToReceive.
Interact with AMM DEX deployed on Sepolia through Etherscan
Exchange - https://sepolia.etherscan.io/address/0xD3AE3E3abb7BFad6745B63b319dd9e52B5570eC4#code
Tokens - https://sepolia.etherscan.io/address/0xaE9310662D7bcaE4b73DFD041292dcdd28AC3A75#code