Cryptoeconomics Series P3: Swapping Logic - Batching and Ending MEV

Cryptoeconomics Series P3: Swapping Logic - Batching and Ending MEV

Welcome to part 3 of the Chainflip crypto-economic series with me, Simon. Sorry it’s been a while, scaling and fundraising while also moving my whole life across the other side of the planet during a pandemic has taken an awful lot of time. In any case, I’m landed and settled now, and thought it was high time to present some more high-intensity alpha for all you crypto nerds out there.

So here we are, part 3 of 5 parts. The final 2 articles will come in due time, but here is the series rundown so far:

This time round, I thought we’d take a departure from the hardcore token economics of Chainflip and focus on something a little more technical and a little less token oriented. However, we are designing an AMM, so I thought today I’d share some of our ideas around what we can do with the Chainflip AMM that isn’t possible on other AMMs. In fact, this topic can barely be described as being relevant to cryptoeconomics, but it’s my series and I’ll do what I waaant!

P3: Swapping Logic - Batching and Ending MEV

There are many ways to execute trades. In traditional order book systems, a central entity receives, sequences, and then matches orders. Trades are executed the moment the trading engine realises there’s overlap between orders. Market orders, when received, are executed immediately against any limit orders that are already there.

This simple trading ruleset powers every high frequency trading system out there. At Chainflip, we’re building an AMM, and there’s one thing you can definitively say about AMMs - they’re not high frequency.

AMMs are a pretty novel solution to a non-obvious but serious problem. Yes, established trading systems have been around for a while, but when you consider that Ethereum has (comparatively speaking) an outrageously long execution time (limited by block speed), hugely expensive data storage, and an execution environment which is only agent initiated, it becomes clear that building a high-frequency orderbook system on Ethereum isn’t gonna work.

So what does the solution actually do? How do Uniswap & Co. succeed with the AMM model?

It’s said that restrictions can improve your creativity, and that seems to certainly have been the case here. Slow blocktime? No problem, we’ll make sure orders are submitted and executed together in a single transaction. Hugely expensive data storage? Also no worries, we’ll delete the concept of an orderbook altogether and replace it with a liquidity curve. Agent activated execution environment? Awesome, we’ll get the user to pay for and execute all of the computation associated with their trade or action.

This is a pretty interesting thing to think about. Uniswap is not automatically run. Every single action that occurs on Uniswap is the result of someone, through an Ethereum transaction, turning on Uniswap, making it execute a handful of small instructions, and then turning it off again.

Uniswap is like a delivery company that only has a single delivery van and no driver. Anyone that wants to use it has to go to the van, pick up the keys, do the delivery, and then give the keys back once they’re done. And while they’re doing their own delivery, no one else can drive the van. In fairness, this is how all smart contracts work - but when talking about an exchange that literally moves billions a day in volume, it’s a pretty wild consideration.

This is quite a restrictive environment for an exchange, and so has forced innovation to occur. While it has created liquidity curves, LP tokens, programmatic interaction, concentrated liquidity, multi-asset pools and a dozen other DeFi innovations, there are limits to how much the trading experience can change when bound by the rules of Ethereum smart contracts.

The physical boundaries of the EVM constrain most AMMs in the same way that we, the human race, are bound to the speed of light and the orbit of the sun. Forever destined to be at least 55 million kilometers away from our closest remotely similar and yet incredibly distant neighbor, the red planet of Mars: a cold and uninhabitable place with a thin and deadly atmosphere, ready to kill you the moment your feeble human body is exposed to this hostile environment of dust and radiation. Without technological advancement, DEXes will be forever bound to the surface of the EVM planet, limited not by imagination, but the harsh realities of technological limitation.

Thankfully, it’s as if Chainflip just invented wormholes. Say goodbye to EVM physics, and hello to the quantum era of the AMM. Let me teleport you to the mysterious world of the Chainflip State Chain, an application specific blockchain whose sole purpose is to coordinate and operate the Chainflip AMM. It is a much more flexible custom execution environment, purpose built for warping liquidity between worlds.

State Chain Features

