Automatic Market Makers Explained

Most decentralized exchanges that you know of run using Automatic Market Maker – a mechanism that holds liquidity and calculates exchange rates. Since the first AMM was implemented in Uniswap in 2018, hundreds of DEXes using this model have popped up.

AMM creators found a perfect way to solve two major problems of the first-generation decentralized exchanges. First, they figured out how to accumulate enough liquidity that DEXes were lacking back in the day. Second, they managed to abandon the cumbersome order book, making decentralized exchanges lightweight and speedy.

What Is АММ?

Automatic Market Maker is an algorithm that keeps liquidity on a decentralized exchange and defines the asset prices. When a user enters an AMM-based DEX to swap their tokens, they trade not with other users, but with a liquidity pool of that exchange.

A liquidity pool is a smart contract where other users (liquidity providers, or LPs) lock their tokens for a reward. This allows everyone to trade with the liquidity pool, that is – swap the coins.

Automated market makers

How is the exchange rate calculated in AMM?

Asset prices in a liquidity pool are defined automatically based on the supply-demand balance. Let’s consider a trading pair ETH/LINK. The ratio of these coins in the pool determines their exchange rate. If someone buys ETH for LINK, this means there is now less ETH and more LINK in the pool. ETH rises in price, while LINK drops.

The rate is calculated via an AMM formula. The simplest one is implemented in Uniswap and looks like this: x * y = k, where** x** is the amount of token A, **y **is the amount of token B, and **k **is a constant meaning that the total liquidity of the pool remains unchanged.

The concept of AMM has evolved over time, and there are now plenty of its different types. DEXes like Balancer allow creating pools for 3 and more tokens, while the AMM of Curve enables trading of assets with similar price: like ETH/WETH or USDC/DAI.

AMM-based exchanges are a backbone of today’s DeFi. The total value locked in all DEXes is $3.4 billion, and most of them run on Automatic Market Maker.

Why AMM?

To understand why AMM was invented, let’s see how all crypto exchanges had operated before.

The cornerstone of a centralized exchange like Binance is the order book – a log where exchange rates are shaped. Traders enter the platform with a proposal to buy or sell an asset at a certain price, and the match engine connects users whose ask and bid prices… match.

This is how the first decentralized exchanges on Ethereum operated, too. But their non-custodial nature suggests that all order book operations have to run on-chain – and this considerably hindered trading. When DEXes first emerged, Ethereum couldn’t boast enough speed and low fees to ensure a good user experience for order book-based DEXes.

This is where AMM came on stage. It replaced order books with concise liquidity pool smart contracts that collected liquidity and defined the price. This made DEXes way more efficient and has helped form DeFi as we know it.

AMM was first proposed by the Gnosis founder Alan Liu. His ideas were outlined in a Reddit post by Vitalik Buterin in 2016 and then later described by Ethereum co-founder in his blog in 2017.

Risks of Trading in AMM

Non-custodial decentralized trading comes at a cost, and AMMs have some risks that are missing in centralized platforms. They include **slippage **(relevant if you swap your tokens in a decentralized exchange) and impermanent loss (a threat for liquidity providers).

What is slippage?

Slippage happens when the expected transaction price and the actual transaction price are different. In decentralized exchanges, this occurs because AMM adjusts prices in the pool when the amount of liquidity changes. In positive slippage, the price goes in a favorable direction and you receive more than expected, and in negative slippage, you get less.

Most DEXes offer the tools to save you from negative slippage. By default, slippage tolerance is usually set at 0.1% or 0.5% – if the price changes unfavorably by more than this percentage, your transaction will revert. You can enter another slippage tolerance rate manually.

What is impermanent loss?

Impermanent loss is when the assets locked in a pool cost less than if you simply held them in your crypto wallet.

This happens when the general market price and the one defined by the AMM are different. “Impermanent” means that until the provider hasn’t withdrawn their liquidity from the pool, their loss remains unrealized.

Let’s give it an example.

  • Say, you’ve put 1 ETH and 1,200 DAI in a pool (at a 50:50 ratio). Let’s suppose that these funds are 10% from the total liquidity of the pool (so there is 10 ETH and 12,000 DAI overall).
  • Ethereum market price suddenly surges to $2,400, but the price is still the same in your DEX. Nimble arbitrageurs come instantly to buy some cheap Ether.
  • Now, there’s only 5 ETH and 12,000 DAI in the pool (whereas total liquidity has remained at the same $24,000 – remember the “k” from the formula?).
  • Your share is still 10%. If you withdraw your liquidity right now, you will receive 0.5 ETH and 1,200 DAI.
  • The funds that you withdraw will cost $2,400 – just like at the very start. But if you simply held ETH in your wallet, you would now have 1 ETH worth $2,400 plus 1,200 DAI. The loss stops being impermanent once you withdraw your liquidity from the pool.

