
Automated Market Maker
2.1 Automated Market Maker
Automated Market Makers (AMMs) play a pivotal role in enabling liquidity provision and trading in the decentralized finance (DeFi) landscape. A key concept in this domain is the Constant Product AMM, which underpins popular decentralized exchanges like Uniswap, SushiSwap and DEX Raiden.
We’ll delve into the Constant Product Automated Market Maker, unravelling the mathematics behind adding and removing liquidity.
What is the Constant Product Formula?
A Constant Product AMM is an AMM where the price of the tokens are determined by the equation:
How the Constant Product AMM works?
When we perform a trade on the AMM, the value for X and Y token will vary but the value for K will remain constant.
For example, we are going to sell Token A and buy Token B. So we can write it as:
Before trade, we have the equation: XY = K
And after the trade, we will get this modified equation:
X is the amount of token that was in the AMM before the trade and since we are selling Token A, we added dX.
Similarly, Y is the amount of token that was in the AMM before the trade and we subtracted dY because we are buying token B.
And K will always remain constant.
You can see, after the trade we had to decrease the value of Y to Y-dY and increased X to X+dX to ensure the value of K remains the same.
Working of the Formula:
Constant Product AMM Smart Contract Explanation.
Swap Function: Calculating dY for dX?
This math is used to calculate the output amount (dy) for a given input amount (dx).
The constant product formula is xy=k. When a user swaps dX amount of one token, the reserve change, and the product must still equal k.
Rearranging the formula, we find dY, the amount of the other token that should be given to the user.
The final formula YdX / (X + dX) gives the output amount considering the input amount and the current reserves.
Corresponding Code:
Add Liquidity Function: Balancing dX and dY?
Adding liquidity requires maintaining the price ratio, expressed as:
This math ensures that the ratio of the token amounts (dX and dY) added to the pool maintains the current price ratio.
The goal is to prevent the addition of liquidity from affecting the relative prices of the tokens.
The final part dY = Y / X * dX gives the required amount of dY for a given dX to maintain the price ratio.
Share Calculation in Add Liquidity: Minting new shares
The share calculation is pivotal for fair distribution:
Total shares should increase proportional to the increase in liquidity
This math calculates the number of shares to mint for the liquidity provided.
The value of liquidity is defined as the square root of the product of the reserves (**√**XY).
The increase in total shares should be proportional to the increase in liquidity.
The formula (L1 - L0) * T / L0 = s calculates the new shares to be minted based on the change in liquidity.
Corresponding Code:
Remove Liquidity Function: Proportional Distribution
Removing liquidity involves returning a proportional amount of each token:
Proof
Let’s find dX, dY such that
where
This math is for calculating the amounts of each token (dx and dy) to return when removing liquidity.
The goal is to ensure that the amount of liquidity removed is proportional to the number of shares (s) surrendered.
The final formulas dx = s / T * x and dy = s / T * y give the amounts of each token to return based on the user's share of the total supply.
Corresponding Code:

Last updated