The State Chain is a blockchain that the Chainflip Validators run. It has no native wallets, no native tokens, and no end-user features. It does however have a balance tracking system, and all of the AMM logic baked directly into its runtime. Because it’s such a weird blockchain, it’s perhaps not a good idea to rant endlessly about it. All you really need to know is this:

  • State Chain processing occurs as a function of the Validators following the consensus rules
  • The State Chain requires a 2/3rd majority of validators to accept blocks and the transactions within them
  • Transactions pertaining to operations within the AMM are entirely deterministic and event driven
  • Therefore, so long as a supermajority remains online, the operation of the AMM is also deterministic and automatic, conceptually similar to smart contracts in its basic execution assumptions.
  • If a supermajority stops being online or becomes malicious, these assumptions no longer hold true. But - if that happens Chainflip has much bigger problems on its hands than the execution of operations of the state chain. More on that in part 5 of this series!

And that sets the scene for some funky stuff. Strap in. Here comes the Chainflip AMM.

The Chainflip AMM

Broadly speaking, the AMM operation is handled separately to the ‘settlement layer.’

When you set up a trade on Chainflip, you register a Quote on the state chain. That’s an instruction recorded in the AMM which says “Hey, I’ll send some BTC, and when you get it I would like ETH to this address kind sir” - to which the state chain says “Of course sir, it would be my pleasure. Here’s where to send your BTC.” The settlement layer notes down where to expect your funds, and will come back later to send your new coins after the AMM is done processing your trade.

Meanwhile, the AMM is just hanging out, doing it’s thing, until you send your coins and the settlement layer sees your transaction enter and confirm in the vault through a process called “witnessing.”

Once the witness transaction is created and signed off by the validators, the AMM kicks into life. It will now register your BTC as an ‘input’ and has got an instruction to produce an ‘output’ of ETH.

Swap Batching

Now, if this were trying to mimic Uniswap, you might expect the AMM to immediately do two things in quick succession: Swap from BTC to USDC and then use that USDC to immediately swap to ETH. (Probably worth mentioning the base pair for all Chainflip pools is USDC!)

The big problem with this approach is that it ignores blocktimes of external blockchains. USDC runs on Ethereum, which has a blocktime measured in seconds, whereas BTC has a blocktime that may as well be measured in decades.

(come on Bitcoiners, that was funny! please don’t kill me!)

During the operation of the AMM, with the standard approach you would see buys (in USDC) coming in several times a minute, with the price climbing and slippage getting worse with every swap that is completed. Then, all of the sudden, the long wait for a new BTC block is over, and a wall of BTC sells comes in all at once, tanking the price and forcing the sellers to once again incur slippage. This is really inefficient and quite bad for traders on both sides, and as a chart, looks like this:

So instead, the Chainflip AMM batches swaps. As USDC buys come in, they are not executed, but instead registered in a queue. Once the next BTC block comes in, we now have a list of buys and a list of sells. The Chainflip AMM then uses a deterministic sorting algorithm to compute the most efficient method of processing this list of swaps in a single step and average out the trades as much as it can. In a perfectly balanced list, the swaps actually cancel each other out, resulting in literally zero slippage for every trader involved. Of course, this would be rare in practice, but the process of swap batching can have a significant impact on reducing realised slippage where the demand for trades is relatively even aggregating slippage amongst participants, potentially to the point where the slippage becomes undifferentiated from price discovery.

The algorithm for how this is deterministically selected and agreed upon by the validators is something above my pay grade, but is something the development team have had a lot of fun working on so far.

LPs will still receive fees in this process as there needs to be incentive for that liquidity to be there even when the trades are even. Liquidity providers earn fees based on volume, and with this batching structure, less slippage means more volume can be processed for any given amount of deployed liquidity.

Concentrated Liquidity on Steroids

Another significant design feature of Chainflip that makes it quite unique is that it will be the only AMM so far that can be API driven after setup. Combined with the V3 style range orders available on Uniswap, this API cabalility creates a unique environment for professional market makers that make Chainflip unique in the AMM space so far.

First of all, I want to address the question about why we care about professional market makers in the first place. Much of DeFi has been built on the back of token incentives which have attracted a lot of retail LPs to the space who have enjoyed very high yields for simply depositing various assets into different types of DeFi products.

