This project was created in order to better understand how the math behind a constant product automated market maker (AMM) works. This is a simplified constant function market maker (CFMM) exchange contract and was created to better understand how the constant product formula works in practice.
NB! This repository was not extensively tested for deploying on the Ethereum mainnet.
forge buildforge test -vvThis swap contract uses the formula:
From:
We can deduce:
We then arrive at the following:
amount of tokens X in the contract = 5
amount of tokens Y in the contract = 10
A user comes along and they want to give us 1 of token X in exchange for token Y.
Following the formula constant product formula (x * y = k), we need to calculate the change of Y given a change in X.
x = 5
y = 10
k = 50
dx = 1
k = (x+1) * (y+dy)
50 = (5+1) * (10+dy)
50 = 6 * (10 + dy)
50 = 60 + 6dy
-10 = 6dy
-10/6 = dy
-1.666 = dy
amountOut = (-dx * y) / (dx + x)
