Balancer, like Uniswap, is a fully decentralized protocol for creating pools of tokens where anyone can trade or add liquidity. But its flexibility opens up a whole new world of possibilities.
Uniswap liquidity as portfolio
In the previous article we talked about Uniswap and how it works: given a pair of assets, a pool is created that holds an equal amount (in value) of both. For example, an ETH/DAI pool, assuming a price of ETH of $400 and no arbitrage opportunities (that is, no one can gain from trading in that pool), should have 400 DAI for each ETH.
From the liquidity provider perspective, another way of interpreting this is that you have a portfolio composed of equal parts of both assets. What does this mean?
Given two assets, like ETH and DAI, there are different ways you could invest on them. At one extreme, you can just invest everything on ETH. This means that you win more when ETH rises in price, but you also lose more if it falls. In financial parlance this is called “having more exposure” to ETH. At the other extreme you can hold zero ETH. That way, when the price of ETH moves, you don’t win nor lose: you are not exposed to ETH at all.
Having equal amounts of ETH and DAI is a middle point between these two extremes. But there’s a catch: you may have an equivalent amount of both today, but in six months those same holdings might not be equal in value. For example, if ETH doubles in price, then your holdings will now be 66% ETH and 33% DAI, and you’ll be more exposed to ETH than before. When this happens, you may want to rebalance your portfolio, selling ETH for DAI until you have an equal investment on each one.
Adding liquidity to an ETH/DAI Uniswap pool is like doing this, with the benefit that you don’t have to do the rebalancing: other people do it for you, and they even pay fees that increase the value of your pool! So instead of buying equal amounts of ETH and DAI, you create (or add liquidity to) this pool and hold the liquidity tokens you get in return. You can see how this is a very attractive idea —assuming someone just happens to want to invest equally on two assets.
Balancer: generalizing Uniswap
From this perspective of “liquidity tokens as an automatically balanced portfolio”, Uniswap has two big limitations: each pool can only have two tokens, and they are equally represented. Balancer fixes both of these issues.
A balancer pool can have two or more tokens (up to eight) and, more importantly, they can have different weights. The multi-token part is not that interesting for our explanation, so let’s focus on the weights; everything we’ll say is easily generalized to three or more tokens.
Let’s go back to our ETH/DAI example. Suppose you want some exposure to ETH, but you don’t want half of your savings there. A sensible choice would be a 80/20 portfolio, with 80% invested in DAI and 20% in ETH. To do this, you can simply create a Balancer pool with these weights (or add liquidity to an existing one) and hold the liquidity tokens you get in return.
What happens if the price of ETH moves? The same thing that happens in Uniswap: anyone can trade in your pool (for a fee), and so the change in price presents an arbitrage opportunity. The amounts that can be arbitraged are different (check the mathy section below if you are interested), but the mechanism is the same.
Smart pools
There are other differences between Uniswap and Balancer. In Uniswap, given two tokens, only one pool can exist for them. If one is already created, you can only add liquidity to it, not start a new one. That's not the case in Balancer.
Another difference is that pools can have different states. When you create a pool, it starts being a private pool that only you can use. This lets you start your pool, add or remove tokens, and play with the weights and fees. When you are happy with it, you can “finalize” it. This means two things: that you will no longer be able to change its configuration, and that anyone can trade on it.
So far, so good. But here's the thing: you can open a pool for trading before finalizing it, and anyone can start buying and selling tokens or adding liquidity.
Upon hearing this, the first thing you should think is “why would I use a pool where the owner can do whatever they want at any moment?” And you'll be right! But what if the owner is 100% trustworthy and predictable? What if the owner is a smart contract?
That's the idea behind Balancer's smart pools: pools that are never finalized but whose change in configuration is ruled by code.
The possibilities are of course endless. A simple example is a smart contract that changes the pool weights as time passes. Check this article to see how that can be used in practice.
Show me the numbers
Let’s say you have 5000 DAI and you want some exposure to ETH. As we discussed before, you could just buy some, but you could also use a pool as an automatically rebalanced portfolio. One option is to sell half your DAI for ETH and use a Uniswap pool. Let’s compare this to creating a Balancer pool with 80% DAI and 20% ETH. For simplicity, we are going to assume that you are the one creating the Uniswap pool, and we’ll ignore fees in both pools.
Assume an ETH price of $100. For the Uniswap pool, this would mean using 25 ETH and 2500 DAI. For the Balancer pool, the amounts would be 10 ETH and 4000 DAI.
The first thing to notice is the spot (or margin) price. This is the price of a token when the amounts traded are infinitesimal. In Uniswap, the spot price can be obtained just by dividing the amounts: 2500 DAI / 25 ETH gives you a price of $100, which is the market price in our example. If you buy a tiny bit of ETH, the price will be very close to $100. If you buy more the price will increase, and the more you buy, the bigger the difference will be with respect to the spot price.
In balancer, the spot price is a little more involved: you also divide the amounts in the pool, but before doing that you divide them by their weights:
spotPrice = (totalDai / wDai) / (totalEth / wEth)
(Check this section of the balancer docs to see the formulas.)
So, what is the spot price of ETH in our 80/20 pool?
spotPrice = (4000 / 0.8) / (10 / 0.2) = 100
It’s the same as in the Uniswap pool, which makes sense since otherwise there would be an arbitrage opportunity.
The difference is what happens when the price changes. Suppose ETH doubles in price to $200. In Uniswap, to maximize your earning, you would spend approximately 1035 DAI and get 7.32 ETH in return (check the new numbers to see that, after this trade, the spot price of the pool reflects the new market price).
In the Balancer pool, for the same change in price, the trade that maximizes profits is of 595 DAI and gets you 4.25 ETH in return. The new balances after this trade are 5.75 ETH and 4595 DAI and the spot price is:
(4595 / 0.8) / (5.75 0.2) = 200
(Disclaimer: I used some code to get the amounts that maximize profit, but there is a closed formula to find it for a given price change. Deriving it is left as an exercise to the reader.)
So you can see that it takes less DAI to rebalance the Balancer pool than the Uniswap pool. If the balances were inverted and the Balancer pool had 80% ETH and 20% DAI, it would take more DAI to rebalance it. This makes sense if you take into account that a Uniswap pool is indistinguishable from a Balancer pool with two tokens and equal weights.
Further reading
The Balancer whitepaper is the place to go if you want to see the math behind it in depth, but it doesn’t have anything on smart pools.
The docs are good enough for an overview of the concepts involved.
In the previous article I linked an analysis of the trade-offs involved for liquidity providers. If you are curious about how this changes in Balancer, check the “Impermanent Loss” section of this article.