**If you can do IT, we have exciting jobs for you.!**

AMM stands for **Automated Market Maker**. It is a type of decentralized exchange (DEX) that is governed by smart contracts. **Think of it as trading tokens without finding someone who wants to buy or sell them.** You trade with a pool of tokens controlled by a smart contract. **But if there is no buyer or seller, who decides the price to sell or buy from?**

That's a valid question; the price of the tokens in the pool is decided by a mathematical formula that depends on how many tokens are in the pool. The most basic formula is called the **constant product formula,** and it looks like this:

`x * y = k`

, where `x`

and `y`

are the amount of two tokens in the pool and `k`

is a constant number. This means that if you increase the amount of one token in the pool, you have to decrease the amount of the other token to keep `k`

the same. This also means that the price of one token in terms of the other token equals the ratio of their amounts in the pool.

For example, let's say you have a pool of **ETH** and **USDT**, and there are **100** ETH (`x = 100`

) and **200,000** USDT (`y = 200,000`

) in the pool. The constant `k`

is 100 * 200,000 = `20,000,000`

. The price of one ETH in terms of USDT is 200,000 / 100 = 2,000 USDT.

If you want to buy 10 ETH from the pool (i.e., you removed 10 ETH), you have to add some USDT to keep `k`

the same. The new amounts of ETH and USDT in the pool are **90** and **222,222.22,** respectively. If we apply simple supply and demand logic, what should happen if someone buys ETH and no one sells it?

Right, its price will increase, and that is what happens in our pool as well. The new price of one ETH in terms of USDT is **222,222.22 / 90 = 2,469.13 USDT**. As you can see, the price of ETH has increased because you have reduced its supply in the pool.

This formula ensures that there is always enough liquidity in the pool for any trade and that the price changes according to supply and demand. However, it also means a **slippage** when you trade large amounts of tokens, which is the difference between the expected price and the actual price you get from the pool. The larger the trade, the more slippage there is. **This is why liquidity providers earn fees or rewards for supplying tokens to the pool because they take on the risk of price fluctuations.**

Let's take an example to understand how slippage affects trade:

For example, let's say you want to trade 100 ETH for USDT on an AMM platform that uses the constant product formula `x * y = k`

to price assets. The current price of ETH is 2,000 USDT, so you expect to get 200,000 USDT for your 100 ETH. **However, when you execute the trade, you find out that you only get 198,000 USDT**. But why? This is because your trade has changed the ratio of ETH and USDT in the pool and, therefore, the price of ETH in terms of USDT. The new price of ETH is 2,020 USDT, which means you have paid a higher price than expected. **The difference between 200,000 USDT and 198,000 USDT is the slippage**.

Slippage can be positive or negative, depending on whether you buy or sell an asset and whether the price moves in your favor or against you. Slippage can also vary depending on the AMM platform, the token pair, and the market conditions. Slippage can be reduced by trading smaller amounts, choosing pools with higher liquidity, or using platforms that offer lower slippage ratios.

Slippage is a financial risk that traders face when using AMM-based DEXs. Before confirming a trade, always check the slippage and be prepared for possible price fluctuations.

That's a valid concern. The price of ETH in the pool is not necessarily the same as the price of ETH on other exchanges because different factors determine them. However, there is a mechanism that helps keep the prices in sync called **arbitrage**. **Arbitrage is taking advantage of price differences between different markets to make a profit**.

For example, let's say the price of ETH in the pool is 2500 USDT, but the price of ETH on another exchange is 2100 USDT. An arbitrageur can buy ETH from the other exchange and sell it to the pool for a higher price and make a profit of 400 USDT per ETH. **However, by doing so, they also change the amounts of ETH and USDT in the pool and, therefore, the price of ETH in the pool. The more ETH they sell to the pool, the lower the price of ETH becomes until it reaches an equilibrium with the other market.** This way, arbitrageurs help to balance the prices across different markets and reduce the arbitrage opportunity.

Of course, arbitrage is not a perfect solution because it involves some costs and risks, such as transaction fees, network congestion, price volatility, etc. Therefore, there may still be some discrepancies between the prices in different markets at any given time. However, arbitrage is a powerful force that tends to minimize these discrepancies and create more efficiency in the market.

The difference between AMM and order book-based exchanges is mainly in how they match buyers and sellers, price assets, and provide liquidity.

**Order book-based exchanges** use a traditional trading model, where buyers and sellers place orders to buy or sell an asset at a specific price. The exchange matches the orders based on their prices and executes the trades. The supply and demand of the orders determine the price of the asset. The liquidity of the asset depends on the number and size of the orders in the book. Order book-based exchanges can offer more advanced trading tools like limit orders, stop-loss orders, margin trading, etc. However, they also require more intermediaries, such as brokers, custodians, or regulators, which can increase trading costs, risks, and delays.

