Constant Product Automated Market Maker (AMM)

Constant Product Automated Market Maker (AMM)

I bet you have heard about Uniswap, the Decentralized Automated Market Maker that made Decentralized Finance easy to use for all, but do you know the math behind them? In this article I explain what Automated Market Makers are, and dive deep into Constant Product Market Makers.

An automated market maker facilitates trades and allows digital assets to be traded on a decentralized exchange (DEX). There are several different types of AMMs and they include:

  • Constant product automated market makers (CPMM): These market makers use a fixed product formula to ensure that the value of a particular market remains constant. XY=K.The best example of a DEX that uses this is Uniswap and Bancor.
  • Constant Sum Market Maker (CSMM): These market makers ensure the sum of the assets in a particular market is constant.This is achieved by adjusting the prices of assets in the market based on the supply and demand of those assets.
  • Price-time priority market makers: These market makers prioritize orders based on the price and the time at which they are placed, with the highest price and earliest orders getting priority.
  • Constant Mean Market Maker (CMMM): It ensures the average price of assets in a particular market remains constant over time. This AMM? enables the creation of AMMs that can have more than two tokens and be weighted outside of the standard 50/50 distribution. In this model, the weighted geometric mean of each reserve remains constant.

We need to know a number of terms that are used in DeFi:

  • Liquidity : This is the ability of an asset to be sold without affecting the price
  • Liquidity provider: is an entity that provides assets to the AMM in order to increase the liquidity of a particular market and earn a small fee.
  • Liquidity Pool:?a liquidity pool is a collection of assets that is used to facilitate trading in an AMM.they help to ensure that there is always a sufficient supply of assets available to buy and sell in the market
  • Minting: Minting refers to the process of creating a new asset or increasing the supply of an existing asset.
  • Burning: This refers to the process of removing or destroying?an asset from circulation?

Generally AMMs use mathematical formulas to facilitate trades in?Decentralized Exchange. (DEX). These AMMs set the prices of assets on a DEX. They do this by using a process called "liquidity provision," in which they act as both the buyer and the seller of an asset.?

AMMs provide liquidity to the DEX by constantly buying and selling assets in order to keep prices stable. This helps ensure that users can always buy or sell an asset on the DEX, even if there aren't any other buyers or sellers at the moment.

Constant Product AMMs.

Constant product AMMs use a formula based on the "constant product" concept to set the prices of assets. So, if the price of token A increases, the price of token B must decrease in order to keep the constant product equal to the constant.?

This relationship between the prices of asset A and asset B is known as "constant product price elasticity."?The formula is:

No alt text provided for this image
Fig: Shows a constant product AMM Formula

When you trade in an AMM X and Y can vary but the result is always a constant.

For example, If you want to sell token A and buy token B in the Constant product AMM then the formula will be?

dx = Change in the amount of token A (there will be an in increase in token A in the AMM)

dy =?Change in the amount of token B (there will be a decrease in token B in the AMM)

Before the trade the formula was : XY = K

After the trade the formula will be (X+dy)(Y-dy) = K.

No alt text provided for this image
Fig: A Graph showcasing the Constant Product AMM Graph

From the above graph you can tell that K is constant.

(X + dx)(Y-dy) = XY

The Formula used to get to know the number of tokens to return in a trade in case we swap token A to token B is:

dy =? Ydx / (X+dx)

Liquidity Addition to a constant Product AMM.

As mentioned above liquidity addition is the process of providing assets to the AMM in order to increase the liquidity of a particular market and earn a small fee. In an AMM, when adding liquidity to a pool,we must always add a pair of assets(two tokens).

  • Before Adding liquidity XY? = K
  • After adding liquidity:? (X +dx ) (Y + dy) = K’
  • Since we are adding both tokens to the AMM as liquidity that means that? K should be less than? K’

K <= K’

No alt text provided for this image
Fig: Graph of the AMM before and after we add liquidity.

When we add liquidity it is important to note that there should be no price change before and after adding liquidity.

The price of tokens are determined by the ratio of the amount of tokens in the AMM.

The price of tokens in the AMM before adding the liquidity = X/Y

The price of tokens in the AMM before adding the liquidity = (X + dx) / (Y + dy):

X/Y ? = (X + dx) / (Y + dy)

From the above equation we can find both the amount of token A added (dx) given the amount of token B added (dy) i.e what is dy given dx ?

dy ? = (Y dx) / ( X )? && dx = (Xdy)/ Y

dx/dy = X/Y

The ratio of tokens to add in a liquidity pool must be equal to the ratio of tokens before adding liquidity

How Many Shares are minted?

Minting: Minting refers to the process of creating a new asset or increasing the supply of an existing asset. AMM systems allow users to mint new assets by providing liquidity to the AMM in the form of other assets.

This can be done by depositing assets into a liquidity pool, which is then used to facilitate trading in the market. In return for providing liquidity, the user may be rewarded with a new asset that is created by the AMM

It is important to note that an increase in liquidity is directly proportional to an increase in shares. If we increase liquidity by 5% the shares also increase by 5 %

  • L0 = total liquidity before adding liquidity
  • L1 = total liquidity after adding liquidity
  • T = Total shares before.
  • s? = Shares to Mint

Since increase in liquidity is equal to increase in shares:

?L1/L0 = (T +s )/ T

Shares to mint (s) =? (L1 - L0)/L0 * T

Burning Liquidity from a Constant Product AMM.

Burning: This refers to the process of removing or destroying?an asset from circulation.

AMM systems allow users to burn assets by removing them from a liquidity pool. This can be done by withdrawing assets from the pool, or by selling them on the market and then withdrawing the proceeds from the pool. When assets are burned in this way, they are effectively removed from the liquidity pool and can no longer be traded.

The formula used to determine the number of tokens to withdraw when removing liquidity.

dx = X (s/T) && dy = Y (s/T)

Advantages of Constant Product AMMs.

?The main advantage of constant product AMMs is that they are relatively simple to understand and use. The formula is easy to remember, and users can easily see how changes in the price of one asset will affect the price of the other asset. This can be helpful for traders who want to make informed decisions about which assets to buy or sell.

Risks involved with Constant Product AMMS.

Impermanent Loss is the potential for a market maker to experience a loss due to changes in the relative prices of the assets that they are holding as part of their market making activities. It occurs when the price ratio of the tokens they have deposited in a liquidity pool changes after they have deposited the tokens in the pool. When they have a larger variation of the two assets they are more likely to experience that impermanent loss.

Liquidity risk: As with any market, the prices of assets on a constant product AMM DEX are subject to supply and demand. If there is not enough liquidity (i.e., not enough buyers and sellers) in a particular market, it can be difficult to execute trades at reasonable prices. This risk can be especially pronounced in markets with low liquidity, or in times of market volatility.

Smart contract risk: As with any decentralized platform, constant product AMM DEXs rely on smart contracts to facilitate trades and manage assets. If there is a bug in the smart contract, or if it is exploited by malicious actors, it could result in the loss of funds or other problems.

Front Running: This is the procees in which traders try to take advantage of the AMM Formula, for instance if a trader knows that the price of asset A is going to increase, they might try to buy a large amount of asset B before the price starts to decrease. Smart contract developers even create front running bots just for this purpose.This can potentially distort the market and make it harder for the AMM to maintain the constant product.

Conclusion.

Constant Product AMMs are simple to implement and understand. Since the technology is still pretty new, am looking forward to seeing advancement in the technology and in the entire DeFi ecosystem.

要查看或添加评论,请登录

Andrew Hemingway的更多文章

社区洞察

其他会员也浏览了