Balancer Basics
From zero to one on Balancer AMM
Last updated
From zero to one on Balancer AMM
Last updated
The problem of creating algorithms for Automatic Market Makers is an academic research problem [1] that was studied prior to the emergence of blockchains and decentralized exchanges.
Being applying to decentralized markets launched on top of Ethereum AMMs are critically important as they solve two problems at once. Basically, AMMs act as a gas-efficient solution for decentralized trading and as liquidity pools at the same time.
The mathematical foundation for AMMs is a mathematical rule named invariant. Definition: an invariant is a property of a mathematical object (or a class of mathematical objects) which remains unchanged, after operations or transformations of a certain type are applied to the objects.
For AMMs it means that the result of a function applied to the number of assets (with additional conditions like weights, etc) is unchanged after the trade. The math behind Invariant links assets inside one AMM-based pool and allows to trade them, calculate spot price, and add liquidity permissionless.
The constant invariant model was proposed [2] by Alan Lu from Gnosis and highlighted several times by Vitalik Buterin [3],[4]. Uniswap was the first AMM based on the constant invariant function when it launched in 2018:
where x - balance of ETH, y -balance of ERC20 token The total value of this trading pair pool (ETH + ERC20 token) is constant while the point at the AMM curve moves during the particular trade:
It means that if somebody trades A to B in the example presented above, the equation A*B=const will take the form (A+dA)(B-dB)=const (if there are no fees), where dA is the number of tokens A exchanged for dB number of tokens B. So, the number of tokens A increased by dA, and the number of tokens B decreased by dB, but the result of multiplication A+dA and B-dB remains unchanged.
The AMM space considerably grew in recent years. Several projects offer their own AMM invariants - Balancer, Curve, 1inch (formerly Mooniswap), and some others.
In 2018, VItalik Buterin and Martin Koppelmann proposed a design for on-chain decentralized exchanges based on the concept of the x*y=k invariant. Balancer is a generalized version of that design that enables multiple tokens with different weights in the same contract:
For deeper dive into Balancer Math read the "Understanding Balancer Math" section. Note, that mentioned before x*y=k formula is a specific case of Balancer AMM formula. Based on generalized invariant, Balancer allows creating pools with 2-8 assets, user-defined weights, and customizable fees(swap fee, entry fee). It allows building pools for completely different use cases, which is declared in official Balancer documentation
Balancer is an automated portfolio manager, liquidity provider, and price sensor.
Let's dig deeper in each case presented in the official definition of Balancer:
Balancer supports pools containing up to 8 tokens. It means that you can create an ETH-WBTC-BAL pool. It will act as a portfolio for all users of this pool. This portfolio will be automatically rebalanced by means of arbitrage trades, fixing price differences at other ETH, WBTC, and BAL markets. The pool share - BPT token of this pool will act as a single token, representing the basket of assets. From another point of view, such a pool acts as an index for these tokens. There are several Defi projects, focused on building indices using Balancer AMM, for example, PowerPool and PieDAO. Note: Balancer is named as the "portfolio manager" since Balancer AMM provides incentives for external traders (arbitrageurs) to re-balance the portfolio. So, literally saying, Balancer is a portfolio manager for all portfolios built on top of it.
Balancer pools use all assets inside as liquidity for trading. It means, that if there is an ETH-WBTC-BAL pool, any user can trade ETH to WBTC, BAL to ETH, etc. So, since Balancer pools are liquidity pools that can be used for trade pool's assets, it is defined as the liquidity provider.
The value of any asset is determined in a deal. Balancer AMM pool is used for trading assets, so it can be used as a price sensor for assets in the pool. Note, that prices of assets are calculated against other pool assets. For example, in the ETH-WBTC-BAL pool BAL price will be determined against ETH and WBTC - BAL/ETH and BAL/WBTC.