A decentralized automated market maker (AMM) protocol implemented with Solidity.
MyAMM enables users to provide liquidity for a token pair and swap tokens through an automated pricing mechanism based on the constant product formula:
x * y = k
The protocol allows liquidity providers to earn trading fees while maintaining decentralized token exchange without relying on order books.
Users can provide two different ERC20 tokens to create liquidity.
Example:
Token0 + Token1
The contract maintains reserves:
token0Reserve
token1Reserve
and uses these reserves to calculate swap prices.
Liquidity providers can deposit both tokens into the pool:
User
|
| deposit Token0 + Token1
↓
MyAMM Contract
The protocol calculates liquidity shares based on the user's contribution.
LP shares are represented internally by:
mapping(address => uint256) balances;Liquidity providers can withdraw their proportional share of the pool.
Example:
User LP Share = 10%
Pool:
1000 Token0
1000 Token1
Withdraw:
100 Token0
100 Token1
The contract burns the user's liquidity share and transfers the corresponding tokens.
Users can exchange Token0 and Token1 directly through the pool.
Example:
User:
Token0
↓
MyAMM
↓
Token1
The swap price is calculated using the constant product formula:
(x + Δx) * (y - Δy) = k
MyAMM follows the constant product AMM model.
The pool maintains:
Token0 Reserve * Token1 Reserve = k
When a swap happens:
- One token reserve increases
- Another token reserve decreases
- The invariant remains approximately constant
The swap mechanism includes a 0.3% trading fee.
Example:
Input Amount:
1000 Token0
Fee:
0.3%
Effective Swap Amount:
997 Token0
The fee remains inside the liquidity pool and benefits liquidity providers.
MyAMM
├── Token0 ERC20
│
├── Token1 ERC20
│
├── Liquidity Management
│
│ ├── addFlow()
│ └── subFlow()
│
├── Swap Engine
│
│ └── swap()
│
└── LP Share Accounting
- Solidity ^0.8.13
- Foundry
- ERC20 Token Standard
- EVM Compatible Blockchain
function addFlow(
uint256 token0Num,
uint256 token1Num
)Users provide liquidity and receive LP shares.
function subFlow(
uint256 liquRequest
)Users burn LP shares and withdraw proportional tokens.
function swap(
tokenType token_type,
uint256 amount
)Swap Token0 and Token1 using AMM pricing.
The contract emits events for important operations:
event AddFlow(...)Records:
- Provider address
- Token amounts
- Minted liquidity shares
event SubFlow(...)Records:
- Withdrawer address
- Returned token amounts
event Swap(...)Records:
- Trader address
- Received token amount
- Paid token amount
Implemented:
State updates are performed before external token transfers where applicable.
External token transfers are checked:
require(success, "transfer failed");Token addresses are stored as immutable:
address public immutable token0Address;
address public immutable token1Address;This prevents modification after deployment.
Run tests:
forge testRecommended test coverage:
- Add liquidity
- Remove liquidity
- Swap Token0 → Token1
- Swap Token1 → Token0
- Multiple liquidity providers
- Insufficient liquidity
- Incorrect token amounts
This implementation is simplified and does not include some production-level features.
Future improvements:
- LP ERC20 token implementation
- Slippage protection
- Minimum output amount parameter
- Deadline protection
- ReentrancyGuard
- Flash swap support
- Better precision handling
- Full Uniswap V2 style invariant checking
This project demonstrates:
- AMM mechanism design
- Constant product formula
- Liquidity provider accounting
- ERC20 interaction
- Smart contract state management
- DeFi protocol architecture
Solidity Developer focusing on:
- EVM
- DeFi Protocol Development
- Smart Contract Security
- Foundry Testing