A robust and feature-rich implementation of a Constant Product Automated Market Maker (CPAMM) smart contract built on Ethereum. This contract provides a decentralized exchange mechanism for ERC20 token pairs with enhanced security features, upgradability, and optimized gas usage.
- Constant Product AMM implementation
- Upgradeable contract architecture
- Slippage protection for swaps and liquidity operations
- Basic price oracle functionality
- Flash loan attack mitigation
- Fee accrual and collection mechanism
- Emergency pause functionality
- Token rescue capability for contract owner
- Comprehensive event logging
- Gas-optimized operations
- Node.js (v12.0.0 or later)
- npm (v6.0.0 or later)
- Hardhat
- OpenZeppelin Contracts
-
Clone the repository:
git clone https://github.com/denizumutdereli/enhanced-cpamm.git cd enhanced-cpamm
-
Install dependencies:
npm install
-
Compile the contracts:
npx hardhat compile
-
Deploy the contract:
npx hardhat run scripts/deploy.js --network <your-network>
-
Interact with the contract using Hardhat console or by writing custom scripts.
swap
: Exchange tokens with slippage protectionaddLiquidity
: Provide liquidity to the poolremoveLiquidity
: Remove liquidity from the pool
setFeeRecipient
: Set the address to receive collected feescollectFees
: Transfer accumulated fees to the fee recipientpause
: Pause contract operations (owner only)unpause
: Resume contract operations (owner only)rescueToken
: Recover accidentally sent tokens (owner only)
getTotalReserves
: Get current reserves of both tokensgetTokenPair
: Retrieve addresses of the token pairgetPrice
: Get current exchange rate between the token pair
The CPAMM is based on the constant product formula and related mathematical concepts. Here are the key formulas used in the implementation:
-
Constant Product Formula:
xy = k
Where x and y are the reserves of the two tokens, and k is a constant.
-
Swap Formula:
ydx / (x + dx) = dy
This formula determines the amount of output tokens (dy) for a given input (dx), maintaining the constant product.
-
Liquidity Value:
f(x,y) = sqrt(xy)
This represents the geometric mean of x and y, used to calculate the value of liquidity in the pool.
-
Liquidity Shares:
s = dx / x * T = dy / y * T
Where s is the number of shares minted or burned, dx and dy are the amounts of tokens added or removed, x and y are the current reserves, and T is the total supply of liquidity tokens.
These formulas form the mathematical foundation of the CPAMM, ensuring proper token exchange rates, liquidity provision, and removal while maintaining the constant product invariant.
Key security features include:
- ReentrancyGuard for all state-changing functions
- Slippage protection for trades and liquidity operations
- Pausable functionality for emergency situations
- Two-step process for critical operations to mitigate flash loan attacks
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.
Deniz Umut Dereli - GitHub
Project Link: https://github.com/denizumutdereli/enhanced-cpamm