The math behind Defi is not as hard as you think
I was never a good student at math. Math made me uncomfortable. But investing in Defi gets me to re-study it. And this time, I found its beauty. Because things that make me money are always lovelyš . If you are playing with Defi being a non-professional player like me and want to understand what your money is doing, this article may help. I will explain the basic math behind the decentralized exchange by exercising middle schoolās math equation.
The significant difference between a traditional centralized exchange and a decentralized exchange is this.
In the centralized exchange, like NYSE, Coinbase, Binance, etc., to make a transaction happen, you need a seller and a buyer that agree on the same price for selling/buying an asset. When the seller wants to sell at $100, the buyer wants to buy at $99, the transaction wonāt happen. Until both sides reach the same price, say $99.5, the transaction will conclude.
In the decentralized exchange, like Uniswap, PancakeSwap, etc., a transaction ALWAYS happens thanks to the smart contract. When a person wants to buy or sell an asset, he interacts with the smart contract but not a physical counterpart. The smart contract can ensure that the transaction goes through. How does it work?
The decentralized exchange needs humans to provide liquidity (liquidity provider) and swap between two assets. The rest of the job is done by the smart contract. A mathematical formula is implemented in the smart contract for the functioning of the liquidity pools. Iād use Uniswap as an example, the largest Defi exchange is functioned by a simple formula that I learned in middle school.
Iām sure youāve met this classic formula at school: x*y=k, where k is a constant.
This is the formula used by Uniswap to govern its liquidity pools. X and y represent the quantity of tokens in a pool of two tokens. As neither of the variables can be zero, the trade will always happen along the curve. A liquidity pool generally requires the pair of tokens have the same value. So, two conditions should be satisfied in a pool,
- Quantity of token A * Quantity of token B = k
- Price of token A * Quantity of token A = Price of token B * Quantity of token B
Letās use a pool of ETH and USDT for simple illustration. Assuming that k= 200,000.
Before any trade happened:
Now suppose that ETH price rises to 2,200 $, USDTās price is stable where 1 USDT = $1. The quantity of ETH and USDT will be changed according to the formula. Iād use this middle school math exercise to find out the new amounts. Consider it a simplified proxy to the actual algorithm.
x = ETH quantity; y= USDT quantity
The quantities of ETH and USDT will become:
As ETH price goes up (from $2,000 to $ 2,200), its quantity goes down (from 10 to 9.53). But if ETH quantity goes up as people are selling ETH for USDT, increasing ETH quantity while decreasing USDT in the pool, what happens? Letās assume the quantity of ETH increases to 11 and the price is $2,200:
This would pull up the price of USDT to $1.33. In this pool 1 ETH equals 1654 USDT, while elsewhere 1 ETH equals 2,200 USDT (1 USDT = $1), creating an arbitrage opportunity. Arbitrageurs will buy ETH from this pool until the price of ETH matches the market price. But in fact, before ETH quantity reaches 11, the price of ETH drops ā the more the quantity of one token, the lower the price. There is no such situation that a tokenās price and quantity both increase. The smart contract automatically balances the two tokensā quantity and price until the price matches the market price. Uniswap is essentially balancing out the value of tokens and their swapping based on how much people want to buy and sell them. As seen from the example, price oracle plays an important role in DEX. This paper suggests that Uniswap must closely track the reference market price. When writing this article, Uniswap announced the v3, a more sophisticated upgrade to make the liquidity more efficient.
Since Iām already in the math exercise, Iād continue to explain the impermanent loss that liquidity providers are concerned about. Letās go back to the scenario when ETH price goes up from $2,000 to $ 2,200, and compare the situations of ETH-USDT are invested in liquidity pool vs. just holding them:
The $68 is the impermanent loss, the difference of total value between holding the assets and investing in LP. The more significant the variance of price between two tokens, the greater the impermanent loss. When two tokens are volatile, the worse situation is when one tokenās price goes up while the other goes down compared with when two tokensā prices move in the same direction. Since it is āimpermanentā, as long as the liquidity provider does not withdraw the funds, the loss is not realized. The impermanent loss is calculated by the beginning point when the fund is invested and the ending point when withdrawn. The fluctuations of price in the middle do not matter.
To compensate for the impermanent loss, liquidity providers receive trading fees for the trades executed in their invested pools. To further incentivize people to provide liquidity, many Dex offer yield farming to make the deal look even more lucrative. I explained yield farming by using the example of Pancake Bunny here. There are many shades of yield farming to lure liquidity providers and maximize the gains.
Personal Thoughts
Defi is a beautiful combination of IT, economics, and math. Bringing something more in any of these 3 areas into the Defi space will elevate the current state of the art. It will be interesting when different studies engage and enrich the current development.