A basic Automated Market Maker (AMM) smart contract implementation with a single pool of two paired ERC20 tokens. The AMM uses the Constant Product Market Maker (CPMM) formula x * y = k
, which is the foundation of protocols like Uniswap.
SimpleCPMM is a minimal implementation of an AMM that demonstrates core DeFi concepts including:
- Liquidity provision and removal
- Token swapping with slippage protection
- Reward distribution for liquidity providers
- Constant Product Market Maker mechanics
- Single pool for two ERC20 tokens
- CPMM-based price determination
- Liquidity provider reward system
- Slippage protection for trades
- Comprehensive test coverage
- Emergency withdrawal capabilities
- Clone the repository
git clone https://github.com/Signor1/simpleCPMM.git
cd simpleCPMM
- Install dependencies
forge install
Run the test suite:
forge test
Run tests with gas reporting:
forge test --gas-report
Run a specific test:
forge test --match-test testFunctionName
Run tests with verbosity:
forge test -vv
The main contract implementing the AMM functionality:
-
Token Management
- Paired ERC20 tokens (TokenA and TokenB)
- Reservoir tracking for both tokens
- Owner-controlled token address setting
-
Liquidity Functions
addLiquidity(uint256 amountA, uint256 amountB)
removeLiquidity()
- Proportional liquidity share calculation
-
Swap Functions
swapAForB(uint256 amountAIn, uint256 minAmountBOut)
swapBForA(uint256 amountBIn, uint256 minAmountAOut)
- Slippage protection
- Price impact consideration
-
Reward System
- Reward distribution for liquidity providers
- Reward claiming mechanism
- Configurable reward rate
The test suite (BasicPoolTest.sol
) includes comprehensive testing of:
- Liquidity provision and removal
- Swap functionality
- Price impact scenarios
- Reward distribution
- Edge cases and extreme scenarios
- Concurrent operations
- Owner functions
The pool uses the constant product formula:
x * y = k
where:
x
is the reserve of TokenAy
is the reserve of TokenBk
is the constant product
Swap amounts are calculated using:
amountOut = (reserveOut * amountIn) / (reserveIn + amountIn)
This formula ensures:
- Larger trades have higher price impact
- Pool maintains constant product after swaps
- Automatic price adjustment based on supply/demand
- Rewards are distributed proportionally to liquidity share
- Reward rate is configurable (default 1%)
- Rewards are tracked per LP and can be claimed as Pool Reward Tokens
- Framework: Foundry
- Solidity Version: ^0.8.20
- OpenZeppelin Contracts:
- ERC20
- SafeERC20
- Ownable
- ReentrancyGuard
- Install Foundry components:
curl -L https://foundry.paradigm.xyz | bash
foundryup
- Compile contracts:
forge build
- Run tests:
forge test
- The contract includes reentrancy protection
- Slippage protection for swaps
- Owner-only functions for critical operations
- Emergency withdrawal capability
- Basic input validation
Note: This is a basic implementation for educational purposes. Additional security measures would be needed for production use.
MIT License
- Fork the repository
- Create a new branch
- Make your changes
- Submit a pull request