This article takes the pioneer Uniswap as an example and does not use PancakeSwap as an example. I hope Binance does not limit the flow, haha!
Everyone is using and playing every day, all trying to make money, but no one is studying the underlying logic. The more you understand, the more ways you will find to make money!
As the saying goes, the higher you stand, the further you see! Let's get straight to the point, it's relatively dry!
Although centralized exchanges (CEX) allow for large transactions when liquidity is abundant, there are still significant risks associated with centralized exchanges because users do not own their assets on the exchange. Issues such as theft and data privacy leaks are common! More and more people are becoming aware of these risks and are turning to decentralized exchanges (DEX).
DEX reduces or eliminates the need for intermediaries by using smart contracts and on-chain transactions. Popular decentralized exchanges include Uniswap, Pancakeswap, and the recently popular project aster.
There are two types of DEX—order book-based DEX and liquidity pool-based DEX. Order book DEX like aster (I will try to use Binance-related examples) operates similarly to CEX, where users can submit buy and sell orders at limit or market prices. The main difference between the two types of exchanges is that for CEX, trading assets need to be held in the exchange's wallet, while for DEX, trading assets can be held in users' own wallets.
However, one of the biggest problems faced by order book-based DEX is liquidity. Orders in the order book may need to wait a long time to be executed. To solve this problem, DEX based on liquidity pools have been introduced. A liquidity pool is essentially a token reserve in a smart contract, allowing users to buy and sell available tokens in the liquidity pool immediately. The price of tokens is determined algorithmically and tends to rise with large transactions. Examples of liquidity pool-based DEX include KyberNetwork, Bancor, and Uniswap.
One of the features offered by CEX is margin trading functionality. Margin trading allows investors to trade with leverage, increasing users' purchasing power for potentially higher returns. Innovations introducing margin trading on DEX have also emerged. Examples of DEX providing decentralized margin trading include dYdX, NUO Network, and DDEX.
The Uniswap exchange is a decentralized token trading protocol built on Ethereum that allows users to directly swap tokens without using a centralized exchange. When using a centralized exchange, you will need to deposit tokens into the exchange, submit an order on the order book, and then withdraw the exchanged tokens.
On Uniswap, you can easily swap your tokens directly from your wallet without going through the three steps mentioned above. You just need to send your tokens from your wallet to the Uniswap smart contract address, and then you will receive the tokens you want in exchange in your wallet. There is no order book, and the exchange rate of the tokens is determined by an algorithm. All of this is achieved through liquidity pools and the automated market maker mechanism.
Liquidity pool
The liquidity pool is a token reserve located on the Uniswap smart contract, available for users to swap tokens with. For example, in an ETH-USDT liquidity pool containing 100 ETH and 20,000 USDT, a user wishing to purchase ETH can send 202.02 USDT to the Uniswap smart contract to exchange for 1 ETH. Once the exchange is completed, the liquidity pool will have 99 ETH and 20,202.02 USDT left.
The reserves of the liquidity pool are provided by liquidity providers, who receive a corresponding proportion of Uniswap's 0.3% trading fee as an incentive. This fee is charged for every token exchange on Uniswap.
Uniswap imposes no restrictions on liquidity providers; anyone can become a liquidity provider—the only requirement is that liquidity providers must provide ETH and another token to trade against (according to the current Uniswap exchange rate). The quantity of reserves held in the liquidity pool plays a significant role in determining how the automated market maker mechanism sets prices.
Automated market maker mechanism
The price of assets in the liquidity pool is determined algorithmically through the automated market maker (AMM) algorithm. The AMM operates by maintaining a constant product based on the liquidity on both sides of the fund pool. Continuing with the example of the ETH-USDT liquidity pool, which has 100 ETH and 20,000 USDT.
To calculate the constant product, Uniswap multiplies these two quantities. "X * Y = K"
Using AMM, a constant product (k) must always be maintained at 2,000,000 at any given time. If someone wants to buy ETH with USDT, ETH will be removed from the liquidity pool, while USDT will be added to the liquidity pool. The price of ETH will be determined incrementally. The larger the order, the higher the premium. The premium refers to the additional amount of USDT required to purchase 1 ETH compared to the original price of 200 USDT/ETH.
How to add a new token on Uniswap?
Unlike centralized exchanges, Uniswap, as a decentralized exchange, does not have a team or auditors to evaluate and decide which tokens to list. Instead, any ERC-20 token can be listed on Uniswap by anyone, and trading can occur as long as there is liquidity for the given trading pair. Users only need to interact with the platform to register a new token, and then the trading market for that new token will be created.
