DeFi Basics #3 | Automated market makers | Constant product AMM

Naveen Kumar
6 min readMar 9, 2023

--

I am writing a series of posts on DeFi basics from the past several months(sorry for my inconsistency). In this post, I will be delving deep into the famous stableswap mechanism and it’s background. Check the previous article here.

Automated Market Makers (AMMs) have revolutionized the way digital assets are traded. They are a key component of the decentralized finance (DeFi) ecosystem. AMMs allow users to exchange units of different types of crypto-assets without the need to find a counter-party.AMMs have become increasingly popular due to their ability to provide liquidity and facilitate trading in a permissionless and automatic way.

An AMM is a decentralized exchange where the token price is determined by a mathematical equation within the contract, as opposed to a centralized exchange where the price is determined by traders.

The three main functionalities of an AMM are

  1. Adding liquidity,
  2. Swapping tokens, and
  3. Removing liquidity.

When adding liquidity, users provide tokens to the AMM and in return receive shares that represent partial ownership. Swapping tokens involves trading one token for another based on the predetermined equation. Removing liquidity allows liquidity providers to withdraw their tokens in exchange for their shares.

Traditional Automated Market Makers (AMMs) such as Uniswap are smart contracts that allow users to exchange between assets. For example, I can exchange token X for token Y using an AMM pair that stores the token pair.

The first type of AMM to emerge was the Constant Product Automated Market Maker (CP~AMM), which was popularised by Bancor, and later by Uniswap which is an AMM-based Decentralized Exchange (DEX).

In Constant product AMM when people make trades, the price varies depends on how many tokens are left in a combined pool. The formula for Constant product AMM is:

x * y = k

where x and y are the reserves of two assets in a liquidity pool, and k is a constant value.

This means that the product of x and y must always remain the same, regardless of how much of each asset is traded. This ensures that the market maker always maintains a balanced portfolio of both assets.

AMM Functioning and Price discovery:

  • Automated market makers create markets using smart contracts.
  • Smart contracts enables the liquidity pools to offer market making and order matching.
  • Liquidity pools ,does the Market making in an AMM
Source: https://www.youtube.com/watch?v=emLopa0I3mM&list=PLS01nW3RtgopoR-FHiMwfoMLT-opXlfJF&index=2
  • A Constant product Automated Market Maker (AMM) uses a mathematical formula to determine prices for assets being traded.
  • Inherent properties of an AMM is we can have instant liquidity, meaning we can exchange any point in time unlike the case with limit order book.
  • However the disadvantage is impermanent loss and huge slippage if there is not enough liquidity.
  • Intuitively in a liquidity pool, the ratio of assets(X & Y) sets the price, meaning if we purchase one asset(X), the price of the other asset decreases(Y)
  • In this architecture we have price finding mechanism, market making and order matching which is very condensed way of enabling the whole trade engine.
Source: DeFi MOOC — Berkeley

Price discovery using bonding Curve of AMM:

  • A bonding curve is a mathematical curve that determines the price of a token in an automated market maker (AMM) system based on the token’s supply.
  • In an AMM, as more tokens are bought and added to the pool, the price of the token increases, and as tokens are sold and removed from the pool, the price decreases.
  • The bonding curve ensures that the price of the token is always in balance with the token’s supply, and therefore, provides liquidity for trading.
  • We have an x amount of assets X pool — X-axis;
  • y amount of assets in asset Y pool — Y-axis

How does the math work ?

  • We have total 10 assets in X pool; 30 assets in Y pool totalling to xy = 1030 = 300
  • If we were to remove 10 assets on y pool, then we have to add 5 assets to the x pool
  • The new pool becomes 15 in x and 20 in y
  • Please see the hand notes that I have made to explain this(excuse me for my bad writing skills and icky images )

State-transition(of price and pool value) in bonding curve:

The bigger and larger the volume of trades and liquidity, there will be further reduction in the price of the coin as shown on the curve.
Slippage protection: We can specify the threshold of slippage and set it prior to the execution of the trade(say ~1% ~ 10% etc.,). You will still be liable to pay the transaction fees to the miners.

Advantages and drawbacks of an AMM:

What are the advantages of AMMs?

AMMs offer several benefits for both traders and liquidity providers:

  • They provide faster and simpler exchange. Traders do not need to browse or create orders, wait for them to be filled, or strategise how they make trades. They can simply swap tokens at a fixed price determined by the function.
  • They provide passive income opportunities. Liquidity providers can earn fees from every trade that occurs in the pool, without having to monitor or adjust their positions.
  • They enable new types of markets and assets. AMMs can support any kind of token pairs, including stable coins, synthetic assets, prediction markets, and more. They can also create new forms of liquidity such as flash loans, which allow users to borrow and repay tokens within one transaction.

What are the drawbacks of AMMs?

AMMs also have some drawbacks and challenges that users should be aware of:

  • They incur impermanent loss. Impermanent loss happens when one item becomes more valuable than another item after you put them in the pool. It makes you lose money compared to holding them outside the pool. Here’s an example of how impermanent loss occurs.
  • This is a loss that liquidity providers experience when the price of one token changes relative to another token in the pool.
  • Here’s an oversimplified example of impermanent loss.
  • Imagine you stake 1 ETH and 100 DAI in a liquidity pool on Uniswap.
  • Let’s, the price of 1 ETH goes up to 200 DAI in a week after you have provided the liquidity.
  • If you had held onto your initial 1 ETH and 100 DAI instead of staking them in the pool, you would have gained 50% because your ETH is now worth 200 DAI while your DAI remains the same.
  • They suffer from slippage. The larger the trade size relative to the pool size, the more the price moves against the trader. This means that traders may get a worse price than they expected when they swap tokens.
  • AMM may face arbitrage opportunities, which are situations where traders can exploit price differences between AMMs and other markets to make risk-free profits. Arbitrageurs can drain liquidity from AMMs by buying low and selling high.
  • Sandwich attacks: A sandwich attack is a type of front-running attack on an automated market maker (AMM) in which a trader places two transactions on either side of a large trade, with the intention of taking advantage of the market impact caused by the large trade. The sandwich attack can lead to a significant loss for the victim, as the attacker exploits the price changes caused by the victim’s trade.

--

--

Naveen Kumar

Optimistic nihilist, Another Atom, in the universe of atoms :)