How to use Automated Market Makers (AMM)
What is an AMM? Plus how to swap, provide liquidity and use aggregators!
TLDR:
AMMs are a type of DEX that replace an exchange’s order books with equations that automatically determine the price of an asset based on its supply and demand.
Swapping tokens on an AMM is very simple and the steps are highlighted below.
Providing liquidity to an AMM is required for tokens to be swapped in the first place, and while its easy to do there are risks like hacks and impermanent loss.
Impermanent loss is where one asset rises much faster than its pair and so more money would have been made holding these tokens outside the AMM.
Aggregators like 1inch search through AMMs like Uniswap and Sushiswap to find the best price when swapping between tokens.
Last week I wrote a post about Centralised and Decentralised Exchanges (CEXs and DEXs), a fundamental topic in Web3 that serves as a base for more advanced topics.
Building off last week’s post, today I will discuss a specific type of DEX known as an Automated Market Maker (AMM). AMMs simplify and automate a lot of the more complicated parts of exchanges and have been the biggest force in driving the growth and popularisation of DEXs in Web3.
AMMs have gone from strength to strength in taking an ever greater market share of Web3 trading volume and I personally believe they are going to become one of the most central pillars of Web3 infrastructure, so its important to learn about them and the main ways you can interact with them.
Automated Market Maker (AMM)
Before AMMs there were near to no popular DEXs in the Web3 space. An early experimental DEX on Ethereum called EtherDelta (and later ForkDelta) showed it was possible to run an exchange entirely through Ethereum smart contracts but the SEC came after it and shut it down. Either way EtherDelta’s UX was terrible, trades required multiple costly Ethereum transactions and there wasn’t enough volume in its order books.
Along came Bancor, who after raising $153m in an ICO, created a new type of DEX now known as the “Automated Market Maker”.
AMMs did away with the order book by introducing a relatively simple formula that automated away all the work. Instead of relying on human market makers to set prices and provide token liquidity, AMMs use a mathematical formula to automatically determine the price of an asset based on its supply and demand in a liquidity pool.
The basic formula most commonly associated with AMMs is:
X * Y = K
Where X = quantity of A tokens, Y = quantity of B tokens, and K = a constant number.
When someone trades on an AMM, the quantities of X and Y change, but the multiplication of X*Y (which is K) always remains the same.
For example:
10 ETH : 10,000 DAI is deposited, ETH = $1000; so K = 10 × 10,000 = 100,000.
ETH goes up to ≈ $1250, and ETH gets bought up while DAI is sold into AMM.
Now there’s 9 ETH : 11,111 DAI in the AMM; and K = 9 x 11,111 = 100,000 still!
This simple formula ensures that as the quantity of one asset decreases, due to it being bought, its price increases relative to the other.
The most successful AMM has no doubt been Uniswap on Ethereum, but many others have emerged and there are other big players like SushiSwap. And the numbers are astonishing, since DEXs are permissionless and anyone can list a token, Uniswap has hundreds more tokens listed than CEXs like Coinbase or Binance.
At their core AMMs have two key functionalities: (a) swapping tokens and (b) providing liquidity for others to swap tokens! Let’s take a practical look at both below.
Swapping Tokens
New users in Web3 often get thrown by just how awkward the UX in the space is. However, AMM’s really buck this trend and are super simple to use.
The steps to swap between two tokens on an AMM like Uniswap are simple:
Open the Uniswap App
Connect your wallet on the top right
Select the two tokens you want to swap for each other
Hit “Swap”
Sign the transaction(s) that come up!
It honestly couldn’t simpler than that!
Take note though that for any token other than ETH you’ll have to sign two transactions, where the first transaction gives permission for the Uniswap contract to spend your tokens on the second transaction.
Once you are done swapping you may want to revoke the permission for the AMM to spend your tokens, which can be done with revoke.cash. This gives you the guarantee that if the AMM is ever hacked then you cannot have your funds stolen!
Also note that there are more nuances involved with swapping such as slippage when a token has very low liquidity, but these more advanced cases I’ll cover in a future post.
As you can see swapping with an AMM really couldn’t be simpler - you see the price, select how much you want to trade, then click swap! Plus you do all this by just connecting a wallet without having to create an account and log in like you would with a CEX!
Providing Liquidity
For people to swap between tokens there need to be tokens on the AMM in the first place! We call these sets of tokens “liquidity pools” and “liquidity providers” (LPs) are those who add tokens to the pools so others can trade between them.
AMMs incentivise this behaviour by giving LPs a % of fees made on swaps - imagine if a CEX like Coinbase paid for you to let them hold your tokens!
However, being an LP is not without its risks and is generally considered a more advanced topic for most. Yet, the UX is still surprisingly simple and worth taking a look at.
Importantly, to swap between two tokens an AMM needs both tokens! This means that in most liquidity pools you need to provide both tokens in equal measure. There are exceptions like Bancor’s single-sided pools, and some other more advanced pools, but for now we’ll just look at the most basic case.
The steps to provide liquidity on an AMM like Uniswap are then:
Open the Pools tab on Uniswap
Press the “New Position” button
Select the two tokens you want to provide liquidity for, eg. ETH and DAI
Enter the amounts under “Deposit Amount” - remember you’ll be made to provide equal amounts for both tokens
You may need to approve the tokens to be traded by Uniswap, eg. “Approve DAI”
At this point you can also “Set Price Range” but for now just leave it at its default (we’ll cover this more advanced concept in a future post)
Select “Preview” and then “Add” and sign the transaction to finalise the process
And that’s it!
You have now provided liquidity to Uniswap and will start earning fees relative to the amount of tokens provided and swap volume in that pool.
You can see all your liquidity provisions on the pools tab. Plus since Uniswap v3 you can check your NFTs and you’ll find you have a new one that represents the debt that Uniswap now has with you!
However, whenever providing liquidity you should be wary of impermanent loss…
Impermanent Loss
Impermanent Loss (IL) is a risk that at first is hard to understand, so below I’ll describe it together with an image to make it easy to visualise.
Imagine you provide liquidity to a pool that contains ETH and DAI. If the price of ETH rises in relation to DAI outside of the AMM, people or bots (known as arbitrageurs/arbs) will buy ETH from the AMM and sell DAI into it until the price inside the AMM matches the external market price.
This causes the AMM to have a larger proportion of the cheaper asset (DAI) and less of the now more expensive asset (ETH). When you decide to withdraw your liquidity, you could now end up with a larger amount of the depreciated asset and less of the appreciated asset, realizing a loss compared to if you'd just held onto the assets outside the AMM!
IL has a bad name because its hardly impermanent, in fact its very permanent! But the loss isn't "locked in" until you withdraw your funds and if asset prices return to their initial state then the loss “disappears” - but why would you be holding onto assets if you didn’t want them to appreciate?
While AMMs offer a % of swap fees, these might not be enough to counterbalance the potential IL, especially in volatile markets. So when providing liquidity you should weigh up this potential risk, along with the very real risk of the DEX suffering a hack, before hading over your money!
Aggregators
We’ve looked at Uniswap today just to keep it simple, but there are already hundreds of AMMs out there on all sorts of different blockchains. The second most popular after Uniswap for example is SushiSwap that works on a large number of EVM compatible networks - and yes its named after food!
There’s honestly too many AMMs to keep track of nowadays so its hard to know where to do a trade, and that’s where aggregators like 1inch come in!
Aggregators do the work in the background of searching all the AMMs for you. So when you want to trade but don’t know where to find the best price, you can go straight to 1inch. And the UX is incredibly similar to Uniswap, so much so that you could even confuse the two - and yes, their logo is also a unicorn!
While 1inch will always find the best prices, when you’re on the Ethereum mainnet it may end up costing you more on gas fees to use them since it adds a few more computational steps. So always check if you are indeed getting the best deal, as in many cases it may be best to just use Uniswap directly!
And that’s a wrap
This post should work well as a primer for anyone starting to learn about AMMs.
Yet, it’ll also be helpful with even more experienced Web3 users, since most will have already used an AMM at some point, but a lot less will have provided liquidity and even less will have heard of impermanent loss.
Next week we’ll continue with a look at bridges and moving liquidity cross-chain!