**AMM-based exchanges use a decentralized trading model**, where buyers and sellers trade directly with a pool of tokens controlled by a smart contract. The exchange does not match the orders but executes them automatically using a mathematical formula. The formula and the ratio of the tokens in the pool determine the asset's price. The liquidity of the asset depends on the amount and diversity of the tokens in the pool. AMM-based exchanges can offer more permissionless, non-custodial, and automated trading of cryptocurrencies. However, they also face some challenges, such as **slippage**, **impermanent loss**, **network congestion**, or **security issues.**

Trading in AMM involves some risk factors you should be aware of before participating. Some of these risks are:

**Liquidity limitations**: AMM platforms depend on the liquidity provided by their users, but sometimes there may not be enough liquidity for certain tokens or pairs. This can result in high slippage, low trading volume, or unavailability of some assets.**Speed**: AMM platforms rely on the blockchain network to execute trades, which can sometimes be slow, congested, or expensive. This can affect the efficiency and cost of order execution compared to centralized exchanges.**Volume and slippage**: AMM platforms use a constant product formula to price assets, which means that the price changes according to the supply and demand of the tokens in the pool. This also means that large trades can significantly impact the price, resulting in slippage, which is the difference between the expected price and the actual price you get from the pool.**Fees**: AMM platforms charge fees for trading, depositing, or withdrawing tokens from the pools. These fees can vary depending on the platform, the network, and the token pair. Some platforms also charge a protocol fee that goes to the platform developers or governance token holders.**Impermanent loss**: Impermanent loss is a risk that liquidity providers face when they deposit tokens in a pool. It occurs when the price of the tokens in the pool changes relative to the price outside the pool. This creates an arbitrage opportunity for traders to buy low and sell high, which reduces the number of tokens in the pool for the liquidity providers.**Impermanent loss is called impermanent because it can be recovered if the price of the tokens returns to their original ratio**.

There are many AMM platforms in the DeFi space, but some of the leading ones by user activity, liquidity, and volume are:

**Uniswap**: It is one of the most popular and widely used AMM platforms on Ethereum. It allows users to trade any ERC-20 token pair with low fees and high liquidity. Uniswap uses a constant product formula.`x * y = k`

to price assets and charges a 0.3% fee per trade. Uniswap also has its own governance token, UNI, which gives holders voting rights on the protocol's development.**Curve**: Curve is another popular AMM platform on Ethereum that focuses on stablecoins and other assets that have low price volatility. Curve uses a different formula than Uniswap to price assets, reducing slippage and maximizing liquidity providers' returns. Curve also charges a 0.04% fee per trade and has its own governance token, CRV, which gives holders rewards and voting rights.**Curve's formula is based on the constant sum invariant, which assumes that the sum of the tokens in the pool is constant**. The formula is x + y = k, where x and y are the amounts of two tokens in the pool, and k is a constant number. This formula works well for assets with a similar price, such as stablecoins.**PancakeSwap**: PancakeSwap is the leading AMM platform on Binance Smart Chain (BSC), a blockchain network that offers faster and cheaper transactions than Ethereum. PancakeSwap allows users to trade any BEP-20 token pair with low fees and high liquidity. PancakeSwap uses the same formula as Uniswap to price assets and charges a 0.2% fee per trade.**Balancer**: Balancer is an AMM platform on Ethereum that allows users to create custom liquidity pools with up to eight tokens and different weights. Balancer pools can adjust their weights automatically based on market conditions, creating dynamic portfolios for liquidity providers. Balancer's formula is based on the**weighted geometric mean invariant**, which generalizes the constant product invariant to multiple tokens and weights. The formula is`w1 * x1 ^ p1 * w2 * x2 ^ p2 * ... * wn * xn ^ pn = k`

, where wi is the weight of token i, xi is the amount of token i, pi is the power of token i, and k is a constant number.

- We learned that AMM is a type of DEX protocol that uses smart contracts and mathematical formulas to facilitate trades without relying on buyers and sellers.
- We compared different AMM platforms and their formulas, such as Curve, PancakeSwap, Balancer, and Aave.
- We also explored some risk factors and benefits of trading in AMM, such as slippage, impermanent loss, fees, liquidity, and arbitrage.

I hope this article gave you a better understanding of what AMM is.

I thank you for taking the time to read my article. Do support me by following me on Hackernoon so that you get notified whenever I publish an article.

Thanks a lot.

*Also published here.*

L O A D I N G

. . . comments & more!

. . . comments & more!