Learn

Swap Functionality and AMMs

The importance of swap functionality and AMMs

Asset exchange is essential to financial inclusion. It enables cross-border payments, provides underserved regions with new access to financial instruments, and allows an increasing number of people worldwide to participate in global markets by saving, investing, and building wealth.

Traditional asset exchanges involve the buying and selling of traditional currencies and assets, like fiat money (USD, EUR, etc.), stocks, commodities, real estate, and other tangible or intangible assets, typically through centralized financial intermediaries, such as currency exchange services, banks, bureaus, centralized online platforms, and stock exchanges.

But many individuals around the world don’t have access to these formal banking services. Digital asset swaps, especially those conducted through decentralized platforms, provide an alternative means of financial inclusion for the unbanked population. This ability to swap assets directly within a decentralized platform or protocol empowers individuals to manage their assets and participate in economic activities in new ways.

For asset conversion to be possible, liquid markets, where people can buy and sell assets, are crucial. Automated Market Makers (AMMs) create a way for people to easily provide liquidity for a range of digital assets, with incentives for them to do so.

In fact, one of the strengths of a smart contracts platform like Soroban is that it enables developers to design innovative new DeFi services like AMMs, which incentivize liquidity provision across the ecosystem. A central goal of the open-source Stellar network is to unlock the world’s economic potential by making money more fluid, markets more open, and people more empowered. Cross-asset exchange capabilities are fundamental to this mission, and successful cross-asset exchanges require liquidity.

Advantages of decentralized swap functionality and AMMs

Blockchain networks allow users to exchange digital assets of all kinds and to easily create liquidity for any pair without relying on centralized exchanges. This interoperability is at the core of what makes digital asset swapping so powerful. On networks where, for instance, there is a prevalence of stablecoins, users can convert assets to make cross-border, cross-currency payments without dealing with the high cost and friction of the correspondent banking system. AMMs bootstrap liquidity for these digital assets, so users can do more than just hold a single asset; they have the flexibility to convert that asset too. Moreover, this interoperability can extend across blockchain ecosystems as well, where cross-chain swaps allow users to convert from one network currency to another.

Enabling direct swaps on-chain can also improve market efficiency by democratizing andincentivizing liquidity provision, which in turn can lead to more participants creating deeper markets for more currencies. AMMs provide access to a wide range of digital assets, including tokens and currencies that may not necessarily be major reserve marquee currencies. In fact, a common limitation of centralized exchanges is the illiquidity users encounter if the centralized company does not choose to offer the trading pairs those users are looking to swap between. This liquidity provision is what makes AMMs so transformative, especially regarding financial inclusion around the world.

Importantly, AMMs put users in control of their own finances, which means that people worldwide, including those in developing countries, can manage their own financial choices, control their own assets, and participate in AMMs without the need for intermediaries, and without having to meet the entry requirements to traditional financial markets that may be otherwise unsurmountable.

Why are AMMs so powerful?

To enable swap functionality and the associated benefits, developers build decentralized exchanges on blockchains where swaps can take place. For those exchanges to be useful, the markets need to be liquid, meaning they need sufficient depth to allow users to convert assets at a fair rate, and to absorb sufficient volume so that trades don't impact asset prices. Liquidity is the degree to which an asset can be quickly bought or sold without significantly affecting its price, and it is crucial for the growth of digital asset ecosystems, where the interoperability and efficiency of decentralized exchanges have the potential to have a real-world impact on global communities.

While there are many methods to create liquid markets to allow users to swap assets, AMMs have proven to be especially effective and have gained traction across the blockchain ecosystem — as of June 2023, Uniswap alone has processed over $1.5 trillion in trading volume. This popularity of AMMs is partly due to their ease of use and their capacity to provide incentives that help bootstrap liquidity for all kinds of digital assets. AMMs are also powerful because they are simple yet elegant tools—(the original Uniswap AMM protocol was notably only 300 lines of code). Since AMMs are generally built using smart contracts, they leverage the advantages of decentralized finance and blockchain, operating in a trustless environment, with immutable rules written into the contracts and no intermediaries. What’s more, because smart contracts are easily programmable, developers can create sophisticated and customizable AMMs to suit their specific use cases and provide a better method for asset exchange.

How do AMMs work?

At their core, AMMs rely on liquidity pools, which are collections of digital assets crowdsourced by users known as liquidity providers who deposit their assets into the pools. Liquidity pools are essential to how an AMM functions since users of an AMM will only be able to perform peer-to-peer swaps if there is enough liquidity for a tradable asset on the given exchange. Again, this is one of the most fundamental advantages of decentralized AMM protocols over centralized exchanges, which may only offer a limited number of trading pairs for users to swap between.

