Skip to main content

1. Basics of Liquidity Pool #


1.1 Swap pair definition #

The swap pair consists of two different tokens that can be exchanged, represented by TokenAToken A and TokenBToken B, respectively. When creating a swap pair, the user needs to deposit corresponding number of TokenAToken A and TokenBToken B with equal value according to the actual market price. If the price differs from the market price, the pair will be arbitraged and the creator will suffer a loss.

Assuming PAP_{A} and PBP_{B} are respectively the price of TokenAToken A and TokenBToken B in any legal currency, PA→BP_{A\rightarrow B} is defined as the price exchanging TokenAToken A for TokenBToken B, which is the amount of TokenBToken B equivalent to one TokenAToken A in value:

PA→B=PAPB(1.1.1)P_{A \rightarrow B} = \frac{P_A}{P_B} \tag{1.1.1}

Similarly, define PB→AP_{B \rightarrow A}, which is the price at which TokenBToken B is traded to TokenAToken A:

PB→A=PBPA(1.1.2)P_{B \rightarrow A} = \frac{P_B}{P_A} \tag{1.1.2}

In theory, without considering trading costs, the following relationship exists:

PAβ†’Bβˆ—PBβ†’A=1(1.1.3)P_{A \rightarrow B} * P_{B \rightarrow A} = 1 \tag{1.1.3}

Assuming NAN_A and NBN_B are the quantities of two digital assets TokenAToken A and TokenBToken B at any time within the trade pair, we express the trade pair as:

(NA ∣ NB)(1.1.4)(N_A \ | \ N_B) \tag{1.1.4}

The trade pair always assume two internal tokens have equal market values all the time, so following formula are equivalent:

NAβˆ—PA=NBβˆ—PB(1.1.5)N_A * P_{A} = N_B * P_{B} \tag{1.1.5}
NA=NBβˆ—PBβ†’A(1.1.6)N_A = N_B * P_{B \rightarrow A} \tag{1.1.6}
NB=NAβˆ—PAβ†’B(1.1.7)N_B = N_A * P_{A \rightarrow B} \tag{1.1.7}
PA→B=NBNA(1.1.8)P_{A \rightarrow B} = \frac{N_B}{N_A} \tag{1.1.8}
PB→A=NANB(1.1.9)P_{B \rightarrow A} = \frac{N_A}{N_B} \tag{1.1.9}

1.2 Constant-product invariant #

While performing token exchange, since the external prices of TokenAToken A and TokenBToken B cannot be easily obtained, it is needed to design a mechanism to determine the exchange ratio between TokenAToken A and TokenBToken B. The DEXs of automated market maker (AMM) type use the "constant-product invariant" formula [1] to determine the change of the number of tokens in the trading pool before and after the exchange, also the amount of tokens traded in and out accordingly.

For the pair of (NA ∣ NB)(N_A \ | \ N_B), "constant-product invariant" formula is represented as:

NAβˆ—NB=K(1.2.1)N_A * N_B=K \tag{1.2.1}

In this formula, the value of KK changes only when the users deposit tokens into, or remove tokens from the trading pair, and alway remains constant during the trading process.

Suppose in an exchange oparetion, the user trades TokenAToken A for TokenBToken B, and the input amount of TokenAToken A is Ξ”A\mathrm{\Delta}_{A}, the output amount of TokenBToken B is Ξ”B\mathrm{\Delta}_{B}. ( "Trade in" and "trade out" here after are from the point of view of liquidity pool, if from the point of users, the relationship "trade in" and "trade out" is completely opposite). According to the "constant-product invariant" formula, there are:

(NA+Ξ”A)βˆ—(NBβˆ’Ξ”B)=(NAβˆ—NB)(1.2.2)( N_A + \mathrm{\Delta}_A ) * (N_B - \mathrm{\Delta}_B ) = ( N_A * N_B ) \tag{1.2.2}
Ξ”B=Ξ”ANA+Ξ”Aβˆ—NB(1.2.3)\mathrm{\Delta}_B = \frac{\mathrm{\Delta}_A}{N_A + \mathrm{\Delta}_A} * N_B \tag{1.2.3}

It can be seen that there are three different prices here, before the trade, after the trade, and happend during the trade.

The token price of the pool before the trade (expressed as PA→B0P_{A \rightarrow B}^{0}):

PA→B0=NBNA(1.2.4)P_{A \rightarrow B}^{0} = \frac{N_B}{N_A} \tag{1.2.4}

The actual token price taken in the trade (represented as PA→B1P_{A \rightarrow B}^{1}) is:

PA→B1=ΔBΔA=NBNA+ΔA(1.2.5)P_{A \rightarrow B}^{1} = \frac{\mathrm{\Delta}_B}{\mathrm{\Delta}_A} = \frac{N_B}{N_A + \mathrm{\Delta}_A} \tag{1.2.5}

The token price after the trade finished (represented as PA→B2P_{A \rightarrow B}^{2}) is:

PAβ†’B2=NBβˆ’Ξ”BNA+Ξ”A(1.2.6)P_{A \rightarrow B}^{2} = \frac{N_B - \mathrm{\Delta}_B}{N_A + \mathrm{\Delta}_A} \tag{1.2.6}

Obviously:

PA→B0>PA→B1>PA→B2(1.2.7)P_{A \rightarrow B}^{0} > P_{A \rightarrow B}^{1} > P_{A \rightarrow B}^{2} \tag{1.2.7}