I think it’s helpful to remember that the purpose of these products is to grant utility in exchange for value from the end users themselves. In lending markets, the borrowers pay for the utility. In trading markets, the traders pay for liquidity and exchange fees. In insurance markets, the purchaser of the insurance pays a premium on their risk.

There’s no such thing as a free lunch, and the people on the other side of these transactions: the lenders; liquidity providers; and the underwriters, are there because without them there would be no product, but even so, they too extract value from users, either through fees, or through an invisible tax on token holders through incentives.

Therefore, it’s better for the end user and token holders alike if the counterparties to their interactions are as efficient and competitive as possible so that they extract less value from users and/or token holders. In Chainflip’s case, the involvement of professional market makers would serve the end user well - that end user is the humble crypto trader.

Professional Market Makers are not just Liquidity Providers. They are quantitative trading firms that utilise a complex system of interconnected accounts across multiple orderbooks of the same market to make a profit by ‘taking a clip on the spread.’ Market makers earn their money by aggressively providing liquidity to traders on orderbooks in such a way where they are offering to buy something at $1.01 and simultaneously sell it for $1.02.

That price difference is where the money is made: buy low and sell high (or vice versa) enough times with enough volume and you can reliably produce significant returns. If you do this across multiple orderbooks at the same time, you are far more likely to be able to exploit price inefficiencies between different orderbooks before they even occur, reducing the need for arbitrage and increasing the speed at which traders can have their orders filled across all markets. This is great for everyone as it makes assets far more liquid (“increasing depth”), pricing less volatile (“reducing the spread”), and entering and existing positions can be done very quickly without reliance on slower arbitrage processes.

Impermanent loss is also a problem for professional market makers. It existed long before AMMs, however firms addressed this problem in liquid markets by simply hedging their positions in the derivatives exchanges. If they’re providing 50 BTC of sell liquidity on any given orderbook, they might set up and dynamically adjust multiple opposing positions of a similar size on FTX or Bitmex. This way, if there are significant price movements in the markets, they still have their overall position largely protected from impermanent loss. This strategy isn’t perfect and it only works where a sufficiently large market exists to facilitate hedging, but does enable Market Makers to aggressively use their liquidity at, or very close to, current market prices without risking significant losses when price ratios change dramatically in a given market.

Early AMMs didn’t suit this strategy at all, and Market Makers have struggled to adapt to this environment, instead simply provisioning ‘dumb’ liquidity like everyone else for other yields. Even with Uniswap V3, where range order updates have made it possible to perform actions much closer to their usual activities, there are still a lot of user experience problems on Uniswap that dampen what is possible with traditional market making strategies.

For one, market makers have to send Ethereum transactions in order to update their liquidity. That's hugely problematic as most market making stacks are built to send RPC calls to an Exchange API. Market makers would have to significantly alter their setups to be able to use Uniswap in the way they interact with traditional exchanges, which from the conversations I’ve had with market makers, is often just not worth it.

This is exacerbated by the fact that every update costs gas and takes a relatively long time to execute. With Uniswap V3, you are not only changing your curve when you update your range order, but you are actually also paying for the computation cost of withdrawing all of your assets from the contract and then redepositing them with the new settings. Such is the land of the EVM. It’s expensive, slow, and limiting. Very secure and simple, but not without downsides.

Chainflip is not bound by these rules. Our quoter network can provide market makers with an RPC interface or websocket to update orders by proxy. We can also get much more granular about liquidity range settings, including allowing multiple ranges across a single LP’s balance. We can also allow for secondary keys which are permissioned differently, allowing traders within firms to access “update only” keypairs which they can use to trade with, whilst only the master key can withdraw, for example. Many of the same features you would expect from the centralised exchanges are possible with the Chainflip AMM, and I think that means that as long as we can bring the users to Chainflip, market makers will always be incentivised to get those users an amazing price on their trade.

The Death of MEV

The concept of “Miner Extractable Value” has become a popular discussion in the Ethereum space recently. Because the miners constructing new blocks on Ethereum have control over which transactions to include in a block and in which way they will be ordered, miners can exploit this execution order to create market opportunities for themselves through frontrunning, backrunning, and sandwich trades. In fact, the miners don’t even have to spot these opportunities themselves - many use software which can identify other trader’s frontrunning transactions and essentially overwrite them with a higher gas fee, taking most of the opportunity for themselves entirely risk-free.

