On a centralized exchange, the price of an asset is determined by a limit order book(matching highest bid with lowest ask).
In a decentralized exchange (DEX) such as Uniswap, liquidity providers (LPs) provide a pair of tokens to a pool (e.g. ETH/BTC)
Invariants
pool contains multiple coins (USDT, BTC, DAI, etc...) and price is determined by keeping a function of the coin supply to a constant: f(x1,x2,…,xn)=cst.
The price of the pair coin i / coin j is then dxjdxi
simplest case is two coins
Linear invariant: x+y=k but the pool can be drained completely.
constant product formula proposed by Vitalik and later popularized by Uniswap: balance of coin Ax×balance of coin By=k where k is a constant balance of assets
the only way to keep k constant as orders come through is that x and y move in inverse directions. They don't move linearly however:
this model can always provide liquidity by asymptotically raising the price of the coin as the desired quantity increases
The price is given by the derivative (slope of the graph). In the Uniswap invariant model, the depth of the pool determines the slippage (see next sections)
The Stableswap invariant combines both linear and Uniswap invariants (see paper):
the pool can slide up or down the linear invariant (price is constant) as long as the pools are somewhat balanced
when the pools fall out of balance, we switch to the uniswap invariant (swapping becomes expensive)
Stableswap invariant for multiple coins: χDn−1∑ixi+∏xi=χDn+(D/n)n
χ represents the amplification parameter of the linear invariant (magnifies the low slippage portion of the curve). We multiply by Dn−1 to make χ dimensionless.
D is the total amount of each coin, were they equal
χ is dynamic: χ=A(D/n)n∏xi. The ratio diminishes as the coins fall out of balance (the max of ∏xi is attained when all the xi=D/n)
Uniswap invariant: price impact
price is given by pA(B)=dxdy=dxdxk=−x2k
imagine one wants to buy Δy coins B. The price one will pay is Δx and must respect:
(x+Δx)×(y−Δy)⇒Δx⇔price per unit of coin BΔyΔx=x×y=k=x(1/ry−1) where ry=yy−Δy∈[0,1] is the percentage of change in the supply of coin B=Δyx(y−Δyy−1)=yxΔyyy−ΔyΔy=yxy−Δyy=base price×size of supply after tradesize of supply before trade≥base price
Therefore, if the pool increases, our order Δy has less of an impact on the supply of coin B:
∂x∂Δx∂y∂Δx=1/ry−1=y−ΔyΔy=size of order relative to new pool=−(y−Δy)2Δy
We can see that:
- as the supply of coin A increases, the rate of change of the price we will pay is smaller if the pool of coin B was larger to begin with
- as the supply of coin B increases, the price we pay diminishes.
x/y represents the price of the trading pair.
front-running: if the miner places your order (therefore increases its price) and sells it to you at a higher price. You can set maximum slippage to avoid this.
the premium makes it prohibitively expensive for whales to transact large size.
price impact: after buying Δy coins B for Δx coins A, the token supply increases for A and decreases for B, therefore the next base price is y+Δyx−Δx≥yx
Uniswap invariant: liquidity provider point of view
Assume you add Δx worth of coin A and Δy worth of coin B. The new constant is (x+Δx)×(y+Δy) therefore the price of the pair coin A / coin B becomes x+Δxy+Δy. If it is different than xy, then an arbitrage opportunity arises between other exchanges. Thus we want: x+Δxy+Δy=xy⇒Δx=xy(rx−1),rx=xx+Δx. That is, the token supply in the pool must increase at the same rate yΔy=xΔx
liquidity providers get rewarded by charging a small exchange fee to the trader (0.3% for Uniswap)
Impermanent loss
When the price of your tokens changes compared to when you deposited them in the pool.
example: if the price of coin A increases on Binance compared to the pool, arbitrage traders buy the pair coin A / coin B (i.e. add coin B to the pool in exchange for coin A until the price of the pair reaches the target)
the size of the pool increases so LPs might profit but they might have been better off hodling. The only thing impermanent loss cares about is the price ratio relative to the time of the deposit.
To read: https://research.paradigm.xyz/amm-price-impact
Let us denote:
price of coin A in USD at time 0: p0A
price of coin B in USD at time 0: p0B
quantity of coin A in pool at time 0: x0
quantity of coin B in pool at time 0: y0
Assume coin A experiences a return rA∈[−1,∞), bringing it to price p1A=(1+rA)p0A at time 1.
Assume coin B experiences a return rA∈[−1,∞), bringing it to price p1B=(1+rB)p0B at time 1.
Since there is no arbitrage opportunity at time 0, one must have p0Bp0A=x0y0 (look at the dimensions of each variable to make sense of it)
Let us calculate quantities of coin A and B at time 1, x1 and y1, such that no arbitrage opportunity arises:
p1A/p1B=x1/y1⇒x1p1A/p1B=y1
Moreover, by the constant product formula: x1y1=k=x0y0
Therefore:
x12p1Bp1A⇒x12γp0Bp0A⇒x12γx0y0⇒x1=γ1x0 and y1=γy0=x0y0=x0y0,γ=1+rB1+rA=x0y0
The return of holding the coins is:
rhold⇒rhold⇒rhold=p0Ax0+p0By0p1Ax0+p1By0=p0Ax0+p0By0p0A(1+rA)x0+p0B(1+rB)y0=2(1+rA)+(1+rB) since p0Ax=p0By by the no-arbitrage argument above
Similarly, the return of providing liquidity (assuming no LP fees):
rpoolrpool=p0Ax0+p0By0p1Ax1+p1By1 replacing quantities at time 1 by their formulas + no-arbitrage argument=2(1+rA)γ1+(1+rB)γ
We can see this doesn't depend on the quantities or prices of the coins but merely their returns. The formula is also symmetrical in coin A or B, regardless of initial quantities and prices. Plotting the impermanent for different values of rA and rB:
Curve
Curve is a stablecoin DEX (exchange between similar ERC-20 tokens such as USDC/DAI an WBTC/renBTC in order to minimize impermanent loss)
Assume you are a whale, sitting in stablecoin (e.g. USDC) and you need to trade for another stablecoing (e.g. MIM), you want to minimize slippage by minimizing fees (0.3% for Uniswap vs 0.04% for Curve) and maximizing ilquidity to remain as close as 1:1 as possible.
Curve TVL (total value locked): 23bn/Uniswap(nextleadingDEX):8bn
Curve has a governance token named CRV. Users can lock their CRV in exchange for vote escrowed-CRV (i.e. veCRV) and a 50% share of each trading fee proportional to their locked amount.
veCRV vote each week to which liquidity pools the set of CRV emissions will be directed (aka gauge weights)
Projects don't need to incentivize liquidity holders with their native tokens if their pools get CRV tokens (i.e. a passive income). https://twitter.com/JackNiewold/status/1476334616797749248
Projects want their pool (e.g. MIM3CRV) to suffer minimal slippage and de-pegging. They want their coin to be the most liquid stablecoin. They can buy up CRV, lock it in exchange for evCRV and sway the vote to ensure that emissions are going towards their pool. However, a more cost-effective way is to bribe veCRV holders individually / the largest holder of veCRV (i.e. Convex)
Convex
Convex allows LPs to deposit CRV and receive the boosted CRV rewards as if they had locked their CRV for veCRV.
They still use Curve on the back end, but they enable the boosted rewards through the amount of CRV that they amassed over the past 6-8 months:
convert CRV to cvxCRV (liquid unlike veCRV), stake cvxCRV and earn CVX, trading fees (normal veCRV rewards: 50% of trading fees), airdrops and 10% of the platform's CRV earnings.
when you lock CRV in return for cvxCRV, on the back end you are perpetually locking up your CRV (although you can trade cvxCRV for CRV)
Andre Cronje created [[bribe.crv.finance]]. Protocols can put up a set amonut of their native token to encourage veCRV holders to vote in favour of their pool.