Building Blocks of Primitive - Constant Function Market Makers [CFMMs]

January 22nd, 2022

Decentralized Exchanges (Dex), which provide a way for market participants to trade pairs of on-chain assets, have this additional feature of acting as an oracle for measuring the relative price for this pair of assets, and because of this can be thought of as Decentralized oracles.

**Decentralized oracles are smart contracts that rely on users voting on particular prediction market outcomes**. A final outcome is chosen via a social choice function, similar to how majority or weighted majority voting is used to decide outcomes in elections.

Decentralized oracles are becoming increasingly important and sought after sources of information because no one agent/entity/organization, i.e. a centralized source of information, can be trusted for its information completely. While this is not something wholly solved by the de-oracles, their design usually involves a “challenge” phase or/and provide users with rewards for reporting info correctly.

**A fundamental problem when it comes to price markets is the “oracle problem”,** the ability to have correct prices for an asset, or more generally, in the case of blockchains, the problem of providing external data (i.e. outside of the system) to a blockchain. One concern whenever using an AMM is that the price of an asset should accurately reflect that on a centralized exchange (assuming this to be the global price). AMMs are automated market makers and have no notion of this global price and aren’t adjusted accordingly; this price adjustment is usually left to arbitrage bots or traders who use these products. Why is the validity of this price being close to the global price important, though? Suppose an AMM generally has a negligible error or constantly reflects the same price as the global external prices. In that case, such an AMM’s price feed can be used as ground truth for various purposes and is said to be a “good price oracle”.

In this post, I will go over how CFMMs (a class of AMMs) can be used as price oracles and what interesting properties they have, while covering the following paper.

· Angeris, Chitra et al. (2020): Improved Price Oracles: Constant Function Market Makers

The paper is a generalization of the authors work, An analysis of Uniswap markets. In this paper the ideas are generalized to CFMMs with the usage of convex analysis.

You can find a smaller summary of this paper, in this thread.

Been on a mission to read more research in the AMM space. My favourite project, Primitive, is launching soon and is based on Replicating market makers[RMM]. There was a lot of precursing research to RMMs, and I’ve been lately making my way through the research. I can’t say that I have grasped every nitty-gritty of the paper or will be able to do justice to every aspect of the paper, but I’ll do my best to provide nuance or access to more resources. [Note: Convex optimization is hard]

Automated market makers were made famous by Robin Hanson’s” Logarithmic Market Scoring Rules for Modular Combinatorial Information Aggregation”, basically the LMSR market makers. These are functions that are extremely popular in prediction markets. (Refer to Gnosis’ blog or this one for understanding how LMSR’s actually work).

AMMs have gained immense popularity in the defi space as a primitive and have become essential building blocks. While the LMSR and its counterparts provided one kind of automated market makers, the cryptocurrency community has developed a different class called constant function market makers (CFMMs). Uniswap’s V2s constant product AMM quickly comes to mind.

Since the CFMM paper was written in march 2020, DEX volume has increased from roughly 10 million per day to a few 100 million, reaching even a 2 billion dollars of trade volume every day [As of 15th Jan 2022]. This has led to Uniswap (the most popular dex) being used as a price source.

A natural question to ask here is, Is Uniswap a good price oracle for *every type of asset pair* possible? i.e. is Uniswap’s AMM design optimal for all assets? The answer for this is no. Stablecoins arent usually best handled by Uniswap and have been addressed by better mechanisms through curve. Although recently since the introduction of Uniswap v3, it has come to light that may be in the current state of the AMM landscape no one AMM is superior to the other when it comes to stables and that we might be hitting the upper limits of asset-based AMM design, as seen follows. In the absence of superiority of product the usage falls back to fees, and Uniswap v3 has been seeing steady growth of its stable coin market share since it introduced the 1bp fee tier.

The paper’s contributions are as follows:

- A complete and general framework for analyzing CFMMs
- Provided sufficient conditions
- Agents are incentivized to have the CFMM report the correct prices of an asset (the implication of this being that AMMs can act as sound decentralized oracles given the conditions are satisfied)
- A malicious agent has no way of draining the assets of a CFMM by
*only*trading with the given CFMM.

- A simple derivation for the total asset value in a CFMM as a function of external market prices

While a mathematical treatment of the entire paper here would do justice to it, I will not go into the depths but instead talk about the definitions in short and what they help accomplish/prove in the paper, so we can also take away the main results. I think the math in the paper is beautifully written and straightforward.

The definitions of the paper help you make a mental framework around market makers and a neat problem formulation to work with if you are trying for optimal trades or are interested in the CFMM design space.

**Constant Function Market Makers**

**Trading Function**

