https://www.youtube.com/watch?v=WhGayGttXA4&t=427s

In our previous blog post, we explored the origins of DeFi and how it differs from traditional finance. Now, let's dive deeper into a cornerstone of DeFi: decentralized exchanges (DEXs).

What are DEXs?

Unlike traditional exchanges controlled by central authorities, DEXs operate on a peer-to-peer (P2P) basis. They leverage smart contracts to facilitate token swaps directly between users, eliminating the need for intermediaries. DEXs reside within the application layer of the DeFi stack, providing user interfaces for interacting with smart contracts in the protocol layer.

Understanding AMMs: The Engine of DEXs

Automated market maker (AMM) model relies on liquidity pools, which are essentially digital reserves of tokens. Users can swap tokens by directly interacting with these pools.

How Liquidity Pools Work

Imagine a pool containing two tokens, red and blue, each with an initial price of $1.

To maintain price stability, AMMs follow a mathematical formula called the constant product formula:

                                                                 $$x * y = k$$

Here, x and y represent the token amounts, and k represents the total value of the pool (which remains constant). Let’s simplify the equation with our token color initials.

                                                                 $$r * b = k$$

Example: Alice Swapping Red Tokens for Blue Tokens

Let's assume there is a liquidity pool containing 10,000 red tokens and 10,000 blue tokens.

Red/Blue Pool

Initially, the equation representing the pool's balance is:

                              $$10,000 (red tokens) * 10,000(blue tokens) = 100,000,000$$

Let's say Alice wants to exchange 1,000 red tokens for blue tokens. The pool has to answer the following question

If I get 1,000 red tokens, how many blue tokens I can give to this person so that equation still holds?

The core principle of the AMM is to maintain the constant product within the pool.

After receiving 1,000 red tokens from Alice, the pool now holds 11,000 red tokens. To maintain the constant product, the pool needs to calculate the new number of blue tokens, let’s call it z. The equation becomes:

                               $$11,000 (red tokens) * z (blue tokens) = 100,000,000$$

Solving for z, we find that z = 9,090 blue tokens. Since the pool initially held 10,000 blue tokens, it can give Alice 10,000 - 9,090 = 910 blue tokens.

This difference in amounts exchanged is called slippage.

Slippage Explained

Slippage refers to the difference between the expected price of a token and the actual price received during a swap. It occurs because the pool's token balance and price change with each trade. While DEXs offer flexibility, slippage can be a factor, especially for larger trades or tokens with low liquidity.

How to Minimize Slippage?

Users can minimize slippage by

  • trading smaller amounts

  • using high liquidity pools as their swap amount will be so small that the slippage wont be noticeable.

Who Provides Liquidity?

Anyone!
Liquidity providers are users who deposit their tokens into liquidity pools. They earn fees from each swap transaction.

Liquidity providers gain fee with each swap operation

However, there's a potential downside: impermanent loss.

Impermanent Loss Explained

While providing liquidity can generate income through trading fees, liquidity providers face a risk called impermanent loss.

Imagine Bob provides liquidity to a pool containing equal amounts of red and blue tokens. Let's say the price of red tokens suddenly increases significantly. To maintain the constant product formula, the pool must adjust its balance. This means Bob's holdings within the pool will adjust accordingly. He'll end up with more red tokens and fewer blue tokens.

Example:

  • Initial Scenario: Bob deposits 1,000 red tokens and 1,000 blue tokens into the pool in the first example.

  • First Action: Alice swaps 1,000 red tokens for 910 blue tokens.

  • Pool Adjustment:

    • To maintain the constant product, the pool's balance must adjust. Let's say, after Alice's trade and subsequent price changes, the pool now holds 11,000 red tokens and 9,090 blue tokens.

    • Since Bob owns 10% of the pool, his holdings also adjust accordingly. He now has 1,100 red tokens and 909 blue tokens.

  • Impermanent Loss: If Bob had simply held his initial 1,000 red tokens and 1,000 blue tokens, he would have profited more from the rising price of red tokens. This difference in value between holding the tokens and providing liquidity is called impermanent loss.

This example demonstrates how a change in token prices can impact a liquidity provider's holdings and potentially lead to impermanent loss.

Uniswap: A DEX in Action

Uniswap is a popular DEX that utilizes the AMM model. When swapping tokens on Uniswap, you'll encounter various factors:

Uniswap's token swapping interface

Here we are trying to exchange 1 ETH with USDC. Let’s go over each factor here.

  • Fee is the platform fee charged by Uniswap and partially distributed to liquidity providers

  • Network Cost is the gas fee required by the Ethereum blockchain to execute this transaction

  • Order Routing: This feature searches for the best swap price across different liquidity pools, potentially utilizing multiple pools for optimal efficiency.

  • Price Impact is the impact your trade has on the market price of the pool

  • Max Slippage : Users can set a slippage tolerance limit to prevent trades with excessive slippage. This means if your expected token amount changes more than this value, the transaction will revert and you are guaranteed to get your tokens back instead of completing the swap operation

Order Book DEXs: A Different Approach

While AMMs are dominant, some DEXs utilize an order book system similar to traditional exchanges. Order books list buy and sell orders at specific prices. Trades are executed when matching buy and sell orders are found.

However, current blockchain limitations make order book DEXs less efficient and more expensive compared to AMMs. The centralized versions of order books work way more efficiently, but they are not aligned with the core principle of web3: decentralization.

Orderbook

Conclusion

Understanding AMMs, liquidity pools, and slippage is crucial for navigating DEXs effectively. And that's it. Thank you for sticking until now, and I hope everything is clear.

In the next post, we'll dive into another crucial DeFi component: Oracles.

Mirror文章信息

Mirror原文:查看原文

作者地址:0x6e9C67aFB0E76d4D3cbAf3A33723bC35e0652465

内容类型:application/json

应用名称:MirrorXYZ

内容摘要:vHVlnkwMmmdGLSON9FHcZjI66RiRhy94WGrwmMgIyZ8

原始内容摘要:0FssA2e0b-Et7ONDiESWdt1f-T8he7lryq5jIjzlWw4

区块高度:1585364

发布时间:2025-01-09 09:47:52