This article is a follow up of the previous article Crypto Exchanges (which provided a brief overview of Automated Market Makers) and aims to provide more information on the nuances of AMMs. Not all AMMs are the same and the Uniswap AMM model is used as the basis for the deeper dive into AMM workings (with the Uniswap documentation as a reference point for this article)
What is EIP-20?
Before we get into AMMs it’s important to be across the EIP-20 proposition or more commonly the ERC-20 token standard. The standard provided APIs which allowed for token functionality to be embedded in smart contracts. The functionalities ranged from token transfers from one account to another, checking token balance and total token supply. These tokens have become the staple asset traded on all AMMs on Ethereum and often in pairs with each other or blue chips such as Ethereum, Bitcoin.
Interaction with Exchange
As mentioned previously, AMMs does not hold customers’ funds, positions, or information, and only serves as a matching and routing layer for trade orders. Users of the AMM exchange hold their assets on the chain in their wallets and interact with the exchange operator via transactions with the exchange’s smart contract that holds a reserve of the tokens.
Like an order book exchange, the trading pairs (ETH/USDC and BTC/BNB for example) are still the standard. When a trade is executed, the purchased tokens will be transferred to the users wallet that is connected to the exchange and the offered tokens will be transferred to the smart contract on the exchange.
Instead of using an order book to determine pricing, tokens on an AMM are priced according to a pricing algorithm. Using the Uniswap pricing formula as the example here; each trade that happens on the AMM has to satisfy the formula x * y = k.
- x is the amount of one token in the liquidity pool.
- y is the amount of the other token.
- k is a fixed constant at a point in time.
Thus for example if a user wanted to purchase ETH with USDC, then the amount of ETH they receive (excluding the fees for the trade which are added back to the pool) for an amount of USDC must be so that the constant k is the same before and after the trade. The fees from the trade are added back into the pool increasing the value of k and is allocated to liquidity providers. The value of k also changes when new liquidity is added to the pool of tokens (more on this in the liquidity pools section).
It can be seen that when trades are large prices are skewed and the trading having to abide by the formula x*y=k will subsequently get a worse price (due to the inverse relationship). Thus this AMM structure favours smaller trades and punishes larger trades aimed at manipulating the market since it would cost you incrementally more every purchase of the desired token.
Arbitrage opportunities on AMMs exist as there are divergences between the AMM’s price of a token and external prices (on other AMMs) due to the localised trading of tokens on the exchange. Arbitrages ensures that the prices always trend toward the market price.
Note; the prices at an infinitesimal levelcan be approximated via the ratio of one token to the other in the liquidity pool. In a pool of tokens A and B, the price of token A in terms of token B is the (total quantity of B)/(total quantity of A). The price is updated after every trade again in the pool.
When a transaction is submitted to the AMM, alterations to the price of the assets being purchased and sold can change. To account for this, slippage tolerances establish a margin of change acceptable to the user. As long as the execution price is within the slippage range, (a percentage signifying the deviation from the submitted price) the transaction will be executed. Otherwise the transaction will fail, and the swap will not occur.
Providing liquidity means depositing tokens into a liquidity pool. Any user of the exchange can become liquidity providers. In return for supplying liquidity, users are awarded liquidity provider (LP) tokens that represent the share of the liquidity pool the user owns. LP tokens are ERC-20 tokens that can:
- Be transferred to other users
- Redeemed to receive a number of tokens equal to sqrt(x * y), where x and y represent the amount of each token provided.
- Staked to receive fees from the trades (each trading fee is proportionately distributed to LP holders).
Liquidity providers function as market makers for the paired tokens and are incentivised by collecting fees on trades. The trading fee received is depedent on trading volume where the higher the volume, the more fees for liquidity providers. Trading fees per liquidity provider is also dependent on their share of the liqudity pool as the higher share of the liquidity pool owned the higher the fees are collected.
Impermanent loss occurs when the price ratio (one token relative to the other) of deposited tokens changes after you deposited them in the pool. The loss is relative to HODLING the coins (holding onto the coin) and will always occur indifferent to the movement of the tokens relative to each other (whether they both rise/fall or one rises the other falls).
This happens due to the nature of AMMs where each trade changes the amount of both tokens so that their product remains constant. Say a user becomes a liquidity provider. The initial users stake of assets for the liquidity pool is automatically rebalanced after every trade. So if price of token x (e.g. ETH) goes up and y (e.g. USDC) remains stable, the user’s own portfolio is rebalanced by selling ETH to USDC (ending up with less ETH and more USDC) and preserving the product constant. Each price increase in ETH results in selling a bit of the user’s ETH to USDC. Thus the user is essentially “sellling on the way up”. Thus even though at a snapshot of each trade the value transfered is preserved, relative to HODLING the initial take of assets that were deposited in the pool is worth more at current prices.
Note, the value of your tokens after you redeem your liquidity can still be greater than the value you started with i.e when both tokens in the pool go up together in value relative to tokens outside the pool. The impermanent loss only refers to the loss relative to HODLING the same amout of those two tokens.
Impermanent loss is lowest or negligible for pairs that are highly correlated since price ratio remains in a small range i.e. stablecoins or tokens and their wrapped token counterparts.
If the price ratio is large, liquidity providers may be better off simply holding the tokens instead of adding funds to a pool as fees accrued may not offset the impermanent loss.
The mathematics behind impermanent loss is well explained in the Uniswap documentation.
Breadth of AMMs
The above perculiarities of AMMs is structured off of Uniswap however there are many other AMMs that have variations of the core principalities above. For example Curve as an AMM has a more sophisticated algorithm for their liquidity pools and limits pool tokens to pairs that are “similar” assets in nature ideally lowering impermanent loss for liquidity providers.