Page cover

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:

XY = K

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:

dX = Amount of Token A in

dY = Amoun of Token B out

Before trade, we have the equation: XY = K

And after the trade, we will get this modified equation:

( X + dX )( Y - dY ) = K

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.

This is how the trade would look like through graph representation.

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:

  • When liquidity is first added to the pool, the initial quantities of Token A (x) and Token B (y) are set such that their product equals k. So, if initially, there are 100 units of Token A and 50 units of Token B then: k = 100 * 50 = 5000

  • When trade occurs, say someone wants to buy Token B using Token A, they add a certain amount of Token A to the pool. This increases the quantity of Token A in the pool (x increases).

  • To maintain the equation x*y=k, the quantity of Token B in the pool (y) must decrease. The new values of x and y are recalculated to ensure the product remains equal to k.

  • The ratio of how much y decreases in response to the increase in x determines the effective exchange rate for the trade.

  • Suppose the pool starts with x = 100 units of Token A and y = 50 units of Token B, so k = 5000.

  • A user wants to buy 10 units of Token B. To maintain the constant product, the pool calculates how much Token A needs to be added.

  • Let’s say the pool calculates that it requires an addition of 25 units of Token A to maintain k. The new state of the pool will be x = 125 units of Token A and y = 40 units of Token B (since 125 * 40 = 5000).

  • The larger the trade relative to the pool size, the more significant the impact on the price. This is because a larger trade requires a more substantial change in x or y to keep k constant.

  • This effect leads to price slippage, especially in pools with smaller liquidity.

Constant Product AMM Smart Contract Explanation.

Swap Function: Calculating dY for dX?

Corresponding Code:

uint256 amountInWithFee = (_amountIn * 997) / 1000;

amountOut = (reserveOut * amountInWithFee) / (reserveIn + amountInWithFee);

Add Liquidity Function: Balancing dX and dY?

Adding liquidity requires maintaining the price ratio, expressed as:

Share Calculation in Add Liquidity: Minting new shares

The share calculation is pivotal for fair distribution:

f(X,Y) = value of liquidity

we will define f(X,Y) = **√**XY

L0 = f(X,Y)

L1 = f(X+dX,Y+dY)

T = Total shares

s = Shares to mint

Total shares should increase proportional to the increase in liquidity

L1 / L0 = (T + s) / T

L1 * T = L0 * (T + s)

(L1 - L0) * T / L0 = s

Corresponding Code:

if (totalSupply == 0) {

shares = _sqrt(_amount0 * _amount1);

} else {

shares = _min(

(_amount0 * totalSupply) / reserve0,

(_amount1 * totalSupply) / reserve1

);

}

Remove Liquidity Function: Proportional Distribution

Removing liquidity involves returning a proportional amount of each token:

dX, dY = amount of liquidity to remove

dX = s / T * X

dY = s / T * Y

Proof

Let’s find dX, dY such that

v / L = s / T

where

v = f(dx, dy) = √dXdY,

L = total liquidity = √XY

s = shares

T = total supply

√dXdY = s / T * √XY

Amount of liquidity to remove must not change the price so dX / dY = x / y

Replace dY = dX * Y/ X

√dXdY = √(dX * dX * Y / X) = dX * √(Y / X)

Divide both sides of the Equation with √(Y / X)

dX = s / T * √(XY) / √(Y / X)= s / T * √(X²) = s / T * X

Likewise dY = s / T * Y

Corresponding Code:

amount0 = (_shares * bal0) / totalSupply;

amount1 = (_shares * bal1) / totalSupply;

Last updated