Now, these definitions break down an AMMs functionality into two parts. The CFMM definition itself defines the domain to the range *R* and secondly, the trading function, which is a representation of a simple trade. Today we only deal with both the input and output trades as vectors of size 2, wherein you have the exchange of (token 1, token 2) → (token 1, token 2). We also deal with larger vectors when it comes to exchanges that have routers wherein you don’t have a direct pool from token 1 → token 2 and choose to hop between n pools to get the trade done, in this case, the size of the vector of both the input trade and output trade will be larger. In the future, we might even have multi-coin tuples where you trade a set of tokens at once with some other set of tokens which saves you the number of distinct transactions that you might have to do.

The trading set is further defined to have the ability to compare two trading functions, and it offers a way to work with the trading functions elegantly. An interesting consequence of this definition is that constructing equivalent CFMMs with trading functions with properties of interest becomes easier.

**Convexity, Path Deficiency and Path Independence**

It is assumed that trading sets are closed convex sets. What does this allow? The field of convex optimization has been thoroughly developed over the past century. A convex definition allows agents to find an appropriate solution that maximizes their payoff to an optimization problem over the trading set. There is no CFMM used in practice whose trading set is non-convex (which is important for generalizing this result).

The Reachable reserve set is the set of reserves that can be “reached” from the current CFMM reserves by performing a trade. “Reached” in this context means the value of the underlying reserves.

Practical CFMMs don’t satisfy path independence, but what does path independence mean? While I earlier thought path independence (from what I understood) was if you arrived on the same values of for the reserves of token 1 and token 2 no matter what path i.e route of assets you take. If you think of the variety of assets that are present in a CFMM, then going from any one asset to some asset in the system will have a variety of paths i.e token 1 → token 2 directly or breaking this down into doing multiple small transactions from token 1 → token 2. Now this is something that is not practically possible because of fees, multiple transactions accumulate the fees so you don’t end up with same amount of token 2 as you would if you would directly do it.

A slightly more general property to talk about when thinking about path independence though is path deficiency, path deficiency instead of talking about the path independence instead makes guarantees about the underlying reserves and this is a property satisfied by all known CFMMs. No trade in a CFMM can make the reserves larger than the current one, i.e they cant make assets out of nowhere.

**Optimal Arbitrage and Marginal Price**

The optimal arbitrage trade is given as follows,

Here **Λ**, is the output tuple, specifying how much of each coin in the ith entry of the tuple after the trade, similarly **Δ** is the input tuple, specifying how much of each in the ith entry of the set before the trade. An optimal arbitrage would try to maximize the difference, as seen in (8). Thinking of an example here you could consider (token1, token2) as your tuple and in an arbitrage, you would want to start with (x1, x2) and end up with (y1, y2) where the sum of these differences i.e (asset price of token 1 ) * y1 - x1 + (asset price of token 2) *** y2 - y1 is highest.

While generally, it could so be possible that solving (8)/(9) does not give the best possible strategy due to market inefficiencies or otherwise or an arbitrageur could break this trade into smaller trades and these could lead to higher benefit. Something that the paper proves is that, **for path deficient CFMMs there is no strategy for which an arbitrageur can get a higher payoff than solving (9) and that solving (9) gives the highest payoff possible.**

There are some optimality conditions that need to hold for (9) to be true, which lead to a simple definition for reporting the price (would suggest people interested in this condition to check the paper pg 15). The definition of this asset implies that whenever the price of the CFMM is mismatched to that of a reference market, an agent is always incentivized to make “free money” by only trading between these two markets which would also bring the two prices to parity.

While the rest of the paper is beyond the scope of this discussion, there are some results worth pointing out:

- The next section elicits the problem to be solved for find the total value of the reserves in a CFMM, ultimately posing a single variable convex optimization problem as follows

- Which when solved for the case of uniswap with zeros fees and a special case of two assets leads to the solution

I wonder how this applies to the Uniswap v3 model which isn’t exactly a CFMM due to its formulation of concentrated liquidity (the ticks fractionalize a continuous curve into discrete parts), but each tick can be seen as a CFMM. I’m excited to write about Replicating Market Makers next, **these are CFMMs whose portfolio value function matches the payoff of a desired product**, in simple words owning liquity in these would be similar to the payoff you would get if you bought the desired product. Primitive’s RMMs are designed to match the payoff of a covered call option. I’ll also be looking into some papers which formalized MEV recently and some others which talk about the importance of liveness in blockchains.

Got a long way to go, Thank you for following along.

Subscribe to Emperor

Receive the latest updates directly to your inbox.

Verification

This entry has been permanently stored onchain and signed by its creator.

Arweave Transaction

nuMgU3VaAqVH2dW…ZKs1ihDB7Oz_7RU

Author Address

0xA95A660FB7688bE…1f0801C045cE24f

Content Digest

Uw7vqtSbUujJOOU…XOiz29isHd51sF0