DEX Overview

Loop DEX is the first AMM (automatic market maker) on Terra.

Decentralized Exchange

Loop DEX is an automatic market maker for token pairs (or native coins like UST) called liquidity pools, which enable users to exchange one asset for another via smart contracts. Users can provide liquidity in exchange for reward-bearing liquidity pool (LP) tokens.


Constant Product

Exchange prices set by liquidity pools are based on a constant product invariant.
X = amount of source asset in liquidity pool (token offered by user)
Y = amount of target asset in liquidity pool (token received by user)
k = constant product invariant
The product of the number of tokens on each side of the pool remains constant across trading operations (buying / selling).


To preserve the constant product invariant,
, prices are automatically adjusted so that the product of liquidity pool balances after a swap is as close as possible to the product calculated before a swap.
XY=k=(X+Ain)(YBout)XY=k=(X+A_{ in}​)(Y−B_{out})
= tokens added to liquidity pool (tokens offered by user)
= tokens removed from liquidity pool (tokens received by user)
To determine the proper value of
given the trader’s offered asset,
Bout=YAinX+AinB_{out}​=\frac {Y A_{in}}{X+A_{in}}
Swaps are executed using the current balances of the liquidity pool (X and Y) and the number of incoming (user offered) tokens,
The reported, or market, price is the number of the pool’s target tokens, Y, divided by the source asset, X (also called the pool ratio), Y/X.
To determine the proper value of the expected price,
, given the trader’s offered asset,
Bexp=YAinXB_{exp}=\frac {YA_{in}}X
The spread is the difference between the executed price
and the expected price,
, is:
spread=max(YAinX+AinYAinX,0)spread=max(\frac {Y A_{in}}{X+A_{in}} - \frac{YA_{in}}X,0){}
When a pool has large balances of tokens on both sides, the spread becomes smaller and helps the pool execute closer to its reported price of Y/X.