1. Basics of Liquidity Pool #
1.1 Swap pair definition #
The swap pair consists of two different tokens that can be exchanged, represented by and , respectively. When creating a swap pair, the user needs to deposit corresponding number of and 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 and are respectively the price of and in any legal currency, is defined as the price exchanging for , which is the amount of equivalent to one in value:
Similarly, define , which is the price at which is traded to :
In theory, without considering trading costs, the following relationship exists:
Assuming and are the quantities of two digital assets and at any time within the trade pair, we express the trade pair as:
The trade pair always assume two internal tokens have equal market values all the time, so following formula are equivalent:
1.2 Constant-product invariant #
While performing token exchange, since the external prices of and cannot be easily obtained, it is needed to design a mechanism to determine the exchange ratio between and . 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 , "constant-product invariant" formula is represented as:
In this formula, the value of 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 for , and the input amount of is , the output amount of is . ( "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:
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 ):
The actual token price taken in the trade (represented as ) is:
The token price after the trade finished (represented as ) is:
Obviously:
It means, the price of trade-in token relative to the trade-out token has slipped after the trade has finished. The price of 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 in the pair after the trade is completed. That is, after the token trade is completed, the price of relative to in the pair has fallen, and accordingly, the relative price of 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 and 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 is calculated as follows:
It can be seen that the greater the ratio the user trade 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 in the amount of , and can get of . After this reverse exchange, the asset price in the pool will return to the initial price . That is, this user completes the exchange at the higher to 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 is , when the user withdraws the liquidity, if the market price of rises relative to , then οΌthe amount of that the user can get from the pool may be less than . 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 , in this case, the liquidity provider will suffer losses relative to simply holding and instead of providing liquidity services. In fact, no matter whether the price of and 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 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 , the price of relative to is , and at the end, the number of tokens that the user withdraws from the trade pair are , and the price of relative to is , if the user does not provide liquidity for the trading pool, but simply holds tokens, the final value is:
Since the user provides liquidity for the trading pool, the actual value of tokens is:
Considering:
Then we have [6]:
So, as long as , is always less than , that is to say, the users's total token value denominated in is definitely reduced compared to simply holding two tokens. Similarly the total token value denominated in 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.