Back to Blog
tutorials

What Is DEX? Beginner Guide to Decentralized Exchanges (Part 2)

This is part 2 of the DEX understanding tutorial. I hope you read that one. If you missed, do check it out here: [**What Is DEX? Beginner Guide to Decentralize...

mscode073 min read
What Is DEX? Beginner Guide to Decentralized Exchanges (Part 2)

This is part 2 of the DEX understanding tutorial. I hope you read that one. If you missed, do check it out here:

What Is DEX? Beginner Guide to Decentralized Exchanges (Part 1)
If you’ve spent time in Web3, you’ve probably heard the term
DEX.medium.com

Now, let’s begin with Part 2 where we are going to explore

  • How does DEX work?
  • What is CPA (Content Product Algorithm)
  • Key points of DEX
  • What is DEX and DEX Aggregator
  • What’s the difference between DEX and DEX Aggregator

How does DEX work?

The DEX works the same as CEX, but the core difference is that. It is decentralized. No one organisation is controlling your money. This is the core here.

Let’s dive deep into how it really works under the hood and what the key jargon is here.

Liquidity Pools:

“Liquidity pools are collections of tokens locked in smart contracts that facilitate trading and other functions within decentralized platforms. In traditional finance, market makers provide liquidity by standing ready to buy or sell assets. However, in decentralized finance, liquidity pools replace the need for these centralized market makers, allowing anyone to participate.

There are 2 types of people 

i Asker — Who has many SOL

You can think of askers as Sellers, who have any token here, for example, we picked SOL, and they want to sell their token in exchange for USDC.

#### ii Bidder — Who has many USDC

They are the customers here who have USDC and want to buy any coins like SOL or anything.

Here in the pool, both come and try to find the best deal for themselves, as I have some I want to sell them, so I need to list it here, and people who want to buy the SOL token will put their deal. And in the end, the best one will win. There is also a role of Slippage, if you don’t know, I have explained it in the first part.

What is the Constant product algorithm?

In decentralized exchanges (DEXs), a constant product formula is the pricing algorithm used in Automated Market Makers (AMMs)

Core Principles of (x times y = k)

  • (x): The reserve amount of Token A (e.g., ETH).
  • (y): The reserve amount of Token B (e.g., USDC).
  • (k): The constant (invariant) that must remain unchanged after a swap.
  • Mechanism: If a user swaps ETH for USDC, the amount of ETH in the pool increases ((x)), which forces the amount of USDC to decrease ((y)) to keep (k) the same. This automated change in ratios calculates the price. [1, 2, 3]

How the Algorithm Works

  1. Initial State: A pool starts with 10 ETH and 10,000 USDC.
  • (k = 10 * 10,000 = 100,000)
  1. Trade Occurs: A user wants to buy ETH, so they add 1,000 USDC to the pool.
  2. New Token Amount: The pool now has 11,000 USDC ((10,000 + 1,000)).
  3. Maintaining (k): The pool must now have (100,000 / 11,000 approx 9.09) ETH remaining.
  4. Output: The user receives (10–9.09 = 0.91) ETH.

✨ Thanks for reading! If you would like more Solana tech blogs, subscribe to me here for weekly insights.

About Me

I’m Abhishek (mscode07) — developer & indie hacker.
I write about Web3, building, and learning in public.

Feedback

I’m still improving my writing — 
So feel free to share honest feedback (good or bad).

Connect: Website |Twitter Read more on Medium: mscode07