Understanding k in DEXs with Trading Fees
In previous discussions, we established that k (the constant product of reserves) changes when liquidity is added or removed but remains constant during swaps – assuming no fees. However, in reality, k almost always changes due to trading fees.
This article focuses on how k evolves when swaps include fees, as liquidity provision/withdrawal does not incur fees.
Why Do Fees Impact k?
This is one of the most misunderstood concepts in DEXes.
Key Insight:
- Fees are not charged when adding/removing liquidity – only during swaps.
- Trading fees accumulate inside the pool, altering the reserves and thus changing k.
Let’s break it down step by step.
How a Swap with Fees Affects k
Consider a liquidity pool with:
- x₁ reserves of Token X
- y₁ reserves of Token Y
- f = DEX fee (e.g., 0.3% for Uniswap)
When a Trader Swaps x₂ of Token X for Token Y
The trader receives y2,with fee (less than the no-fee scenario due to the fee deduction):
Intuition Behind the Fee Mechanism
- Without fees, the trader would receive y2,no fee.
- With fees, they get y2,with fee, where the difference approximates the fee rate f:
Where does the fee go?
- The fee is paid in the input token (Token X).
- While the trader spends x2, only (1 – f) × x2 is used for the swap.
Calculating k Before and After Fees
1. No-Fee Scenario
After the swap, the new reserves are:
- x1 + x2 (Token X)
- y1 – y2,no fee (Token Y)
Thus, k remains:
2. With Fee Scenario
The new reserves are:
- x1 + x2 (Token X)
- y1 – y2,with fee (Token Y)
Thus, k changes to:
Key Takeaway
- Each Token X → Token Y swap increases Token X reserves only.
- Each Token Y → Token X swap increases Token Y reserves only.
- This asymmetrically impacts k over multiple swaps.
Why Does This Matter?
The change in k helps measure total accumulated fees in the pool. This is crucial for:
- Liquidity providers (LPs) evaluating returns.
- Impermanent loss (IL) calculations (next topic).
Brief Summary
✅ k changes with swaps when fees are applied (but not with liquidity adjustments).
✅ Fees are paid in the input token, altering reserves asymmetrically.
✅ Tracking k helps quantify fee accumulation for LPs.
Next: Impermanent Loss and Fee Impact
Now that we understand how fees alter k, we can explore how this affects impermanent loss – the difference between holding assets in a pool versus holding them outright.