It means, the price of trade-in token TokenAToken A relative to the trade-out token TokenBToken B has slipped after the trade has finished. The price of TokenAToken A in the pair before the trade starts is higher than the actual price the trade adopts, and the actual price the trade adopts is higher than the price of TokenAToken A in the pair after the trade is completed. That is, after the token trade is completed, the price of TokenAToken A relative to TokenBToken B in the pair has fallen, and accordingly, the relative price of TokenBToken B has risen.

1.3 Trade arbitrage #

The exchange operation happens at the moment the transaction block is confirmed on the blockchain. The external prices of TokenAToken A and TokenBToken B actually do not change at that moment. Due to the constraint of "constant-product invariant", the token price will slide within the exchange, which will cause the exchange user to suffer a certain marginal exchange loss. The exchange loss in the amount of TokenBToken B is calculated as follows:

LostAβ†’B=Ξ”Aβˆ—NBNAβˆ’Ξ”Aβˆ—NBNA+Ξ”A=Ξ”Aβˆ—NBNA/(1+NAΞ”A)(1.3.1)\begin{aligned} Lost_{A \rightarrow B} &= \mathrm{\Delta}_A * \frac{N_B}{N_A} - \mathrm{\Delta}_A * \frac{N_B}{N_A + \mathrm{\Delta}_A} \\[3mm] &= \mathrm{\Delta}_A * \frac{N_B}{N_A} \Large{/} \normalsize ( 1 + \frac{N_A} {\mathrm{\Delta}_{A}} ) \end{aligned} \tag{1.3.1}

It can be seen that the greater the ratio (Ξ”A/NA)(\mathrm{\Delta}_{A} / N_A) the user trade TokenAToken A into the pool, the greater the exchange loss he/she will suffer.

After the exchange is completed, if another user performs reverse exchange, according to the "constant-product invariant" formula, he/she only needs to pay TokenBToken B in the amount of ΔB\mathrm{\Delta}_B, and can get TokenAToken A of ΔA\mathrm{\Delta}_A. After this reverse exchange, the asset price in the pool will return to the initial price PA→B=NB/NAP_{A \rightarrow B} = N_B/N_A. That is, this user completes the exchange at the higher TokenBToken B to TokenAToken A price, and commits the arbitrage of the previous user's exchange loss, the arbitrage profit is equal to the previous user's exchange loss.

1.4 Impermanent loss #

When providing liquidity, the provider needs to deposit into the trading pool two kinds of tokens with the corresponding amounts of same total value. With the trade on going, the number of tokens corresponding to the user's liquidity will change. Assuming that the user’s initial investment of TokenAToken A is XAX_{A}, when the user withdraws the liquidity, if the market price of TokenAToken A rises relative to TokenBToken B, then Xβ€²A{X'}_{A},the amount of TokenAToken A that the user can get from the pool may be less than XAX_{A}. If the liquidity service income obtained by providing liquidity are not enough to compensate for the loss caused by the decrease in the amount of TokenAToken A, in this case, the liquidity provider will suffer losses relative to simply holding TokenAToken A and TokenBToken B instead of providing liquidity services. In fact, no matter whether the price of TokenAToken A and TokenBToken B goes up or down, as long as their price fluctuates from the time providing liquidity, the user will always suffer losses. The amount of the loss is completely determined by the market price variation, beyond any user's control, which is also called "Impermanent Loss" [5].

Taking TokenBToken B as the price benchmark, following quantitative analysis of impermanent loss is conducted without considering the exchange fee income. Assuming that the initial number of tokens that the user puts into the trade pair are (XA∣YB)(X_A|Y_B), the price of TokenAToken A relative to TokenBToken B is P1P_1, and at the end, the number of tokens that the user withdraws from the trade pair are (XAβ€²βˆ£YBβ€²)(X'_A|Y'_B), and the price of TokenAToken A relative to TokenBToken B is P2P_2, if the user does not provide liquidity for the trading pool, but simply holds (XA∣YB)(X_A|Y_B) tokens, the final value is:

V1=XAβˆ—P2+YB(1.4.1)V_1=X_A*P_2+Y_B \tag{1.4.1}

Since the user provides liquidity for the trading pool, the actual value of (XAβ€²βˆ£YBβ€²)(X'_{A}|Y'_{B}) tokens is:

V2=XAβ€²βˆ—P2+YBβ€²(1.4.2)V_2=X'_A*P_2+Y'_B \tag{1.4.2}

Considering:

{XAβˆ—YB=XAβ€²βˆ—YBβ€²P1=YB/XAP2=YBβ€²/XAβ€²(1.4.3)\left\{ \begin{matrix} \begin{aligned} X_A * Y_B &= X'_A * Y'_B \\ P_1 &= Y_B / X_A \\ P_2 &= Y'_B / X'_A \\ \end{aligned} \end{matrix} \right. \tag{1.4.3}

Then we have [6]:

V2V1=Β 2P1βˆ—P2Β P1+P2=4P1P2(P1βˆ’P2)2+4P1P2≀1(1.4.4)\begin{aligned} \frac{V_2}{V_1} &= \frac{\ 2\sqrt{P_1*P_2}\ }{P_1+P_2} \\[3mm] &= \sqrt{\frac{4P_{1}P_{2}}{( P_{1} - P_{2} )^{2} + 4P_{1}P_{2}}} \leq 1 \end{aligned} \tag{1.4.4}

So, as long as P1β‰ P2P_1 \neq P_2, V2V_2 is always less than V1V_1, that is to say, the users's total token value denominated in TokenBToken B is definitely reduced compared to simply holding two tokens. Similarly the total token value denominated in TokenAToken A is also definitely reduced compared to simply holding two tokens. Therefore, the so-called "Impermanent Loss" is actually a permanent loss that is bound to occur, only the amount of loss may change with the price ratio.