# Automated Market Maker

Every liquidity pool is based on an automatic market making (AMM) algorithm that determines the prices and amounts of tokens in each trade. Geminon's GLPs use a proprietary algorithm derived from the constant product AMM that gives them the liquidity creation capabilities of an LBP.

### Constant product AMM

Practically all of the decentralized exchange (DEX) protocols that exist today use the so-called constant product formula for their liquidity pools:

Where $x$ and $y$ are the amounts of the pair of tokens in the pool and $k$ is a constant that is fixed when the initial liquidity is added during its creation. The liquidity pool creates an automatic market for the token pair simply by imposing the condition that, after every trade, the product of the amounts of both tokens cannot be less than the liquidity constant:

Given an amount of tokens $\Delta x$ that you want to exchange using the liquidity pool, the amount of the exchanged token to receive $- \Delta y$ is calculated as:

### Generalized constant product AMM

The constant product AMM formula can be generalized for a pool consisting of n tokens with weights $w_n$ such that $\sum_n w_n = 1$:

Where $B_n$ is the balance of the nth token in the pool. Developing the previous formula, it is obtained that for an exchange of any pair of tokens in the pool, given the input amount of one token $\Delta x$, the received amount of the second token is given by:

From the previous equations it is derived that the quote or relative price of one token based on another in the pool depends exclusively on the balances and relative weights of both tokens in the pool:

It can be easily shown that these equations are transformed into the equations for a pool of two tokens seen in the previous point by doing $w_x = w_y$.

### Geminon AMM

The generalized AMM equations from the previous point are the ones used by the Balancer protocol for its pools. In Balancer LBPs it is possible to alter the relative weights of the tokens in the pool, which changes the relative price of both tokens and also the value of the pool's liquidity constant $k$.

The AMM algorithm developed for the Geminon GLPs also alters the value of the pool invariant, although it employs a completely different mechanism. The main differences are:

The variation of the liquidity constant is completely automatic in GLP: it is not possible to manually alter the weights of the tokens in the pool, and therefore the prices.

The parameterization of liquidity in a GLP varies depending on the operations carried out, not depending on time as it happens in LBPs.

The GLP algorithm does not vary the weights of the tokens in the pool, but rather the balance of the GEX token.

#### Algorithm description

The Geminon AMM works in two steps:

The amount of the output token is calculated using the constant product AMM formula with the liquidity available at that moment in the pool, that is, in this step the LPG behaves exactly the same as a Uniswap-type liquidity pool.

In the second step, the balance of the GEX token in the pool is modified by minting or burning tokens. The variation of the supply of the GEX token is calculated from the number of tokens that the user has received or delivered to the pool, depending on the operation carried out.

To help reduce trade slippage, which negatively impacts user output, the pool automatically splits the input amount in half and runs the above steps twice, so that on the second iteration the user benefits from the increase in liquidity that he himself has generated. This mechanism is especially effective when the liquidity of the pool is very low.

#### Mint GEX tokens (mintSwap)

For a GEX token purchase (mint) operation, the amount of GEX tokens to be received is initially calculated as if it were a Uniswap-type liquidity pool. If $c_0$ is the balance of collateral in the pool, and $g_0$ the balance of GEX tokens in the pool, the pool invariant is:

And the amount of tokens received by the user, $g_{out}$, when he delivers an amount of collateral, $c_{in}$, is calculated by isolating the invariant in the previous equation:

After calculating the number of output GEX tokens, the mint coefficient $\mu$ is applied, which increases the balance of GEX tokens in the pool:

Finally, the commissions for minting the protocol are deducted from the number of GEX tokens to be sent to the user.

The minting coefficient takes its values in the interval $[1,2]$, which makes it possible to deduce the behavior of GLP in GEX token mint operations:

1. In a minting operation, liquidity always increases: since the values of the minting coefficient are restricted ($\mu \in [1,2]$) the difference $1-\mu$ is always less than or equal to 0, which implies that $g_1 \ge g_0$, the final balance of GEX in the pool will always be equal to or greater than the initial. Also, since $c_1 > c_0$ because it is a minting operation, $k_1 > k_0$, then the liquidity product of the pool after the operation will always be strictly greater than the initial one.

2. The result of an operation in GLP is closer to that obtained in a Uniswap-type liquidity pool, the lower the amount of the operation. If the $k_1$ invariant equation is developed to express it solely as a function of the input quantity, we have:

It's easy to see that the above expression converges to the value of the initial invariant $k_0 = c_0 \cdot g_0$ the lower the input value $c_{in}$ is, or more formally:

3. The final price of the GEX token in the pool after the transaction is lower than the price that would be obtained in a pool of pure constant product. It is easy to intuitively see that by increasing the balance of GEX tokens in the pool, the relative price of this token against the other token should decrease:

This means that transactions in a GLP are not reversible even without taking into account the protocol commissions: if a mint operation is done and immediately after the opposite is carried out, redeeming the tokens in the pool, a lower amount will be obtained depending on how far the liquidity product $k_1$ is from the initial one $k_0$. As seen in the second property, the invariants coincide at zero and separate as the amount of the trade $c_{in}$ increases, or more specifically the ratio $c_0/c_{in}$ that determines the slippage that the trade produces in the pool price. The greater the slippage (entropy) generated by an operation in the pool, the more irreversible that operation is. Said entropy is captured by the GLP by increasing the value of the GEX token, which is why GLPs are called entropy capturing machines.

The irreversibility of the operations presents important advantages for the security of the Geminon protocol, since it implies that flash loan attacks cannot be carried out on a GLP: since the amount lent cannot be repaid in its entirety, the transaction will revert. In general, any attack that involves price manipulation in a GLP using large transactions that must be undone in a short period of time, will mean a financial loss for the attacker in favor of the protocol.

#### Redeem GEX tokens (redeemSwap)

The equations for GEX token redemption are very similar to those for minting. The main difference between both processes is that during the exchange, the protocol commission is deducted from the amount of GEX tokens entered. The amount of exit collateral $c_{out}$, given an input amount $g_{in}'$ (minus the commission) is worth:

And the new balances that determine the liquidity product of the pool:

### Price curve

In a constant product liquidity pool, the price of a token with respect to its peer is the quotient of the reserves of both. Using the constant product ratio $x \cdot y = k$ we get:

The price of one token in a liquidity pool is a parabolic function of the amount of the second token in the pool. In a GLP, on the other hand, the value of $k$ is not constant, but rather its value varies with the amount of collateral in the pool. The GEX token price function in a GLP can be written as:

Where $k(c)$ is a function that varies linearly with the amount of collateral tokens. As a consequence, the GLP price function for the GEX token does not vary quadratically with the amount of collateral as it happens in a Uniswap-type pool, but rather linearly. The goal of this design is to reduce the volatility of the GEX token.

For a detailed discussion on the influence of the mint ratio on the value of $k$, see the next page "Supply Control".

Last updated