# Simple Automated Market Maker

## 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>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ancientkraken.gitbook.io/simple-amm/master.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
