Decentralized exchanges (DEXs) can surprise you with moving prices while you trade which causes you to receive less from a trade than you thought you would. It’s like some of your value evaporating each time you trade.
When you trade tokens on a DEX, you "swap" a token by transferring it into a liquidity pool and getting another token in exchange. In order to make this possible, the liquidity pool has to hold both assets, and it needs to continue to hold enough of both assets to be able to handle the next trade as well. To ensure that it maintains a balance of both assets, liquidity pools determine their exchange rate based on the ratio of the two tokens in the pool, which means that the more you change the ratio, the more you pay to exchange assets.
For example, imagine you want to exchange 20 SOL
for LUNA
. A liquidity pool holds 100 SOL
and 200 LUNA
. A problem arises because liquidity pools set exchange rates based on the ratio of the two assets they hold. A simplified but representative pricing algorithm [1] would determine the exchange rate as $\frac{200 \text{ LUNA}}{100 \text{ SOL}} = 2$.
However after you've exchanged your first SOL
, the liquidity pool now holds 101 SOL
and 198 LUNA
, and the exchange rate is now $\frac{200 - 2 \text{ LUNA}}{100 + 1 \text{ SOL}} = 1.96$.
After exchanging another SOL
at this price the pool's exchange rate would be $\frac{200 - 2 - 1.96 \text{ LUNA}}{100 + 1 \text{ SOL} + 1} = 1.92$. Your trading is causing price impact and it will cause you to get significantly worse prices when trading illiquid tokens.
Price impact is a form of slippage: the difference between the price you expect from a trade and the price you get. A naive trader may expect to get an exchange rate of 2 SOL
per LUNA
from the example liquidity pools and expect to get back 40 LUNA
, but do the math- they'll end up with only 33.6.
Efficient traders obsess over maximizing their price performance because the compounding drag of slippage tends to dramatically reduce long term performance. What can you do to minimize price impact?
As a rule of thumb, a trade's price impact is usually twice the ratio of the trade volume to the liquidity pool volume. If you double the liquidity pool size, you'd cut your price impact in half, so as a trader you'd wish all liquidity pools would combine into one. [2] In reality, there are lots of liquidity pools but their liquidity is independent.
Jupiter simulates this benefit of combined liquidity by splitting orders across multiple liquidity pools simultaneously. The result is equivalent to adding all available liquidity pools into one!
With two identically sized liquidity pools in this example, the resulting 36.7 LUNA
is significantly better than 33.6 LUNA
received using a single liquidity pool.
Optimizing the use of liquidity requires monitoring the size of many liquidity pools and calculating the optimal way to split the order across pools. Even with just a few liquidity pools that's infeasible without a service like Jupiter to handle it for you.
One of the biggest problems of trading altcoins is that it's hard to find liquidity pools between two relatively smallcap tokens. For example imagine we want to trade 7 mSOL
for RAY
. As of writing the Serum mSOL-RAY
pool available on Raydium has just $277,160 of liquidity. Trading just $1,000 of value may cause nearly a 1% price impact. At the time of writing, however, that there is an mSOL-USDC liquidity pool with $56M of liquidity and a RAY-USDC
pool with $67M of liquidity.