Uniswap


Uniswap is an automated liquidity protocol powered by a constant product formula and implemented in non-upgradeable smart contracts on the Ethereum blockchain through which users can create liquidity pools by providing a certain percentage of ETH and any other ERC20 assets. The idea  being that each liquidity pool reserve provide liquidity for each trading pair transactions vs the payment of a 0.3% liquidity provider fee split equally between all the liquidity providers of the pool.


Quick overview of the Uniswap architecture


Liquidity pool


In Uniswap, each trading pair has only one liquidity pool, which is generally created by the first liquidity provider.

 

For example, in the case of a  liquidity provider creating an ETH-TRX liquidity pools and then adding liquidity, the initial amount of ETH deposited will be x0, the number of stored TRX will amount to y0 with x0 . y0 = c0.

 

Liquidity token


Liquidity provider (LP) obtain liquidity provider tokens (LP token) proportionally to their share in the liquidity pool.

 

For example, in the above-mentioned example, a liquidity provider will receive a number of LP tokens equal to :

 

With :


MINLIQUIDITY the minimum reserved amount of LP token set by the system when adding liquidity for the first time  to prevent the total amount of LP tokens from becoming zero.

 

Adding Liquidity


When adding liquidity to an existing pool, users must deposit pair tokens proportional to the current ratio. Suppose a new LP deposits of X ETH and Y TRX to the liquidity pool, the LP must ensure that :

 

 

The LP will in turn receive the following amount of newly minted LP tokens : 

 

 

After adding liquidity, the reserve of the liquidity pool will be :

 

 

And the amount of LP tokens will be equal to :

 

 

Removing Liquidity


Liquidity providers can remove liquidity by burning their LT in the liquidity pool contract to withdraw their share of ETH and TRX tokens from the pool.
Following on our previous example, assuming. That the amount of LT burned by LP is N’, the amount of ETH that the liquidity provider can withdraw is :

 

 

And the amount of TRX is :

 

 

After the liquidity is removed, the reserve of the liquidity pool will be updated to :

 

 

n.b The reserves of the liquidity pool might be different at the time when the LP deposit and withdraw, therefore the number and the ratio of tokens of the LP can withdraw might change accordingly.


Swap transaction

 

After the liquidity pool is created, and liquidity is injected, users who hold. ETH or TRX can swap in the liquidity pool.
For example, let’s say that our user wants to transform m ETH to the liquidity pool.
The ETH in the liquidity pool will become :

 

And the user will in turn receives yi+1 TRX, which according to the AMM algorithm in Uniswap will satisfy the following equation :

 

Which gives us :

 

The liquidity provider fees will be automatically added to the liquidity pool after the transaction reserve, so after the transaction, the reserve of the entire liquidity pool becomes :

 

Since there is no other LP token. Minted or burned at that moment, the total amount of LP token remains unchanged, that is ni+1 = ni .
This means that all LPs ratios remain the same, but each unit share now correspond to more amount of the liquidity pool reserve.