Finance is a general term for monetary financing, mainly referring to various activities related to currency circulation and banking (Baidu Encyclopedia)

In the off-chain world, the government and the central bank under government management are the ones who control the financial lifeline. Despite the regulation from Mr. Market, and the fact that every country with a financial market has a relatively mature financial market supervision system, when a crisis occurs, we all know who is the most powerful role - the government.

When money is brought to the blockchain by cryptography, it means that finance is also brought to the blockchain. Cryptography and Internet technology give the public the possibility to explore the possibility of code replacing laws and regulations (government) to a certain extent, that is, the possibility of Code is law.

Why study the Uniswap protocol (UNI token)?

If we compare each chain to a different country, Ethereum must be the country with the highest comprehensive strength ranking. Then I think Uniswap is the largest "securities" exchange in this country. It carries the liquidity of a large number of tokens on Ethereum and meets the needs of a large number of users to buy and sell ERC20 tokens.

From which angles does this article study Uniswap?

There are already many articles about Uniswap. This article mainly studies and discusses user needs, the implementation principles of Uniswap v2 AMM, and incentive mechanisms.

What is the Uniswap Protocol?

Uniswap is an open-source, decentralized platform for submitting liquidity and trading ERC20 tokens on Ethereum, which is what we often call a decentralized exchange (DEX).

What problem does Uniswap solve? What needs does it meet?

Disadvantages of centralized exchanges: User assets are not safe

Compared with decentralized exchanges, centralized exchanges (CEX) have better liquidity, are easier to use, have more user-friendly interactions, and are more convenient for deposits and withdrawals. In addition, some exchanges often hold activities such as IEO and financial management, so centralized exchanges have a large number of users and trading volumes in this circle. And most users are accustomed to storing their tokens in exchanges.

However, there is a problem that is often criticized in centralized exchanges, which is the opacity of assets and liabilities. The lack of supervision in this circle and the opacity of assets and liabilities can easily lead to favoritism and fraud, the most typical of which is the misappropriation of user assets. The fuse of FTX's explosion this time was that SBF misappropriated $4.1 billion of user assets in the middle of this year to save Almeda Research.

We know that the ownership of tokens belongs to the owner of the wallet private key (not your key, not your coin). When we transfer tokens to centralized exchanges, due to the lack of supervision, your token assets essentially belong to the exchange. The premise for you to get your assets back is that the exchange does not do evil. For exchanges, the token price is just a number, especially for those altcoins with smaller market capitalizations. This is why some exchanges can open trading for users to participate in buying and selling before the tokens are issued.

How does Uniswap solve the problem of insecurity in user trading tokens and assets?

Asset security issues

First of all, unlike centralized exchanges, Uniswap does not require users to deposit tokens to the platform before trading. Before Uniswap executes user transactions, the user's tokens are always stored in their own wallets such as Metamask and Tokenpocket. Unless the user leaks the private key of his wallet or the wallet is hacked, the tokens are safe in the wallet.

trade

The purchase and sale of a token requires a buyer and a seller. If the transaction occurs in a centralized exchange, the exchange acts as an intermediary to match the transaction and collect a fee. The exchange generally uses a central order book to match transactions between buyers and sellers, so there is a market maker (Wintermute/Almeda Research), as shown in the figure below.

There is no central server or order book in Uniswap. It uses something called an automated market maker (AMM) to match users' transactions. The following article will discuss the AMM mechanism of Uniswap in detail.

Therefore, Uniswap provides blockchain users with a decentralized and secure place to trade ERC20 tokens, meeting people's needs to trade tokens while ensuring the security of wallet assets.

Uniswap AMM implementation principle

Uniswap’s liquidity pool calculates prices using a formula we call the constant product formula:

x * y = k, where k is a constant (a constant whose value does not change)

Before discussing this formula, you need to understand the following concepts:

  • Liquidity: It is composed of two tokens, such as token1 and token2, which are called a pair. Note that after being combined into a pair, a pair is an ERC20 token.

  • Liquidity Pool: A pool where liquidity is gathered, i.e. the asset pool of AMM

  • Liquidity Provider: A person who provides liquidity to a liquidity pool is called Liquidity Provider/LP

  • Pool Token (also called Liquidity Token): Pair in UniswapV2 is an ERC20 token, which is a "voucher" for LP to provide/withdraw liquidity. When LP provides liquidity, it automatically generates tokens for LP, and burns LP's tokens when LP withdraws liquidity.

  • Liquidity Pool Share (LPS): calculated to record the liquidity contribution ratio of each LP

The process of establishing a liquidity pool and token exchange

