🖍️
Simple AMM
  • Simple Automated Market Maker
Powered by GitBook
On this page
  • Constant Product Market
  • Simple Calculation
  • Constant Mean Market

Was this helpful?

Simple Automated Market Maker

Simple version of the Uniswap AMM

Last updated 4 years ago

Was this helpful?

Constant Product Market

A group of liquidity providers deposits token α \alpha αand token β\betaβwith reserves RαR_{\alpha}Rα​and RβR_{\beta}Rβ​, respectively. The fee for a transaction is (1−γ)(1-\gamma)(1−γ) and the conserved quantity is k=RαRβk=R_{\alpha}R_{\beta}k=Rα​Rβ​. A user wants to trade an amount of token α\alphaα, Δα\Delta_{\alpha}Δα​, for some amount of token β\betaβ,Δβ\Delta_{\beta}Δβ​ such that k remains conserved.

Each transaction must satisfy the following equation:

(Rα−Δα)(Rβ+γΔβ)=k(R_{\alpha} - \Delta_{\alpha})(R_{\beta} +\gamma\Delta_{\beta}) = k(Rα​−Δα​)(Rβ​+γΔβ​)=k

After the transaction the reserves and k are updated as follows:

Rα=Rα−ΔαR_{\alpha} = R_{\alpha} - \Delta_{\alpha}Rα​=Rα​−Δα​
Rβ=Rβ+ΔβR_{\beta} = R_{\beta} + \Delta_{\beta}Rβ​=Rβ​+Δβ​
k=(Rα−Δα)(Rβ+Δβ)k = (R_{\alpha} - \Delta_{\alpha})(R_{\beta} + \Delta_{\beta})k=(Rα​−Δα​)(Rβ​+Δβ​)

Simple Calculation

Constant Mean Market

The conservation equation becomes

An example can be seen at

Alice wants to sell an amount Δα\Delta_{\alpha}Δα​ for token β\betaβ.

What amount, Δβ\Delta_{\beta}Δβ​, does Bob pay to Alice given RαR_{\alpha}Rα​, RβR_{\beta}Rβ​, and γ\gammaγ?

Δβ=−RβΔαγ(Δα−Rα)\Delta_{\beta} = -\frac{R_{\beta} \Delta_{\alpha}}{\gamma(\Delta_{\alpha} - R_{\alpha})}Δβ​=−γ(Δα​−Rα​)Rβ​Δα​​

The ratio of the changes: ν=Δα/Δβ\nu = \Delta_{\alpha} / \Delta_{\beta}ν=Δα​/Δβ​.

ν=−γΔα(Δα−Rα)RβΔα\nu = -\frac{\gamma \Delta_{\alpha}(\Delta_{\alpha} - R_{\alpha})}{R_{\beta}\Delta_{\alpha}}ν=−Rβ​Δα​γΔα​(Δα​−Rα​)​

The market price is mμ=Rα/Rβm_{\mu} = R_{\alpha} / R_{\beta}mμ​=Rα​/Rβ​.

A constant mean market is a generalization of a constant product market. The conserved constant becomes a product over all the reserves for n tokens where wiw_{i}wi​is the reserve weight for the ith token.

k=∏i=1nRiwik = \prod_{i=1}^{n} R_{i}^{w_{i}}k=i=1∏n​Riwi​​
c(Rj+γjΔj)wj(Rℓ−Δℓ)wℓ=kc(R_{j} + \gamma_{j}\Delta_{j})^{w_{j}}(R_{\ell} -\Delta_{\ell})^{w_{\ell}} = kc(Rj​+γj​Δj​)wj​(Rℓ​−Δℓ​)wℓ​=k

where γj\gamma_{j}γj​is the fee for the jth coin and c is given by

c=∏i=1ℓ≠jnRiwic = \prod_{\substack{i=1 \\ \ell \neq j}}^{n}R_{i}^{w_{i}}c=i=1ℓ=j​∏n​Riwi​​
LogoExample-Code/constant_product.py at main · logicalmechanism/Example-CodeGitHub