News
16 November 2020

AMM Braindump

This post is intended for: Ethereum DApp devs, DeFi traders, crypto product managers, Messari devs... and you! (we're hiring golang/react devs)

It is the result of notes taken while integrating Uniswap into Messari's market data infrastructure and writing a Uniswap/Sushiswap arbitrage bot.

--

AMM, DEX, Uniswap, Curve, Bancor, Balancer, xy=k, constant product, liquidity mining, yield farming.

What do these words mean?

And why should you care? Because AMMs are popular and understanding their various quirks will make you money (or help you avoid getting REKT from slippage).

ELI5: They are apps or techniques involved with creating and participating in markets on Ethereum.

Layman TL;DR: They are apps or techniques for creating financial incentives that enable self sustaining markets under the constraints of Ethereum.

Academic TL;DR (Simon de la Rouviere voice): They are apps or techniques with"new novel signals to curate information".

(this red curve is a result of the "constant product" function, xy=k, which is how Uniswap prices are defined)

If you haven't personally used Uniswap yet. Try it. It will feel scary because it's almost too easy to use. You'll need an Ethereum wallet (Metamask) and some Ether (buy from any exchange, Coinbase, Binance, etc.). Transfer the Ether to your new Metamask wallet. Then use Uniswap to buy $1 of USDC using Ether. Hurray! You now have USDC! Then trade your USDC back for Ether. Each trade will probably cost ~$3 USD equivalent worth of gas, and the trading fees are 0.3%.

Uniswap is an Ethereum app. The core functionality is based on the design of a "constant product" style of automated market maker (AMM).

Automated market maker (AMM) is a generic term for a simple algo (aka bonding curve aka mathematical curve) that enable the equivalent of a stock exchange "market order" on a blockchain (Uniswap being the latest example). This means the "price" is a point on a mathematical curve (e.g. xy=k, see, it's simple!) as opposed to orders buyers and sellers have placed. They're a building block (think: stdlib) of DeFi because they're a clean solution for solving a generic problem: creating markets without an order book (aka price discovery).

Why can't we use an order book? We can, but they don't make as much sense as an AMM that was designed to function under the constraints of the Ethereum blockchain (too slow and expensive to write limit orders).

But AMMs aren't necessarily better than "order books". See SBF's (who advocates for an Ethereum alternative called Solana) argument for order books over AMMs here. His basis is that AMMs are only recently popular because of transient demand for "liquidity mining" (aka yield farming). Only history will tell if SBF (also the CEO of a centralized crypto exchange) is correct.

And see a Dan Robinson tweet clarifying a common misunderstanding that Uniswap (constant product AMM) LPs lose money.

Are AMMs just a blockchain-safe substitute for conventional "order books"? No, they're more abstractly a way to incentivize behavior and thus far a "good enough" alternative to order books. That means they're not just a "stock exchange market order" equivalent but rather a tool to incentivize behavior needed to sustain a use case. But that shouldn't be surprising since, again, it's just an algo. For example: - Uniswap's "constant product" AMM design creates a sustainable ERC-20 exchange by incentivizing LPs with trading fees - Bancor's AMM original "smart token" aka "bonding curve" design creates a sustainable organization (think ICO) by limiting how much of a token the founding team can sell - Gnosis LMSR AMM design enables prediction markets (which have high "outcome space") - Curve.fi hybrid AMM design enables a stablecoin exchange with less slippage than Uniswap's design In other words, AMM is a fancy term for an arbitrary function that returns a price based on the reserves of the staked tokens typically according to some mathematical curve picked for its ability to minimize price fluctuation for the intended use cases. That sentence is a mouthful... so just read it slowly.

If you're a DeFi developer studying AMMs, I consider the following articles, must reads: - (2018-12-03) Bonding Curves In Depth (explains the original "Bancor formula") - (2018-12-11) Compares Bancor's "smart token" to Simon de la Rouviere's "bonding curve" - (2020-01-03) Understanding Uniswap's "constant product curve" - (2020-05-12) Andrew Kang Tweet on AMMs - (2020-06-29) Weaknesses of current AMM designs and reducing them with oracles - (2020-09-03) Academic paper explaining the most common AMM math designs (CP,CS,LMSR) What you should be thinking about while reading those is what happens at the limits of any particular design (where the price fluctuates aggressively). For example, with Uniswap's "constant product" design, the price approaches infinity as the reserves for either of the underlying tokens approaches zero. The limit or edge cases of the curves are interesting because most users never see it, but DeFi devs need to know how their systems perform under stress, and arbitrageurs need to know how to calculate profitable trades (based on curve behavior).

What does Uniswap's data structure look like?

{ token0Reserves, token1Reserves }

(yes, it's that simple)

What methods?

Essentially: Swap(), AddLiquidity(), RemoveLiquidity()

How do we calculate a Uniswap price?

By calculating ratio of token0Reserves to token1Reserves.

If our hypothetical Uniswap pool has 400 units of USDC and 1 unit of ΞEther.

The ratio of token0Reserves to token1Reserves is 400/1, or 400 units of USDC per unit of Ether (aka the price of ether in terms of USDC).

The inverse ratio is 1/400, or 0.0025 units of Ether per unit of USDC (aka the price of USDC in terms of Ether).

What happens "at the limit" (when 80% of Eth reserves are bought)?

If our hypothetical example now has 0.2 units of Ether, that would mean it has 2000 units of USDC in order to preserve the "constant product" formula xy=k (k in this case being 400, which was defined at the last liquidity addition/removal by multiplying the reserves) - i.e. 400/0.2 = 2000. If the token0Reserves are now 2000 units of USDC, that means the price is now the ratio, or 2000/0.2 = $10,000 per unit of Eth!!! (even though there's only 0.2 Eth left in the reserves).

In practice these limits are only reached by arbitrageurs or adversarial economic attackers since Uniswap has max slippage safety checks.

What is Impermanent Loss?

It applies to Uniswap LPs (liquidity providers are users who have deposited/staked their tokens into Uniswap to allow others to buy/sell that pair).

It refers to the scenario where you're making less money as a Uniswap LP than you would simply holding the token you staked. Which leads people to ask why they ever staked in the first place. (collecting trading fees).

It happens dramatically if the price of either token goes below -50% for either of the 2 tokens in the pool, use this tool to see for yourself (see how quickly the impermanent loss increases as the price change goes below -50%). What it literally means is the pool is being drained of the valuable token (perhaps Ether or a stablecoin) and what remains is an increasing supply of the valueless token.

Ergo, be wary of adding liquidity to a pool where one of the tokens in the pool can dip in value by 50%.

Come work with us!

We're currently using Kubernetes, Golang, Postgres, InfluxDB, React (w/GraphQL). See our open roles for frontend and backend engineers and drop us a line at careers@messari.io.

-J. Otto

No other news.
No upcoming event.

3 coins to keep an eye on 👀

Subscribe to receive a weekly selection of 3 coins to watch closely, based on upcoming events and technical analysis.