Introduction to AMM

Have you ever wondered how decentralized trading works? The backbone of decentralized trading is the Automated Market Maker (AMM). AMM is a game-changing technology in the world of decentralized trading that operates on the blockchain and uses a mathematical formula to determine the price of assets based on supply and demand.

Central Limit Order Book vs. Automated Market Maker Most traditional exchanges use a trade execution model called the Central Limit Order Book (CLOB) to process all trading orders. The CLOB model is transparent, low-cost, anonymous, and can match orders in real-time, but it is slow and requires an intermediary to execute individual orders.

On the other hand, decentralized exchanges have developed their own solution to achieve better capital efficiency, speed, and ease of use. The protocol is called Automated Market Maker (AMM) and it allows traders to exchange cryptocurrencies without an intermediary. While AMMs are commonly used in decentralized exchanges, some centralized exchanges are also implementing AMM protocols.

What is a Liquidity Pool?

A liquidity pool is a virtual pool of money that contains a pair of crypto assets, for example, ETH/BTC, and runs on a specific algorithm. Its foundation is a smart contract that maintains the crypto pair value in a constant 50:50 ratio. Investors who decide to contribute their assets to the liquidity pool are known as liquidity providers and are entitled to a certain percentage of fees that occur with every trade in the specified pool.

How Does AMM Work?

AMM connects users directly without an intermediary to exchange cryptocurrencies. It makes sure the ratio of assets in liquidity pools remains balanced and calculates the price of a crypto asset depending on its paired token. The most common AMM algorithm is the Constant Product Market Maker (CPMM).

Once a liquidity pool is set up, it must be filled up with a pair of crypto tokens in a 50:50 ratio. The AMM uses the following formula to calculate the constant, which must remain unchanged at all times:

x * y = k

Where x is the quantity of asset A, y is the quantity of asset B, and k is the constant.

The AMM first calculates the constant and then uses this formula to calculate the price of both assets. When buyers start buying asset A in exchange for asset B, asset A becomes more expensive, and the price of asset B starts to fall, following the basic supply and demand approach.

Slippage and Impermanent Loss

Slippage is closely tied to liquidity. When liquidity is low, slippage can occur, causing the price of an asset to change drastically during trade execution, affecting the outcome of the trade. To avoid slippage, transactions must be executed as instantly as possible.

Types of Algorithms

Different DEXs use different AMM algorithms or a combination of two. While the most common is CPMM, others include:

  • Constant Sum Market Maker (CSMM)
  • Constant Mean Market Maker (CMMM)
  • Advanced Hybrid Constant Function Market Maker (CFMM)
  • Dynamic Automated Market Maker (DAMM)

Best Known AMMs

The DeFi space is constantly growing, so the list of AMMs is constantly growing as well. Currently, the top seven Automated Market Makers are:

  • Uniswap
  • Pancakeswap
  • Osmosis
  • Curve DAO
  • Sushi

Code Implementations

How AMM could be implemented in Solidity:

pragma solidity ^0.8.0;

contract AMM {
  // Variables to store the token balances in the liquidity pool
  uint256 public totalSupplyA;
  uint256 public totalSupplyB;

  // Function to add liquidity to the pool
  function addLiquidity(uint256 _amountA, uint256 _amountB) public {
    totalSupplyA += _amountA;
    totalSupplyB += _amountB;
  }

  // Function to calculate the price of token A in terms of token B
  function getPriceA(uint256 _amountA) public view returns (uint256) {
    return (_amountA * totalSupplyB) / totalSupplyA;
  }

  // Function to calculate the price of token B in terms of token A
  function getPriceB(uint256 _amountB) public view returns (uint256) {
    return (_amountB * totalSupplyA) / totalSupplyB;
  }
}

Ethers.js implementation

const ethers = require("ethers")
const provider = new ethers.providers.JsonRpcProvider()

async function main() {
  // Connect to the network and load the contract
  const wallet = new ethers.Wallet(privateKey, provider)
  const contract = new ethers.Contract(contractAddress, AMM.abi, wallet)

  // Add liquidity to the pool
  await contract.addLiquidity(ethers.utils.parseEther("1.0"), ethers.utils.parseEther("1.0"))

  // Get the price of token A in terms of token B
  const priceA = await contract.getPriceA(ethers.utils.parseEther("1.0"))
  console.log(ethers.utils.formatEther(priceA))

  // Get the price of token B in terms of token A
  const priceB = await contract.getPriceB(ethers.utils.parseEther("1.0"))
  console.log(ethers.utils.formatEther(priceB))
}

main()

Conclusion

The basic concept behind cryptocurrency is its decentralized approach, and Automated Market Makers are realizing this idea by successfully eliminating the need for intermediaries in the trading process, making the entire service faster and more user-friendly. AMMs enable digital assets trading without permission and automatically by using liquidity pools as opposed to traditional market order books.