An exchange is an organized marketplace where assets such as securities, commodities, currencies, or derivatives can be bought and sold. Example exchanges include the Tokyo Stock Exchange, for the trading of company shares in Japan; or the Chicago Mercantile Exchange, for the trading of derivatives in the United States.
Decentralized Exchanges (DEXs), a new type of exchange enabled by DeFi, are a means of facilitating the trading of assets without relying on financial intermediaries such as the abovementioned exchanges: DEXs are peer-to-peer. They have the potential to be a real game changer - providing a host of new features and possibilities not previously possible.
This article provides a brief overview of exchanges in traditional finance, compares this to the DEX, and summarizes the benefits of a certain type of DEX, the Constant Function Market Maker (CFMM). We explore how this technology might disrupt the existing exchange industry, and through increased access and choice, permeate more deeply into our everyday lives.
Exchanges work on one simple premise: bring buyers and sellers together, so that their buys and sells are efficiently matched. Doing this well results in more transparent pricing and a quicker time to find a counterparty to trade with than without an exchange. It also results in efficient discovery of a market price for whatever assets are being traded.
Exchanges help buyers and sellers find an equilibrium price. Without an exchange, buyers and sellers have to independently find one another, resulting in different prices for the same asset, and a less efficient market.
All exchanges in traditional finance work on the concept of an order book. This is where buyers and sellers each submit orders to buy or sell an asset, each with their own desired buy or sell price. If an order between a buyer and seller matches, then the order is fulfilled.
To fulfill the order, a complex series of steps involving multiple financial intermediaries takes place, ending in settlement typically two or more days after the trade was made.
Like traditional exchanges, DEXs allow groups of buyers and sellers to exchange assets more efficiently. The key difference is that in the case of a DEX, there are no intermediaries - all transactions are executed peer-to-peer through smart contracts defined on public decentralized ledgers.
This allows for not only the traditional order book model to be replicated in a trustless and permissionless manner, such as with Serum or DeversiFi; but also makes possible an entirely new type of exchange - the Automated Market Maker (AMM).
The rest of this article focuses on a highly popular type of AMM - the Constant Function Market Maker (CFMM).
Let’s walk through an example transaction below - a user exchanging Token A for Token B.
To perform a trade, a user sends an asset, Token A, to the CFMM smart contract which owns “pools” of Token A and Token B that define the two assets of the trading pair. An exchange rate between Token A and B is then calculated based purely upon the ratio of Token A and Token B pools held by the CFMM, using a constant function (the curve in the graph). In our example, the Token A pool is approximately twice as large as the Token B pool, and so the exchange rate between the two assets is the point at which the quantity of Token A and B in both pools intersects with the function. With the exchange rate now established, the CFMM sends the correct quantity of Token B to the user to complete the trade.
Note that no order book was needed; the trade resolved instantly and was completed directly against the pool – not another trader’s order. The price of the pair that a trader can buy or sell at is an emergent property of the buying and selling activity of the users that trade against the CFMM.
As the price of an asset changes in other markets, buyers and sellers would be expected to arbitrage any price differences between those markets and the CFMM, resulting in the price offered by the CFMM at any one time being broadly in line with those other markets. If it’s not - you have a risk free opportunity to arbitrage the difference! Flash loans are a powerful example of a capital-efficient way of arbitraging these differences in DeFi.
The Token A and Token B pools receive their liquidity from users who contribute tokens through a different type of transaction.
When a user wishes to supply the CFMM with liquidity, they must deposit a quantity of Token A and Token B into the pool that matches the current ratio of the two pools – meaning that the price calculated by the constant function is unchanged. The user is then provided “LP” (Liquidity Provider) tokens, which represent a claim on the tokens in both pools and can be redeemed at any time.
Typically, whenever a trade occurs, the CFMM charges a small fee - such as 0.05%, 0.3%, or 1% of the assets, depending on the CFMM. These fees accrue directly to the pools, and so as trades take place, the LP token represents a claim on a larger pool of underlying tokens. Liquidity Providers are therefore rewarded for providing liquidity as they earn fees over time proportional to the amount of liquidity they provided.
So what’s so special about having a smart contract (effectively a machine) that incentivizes people to provide it with liquidity and allows anyone to trade with it?
The first thing is that CFMMs operate 24/7 and can be accessed by anyone around the world. Because this is a smart contract that lives on a global decentralized public ledger, you don’t need an exchange to be open, and you don’t need people submitting orders. Exchanges in traditional finance are typically only open eight hours a day, five days a week, and accessible only after the exchange, or a broker, has given the permission to do so.
Additionally, because DeFi is globally accessible, you can compose transactions across multiple CFMMs no matter the country in which the CFMM was established. This is not possible in traditional finance, such as selling a share on the Tokyo Stock Exchange to buy one on NASDAQ within the same transaction.
Second, there is no separation between trade and settlement - it all happens instantly without the need to trust any third party - eliminating counterparty risk and allowing for capital to be used much more quickly and efficiently. By comparison, in traditional finance, trading and settlement are two separate processes undertaken by separate sets of intermediaries, with each bringing some level of counterparty risk. The processing required to complete a transaction often takes two or more days.
Third, a CFMM exchange makes it possible to create a market for anything - so long as it can be tokenized - for just the cost of a transaction. In traditional finance, the exchanges and other financial institutions act as gatekeepers, limiting the types of assets that can be traded on an exchange. It only makes sense to include assets on a traditional exchange that justify the significant costs of running it.
Imagine if concert tickets were tokenized. If you could no longer go, you could easily sell those tickets for the correct market price on a CFMM. This means that as DeFi matures and more assets are tokenized, we will likely be interacting with CFMMs (and other types of DEXs) much more often, as markets can be created for anything from tokenized parking spaces to in-game currencies.
Last, CFMMs provide guaranteed liquidity at all price points. This is possible because the constant function tends towards infinity as the relative price between assets becomes more extreme. The price may change dramatically at the extremes, but there will always be liquidity for the trade.
CFMMs are therefore integral to the proper functioning of other parts of DeFi that require guaranteed liquidity - reinforcing how important composability is within a DeFi ecosystem.
For a traditional order book exchange, in order to have liquidity, you must have someone else on the other side of the order. This means that there are some extreme scenarios where the liquidity for an asset could disappear. For lending dApps in DeFi such as Aave or Compound that need to liquidate positions at certain prices, this would be a problem, as an order book exchange cannot guarantee that orders will be matched.
However, guaranteeing liquidity at all price points isn’t always a good thing as the liquidity offered by the more extreme ends of the function, in 99.99% of cases, never gets used. This makes CFMMs relatively capital inefficient. Solutions to this are in the process of being addressed with innovations such as Uniswap v3, which allows for liquidity providers to more tightly define the function that they provide liquidity against, resulting in better capital efficiency.
DEXs are a real innovation. They allow for anyone, no matter where they are, to be able to trade any tokenized asset instantly without needing to trust anyone else. That’s a game changer.
But to harness the innovative disruption that DEXs can provide, the programming language used to create them must be fit-for-purpose. With DeFi hacks occurring weekly, and current languages such as Solidity being devilishly complex and unintuitive, a new development paradigm is needed if the potential of DEXs is to be fully realized.
This is why Radix is building Scrypto - the only programming language tailor-made for DeFi - with the concept of assets as a core feature. To understand why developing DEXs in Solidity just makes no sense, check out our article The Problem With Smart Contracts Today. To find out how Scrypto allows for DeFi development at the speed of thought, be sure to read Radix Engine v2: An Asset-Oriented Smart Contract Environment and Scrypto: An Asset-Oriented Smart Contract Language.
Be sure to also check out our developer site, developers.radixdlt.com, to get going with Scrypto and build your first DeFi dApp.