Automated Market Makers - AMM
An automated market maker (AMM) is a smart contract that holds assets and is always willing to quote you a price between two assets. You can trade against the AMM’s capital in the smart contract instead of between peers. It uses the trades to update the size of the assets and update their price accordingly. The AMM can always guarantee liquidity by raising the price for an asset according to market demand.
There are various ones, however we will focus on the most important features popularized by UniSwap. Haseeb Qureshi explains UniSwap well here. We will cover another type of DEX protocol in a later section.
This section will give an overview of two key concepts that make AMMs possible.
Liquidity Pools
Automated Market Makers rely on liquidity pools to source capital. Liquidity pools are collections of tokens locked into a smart contract. This allows for decentralized capital formation. They are used to facilitate trading by providing liquidity, defined as the ability to convert an asset into cash or its equivalents without greatly affecting its market price. This is important because an asset’s value is determined by what others are willing to pay for it and how easily it can be bought or sold.
The main ideas behind liquidity pools are:
- They are used to source capital which is used to provide liquidity.
- Liquidity is the ability to convert an asset into cash without affecting its price.
- They are useful because order book models are not feasible on-chain completely since market makers cannot update prices all the time due to gas fees and Ethereum’s throughput being too slow.
- Liquidity pools source capital from anyone and are used to allow anyone to trade against the smart contract. Liquidity providers earn a fee for doing so in the proportion of capital they provide to the pool.
- To become a liquidity provider, users must deposit equal amounts of token based on their price into a pool. If they don’t, they risk being arbitraged by traders who find a good deal. -AMMs like UniSwap hold liquidity pools in token pairs. An example can be the ETH - DAI or CRV - COMP (Curve to Compound Finance).
The downside:
- Because trades happen on-chain, bots can front-run transactions and attempt other sorts of attacks. This results in a user paying more than they intended.
- Front running occurs when bots read Ethereum’s current set of unprocessed pending transactions called the mempool and find an opportunity to outbid a transaction to be processed by the network miner (or validator after ETH 2.0). The bots get ahead of the line, which results in a better price for them at the expense of the other traders. -This stems from the Miner Extractable Value, where a miner can dictate when, how and where a transaction will go into an Etheruem block. When a transaction large enough to create slippage is sent to the network, bots will notice and set off a bidding war to capture the stop and front-run.
Bonding Curves
The other key to Automated Market Makers is the bonding curve. A bonding curve is a mathematical formula used to describe the relationship between the price and the supply of an asset. This can be thought of as a deterministic pricing formula. This formula is called the Constant Product Formula in UniSwap. This curve can be represented in a smart contract that can buy or sell the underlying token. Bonding Curves emerge out of the ability to escrow funds in a smart contract.
The main ideas behind the Constant Product Formula are: -To always ensure liquidity for any asset by modelling the demand curve in the smart contract.
- Users trade against the smart contract, not between each other. k always has to stay the same number, no matter what x or y does. The price quoted is directly dependent on the size of the order.
- The trade-off for assured liquidity is slippage, also known as getting less for more.
x * y = k
x = supply of asset 1 y = supply of asset 2 k = Fixed Size of Pool
The downside:
- Note that the further one moves along the curve, the less they get.
- In UniSwap, the slippage starts to seriously affect orders; around 2% of the liquidity of a token is traded against. A good description of this can be found via this article and most recently here.
Different AMMs have variations of this deterministic pricing formula depending on their need. Curve implements a special formula to allow stablecoins trades for assets that are a stable representation of each other, which results in a narrow trading band. An example of this is trading between ETH and sETH (synthetic ETH) or USDC and DAI, which should both be pegged close to the US Dollar.
Synching Prices Across Markets
With Automated Market Makers similar to UniSwap v2, prices are synched with outside markets via arbitrageurs who spot price differences between exchanges. These exchanges can be other DEXes or centralized exchanges. Arbitrageurs capture the profit, which is the difference in price between the two markets.
A consequence of this is impermanent loss for liquidity providers, the difference between holding an asset and providing liquidity. This is similar to an opportunity cost which if realized turns into a real loss. Impermanent loss is also described in this animated video and article and here.
Uniswap V3 has other features like Range Orders and Limit Orders which you can explore. Some AMMs can hold more than two assets, namely Balancer, but these are more sophisticated and rely on Bonding Surfaces.
Additional Resources
DeFi and the Future of Finance (section 4.7.2) DeFi and the Future of Finance (section 6.2) Graphical Guide for Understanding Uniswap via EthHub