> For the complete documentation index, see [llms.txt](https://ancientkraken.gitbook.io/simple-amm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ancientkraken.gitbook.io/simple-amm/master.md).

# Simple Automated Market Maker

Simple version of the Uniswap AMM

## Constant Product Market

\
A group of liquidity providers deposits token $$\alpha$$and token $$\beta$$with reserves $$R\_{\alpha}$$and $$R\_{\beta}$$, respectively. The fee for a transaction is $$(1-\gamma)$$ and the conserved quantity is $$k=R\_{\alpha}R\_{\beta}$$. 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\_{\alpha} - \Delta\_{\alpha})(R\_{\beta} +\gamma\Delta\_{\beta}) = k
$$

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

$$
R\_{\alpha} = R\_{\alpha} - \Delta\_{\alpha}
$$

$$
R\_{\beta} = R\_{\beta} + \Delta\_{\beta}
$$

$$
k = (R\_{\alpha} - \Delta\_{\alpha})(R\_{\beta} + \Delta\_{\beta})
$$

### Simple Calculation

Alice wants to sell an amount $$\Delta\_{\alpha}$$ for token $$\beta$$.&#x20;

\
What amount, $$\Delta\_{\beta}$$, does Bob pay to Alice given $$R\_{\alpha}$$, $$R\_{\beta}$$, and $$\gamma$$?

$$
\Delta\_{\beta} = -\frac{R\_{\beta} \Delta\_{\alpha}}{\gamma(\Delta\_{\alpha} - R\_{\alpha})}
$$

The ratio of the changes: $$\nu = \Delta\_{\alpha} / \Delta\_{\beta}$$.

$$
\nu = -\frac{\gamma \Delta\_{\alpha}(\Delta\_{\alpha} - R\_{\alpha})}{R\_{\beta}\Delta\_{\alpha}}
$$

The market price is $$m\_{\mu} = R\_{\alpha} / R\_{\beta}$$.

## Constant Mean Market

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 $$w\_{i}$$is the reserve weight for the ith token.

$$
k = \prod\_{i=1}^{n} R\_{i}^{w\_{i}}
$$

The conservation equation becomes

$$
c(R\_{j} + \gamma\_{j}\Delta\_{j})^{w\_{j}}(R\_{\ell} -\Delta\_{\ell})^{w\_{\ell}} = k
$$

where $$\gamma\_{j}$$is the fee for the jth coin and c is given by

$$
c = \prod\_{\substack{i=1 \ \ell \neq j}}^{n}R\_{i}^{w\_{i}}
$$

An example can be seen at

{% embed url="<https://github.com/logicalmechanism/Example-Code/blob/main/constant_product.py>" %}
