Toxic Order Flow and Liquidity Provision in DeFi

Toxic Order Flow is commonly defined as a trader’s exposure to the risk of counterparties taking advantage of private or privileged information to create disadvantages for traders and market makers. Toxic order flow is the most common reason that market makers end up providing liquidity at a loss.

Redesigning global financial infrastructure based on open and permissionless systems involves re-learning why various regulations and services exist. This article will take a look at Toxic Order Flow as it pertains to DEXs built with Automated Market Makers, particularly those requiring two-sided liquidity provision, and some other common appearances of Toxic Order Flow in DeFi. 

What is Toxic Order Flow?

A practical example of Toxic Order Flow is front running, which in (centralized) exchanges for conventional securities and equities, has been a cause of public concern sufficient to warrant effective regulation since the 1980’s. Front-running transactions on the Ethereum mainnet is a feature of the public mem-pools design, except this sort of front-running occurs in public view and not necessarily in virtue of privileged information belonging to private parties.  

“high levels of toxicity can be harmful to overall market liquidity and precede precipitous drops in asset prices”

Impermanent Loss in DeFi

Constant Product AMMs and their symbiotic forms of Toxic Order Flow

Liquidity Providers (LPs) for many DEXs in DeFi are subject to toxic order flow. When providing liquidity to Automated Market Makers (AMMs) such as Uniswap, users are exposed to the risk of Impermanent Loss (IL). That is to say, LPs who deposit tokens into AMM smart contracts in order to earn a share of the exchanges trading fees (or other rewards) run the risk of closing out their positions with less funds than they started with, even once accounting for trading fees (and other sources of revenue). In order to understand why this is the case, it’s necessary to dig into how constant product AMMs work.

AMMs serve to enable reliable liquid markets without a centralized third party managing the liquidity and order book. It is no surprise that Uniswap rose to such prominence when myriad tokens have been issued on the Ethereum blockchain, many of which without sufficiently liquid markets to facilitate trades. 

AMMs allow anyone to support a liquid market for such tokens by depositing assets into a smart contract governing a Liquidity Pool. Any user can then, without having to ask anyone for permission, trade assets in the liquidity pool at a price determined by the AMM. The trade off for creating liquid markets where they previously didn’t exist is that the price isn’t necessarily going to be as good as one might get elsewhere, such as a CEX with sufficient liquidity. In fact, the execution price of a trade gets exponentially worse as the size of the trade increases.  

The constant product means that the value of token balances on each side of the pool (many AMMs require users to provide liquidity to both sides of a pool) must remain unchanged by trades relative to their product.

x * y = k

Constant Product equation, where k is the “invariant” (source)

Trades on Uniswap’s AMM

Trades on Uniswap’s AMM change balances and therefore also price (source)

Any trade via an AMM Liquidity Pool is going to see the balances of the underlying tokens change. The fact of this change in balances, combined with the constant ratio of value between the sides of the pool, results in an immediate arbitrage opportunity for anyone paying attention and sufficiently motivated to capitalize on it. 

A trade against an AMM involves adding liquidity to one side of the pool and taking it away from the other. Consider the example of buying ETH with DAI in an ETH:DAI pool, increasing the number of DAI tokens in the pool and decreasing the number of ETH tokens. The balance of ETH has decreased and the balance of DAI has increased. Since prices for trades are determined by the AMM’s constant product, the ETH side of this pool has just become more expensive and the DAI has become less expensive. That is, relative to markets external to the AMM with all other things being equal. Arbitrageurs, automated scripts monitoring smart contracts and exchanges for opportunities to buy lower and sell higher (or vice versa) across markets, can buy DAI from the above ETH:DAI pool at a discount and sell it at market rate elsewhere in order to take a profit. This is why LPs are incentivized to deposit an equal amount of tokens (in terms of value) into either side of a pool when first seeding it with liquidity (otherwise they immediately become marks for Toxic Order Flow from arbitrageurs). 

Impermanent Loss

