How AMM Works: Technology Basics
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 exchange is 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. Automatic market maker mechanism had been known some years before that, but creation of DEX became a huge step for its adaptation to the crypto market.
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 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 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 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.
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, 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 modul. 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 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, 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 the work slow. That is why, the creation of an AMM 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 exchange with AMM model, users do not need to wait for another trader to place a desirable offer. Automatic market maker creates the 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 smart contract 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.
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 “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 amount 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 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 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 splitted between liquidity providers in accordance with their share percentage. Therefore, in 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 which are aimed to catch the best deals, trade them to the pool and therefore maintain the balance of the curve (and token price). Using previously mentioned equation we follow the same assumption and describe 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, where in case of trading the purpose is to get a certain needed asset in exchange for what the trader has in the pocket, while in 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 insolent behaviour 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.
Automatic market maker is a convenient resolution and very vulnerable mechanism at the same time. Unfortunately, AMMs along with bonding curves 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. We are going to discuss various types of resistance increasing in further articles.