logo
projects

My Projects: Automated Market Maker

6 min read

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

paul simroth portfolio, title image automated market maker
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

author
Avatar Paul Simroth

Paul Simroth

Full-stack & blockchain developer

Blockchain developer from Austria focused on Web3 technology, smart contracts, and decentralized applications. Passionate about building innovative solutions in the blockchain space.

keep reading

8 more articles in similar categories.

paul simroth, projects foundry defi protocol

My Projects: DeFi Protocol with Stablecoin

Feb 27, 2024

In this article I go over my study project called "Foundry DeFi Protocol". It is a DeFi Protocol with Stablecoin. Check out the article to find out all about it.

Read article
paul simroth, projects foundy lottery

My Projects: Foundry Lottery

Feb 27, 2024

In this artilce I go over my implementation of a Smart Contract Lottery, which uses Chainlink VRF in order to achieve true randomness on the blockchain.

Read article
paul simroth, projects foundry fund me

My Projects: Foundry Fund Me

Feb 27, 2024

In this article I go over an implementation of a crowdfunding smart contract built with using Foundry.

Read article
paul simroth portfolio, real estate escrow

My Projects: Real Estate Escrow Contract

Feb 21, 2024

Here you can read more about my Project "Real Estate Escrow". Explore on of the many usecases of Web3 with me - the Tokenization of Real Wolrd Assets!

Read article
paul simroth portfolio image of solidity code, ethereum domain registrar

My Projects: Ethereum Domain Registrar

Feb 20, 2024

Dive into Web3 with a decentralized domain platform on Ethereum. Learn about ENS, smart contracts, and the future of decentralized apps.

Read article
paul simroth portfolio image of solidity code

My Projects: Building an Ethereum Game

Jan 26, 2024

In this project I venture into the area of Web3 gaming. I go over one Game I coded myselff. What is Web3 Gaming? What Web3 Games are there? Why is Web3 Gaming important? I will go over all these questions!

Read article
paul simroth portfolio image of solidity code

My Projects: Decentralized Ethereum Exchange

Jan 26, 2024

In this article I go over another one of my coding projects. This time I walk you through my project coded as part of the course Ethereum Smart Contract Programming 201 at Moralis Academy. It is a Decentralized Exchange. Read this article to find out more!

Read article
paul simroth solidity cover image

My Projects: Multi Signature Ethereum Wallet

Jan 12, 2024

Made as part of Moralis Academy's Ethereum Smart Contract Programming 101 course, this project is more than just code - it's a Multi-Signature Ethereum wallet, my first project using Solidity and my beginning of navigating the decentralized web..

Read article

Structured overview for LLMs and search: llms.txt