Changelog Image
Back
November 26, 2021
Research

Uniswap V2: A Constant Function Market Maker

The mechanics of Uniswap V2, a constant function market marker, explained.

Changelog Image
0xDanr 
Uniswap V2: A Constant Function Market Maker

Introduction to CFMMs

Constant Function Market Makers (CFMMs) are innovative solutions to the problems of decentralized exchanges. In the years prior to the implementation of CFMMs, developers implemented DEXs by replicating traditional order books (CLOBs) used by centralized exchanges. The result was excessive network transaction fees & high latency due to the difficulty in managing each order on chain.

Fortunately, the implementation of CFMMs solved the problems of excessive fees and low liquidity. Although there are several types of CFMMs, in this article I will explain the mechanics behind the most popular CFMM model, Uniswap V2.

What is a CFMM?

A CFMM is a type of automated market maker, AMM, utilizing a function that relies on a constant value to quote prices to users. The most popular implementation of this model is Uniswap.

Uniswap quotes prices based on a ratio of the reserves of two tokens held within its pools. When a user trades with Uniswap, she adds X amount of one token in exchange for a Y amount of another token. However, the Uniswap smart contract pays Y amount of token according to the product of the reserves in that pool, which must remain constant. Uniswap keeps track of the product of its reserves according to the formula x * y =k. As the quantity of the reserves changes, the price of each asset moves to maintain the constant product, k.

How does Uniswap Quote Prices?

To delve further into the subject, take for example a pair ETH/USDC with reserves 100 ETH and 10,000 USDC.

In this scenario, Uniswap determines the price of ETH in terms of USDC by dividing the 10,000 USDC by 100 ETH.

This method of quoting price implies that the value of the reserve of one token is equivalent to the value of the reserves of the other.

To determine the amount that will be exchanged in the event of a trade, Uniswap keeps track of an invariant through the following formula

Where L is the liquidity invariant, and x and y are the reserve quantities of token X and Y respectively. L is the geometric average of the reserve quantities within Uniswap.

If a user wishes to sell Δx amount of token X in exchange for Δy amount of token Y, the Δy will be given to the user for the Δx the pool receives while keeping L constant. That is

To calculate the value of Δy as a function of Δx, assuming no trading fees charged by Uniswap, we can use the following formula

If a user were providing Δy to obtain Δx instead, simple replace the parameters x and Δx for y and Δy.

Uniswap does not provide exchange services for free, however. It charges a trading fee, φ, which is currently 0.3%

This invariant formula to determine the quantities paid out gives a worse price than what is quoted by simply calculating the ratio of the reserves. The reason Uniswap is built this way is to replicate the market impact a trade has on the price as in the case of a traditional order book. As traders supply more of X in exchange for Y they receive progressively less of token Y. The price of token Y is rising. The reverse happens when traders supply more of token Y to get token X.

As Δx goes to infinity, Δy converges to Y but never equals it. Y is the horizontal asymptote. Therefore, one can never buy the entire amount of Y available in the pool.

Are Prices Fair?

As you can imagine from the above formulas, price discrepancies will be frequent. At any time Uniswap could be quoting a price that is too high or low compared to other exchanges. However, given the open nature of Uniswap (anyone with internet & ETH can use the DEX), these discrepancies give opportunity for arbitrageurs to take profit.

If Uniswap is selling a token at a cheaper value than other exchanges, traders can purchase the token in Uniswap and sell it elsewhere for a profit. The reverse is true if Uniswap is selling a token at a higher price than other exchanges.

The possibility of arbitrage is what ensures that prices in Uniswap are fair. A Uniswap pool with a large amount of liquidity would need a large transaction to significantly alter the price from the rest of the market. However, such an event would create a great incentive for an arbitrageur to correct it, since the greater the deviation and quantity required to correct it, the greater the profit.

Where do the Reserves in Uniswap come from?

Uniswap gets its reserves from other users that wish to earn fees from the DEX. Users providing reserve tokens are known as Liquidity Providers (LPs). They provide liquidity, an amount of token X and token Y in the same ratio as the current Uniswap ratio price. In exchange for providing liquidity, LP’s earn a percentage of the fee that is equivalent to the percentage their liquidity represents in the pool. This liquidity is paid out in the form of growth in the liquidity invariant (L) from the fees paid by traders.

Uniswap V2 pools are implemented as ERC20 tokens so that liquidity providers receive shares that represent their ownership of the Uniswap pool reserves. This enables LPs to withdraw their reserves from the pool at a future date. If the pool has accumulated trading fees, the liquidity invariant, L, has grown, that means the reserves within the pool have grown. Therefore, the LP earns fees as the additional reserve tokens his liquidity pool share represents.

The value of the LP shares are independent of other liquidity providers addition or removal of liquidity. The value of the liquidity pool shares are solely dependent on the growth of the pool, that is the growth of L from trading fees, and the price being quoted by the liquidity pool.

Reserves: 100 ETH / 10,000 USDC

Liquidity Invariant = 1,000

Total LP Shares = 1,000

LP1 owns 100 LP shares, equivalent to 10 ETH and 1,000 USDC

Assume invariant grows 50% from trading fees to 1,500 and the price remains constant. The reserves are now 150 ETH and 15,000 USDC.

Another LP redeems 200 LP shares, equal to 300 units of L (30 ETH & 3k USDC). There are now 800 LP pool shares, 120 ETH & 12k USDC in the pool, and L is 1,200.

Despite the withdrawal, the value of LP1's position is unaffected, assuming constant price.

Before withdrawal,

LP1R = 1,500/150 = 1/10th of 150 ETH & 15k USDC = 15 ETH & 150 USDC

After withdrawal,

LP1R = 1,200 / 150 = 1/8th of 120 ETH & 12k USDC = 15 ETH & 150 USDC

Had the price changed however, her total return would’ve been affected by it, although still not by the actions of other LPs.

The formula for determining the number of shares a liquidity provider receives is not relevant to this article and can be found in the Uniswap V2 white paper.

What truly has an effect on the value of LP shares is the price of the tokens. As the price goes up, the value of the shares increase and vice verse. We will talk more about the effects of price variation on the total returns to LPs in the subsequent article.

Safety

Since CFMMs are smart contracts, they are not subject to the principal-agent problem traditional exchanges suffer. As Crypto Twitter likes to say if you dont own the keys then they are not your coins. That is, since there are no intermediaries, assuming a properly developed smart contract, there is no way for an exchange or third party to steal your funds. However, that does not imply that CFMMs are risk free.

CFMMs are subject to market manipulation from the smart contracts of the tokens they make markets on. For example, one can design a rug pull where a smart contract has the option to give founders the ability to mint an infinite supply of tokens. Once there is some liquidity in their CFMM pool, they mint enough tokens to remove the liquidity by crashing the price of the token.

Conclusion

CFMMs were the 0 to 1 innovation for DeFi since they are a more scalable approach for the limitations of blockchain architecture. In addition, they provide a novel opportunity for passive investors in the form of market making.

Although, CFMMs are built as smart contracts and therefore not dependent on the risk of intermediaries, they can still be subject to attacks through the tokens they hold as reserves. Therefore, a LP should verify that the tokens he is providing liquidity for have sound tokenomics and smart contracts.

In the following article, I’ll discuss additional risks to providing liquidity from market movements and how these risks affect returns in Uniswap V2.