logo
    Go back
    paul simroth portfolio, title image automated market maker

    My Projects: Automated Market Maker

    February 23, 2024

    Avatar Paul Simroth

    Paul Simroth

    Discover the future of DeFi with our blog on an Automated Market Maker project. Dive into smart contracts and decentralized exchanges.

    • Projects

    My Projects: Introduction to Automated Market Makers

    In the fast-evolving landscape of blockchain technology, understanding the principles behind decentralized finance (DeFi) and the emergence of Web3 is crucial. Today, we're diving into an intriguing project that embodies these principles and offers a glimpse into the future of decentralized exchanges (DEX). Let's explore the fascinating world of Automated Market Makers (AMM) through a minimal example of a Constant Product AMM.

    Demystifying Automated Market Makers: A Closer Look

    At its core, an AMM is a smart contract protocol designed to enable automated trading of tokens in a decentralized environment. This particular implementation, crafted using Solidity, TypeScript, and Hardhat, showcases the fundamental principles of a Constant Product AMM. But what does that entail? Let's break it down.


    In 2018, Uniswap pioneered decentralized exchanges by introducing the first successful automated market maker (AMM) system. Unlike centralized exchanges, decentralized exchanges (DEXs) facilitate direct cryptocurrency transactions without intermediaries. Automated Market Makers (AMMs), the backbone of DEXs, automate trading processes, eliminating the need for centralized exchanges and their related techniques. Liquidity is pooled into smart contracts, with users trading against the liquidity within these contracts rather than counterparties. Unlike traditional exchanges where only high-net-worth individuals or companies can be liquidity providers, AMMs allow any entity to participate as long as they meet the smart contract requirements. Notable examples of AMMs include Uniswap, Balancer, and Curve.[1]

    The operation of an Automated Market Maker (AMM) relies on a straightforward principle: instead of seeking a seller willing to trade the same amount of a token, users input the desired amount they wish to purchase, and the AMM calculates the corresponding price using a predetermined algorithm and the available liquidity. This process enables continuous trading without human intervention. AMMs offer advantages such as decentralization, 24/7 trading availability, liquidity provision, and potentially lower fees compared to traditional exchanges. However, challenges include potential pricing inaccuracies for less-traded cryptocurrencies, slippage in large trading volumes, and security risks associated with smart contracts.[2]

    Differences in AMM Models

    The various Automated Market Maker (AMM) models encompass constant function market makers (CFMMs), exemplified by constant product, constant sum, and constant mean market makers. These models gained prominence through platforms like Bancor, Curve, and Uniswap. Operating on a fixed function, CFMM-based exchanges maintain unaltered combined asset reserves for trading pairs. In non-custodial setups, user deposits are pooled within a smart contract, facilitating token swaps against pooled assets rather than direct counterparties as seen in order book exchanges.

    Constant Product Market Maker (CPMM) is the pioneering CFMM model, introduced by Bancor. CPMMs employ the function x*y=k to set token prices based on available liquidity. With increasing supply of one token, the other's supply must decrease to uphold the constant product K. The resulting hyperbolic plot ensures continuous liquidity albeit at escalating prices.

    Constant Sum Market Maker (CSMM), another CFMM variant, suits zero-price-impact trades but lacks infinite liquidity. Governed by the formula x+y=k, CSMM yields a linear plot. However, it exposes vulnerability to arbitrage if off-chain token prices deviate from parity, potentially depleting one reserve and rendering the pool inaccessible to traders.

    Constant Mean Market Maker (CMMM), the third CFMM type, facilitates multi-token AMMs with customizable weightings. Maintaining a constant weighted geometric mean of each reserve, CMMM allows for diverse asset exposures within the pool and supports swaps among any combination of assets.

    In conclusion, understanding the distinct characteristics and functionalities of various CFMM models is essential for navigating the evolving landscape of automated market makers in decentralized finance. [3]

    Tech Stack and Functionalities

    At the center of this project lies a powerful blend of technologies and tools that enable the creation and deployment of smart contracts, essential components of decentralized applications (dApps). Using solidity, we craft smart contracts that run on the Ethereum blockchain, ensuring transparency and security in financial transactions. TypeScript, a statically typed superset of JavaScript, was used by me here for robust testing and seamless deployment, enhancing the reliability of the codebase. Supporting these efforts is Hardhat, a development framework that streamlines the entire development process, from writing code to testing and deployment.

    1. Establishing Reserves

    In our AMM smart contract, we begin by defining two immutable ERC20 tokens, `token0` and `token1`, representing the assets to be traded. These tokens serve as the foundation upon which our liquidity pools are built. The reserves of these tokens, denoted as `reserve0` and `reserve1`, dynamically adjust as users engage in swapping and liquidity provision activities within the AMM.

    2. Swaps

    One of the primary functionalities of our AMM is the ability to swap tokens seamlessly. Through the `swap` function, users can exchange one token for another at a calculated price determined by the existing reserves. This process ensures efficient price discovery and minimal slippage, crucial elements in decentralized trading environments.

    3. Providing Liquidity

    Liquidity is the lifeblood of any trading platform, and our AMM empowers users to contribute liquidity to the ecosystem through the `addLiquidity` function. By depositing an equal value of both tokens into the liquidity pool, users receive shares representing their proportional ownership of the pool. These shares entitle users to a portion of the trading fees generated within the AMM.

    4. Withdrawing Liquidity

    Should users wish to exit the liquidity pool and retrieve their assets, the `removeLiquidity` function allows for seamless withdrawal while maintaining the integrity of the pool's reserves. This process ensures that liquidity providers can enter and exit the ecosystem with ease, ensuring the functionality of a dynamic and resilient marketplace.

    Conclusion: Join the Web3 Revolution

    In conclusion, our journey through the topic of Automated Market Makers has shed light on the innovations being made in decentralized finance. With a solid foundation in smart contract development and a commitment to leveraging cutting-edge technologies, projects like this pave the way for a more inclusive and transparent financial future. Whether you're a seasoned blockchain developer or a curious enthusiast exploring the world of Web3, embracing the principles of decentralization and immutability is key to unlocking the full potential of this transformative technology.

    Sources

    [1] Andrey Sergeenkov, What Is an Automated Market Maker?, In: CoinDesk, Learn (January 13th, 2023), link: https://www.coindesk.com/learn/what-is-an-automated-market-maker/

    [2] N.N., Automated Market Maker (AMM), In: BTC Echo, Academy, link: https://www.btc-echo.de/academy/bibliothek/automated-market-maker-amm/

    [3] N.N., What Are Automated Market Makers (AMMs)?, In: Chainlink, Education Hub (May 24th, 2023), link: https://chain.link/education-hub/what-is-an-automated-market-maker-amm

    Go back

    This site uses Cookies. By clicking "Accept All" you help me with improving this website and its performance better. It helps me understand where the website is visited from and by how many people. "Accept (required only)" only tracks the performance of the website. Visit the Data Policy for more information!