In an AMM, each liquidity pool contains two or more different tokens that can be exchanged with each other. The heart of an AMM is a mathematical formula used to determine the price of the tokens within the pool. When a liquidity provider wants to contribute to a liquidity pool, they deposit a certain value of tokens into the pool, and the pool adjusts the token pricing based on its formula. Then, when users want to trade tokens on the AMM, they interact with the AMM's smart contract. This smart contract calculates the corresponding amount of tokens to be received based on its specific formula and the amount of tokens sent by the user. The user's trade is executed directly against the liquidity pool at the current market price determined by that formula. As trades occur, the AMM adjusts the pool's token balances, which affects the price of the tokens relative to each other.

For example, one of the most common types of AMMs, the protocol used by Uniswap, relies on the constant product formula, where the AMM ensures that the liquidity pool always has a balanced ratio of a given pair of tokens. When trades occur, the smart contract adjusts the token quantities in the pool to maintain the constant product. The more tokens of one type are traded, the more the price of that token increases compared to the other token in the pool.

The two other major AMM protocols, weighted average price AMMs and stableswap AMMs, rely on their own mathematical formulas. Weighted average price AMMs let liquidity providers create custom liquidity pools with different token weightings, where each token's weight determines its impact on the pool's price calculation. Stableswap AMMs, on the other hand, are designed to facilitate swaps between stablecoins or assets with low price volatility, with the goal of maintaining a stable output price. These AMMs are especially suitable for stablecoin trading pools.

For any of these protocols, each time a trade is executed, the AMM charges a small fee, typically a percentage of the traded amount. This fee is then paid back to the liquidity providers in proportion to their share of the liquidity pool to further incentivize their liquidity pool deposits.

Impermanent loss

Of course, swap tools like AMMs aren’t without their challenges. One of the most significant risks posed by AMMs is the potential for impermanent loss, which is the value difference over time between tokens deposited into an AMM’s liquidity pool and those same tokens held in a digital wallet.

This temporary loss in value may occur when, for example, the price ratio of the two tokens in a constant product liquidity pool changes significantly after the liquidity provider has deposited their assets. If prices move in a way that is not symmetrical—if, for instance, some real-world event, unbeknownst to the on-chain AMM, directly impacts the value of one or both of those digital tokens—the liquidity provider may end up with fewer assets than they would have had they held the tokens outside the liquidity pool. The constant product formula, in particular, always recalibrates the ratio between tokens in the pool until the constant product equation is satisfied without necessarily considering (or being aware of) off-chain fluctuations in the exchange rate between those tokens.

Impermanent loss is particularly relevant in volatile markets, causing liquidity pools to lose value when prices between tokens diverge, just as they generate fees when there is high trading demand.

Future of AMMs and swap functionality

Developers and researchers are continuously working on building sophisticated AMM protocols that leverage the power of smart contracts and blockchain. Some of the most significant improvements to AMM protocols may involve developing more advanced AMM models to attract different types of users to decentralized exchanges. The future may also see the development of more sophisticated cross-chain AMMs that enable seamless swapping of assets across multiple blockchain ecosystems, fostering greater interoperability and liquidity flow between different blockchains.

But the potential for a universal asset exchange might be one of the most promising roles AMMs may play in the future of DeFi ecosystems. Incorporating AMMs into an asset issuance platform, where AMMs sit in the middle of cross-border payments to create liquidity, could allow that platform to power universal access exchange in new and transformative ways. Take the Stellar network, for instance: as a blockchain, the Stellar network does a great job at powering on- and off-ramps for assets, but it requires a large amount of liquidity to realize its asset exchange potential, which is difficult to bootstrap. With the development of the Soroban smart contracts platform, ecosystem developers can create AMM protocols that use smart contracts to allow trustless, incentivized participation in liquidity provision, which can increase liquidity, enabling swap functionality and cross-asset exchange on the blockchain… and that's a stellar combo!

The Soroban ecosystem is growing!

We are excited to grow the Soroban ecosystem.

Smart contracts are usable on the Stellar network with Soroban, Stellar’s native smart contracts platform. Soroban is an additive feature that is integrated into the existing Stellar blockchain and can interact with Stellar accounts and assets. Soroban is currently live on Futurenet, a shared test network with Mainnet launch happening later in 2023.

Get started with Soroban now by accessing our Soroban documentation.

Explore Next

Your Next Read

Lending and Borrowing Markets

Learn the power of lending and borrowing markets and how they work in a DeFi ecosystem.

View

Smart Contract Building Blocks

Smart Contracts

Explore building blocks for secure Smart Contract use cases.

Read More

Stellar Developer Hub

Explore documentation, tooling, & community support to help you get your blockchain project up quickly. Take advantage of learning resources to…

View

Soroban: Smart Contracts Docs

Start building on Soroban today with the help of established tools and documentation

Go