This is another pretty wild concept, and arises mostly because of the nature of execution on Ethereum. There isn’t really an easy way around this problem in EVM land. But Chainflip isn’t in EVM land - although we have other new challenges to face, we thankfully get to dodge problems like this.

On Chainflip, every actor knows ahead of time what trades are being placed and when they can be expected to execute. For example, by looking at a list of BTC to USDC quotes, anyone can pull a list of addresses to watch in the BTC mempool and detect when transactions have been submitted. Because those transfers are in the open and will take 3 blocks to confirm, anyone can know that there’s 12 BTC of sells coming in about 10 minutes, another 5 BTC in around 20 minutes, and a further 15 BTC in about 30 minutes.

Perhaps your first reaction to this idea: everyone is going to get frontrun. And you’d be right about that, but not in the way you’d think.

Liquidity providers in Chainflip don’t get paid just by turning up. They get paid when their liquidity gets used, and the most efficient way to get the liquidity used is to constantly update your range orders so that your liquidity is always ahead of the competition on the liquidity curve. If you can set up your orders so that when the swaps are executed, you can or may already have executed against liquidity on other books as close as technically possible, you can safely jump ahead of all other liquidity providers, in theory being able to claim nearly all 0.25% of the liquidity fee per trade almost risk free.

If you know which trades are coming and roughly when, you can prepare your positions on other markets to confidently jump the queue and offer the incoming users the best possible prices at the moment their trades execute. With 2-3 liquid market makers doing this same activity, the users have market makers competing to offer them the best price and the lowest slippage at all times. This is a form of frontrunning - the minimum delta is pre-programmed in the form of the liquidity fee - but it’s a type of frontrunning where the end user wins. With a pretty minimal amount of apparent liquidity compared to other AMMs, Chainflip should be able to offer users exceptional pricing.

Of course, the question stands - can the Chainflip validators manipulate transactions on the state chain to meaningfully benefit themselves?

Because the Chainflip AMM runs deterministically though its consensus rules, validators only have a couple of options to manipulate transaction and execution order. 1/3rd of the Validators working together could delay broadcasting certain actions, like witnessing or certain messages required to progress blocks along, however these all contribute to the validator’s ‘online’ status and thus can result in slashing for those validators if serious and consistent enough - the upside just wouldn't be enough.

Because witnessing is done on a per external block basis, and execution moves forward in deterministic steps, delaying or withholding certain actions within an execution step results in a failed block and the responsibility moves to the next validator, who is randomly selected. This makes it next to impossible to manipulate the order of swaps and thus grants no MEV opportunities.

The only real avenue for frontrunning and other types of MEV attacks would be on the external chains themselves. Chainflip, through it’s swap batching mechanism, deletes any advantage to transaction ordering within each external block. Every block that contains relevant transactions is witnessed as a whole, meaning the deposits within that block are included in the next batch queue at the same time. It also tries to average out and make the trades within a batch queue as fair as possible. This is still being experimented with, but the long and the short of it is that although there’s nothing to stop anyone trying to frontrun other traders, it is very hard to see how there could be any consistent upside potential from these actions.

Of course, we won’t be absolutely certain that there will be absolutely no MEV opportunities in Chainflip until it’s been running for a couple of years - but from where we’re sitting, we can’t see any so far.

Conclusion

With swap batching, advancements in concentrated liquidity provision, and API driven curve updates, Chainflip is not merely replicating AMMs and making them cross-chain. I hope this article gives you an idea of the mental gymnastics that we’re performing, all in the name of creating an innovative product that we hope will have a meaningful and lasting impact on the industry as a whole. Join me next time when we dive deeper into the jungle of Chainflip in Part 4 of the Chainflip Mostly-cryptoeconomics-but-apparently-sometimes-not Series!

Join the discussion today on our Discord and Telegram. Follow us on Twitter. Follow me on Twitter too. Do all of the things and I’ll love you forever <3

Til then,

Simon