In today’s post I am going to take a look at another part of Decentralized Finance (DeFi), liquidity pools, and their price setting algorithm, the automated market maker. These are the underlying foundation for how an asset is managed and priced on a Decentralized Exchange (DEX) such as Pancakeswap or Uniswap.
For the usual disclosure, I am not a financial advisor, I don’t even work in finance at all. My day job is as a telecommunications software engineer. Treat everything you read here as some educational resources and not financial advise.
What Is A Liquidity Pool
A liquidity pool is just a smart contract that is used to store tokens and facility trading between them by providing the necessary liquidity. The Decentralized Exchange (DEX) will use these in order to manage the trading pairs they list, so it can all be managed directly on the blockchain.
The automated market maker within the liquidity pool is what manages the prices, not like the traditional order book trading model that Centralized Exchanges (CEXs) use, which mirrors the way traditional stock trading is done. We are going to take a look at how this model works first, so we can understand why liquidity pools are necessary for a Decentralized Exchange (DEX).
Order Book Model
In this traditional model, there needs to be both a buyer and a seller, and an agreed upon price. If an agreed upon price can’t be reached, there are entities known as market makers that facilitate trading, which provide liquidity by always being willing to buy or sell an asset.
In the traditional model, without the market makers that are willing to buy and sell the market price and keep liquidity flowing, the exchange would become illiquid and essentially become unusable. An order book model also tends to have a lot of orders being placed and cancelled, which generates a lot of transactions. This would be a burden on a blockchain, needing to process all of those to put the orders on and off the books. And of course gas fees would incur for just placing or cancelling your orders.
Liquidity Pool Model
In a liquidity pool setup, we have a smart contract that is holding two tokens, which becomes the trading pairs for the exchange. Every trade that a liquidity pool handles is processed through the automated market maker, which is what determines the price that is paid for the transaction and results in a price adjustment. There are a number of different algorithms that can be employed to handle this.
Most use the standard x * y = k algorithm to set the price based on the ratio of the two tokens. These types of liquidity pools can always provide liquidity, because as the balance of 1 token reaches zero, the other will reach infinity. The ratio of the two tokens in the pool determines the price.
When you provide liquidity to a pool, you are provided a LP token. When a trade is facilitate through the liquidity pool, a fee is charged and given out to all of the LP token holders, based on the amount of tokens they hold. These tokens are traded back in and burned to get your liquidity back.
When a trade happens, the price impact is determined by the size of the trade, in relation to the total size of the pool. A larger pool is able to handle larger trades with less impact, which is why a number of DEXs provide incentives in the form of extra tokens for providing liquidity. These will also provide less slippage, which is the amount the price can change between the start and end of your trade, based on the size of your trade compared with the pool.
Some other Decentralized Exchanges (DEXs) have taken a slightly different approach to how they handle their automated market maker. For example, Curve uses a different algorithm for handling pools with tokens that should have similar prices like stablecoins, or pairs of different flavors of the same coins. Balancer even has pools with up to 8 different tokens in them, so there are some interesting implementations of the technology out there.
Let’s use an example using Tether (USDT) paired with Ethereum (ETH), and I’m going to use my normal imaginary prices to make the math easier. We are going to create this pool on some imaginary exchange, and we will be the initial liquidity provider, so we get to set the prices.
Obviously, in normal circumstances, the pool would be created setting the current fair market price by providing an equal value of each of the tokens to the contract, because if the price is not the same as on another market, it provides an arbitrage opportunity and can result in lost capital. This would also hold true for anyone adding to the liquidity pool later on. See my post on Impermanent Loss for some more details about this important risk with liquidity pools.
In this pool I throw in 50,000 USDT, and 50,000 ETH. This sets the initial price at 1 USDT per ETH, and when we plug this into our x * y = k formula to get our initial k value, we get 2,500,000,000. This value is now a constant for the pool (it could change if someone adds liquidity disproportionally later, but let’s ignore that fact for simplicities sake of the explanation).
Now, someone comes along and wants to buy 5000 ETH from the pool, so we need to do some math here to figure out how much USDT needs to be provided for this trade. We take the 5000 ETH out and have 45,000 left. So we take our constant k value of 2,500,000,000 and divide it by 45,000, giving us about 55,555.56. We already have 50,000, so the buyer needs to provide 5555.56 USDT for the trade. This also gives us a new price of 1.23 USDT per ETH. The buyer also pays an average price somewhere in the middle for their order. This difference is what is referred to as the slippage.
The next person comes along, and they want to buy 10,000 ETH. The process runs again, so we take the 45,000 ETH we had left and pull the 10,000 out. Now we take the magical 2,500,000,000 number, and divide it by the leftover 35,000 ETH and we get 71,428.57 USDT needed. Since we already have 55,555.56, this trade is going to cost 15,873.01 USDT and give us a final price of 2.04 USDT/ETH.
Now the last person that was in our line, well, they want to sell us 1000 ETH, because they love that price they are seeing. So we take their 1000 ETH and add it to the 35,000 we already had, and run it through our formula. 2,500,000,000 / 36,000 gives us 69,444.44 USDT we should have left over. Since we have 71,428.57, that means we pay out 1984.13 USDT for the trade, and our final price is of course 1.92 USDT/ETH.
As you can see that automated market maker is the key that allows Decentralized Exchanges (DEXs) to operate and maintain liquidity, and how with the fee sharing and bonus tokens, it can be a way for you to earn passive income on your crypto holdings.
But of course, all the usual risks in the Decentralized Finance (DeFi) are there. And you have to worry about the creator of the smart contract having an exploit that lets them drain the liquidity, or hacks, and Impermanent Loss, so like I always say, Do You Own Research before you go throwing your money into anything, and never play with more money than you are ok with having go to zero.
Socials And Other Links
I also post a weekly price update video every Saturday over on my YouTube channel, where I will be discussing the weekly price action for some of the major cryptos. You can also sign up for my newsletter which I send out every Friday with news and whatnot from the crypto space, delivered right to your inbox!
You can also find links to resources such as research and news sites over at this link.
Want some more content right now? Check out some of my previous posts:
A few referral links, in case you are interested in the service, and it also helps me out.
Binance – large centralized exchange – referral link saves you 10% on trading fees
Coinbase – basic crypto exchange – referral link gets you bonus crypto on first deposit
Cointiply – very good crypto faucet and earning site – no bonus for you on this referral unfortunately