After deploying a liquidity pool contract, such as the pair of token1 and token2, a liquidity provider LP needs to put a certain amount of tokens into the pool to start the liquidity pool. At this time, the LP will obtain the liquidity token Pool Token. The first LP can set the initial amount of the liquidity pool and own 100% of the LPS. After completing the above process, the liquidity pool has liquidity and can start to exchange tokens for traders. Please refer to the figure below for the process diagram:

x * y = k to calculate the token exchange price

The price of token exchange is calculated by the formula x * y = k (k is a constant)

x and y represent the number of two tokens in the liquidity pool. The total value of the two tokens needs to be the same. Assuming that the current price of one ETH is 1000 USDT, when establishing a liquidity pool, you need 5ETH - 5000USDT (other stablecoins are also acceptable) or 10ETH - 10000USDT

From the formula, we can see that the price of token exchange is determined by the number of x and y tokens.

x represents the number of token1, i.e. reserve1

y represents the number of token2, i.e. reserve2

Since k is a constant,

If you increase reserve1, you must reduce reserve2

If you increase reserve2, you must reduce reserve1

Let's take a concrete example:

Assuming the price of ETH is 1ETH=1000USDT, since the total value of the two tokens needs to be the same

x = amount of ETH in the liquidity pool = 5 ETH

y = liquidity pool USDT quantity = 5000 USDT

k=x * y

k=5*5000

k=25000

Now someone wants to buy (withdraw) 1 ETH from the liquidity pool

x = amount of ETH in the liquidity pool

x=5-1

x=4ETH

Since k remains unchanged

y=k/x

y=25000/4

y=6250

Then the USDT this person needs to pay is

6250-5000=1250USDT

Putting aside the transaction fee, 1ETH=1000USDT, but buying 1ETH in this liquidity pool requires 1250USDT, which is what we often call transaction slippage. But generally speaking, the larger the liquidity pool, the smaller the number of tokens exchanged relative to the liquidity pool, and the smaller the transaction slippage.

Still using the above example, but now the liquidity pool becomes: x=500ETH, y=500000USDT

k=x * y

k=500*500000

k=250,000,000

Now someone wants to buy (withdraw) 1 ETH from the liquidity pool

x = amount of ETH in the liquidity pool

x=500-1

x=499ETH

Since k remains unchanged

y=k/x

y=250,000,000/499

y=501,002

Then the USDT this person needs to pay is

501002-500000=1002USDT

Putting aside the transaction fees, the transaction slippage for trading 1ETH in this relatively larger liquidity is 2USDT, which is significantly smaller than the slippage of 250USDT in the previous transaction.

However, there are usually more than one person exchanging tokens in a liquidity pool, which means that the transaction slippage is always in a dynamic change, and the smaller the liquidity pool, the greater the fluctuation of the transaction slippage. As shown in the figure below, from the perspective of a token exchanger (Trader), a transaction is completed and the price of the token in the liquidity pool is changing.

Incentives

In the words of Charlie Munger, a person's decision is determined by his or her incentive mechanism, which is the reward and punishment superreaction tendency he mentioned in "Poor Charlie's Almanack".

From the perspective of token exchangers, some people will use Uniswap because it meets users' pursuit of asset security and the need to exchange ERC20 tokens. If circumstances permit, no one will be willing to use a platform to trade tokens at the risk of their assets being misappropriated.

From the perspective of liquidity providers in Uniswap, what attracts them to establish liquidity pools or add tokens to liquidity pools?

In Uniswap v2, during the transaction, the protocol will extract 0.3% of the transaction token amount as transaction fees, and put these transaction fees back into the liquidity pool to reward liquidity providers. The more liquidity providers provide, the more liquidity tokens they have (Pool token), which means the larger the share of the liquidity pool (LPS), so the more transaction fee rewards they can get in the end.

Liquidity providers LP generally have three main groups: passive liquidity providers, project parties, and professional market makers

Passive liquidity providers: Token holders who want to earn passive income

Project side: They add liquidity to facilitate traders to buy and sell, which is commonly known as the project side adding pools

Professional Market Makers: A Strategy to Accomplish Their Market Making Mission

Disadvantages of Uniswap (v2)

One advantage of Uniswap v2 is that it concentrates a lot of liquidity through incentive mechanisms, but the disadvantage is that the efficiency of liquidity utilization is relatively low. In v2, the formula x * y = k does not specifically limit the range of x and y, so the number of x and y can tend to 0 or infinity, which means that the prices of the two tokens represented by x and y can also tend to 0 or infinity, but the actual situation is: within a certain period of time, the price of a token will fluctuate within a range, so part of the liquidity provided by LP is dormant (as shown in the figure below), and LP cannot earn transaction fees.

In Uniswap v3, the project added a concept of centralized liquidity (liquidity range) to solve this problem. We will write another article to introduce this later.

References for this article:

The articles are basically coded by myself. If there is any infringement in the content, please contact me to delete it.