A Constant Sum Automated Market Maker (AMM) is a decentralized exchange mechanism that maintains liquidity using a constant sum formula, differing from the more prevalent constant product models like Uniswap. The defining equation for a constant sum AMM is:
x + y = k
Here, x
and y
represent the quantities of two different tokens in the liquidity pool, and k
is a constant representing the total combined quantity of these tokens. This linear relationship ensures that the sum of the token reserves remains unchanged during trades.
-
Linear Pricing: The price remains constant for any trade, unlike constant product AMMs where the price varies based on liquidity depth. This results in zero slippage for trades.
-
No Impermanent Loss: Since the price doesn't change with trades, liquidity providers (LPs) are not exposed to impermanent loss, which is common in other AMM models.
-
Optimal for Stable Assets: This model is particularly suitable for trading assets that are meant to maintain a stable value relative to each other, such as stablecoins. However, it is less effective when reserves are depleted, as it can lead to arbitrage opportunities that may drain the pool.
-
Zero Slippage: Large trades do not impact the price, making it capital efficient in low-volatility environments.
This repository provides tools and scripts to:
-
Set Up a Local Hardhat Blockchain: Initialize a local Ethereum-like development environment using Hardhat.
-
Deploy Contracts: Deploy a Constant Sum AMM contract along with two ERC20 token contracts to the local blockchain.
-
Interact with the AMM: Utilize a client interface to interact with the deployed AMM contract, facilitating operations such as adding liquidity, swapping tokens, and removing liquidity.
-
Connect via MetaMask: Configure and use the MetaMask wallet to interact with the local blockchain and deployed contracts.
Follow these steps to set up and run the project:
-
Install Dependencies:
yarn install
-
Compile Contracts:
yarn hardhat compile
To execute the test suite and ensure all components are functioning correctly:
yarn hardhat test
-
Start the Hardhat Node:
yarn hardhat node
-
Deploy Contracts:
yarn deploy_local
-
Configure Client:
- Copy the
deployed_addresses.json
file from./ignition/deployments/<network>/
to./client/src
.
- Copy the
-
Fund Accounts:
yarn fund
-
Approve Allowance for AMM:
yarn allowance
-
Add Initial Liquidity:
yarn add_liquidity
-
Set Up Client:
-
Navigate to the client directory:
cd client
-
Install client dependencies:
yarn
-
Return to the root directory:
cd ..
-
-
Start Client:
yarn start_client
Once the client is running:
-
Access the Interface: Open the provided local URL in your web browser.
-
Connect MetaMask: Ensure your MetaMask wallet is connected to the local Hardhat network.
-
Perform Actions: Use the interface to add or remove liquidity, swap tokens, and observe the constant sum behavior of the AMM.
-
MetaMask Configuration: You may need to manually add the local Hardhat network to MetaMask and import the deployed token contracts using their addresses from the
deployed_addresses.json
file. -
Hardhat Accounts: The Hardhat node provides predefined accounts with known private keys, which can be used for testing purposes.
-
Further Reading: For a deeper understanding of constant sum AMMs and their mathematical foundations, refer to The AMM Book.