Giter Club home page Giter Club logo

enhanced-cpamm's Introduction

Enhanced Constant Product Automated Market Maker (CPAMM)

License: MIT

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.

Table of Contents

Features

  • 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

Prerequisites

  • Node.js (v12.0.0 or later)
  • npm (v6.0.0 or later)
  • Hardhat
  • OpenZeppelin Contracts

Installation

  1. Clone the repository:

    git clone https://github.com/denizumutdereli/enhanced-cpamm.git
    cd enhanced-cpamm
    
  2. Install dependencies:

    npm install
    
  3. Compile the contracts:

    npx hardhat compile
    

Usage

  1. Deploy the contract:

    npx hardhat run scripts/deploy.js --network <your-network>
    
  2. Interact with the contract using Hardhat console or by writing custom scripts.

Contract Functions

Core Functions

  • swap: Exchange tokens with slippage protection
  • addLiquidity: Provide liquidity to the pool
  • removeLiquidity: Remove liquidity from the pool

Administrative Functions

  • setFeeRecipient: Set the address to receive collected fees
  • collectFees: Transfer accumulated fees to the fee recipient
  • pause: Pause contract operations (owner only)
  • unpause: Resume contract operations (owner only)
  • rescueToken: Recover accidentally sent tokens (owner only)

View Functions

  • getTotalReserves: Get current reserves of both tokens
  • getTokenPair: Retrieve addresses of the token pair
  • getPrice: Get current exchange rate between the token pair

Mathematical Foundations

The CPAMM is based on the constant product formula and related mathematical concepts. Here are the key formulas used in the implementation:

  1. Constant Product Formula:

    xy = k
    

    Where x and y are the reserves of the two tokens, and k is a constant.

  2. Swap Formula:

    ydx / (x + dx) = dy
    

    This formula determines the amount of output tokens (dy) for a given input (dx), maintaining the constant product.

  3. 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.

  4. 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.

Security Considerations

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

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

Deniz Umut Dereli - GitHub

Project Link: https://github.com/denizumutdereli/enhanced-cpamm

enhanced-cpamm's People

Contributors

denizumutdereli avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.