In reality, such price swings occur far more smoothly. Extremely sharp market price changes are uncommon, and there are always arbitrageurs – bots that spot the price gaps between DEXes and smooth them out while earning the difference.

Let’s now take a look at some of the most prominent decentralized exchanges leveraging the AMM model.

Uniswap

unnamed (15).png

Image source

Uniswap is the first-ever AMM DEX launched in November 2018. It was conceived by a young engineer Hayden Adams soon after he was laid off from Siemens – which, in his words, was "the best thing that ever happened to me." Adams simply started to learn Solidity, Ethereum’s programming language, and soon, the first AMM went live.

Uniswap is a classic of decentralized exchanges with over $1 billion of total value locked. The DEX is available on Ethereum and its L2 chains (Polygon, Arbitrum, and Optimism) and boasts a radically simple design that has been copied by dozens of DEXes. The number of features available here isn’t high, but their convenience has secured the leading position of Uniswap: it’s super easy to connect a wallet, swap tokens, provide liquidity, and track pool statistics.

Despite its simplicity, Uniswap always finds things to improve. In May 2020, the team launched Uniswap V2, enabling direct ERC-20 to ERC-20 token swaps – and making ETH unnecessary as an intermediary asset, which dropped the platform’s gas fees. Later in 2020, UNI governance token was introduced, and in May 2021, Uniswap V3 was set in motion. The third iteration of the protocol enabled concentrated liquidity – a more efficient way for liquidity providers to allocate their funds in pools.

Curve

unnamed (16).png

Image source

Curve is an AMM focused on stablecoin trading. Liquidity pools of assets with similar value require a special technical solution, and Curve has managed to devise one. In Curve, you can swap DAI, USDT, USDC, FRAX, and many other tokens with low fees and slippage.

The decentralized exchange is the second-largest in terms of TVL ($876 million), and besides Ethereum with its L2 networks, it operates on other blockchains like Fantom and Avalanche. Liquidity providers of Curve receive a reward in CRV – a governance token for CurveDAO that can be used for decentralized governance of the protocol.

PancakeSwap

unnamed (17).png

Image source

PancakeSwap is the largest decentralized exchange on BNB Smart Chain and the third-largest among all DEXes ($270 million of TVL). After BSC was launched in September 2020 with its revolutionary low fees, PancakeSwap has become the backbone of the nascent BNB Smart Chain ecosystem.

Luckily for BSC fans, PancakeSwap offers plenty of features inaccessible in Uniswap. For instance, liquidity providers can earn CAKE token as reward and stake it right here in “Syrup pools” to maximize their gains. In PancakeSwap farms, users can also stake LP tokens (assets that LPs receive as a representation of their share in the pool). Another feature that PancakeSwap enables is quite rare for DEXes — limit trading.

In the Swap section, users can click the “Scan Risk” button to assess the credibility of a chosen token. This adds safety – there are thousands of tokens in BSC, and far not all of them boast secure smart contracts.

Balancer

unnamed (18).png

Image source

Balancer is an AMM released in March 2020 on Ethereum, Arbitrum, and Polygon. The protocol is the fifth-largest DEX with $96 million of TVL.

In Balancer, users create liquidity pools of 3 and even up to 8 assets. LPs can deposit tokens not only in a 50:50 ratio like in Uniswap: it’s possible to provide the pool with, for example, 60% of ETH, 20% of MKR, and 20% of USDC. This makes Balancer pools similar to asset portfolios or even financial indexes. And these pools are customizable, which gives LPs more yield opportunities and grants regular users with favorable swap conditions.

Balancer also has its native token, BAL – LPs receive it as reward for depositing liquidity in pools. BAL and the tokens of other AMMs – UNI, CRV, and CAKE – are available on ChangeNOW instant crypto exchange service. Unlike a DEX, this platform allows trading 450+ assets from dozens of blockchains with an average swap time of 5 minutes. Anyone can use ChangeNOW without registration and hidden fees.

Academy