- Unlocks Calendar
- Posts
- The math behind DEXs
The math behind DEXs
How do they work + how do they generate value for token holders
Hello anon,
Everybody used Uniswap / Sushiswap in its crypto experience, but how Decentralized Exchanges actually work technically? What’s the math behind them?
Decentralized exchanges (DEXs) enable the exchange between a pair of tokens through a liquidity pool, and the exchange rates between two tokens are determined through a predefined function of the supply of tokens in the pool
This is fundamentally different from centralized exchanges (e.g., Binance and Coinbase) which match between buy orders and sell orders.
The price on centralized exchanges is the price of the most recent trade.
The pricing function implemented by DEXs like Uniswap and Sushiswap is the “constant product function”
Consider a liquidity pool associated with token A and token B.
The constant product function means the amount of token A times the amount of token B is a constant K
When a liquidity transaction happens, the liquidity supply in the pool is changed, so K will be changed, but if it is a trading transaction, the liquidity supply does not change and K keeps the same
We use two numeric examples to demonstrate how liquidity and trading transactions work in Uniswap
1) How Liquidity transactions work:
Assume the initial liquidity supply is 100 token A and 10,000 token B in the liquidity pool.
Since the value of token A is always equal to the value of token B, the current exchange rate between token A and token B is 100 : 1.
Whenever liquidity is deposited into a pool, unique tokens known as LP tokens are minted and sent to the liquidity providers as a receipt of depositing liquidity.
The amount of LP tokens a liquidity provider receives is proportional to the amount of liquidity the liquidity provider deposits into the liquidity pool
If it is a new liquidity pool, the amount of LP tokens the liquidity provider will receive is equal to sqrt(x × y), where x and y is the amount of token A and token B deposited
Suppose a liquidity provider deposits an equal value of both tokens, 10 token A and 1000 token B. It is 10% share of the liquidity and the amount of LP tokens the liquidity provider receives is 100
The liquidity supply becomes 110 token A and 11,000 token B in the liquidity pool.
2) How Trading transactions work:
Assume currently there are 100 token A and 10,000 token B in the liquidity pool.
The question is how many token B should be put into the pool if a trader would like to get 20 token A
Since the pricing curve is defined by x × y = 1,000,000 and the trading transaction does not change the constant, we will get (100 - 20) × (10,000 + y ) = 1,000,000
y = 2,500, so the trader needs to deposit 2,500 token B to get 20 token A. To facilitate the trading transaction, the trader needs to pay 0.03% of the amount of input token, 0.75 token B as trading fees
The 0.03% fee is added to the pool as new liquidity
The fee is distributed proportionally to all liquidity providers in the pool upon completion of the trading transaction.
************
If interested in supply emissions and tokenomics for >100 coins, we sell full access to our databaseSend an email to [email protected] or DM us on Twitter https://twitter.com/UnlocksCalendar)
************