Understanding Constant Function Market Maker and its implicit costs

Written By: Sekar Langit –  2 min read


Imagine you read the news that the ETH price sees a steady rise. You want to buy it for a profitable trade, but you only have DAI. The solution to this situation is naturally an exchange where you can trade the DAI for a cost to possess the ETH for later trading. This is how trade occurs in Traditional Finance (TradFi) with fiat money, securities, and commodities. With the natural supply and demand circumstances that propagate in Web3 space, cryptocurrencies also create opportunities for the exchanges and enable market makers to profit from these transactions.

In TradFi exchanges, the market makers’ role is to “meet” the sellers and the buyers, sometimes using what we call the Limit Order Book (LOB) to record the orders at the “bid” (price initiated by the buyers) and “ask” or “offer” (price initiated by the sellers) prices. With the advance of technology, the trades in person where the brokers shout their quotes at the stock exchanges now occur in the cold, buzzing server rooms that run 24/7, transmitted using the optical fibre, of which the innovations are in the cutthroat competition: the fastest wins.

In the Centralised Finance (CeFi) world, although the trading is executed for cryptocurrencies built upon the decentralised world of blockchains, the trades are still facilitated by the central authority, the platform providers. Therefore, in the Centralised Exchange (CEX), the mechanics are derived from the Web 2.0 fintech standard user journey of registration à onboarding and KYC à platform wallet management. The deposits, trades, and withdrawals data are stored in the platform’s servers, inaccessible to unauthorised parties, and each platform’s management solely handles the governance.

However, with the cases of hacks in several leading CEX platforms, the blockchain community turned to tap the essential blockchain’s philosophy of decentralisation: reducing the risk by enabling its distribution into nodes. In Decentralised Finance (DeFi), the flow is different with the presence of DEX (Decentralised Exchange) as the Automated Market Makers (AMM). For example, the user wallet in DEX is maintained by the user themself (self-custodial) instead of by the central platform. Moreover, the protocol governance is maintained by the votes of the users who stake their tokens, hence, achieving decentralisation in financial transactions and governance alike.

How would the transactions happen without the central platforms regulating the exchanges? The answer lies in smart contracts and liquidity pools. Simply put, a smart contract is a computer program or lines of code that executes the commands given specific conditions. If A, then B, else C. Because of the conditional statements built into the code, once the transaction suffices the conditions, it can be executed immediately. By this principle, smart contracts work without human intervention and eliminate the need for a centralised system as intermediaries in trade execution and user management. Liquidity pools, on the other hand, provide the function of consolidating the crypto-assets to be exchanged. Since there is no order book to allow price discovery, the pricing and volume of the crypto-assets in pairing are determined using the conservation function (Xu et al., 2023), which rules the volume of token A and token B.

The Implicit Costs

As traders, seeing AMM open more opportunities and control for providing capital gains and income sounds promising. However, with their distinguished characteristics, AMM poses unique costs as the inherent properties of their conservation functions.

 A conservation function is a mathematical equation governing how many tokens A and B are in the liquidity pool and the values are kept constant. As such, AMM with conservation functions can be called “Constant Function Market Makers” or CFMM (Berenzon, 2020), with the types of the function being constant-sum, constant-product, constant-mean, and hybrid. 

Because of the smart contracts’ realm, where the orders are executed based on the sufficient conditions of the transactions, the conservation function significantly impacts the trading variables. The two economic risks for the investors and traders that come with the AMM models with the conservation function are slippage and impermanent loss.


Slippage occurs because of the nature of the constant result in the conservation function that calculates how many tokens you will get at the swap transaction. Liquidity plays a key role here.

Suppose the ratio of each token in the pairing is 1:1 (i.e. the value of token A must equal the value of token B), and we start the liquidity pool with token A and token B. We cannot just put 1 token A and 1 token B to calculate the function because we have to factor in the market price for the pool exchange rate. Take the market exchange rate of 1 token A as 2 tokens B, the liquidity providers must deposit 10 tokens A and 20 tokens B, for example, at the provision of the pool.

Therefore, if we take the constant-product formula: Where K is kept constant, and the numbers at the initial stage are plugged into this equation, we start with K = 10 tokens A * 20 tokens B = 200.

The curve below shows the relationship at any given point of the A and B ratio.

Exhibit 1. The conservation function on the constant-product market maker. My source code can be found in the link at the end of the article.

From the table below, we can see that the price of token A with respect to token B changes as the impact of the trade fluctuates the volume of each token inside the pool. If the market is liquid, the slippage impact does not alter the final token amount we receive from the trade, whereas the less liquid market is slow to return to the initial value of 1 A = 2 B. 

Table 1. Simulation of the Token A – B relationship based on the conservation function above.

In the market orders, the slippage is more prominent because the orders are executed at the market price, while in the LOB model, it has a “limit” or cap, on your tolerance of the price and volume of the tokens. Therefore, from the simulation above, the bigger the size of each subsequent order, the deeper the price impact that can affect the exchange rate between token A and token B, resulting in slippage, whereas, in the LOB model of AMM, your order might not get executed if the market price is not within your desired range.

Impermanent Loss

The impermanent loss has a more complicated mathematical relationship, but it can be summarised into the impact that the liquidity providers suffer from depositing their tokens to the liquidity pools compared to the state where the tokens are held outside the pools. To illustrate this, please find the following simulation.

Suppose user X deposits 10 token A and 20 stablecoin B to the pool, with his share of this pool being 10%. It means the K is 2000, similarly calculated as above. Therefore, there are 100 tokens A and 200 stablecoin B in total in the pool. Say the A market price is $75, and B is pegged to $1, so his initial deposit is worth $770.

Now, with the volatility of cryptocurrencies, token A’s price suddenly surges to $150. By the action of arbitrageurs who want to buy token A that is still below $150 from this pool (by exchanging with stablecoin B) to sell in other markets, the pool composition changes. Without delving into the arbitrage calculation, let’s assume now the pool has 80 A and 250 B. If the user wants to withdraw his capital, preserving his 10% share of the pool, he will obtain 8 A and 25 B, now worth $1,225. Here, he makes a profit of $455.

However, compared to leaving his assets untouched outside the pool, the initial number of tokens of 100 A and 200 B are now worth $1,520. This unrealised opportunity costs him $1,065, which is called the impermanent loss.

From the illustration above, both slippage and impermanent loss are not negligible in AMM trading. I made two simple Python functions to play around with the simulation. You can find the source code here.