2 FreeSwap Exchange protocol #
2.1 FreetSwap protocol goal #
The goal of FreeSwap protocol is to implement a decentralized exchange protocol with no trade fees at all.
The "constant-product invariant" mechanism inevitably leads to token price slippage, from which FreeSwap could make profits for liquidity provider by arbitraging automatically.
2.2 FreeSwap trade pair setup #
FreeSwap trade protocol sets up two independent one-way sub-pools for the token pair , expressed as:
Where, the sub-pool (called as A-Pool hereinafter) can trade in , trade out , is the number of , and is the number of in A-Pool.
The sub-pool (called as B-Pool hereinafter) can trade in , trade out , is the number of , and is the number of in B-Pool.
When the liquidity provider adds tokens into the trading pool, he can specify which sub-pool to add, or he can add into both the sub-pools at the same time with specified ratio.
2.3 FreetSwap token exchange #
When the user trades and with the trading pool, there are two opposite trading operations, one is to trade in , and trade out ; the other is to trade in , and trade out . Here, "trade in" and "trade out" are relative to the trading pool.
If the user trades for , FreeSwap will pass this trade to A-Pool, the sub-pool to process. The A-Pool always trades in and trades out , so the number of always goes up, and the number of always goes down, which will lead to the monotonous decrease of price denominated by , and finally resulting price deviating from actual market price.
Similarly, if the user trade in for , B-Pool will handle this trade. B-Pool always trades in , and trades out , so the amount of will keep rising, and the amount of keep falling, which will also cause that price denominated in decreases monotonically and eventually deviates from the actual market price.
The sub-pool and are two one-way trading pairs in opposite directions, they are always fixed at one token in and the other token out. As the trades happening, the prices of the pair tokens always slide in opposite directions. After some trade volume accumulation, the price difference of the same token in the two sub-pools will exceed a certain level, which is too large to prevent users from participating the exchange. At this time, it is necessary to internally exchange the tokens between the sub-pools to smooth the token price difference. The process of internal token exchange within the sub-pools is actually a arbitrage process. As trading users trade with the two one-way trading pools, token prices will slide in opposite ways, and consequently users will suffer trade losses becasue of this price slippage. By executing the internal exchange between two sub-pools, price slippage can be fixed, and the arbitrage to user trade slippage losses can be achieved, which may make profits for the liquidity providers of the pool.
2.4 FreeSwap exchange arbitrage #
As said above, FreeSwap sets up two independent sub-trading pools:
The prices of and in the two sub-pools will deviate as exchange occurs. When the price deviates to some extent, the two sub-pools need to internally exchange tokens to restore the token price. The token exchange between two sub-pools can restore the token prices of both side, and coulde also commit some kind of arbitrage between the pools. The analysis following:
Before arbitrage, prices in two pools are:
The deviation ratio is:
Similarly, prices in the two pools are:
The deviation ratio is:
It can be seen that the price ratios of and in the two sub-pool are the same, namely:
The FreeSwap protocol specifies that when the token prices of two sub-pool deviate to a certeain threshold , such as the value of is greater than 101%, the internal arbitrage exchange is automatically executed to restore the token price.
Arbitrage operation is to exchange one token with more amount in one sub-pool for the other token with more amount in the other sub-pool with the equivalent value, that is, sub-pool swap for with same value in . From another perspective, it is for sub-pool to exchange for in sub-pool with equal value.
To ensure the exchange is fair, the exchange price is set as the average price of all tokens in the two sub-pools, namely:
After the arbitrage completed, the two sub-pools can be expressed as:
That is, in this trade arbitrage, A-Pool exchanges with a quantity of for with a quantity of . Considering the exchange value is equal, the following equation exists:
After the arbitrage operation, price of the A-Pool (priced in ) will increase, but it should not exceed the average price of the overall trading pool for the reason of fairness. Similarly, price (priced in ) in B-Pool will also rise, but also should not exceed the average price , That is, the following relationship exists:
Combining , following relationships can be derived:
In order to reduce the times of arbitrage operation, arbitrage between two sub-pools should exchange as many tokens as possible to smooth token price deviations to the greatest extent. So , should take the maximum vaue given by and . It is easy to find, in this case, the token prices of the two sub-pools both happen to be restored to the average token price of the whole trade pool , which is also the price at which the two sub-pools exchange tokens.
In general, FreeSwap arbitrage can be expressed as follows:
Among the above equation, is the condition for triggering the arbitrage operation, which means that arbitrage is executed when the price deviation of the two sub-pools is greater than or equal to . , are the amount of tokens exchanged between the two sub-pools within the arbitrage operation. Arbitrage is taken by exchanging , amount of TokenA
, at the price of , on arbitrage completion, the token prices of the two sub-pools are exactly the same, both are .