How AMM Works: Technology Basics
In this article we will explain how AMM technology works and show the difference between AMM exchange and traditional order book. You will also know some basics of technology to build your AMM-based DeFi solution in future.
The exchange has become a part of a traditional financial system a long time ago. Its mechanism of work is very well known, in a broad sense, an exchange is a market where sellers meet with buyers in order to make a transaction. The relationships on the exchange are supported by order books which are (electronic) lists of orders.
This common mechanism has been successfully working until the crypto intervention. Along with the technology development and increase of locked amounts in crypto assets, holders started to seek new solutions. The automatic market maker mechanism had been known some years before that, but the creation of DEX became a huge step for its adaptation to the crypto market.
Some of HISTORICAL BACKGROUND
The idea of so-called “on-chain market maker” was firstly described by Vitalik Buterin and explained as the idea of “On Path Independence”. It was further implemented in Bancor (V1) Protocol and introduced to the public in 2017. Yet, protocol gathered a lot of critics and doubts around it, so stayed kind of “behind the curtain” of the AMM fame.
The first and probably still maintained, buzz around AMM and decentralized finance itself happened along with the Uniswap release in 2018. It made a real breakthrough on the market because of a couple of features: fast and secured trading deals, earnings from liquidity, and decentralization of exchange itself.
As long as Uniswap was aimed for token exchange, we experienced Curve invention in 2019, which enabled the decentralized and non-custodial exchange of stablecoins. Curve protocol has been always compared to Uniswap model and has been named as its follower, which is not really true in nature. This difference will be further discussed in an article dedicated to the AMM types.
Uniswap uses a so-called constant function market maker model which, as appeared, faces a few serious obstacles, for instance, front running. This contributed to the creation of other protocols like Balancer, Mooniswap or FairSwap, all in 2020, which led to the huge boom in dApps development and DeFi in particular.
New protocols aimed to solve the common problems classic AMM exchanges experiences and therefore use other, quasi-AMM models to perform continuous token curves. In this article, we are going to concentrate on the “traditional” AMM model in order to understand the basics. If you want to know more about Balancer and how to solve the front-running problem check the pinned article.
Centralized vs decentralized exchanges
Well, how decentralized exchanges are different from traditional ones, and why users want to stay away from such a simple and universally known centralized model?
First of all, a centralized exchange contains risks for traders. One of the main reasons is that the custodian of all assets and information about all transactions is one organization. Additionally, the exchange requires users to deposit funds in a wallet that is owned and operated by the exchange itself. Furthermore, it is easier for hackers to detect and attack vulnerabilities concentrated in one center, so in case of attack, the hacker can shut down the exchange and “go away” with all funds. Due to traditional order book usage, the work of centralized exchange is relatively slower and sometimes may lead to funds loss, in case of certain technical mistakes.
In contrast, the order book of a decentralized exchange is stored on the blockchain. All processes connected with order placement, security audit and maintenance are under the smart contract power. IDEX is one of the well-known representatives of decentralized exchanges with traditional order book module. Yet, IDEX is rather should be called partially decentralized, because it uses a centralized order book which then sends the information to the smart contract on the blockchain. Hence, in order to get such a crucial decentralized environment, users can go to Dy/dx which is an IDEX mate, but with a totally decentralized order book in it.
So, why do we need an AMM model if DEXs are cool with the traditional order book practice?
Essentially, the work of order book lies in recording the buy/sell offers and then matching traders with the same or similar requests, but in terms of blockchain, this has become the main problem of this system. These processes make the work costly and make work slow. That is why the introduction of an AMM in DEXes has brought crypto trading to a completely new level giving the ability to atomic trading or instant feedback loops.
The main difference between those two approaches lies in the fact that while using the AMM based DEX model, users do not need to wait for another trader to place a desirable offer. Automatic market maker creates an environment where the user trades with the smart contract within the chosen pool and then gets the requested tokens and happily leaves. The algorithm of the smart contracts dictates how the assets should be traded and therefore do the whole work by itself. What kind of magic they use to fulfill such will be further explained below.
Thinking of creating your own AMM-based DeFi solution? Start DeFi development with Blaize by contacting our expert!
What is AMM?
If say a general definition of an AMM, it would sound that AMM is an automatic actor whose work is defined by conditional rules of trade inserted into smart contracts. Yet in Blaize we do not follow a general definition, so let’s dive deeper into the math. The formula for let’s call the “traditional” automatic market maker model looks as follows:
where X and Y are reserves of certain (chosen) assets in the pool, and C is an unchangeable constant. Due to the presence of such a constant this model is also called a constant product market maker.
The next important part of the AMM is a pool where actually the whole process is going on. Well, in its traditional form, the pool consists of two different tokens supplied equally. Yet, this does not mean that the provider should put in the same amount of each token. Tokens equality in this case should be calculated out of the market price and then translated to the relevant conditional number. Imagine you create a pool with ETH and Dai in it, the price for those two differs considerably on the market (when 1 ETH = $100; 1 DAI=$1), so in order to make them “equal” we need to put, for instance, 10 ETH to 1000 Dai, then the overall cost of each will be 1000:
$100 (ETH price) *10 = 1000 and,
$1 (DAI price) *10 = 1000.
In this case we can say they are supplied equally to the pool. So in terms of pools, what you should pay attention to is the relation between two (chosen) tokens and its volatility on the market.
Coming back to formula, now we can easily establish our constant value for this pool. So having ETH as X and Dai as Y we will get:
10 (ETH)*1000(DAI)= 10000
If nothing happens, the user will see these stable values for each. Yet, what if something does happen, and somebody wants to get in and take 1 ETH, as an example. Then, the formula will change as follows:
(10ETH – 1ETH)*(1000DAI + (1ETH*price + fee) = C1
where X and Y are reserves of certain (ETH and Dai) assets, 1 is the desirable withdrawal number, a is the price of 1 ETH in DAI (1 ETH = 100 DAI) and fee is the amount the trader pays additionally to the DEX for the ability to fulfill such an operation. In this way, we see the constant has changed due to transaction fee, and will continuously change with every further trade.
But how can we say it is the “constant” if it actually changes?
The answer is, we can, as long as the product of X*Y is equal to C. Transaction fee does not dramatically influence the pool curve and varies each way thousands of times per day. In order to make a fair trade, AMM DEXs use the constant invariant equation to always balance assets in a pool, this happens thanks to the creation of a bonding curve.
How to detect an accurate price?
In its sharp explanation, we can say that if you want to sell a token, then the price will be equal to the number of tokens in the pool, but if you want to buy a token, the price will be as if you have already purchased one. There are two main options of how the user can use the curve. Assume there are 10 certain tokens in the pool and the price is 10 for each. You want to sell back 3 tokens.
- In the common scenario, for selling them all at once you will get 3×8=24 in return (because the price after your sell will be 8 already)
- In a smarter scenario, you would pay out 1 token at a time, and then get 10+9+8=27 in return.
The only problem is the gas fee which is issued for each transaction, therefore the second option seemed reasonable only while dealing with bigger amounts of tokens and needs in-depth pre-calculation.
Where does money come from ?
One of the most important questions to ask is where exactly money comes from in all this. Well, the profit may come in two scenarios: while providing the liquidity and while fulfilling the trade.
There are following types of actors participating in the process:
- Liquidity providers – users who want to supply their crypto assets into the pool and therefore make profit out of it;
- Traders – users who want to buy or sell certain type and amount of tokens;
- Arbitrageurs – actors who maintain the pool price in exchange for reward.
How do liquidity providers make profits?
Remember the “+fee” in the second formula? That is how.
For those, who want to supply their liquidity to the pool, DEX’s work is similar to traditional banking depositing. So you hold a certain amount of crypto (assume ETH) and you want your money “to work”, so you can “give” them to the chosen exchange (assume Uniswap) and therefore you become eligible to earn profit from your assets’ usage.
In traditional AMM DEX it works as follows: liquidity provider A supplies (assumed) 1000 ETH to the ETH/Dai pool, where (assumed) 9000 ETH have been provided already. Now the total amount of ETH in the pool is 10000 ETH (9000 ETH + 1000 ETH). So the A “share” in the pool will be 10% of the whole ETH pool. When any trade within the pool occurs, the trader pays a transaction fee which is then transferred to the liquidity providers as a reward.
The fee is split between liquidity providers in accordance with their share percentage. Therefore, in the case of A, he will get a 10% reward from each transaction in the poll he supplied money to.
How do arbitrageurs make profits?
Arbitrageurs are usually represented by bots that are aimed to catch the best deals, trade them to the pool and therefore maintain the balance of the curve (and token price). Using the previously mentioned equation we follow the same assumption and describe the trading process as follows:
(10-1)*(1000 + (1*a +fee)) = 10000
assume (1*a) as X
1000+X = 10000/9
X = 1111-1000=111
We can see that due to a trade fulfillment, the price of tokens in the pool has changed and the price of ETH increased to $111 (instead of $100). Yet, the normal price for ETH on the regular exchange (let’s assume CoinBase) still remains $100. When such happens, the arbitrageur buys ETH from CoinBase and sells it to the pool (still for $111). In this particular example, the profit of the arbitrageur will be $11 (and all this from a single transaction!!!).
What is the difference between arbitration and trading ?
Traders are basically retailers who want to exchange their crypto assets at a good price. In traditional AMM, traders have no additional information about general market prices and have to trade only at the price offered by the pool. Well, fulfilling the trade they do influence the pool price, but this does not make them arbitrageurs.
The main difference lies in the purpose of conducting such an action, wherein the case of trading the purpose is to get a certain needed asset in exchange for what the trader has in the pocket, while in the case of arbitrageur the main purpose is to buy low – sell high and make a profit out of it.
Another interesting feature of AMM is that despite the insolent behavior of arbitrageurs the system basically cannot exist without them. In case of lack of permanent token supply or buy flow, the curve will not maintain at the same level so will not create a pleasant exchange environment for traders.
An automatic market maker is a convenient resolution and very vulnerable mechanism at the same time.
As one of the main issues with AMM DEXes is that their mechanisms are sensible to front running and pumping attacks. The paradox is, the easier to find the intrinsic value of a token, the harder it is to manipulate the system. Yet, gladly, there are already a few solutions on the DeFi DEX market that successfully solves these problems. We have discussed various types of resistance increasing in Impermanent loss and Front-running solutions for the AMM DEX article.
Blaize team of DeFi developers has gathered extensive knowledge of the decentralized finance market and DeFi dApps development. If you are thinking of building AMM-based DeFi project similar to Uniswap, for example, do not hesitate to contact Blaize for further discussion.