Arbitrageurs are an essential part of keeping AMM prices in-line with prices on other markets. In the following we look at how price changes, on external markets, for the assets deposited into two-sided liquidity pools can generate a sort of Toxic Order Flow that costs Liquidity Providers. Unbalanced Liquidity Pools provide an opportunity for traders to make money at the expense of those providing liquidity. LPs on AMMs can become unbalanced simply in virtue of their trading activity, as seen above, or in virtue of changes in asset prices on the open market. Once a user deposits assets into a Liquidity Pool, such as those supported by the Uniswap smart contracts, they are at risk of Impermanent Loss. This risk is not as prevalent when a user leverages an AMM that requires only single-sided depositing of funds to Liquidity Pools, such as when a user can contribute just ETH to the ETH:DAI pool. 

Imagine we’re looking at the ETH:DAI pool for Uniswap. Say ETH is worth $2,000, DAI is worth $1, and a user deposits $2,000 of ETH (1 ETH) and $2,000 dollars of DAI (2000 DAI) into the liquidity pool’s smart contract. Imagine ETH’s price doubles, on the open market, such that it is now worth $4,000. There is now an arbitrage opportunity for traders to buy ETH at half the market price, via the ETH:DAI hypothetical liquidity pool imagined here, and sell it on the open market for a profit. In so doing, these arbitrage traders increase the DAI holdings in the pool and decrease the ETH holdings and can do so until the pool is rebalanced. So what are the balances of ETH and DAI in this pool after this process?     

x = sqrt (k / r) 

y = sqrt (k * r)

Where x is the ETH balance, y the DAI balance, k the total liquidity across the pool and r is the new ratio of assets in the pool after rebalancing

Let’s say this user’s initial deposits represent 1% of the liquidity pool’s total balances (including all other deposits from all other LPs). So k in the above is 20,000,000 and r is 4,000 (with 1 ETH = 4,000 DAI). From this we can compute our user’s ETH and DAI balances as 1% of the pools total ETH and DAI balances following the price movement and rebalancing arbitrage trading. Computing x in the above gives us ~70.71 ETH and the above equation for y implies there are ~282,842 DAI. The above described user is entitled to 1% of this, should they choose to close their position & render any impermanent loss a distinctly permanent loss, so they have 0.7071 ETH and 2,828.42 DAI. At the current market prices that’s ~$2,828 worth of ETH and $2,828 of DAI. 

So they gained $1,656 compared to their initial position and have $5,656 total. Had they held onto their positions and not bothered providing liquidity to this pool then their assets would have appreciated to a total value of approximately $6,000. So the potential upside in this example was $2,000 but due to toxic order flow the LP see’s only $1,656 of this and has incurred $344 in Impermanent Loss. In DeFi, as in life, no good deed goes unpunished.  

**to sanity check these numbers we can look at Uniswap V2’s documentation & their rubric for price changes and associated, approximate, losses relative to HODL’ing. Particularly “a 2x price change results in a 5.7% loss relative to HODL”, 5.7% of the the HOLD’ing outcome in the above is 0.057 * 6,000 = 342, hardly a rounding error on the above computed figure. 

Front Running by Design

All pending transactions on the Ethereum mainnet live in the public mempool and miners choose which to include in the next block. This influence over transaction prioritization and ordering has several analogies with a traditional marketplace, insofar as people are broadcasting intentions to transact and those able to fulfill the transactions compete for the privilege to do so. This is obviously not an ideal tooling in and of itself for building end-to-end financial exchange solutions, since the public mempool entails publicly broadcasting sensitive, privileged, information.

Miner Extractable Value (MEV) and Maximal Extractable Value refer to the broadly defined practice, which all miner’s are incentivized to participate in assuming they are economically rational agents, of ordering transactions (and including or excluding transactions) in order to extract the most value from the block generation process. 

There is a sense in which everyone who publishes an Ethereum transaction to the mainnet is generating Toxic Order Flow to the benefit of miners. Miners can include whatever transactions they choose to in the next block and they’ll ruthlessly, algorithmically, select transactions to be included which generate them the most revenue. This might involve ignoring your transaction, preferencing it or front-running/sandwich attacking it. The makers being adversely selected here are all those publicly transacting on Ethereum. 

Making DeFi an attractive place for Market Makers

DeFi needs to be an attractive place to provide liquidity in order to achieve its potential. Not being routinely front-run is going to be one of myriad requirements for institutional, or even seriously professional, market participants. Insofar as Layer 2 solutions obfuscate sensitive details of transactions from predatory market participants, such innovations stand to drive the adoption of blockchain technology.

Sign up for our newsletter.