How DeFi Exchanges Work

Trading is complicated enough when you’re just punching numbers into a black box: Understanding exchange mechanics and the differences between traditional finance, centralized crypto exchanges, and decentralized finance makes it even more complicated.

One of the core value propositions of Krypton is an advancement on order matching in DeFi–but to really get what that means, it’s useful to have an understanding of how orders are matched in the first place.

Here’s a little history on trading mechanics in both traditional finance and crypto, and why we believe that what we’re doing is going to help DeFi so much.

How TradFi exchanges work

In traditional finance, your typical exchange functions as a Central Limit Order Book (CLOB), which collates buy and sell orders. In a CLOB, when a new order is entered, the system checks if it can be matched against an existing order. If it can, it’s executed; if it cannot, it’s entered into the database until it can be offset. These are all limit orders, which means you note that you want to buy or sell a security at a certain price or better.

So, for example, if you want to buy shares of the hot new shipping company Odysseus at $100, you could put in a limit order that tells the market you’ll pay $100 or better (which, in this case, means $100 or less). Say there’s a resting order to sell Odysseus at $99–when your order hits the CLOB it will be executed at $99.

This is a clear win for you, unlike the naming decision behind your new investment.

Classic meme, source unknown.


In the crypto world, the major centralized exchanges function as CLOBs. So, if you want to buy the Odysseus token this time, you’ll enter a buy order, and your CeFi exchange will match that against resting orders or enter it into the database to be matched in the future. Same idea.

Now this next part will become important in a moment:

CeFi crypto is sort of a compromise. It offers traders access to cryptographic coins and tokens that exist on decentralized blockchains while being under the custody of the exchange (rather than the trader) which mirrors/resembles the structure in TradFi and facilitates regulatory oversight.¹

What goes on behind the scenes in the CeFi world is that the centralized exchange (CEX) requires each user to open an account with them. In other words, the centralized exchange maintains custody of your tokens. It’s a lot like a regular brokerage account in traditional finance. So, when you make a trade, the centralized exchange either credits or debits the account you’ve opened by the appropriate amount traded.

What Happens in DeFi

One major question in DeFi is how to allow people to trade directly with one another’s wallets without the need for a centralized, trusted intermediary.

“This is why banks have such nice buildings, right? It’s not because they want to spend money on buildings; it’s to create confidence in them as an entity in order for people to transact through them.” — Sergey Nazarov²

This is actually a really complicated problem to solve. In fact, it hits at one of the main limitations of a CLOB, which requires a buyer for every seller and vice versa. If no one happens to be buying when you’re selling, you can’t execute the transaction. Ideally, there is also a market maker in the middle to bridge gaps in order flow and manage liquidity .

These two challenges were particularly thorny before scaling solutions, and ultimately gave rise to AMMs.

Instead of matching your order to another individual order, AMMs match your order to a liquidity pool, which in practice means that there’s always someone to trade with. Your trade is carried out by a computer program, which runs a simple formula to determine pricing. This program is written as a Smart Contract on top of a blockchain like Ethereum, so if it’s open-sourced, the program is trustworthy in the sense that it’s predictable and verifiable.

There are a few key advantages of this system.

  • You don’t need to match individual orders, which means you don’t need to wait for a counter-party in a thinly-traded market.
  • Because there is no requirement for market makers, your operational requirements are simplified.
  • Finally, the computational cost is low. Particularly in the early days of DeFi, anything other than simple computation would be too costly to be feasible.

So What’s the Problem?

Because of their structure, price discovery on AMMs only occurs if a trade occurs. The AMM works on behalf of the liquidity pool, but it’s very simplistic and cannot process outside price information in the way a traditional market maker would.

The impact is that AMMs can be easily taken advantage of by informed traders — and it’s not difficult to become an informed trader because true market prices can be gleaned from other exchanges, namely CeFi order books.

Some more detail on a few of the most pronounced costs that result from this:

AMMs are susceptible to “Sandwich Attacks,” a form of Miner Extractable Value (MEV).

Since the outcome of a trade with an AMM is entirely predictable according to the AMM’s formula, any third party can manipulate the token trading ratio (e.g. token price). Given the knowledge of a new incoming trade like a buy order at a new price, any front runner simply has to issue a new buy order in front of this new price, pushing the price the incoming trade has to pay up. The original trade executes, pushing the price even higher. Then the front-runner can execute a sell order immediately after at this even-higher price, collecting the difference immediately.

This is exacerbated in a world of lower gas fees.

With advent of scaling solutions, gas fees will continue to drop, which it makes it profitable to front-run or sandwich ever smaller trades. Recent academic research shows significant untapped potential for predatory traders to exploit these opportunities. Together, this implies that the problem of predatory trading is bound to get worse.

With the rise of Flashbots and its competitors, it’s very easy to take advantage of this.

Flashbots Auction is a private communication channel between miners and searchers which allows those searchers to directly bid on their desired transaction order. Flashbots maximize miner payoffs and allows for price discovery on the value of a given MEV “opportunity” (take a look at how much it has cost markets here).

That the simplicity of AMMs is exploited by miners is a disadvantage of the mechanism, not an issue of miners or MEV. That simplicity also gives rise to other costs and market inefficiencies, which we’ll cover in future articles.

What’s next?

AMMs were an awesome way to introduce the world to DeFi. Unfortunately, problems like these make it difficult for DeFi to scale with the use of AMMs alone. This is why we created Krypton — a novel approach to the classic order book that protects traders from these shenanigans and helps everyone achieve best trade execution.

What do you think about this article? Did you know about the different ways that markets match orders, and did any of this surprise you?

[1] Obviously, whether all this is right or true or good or bad depends on your point of view. We are partial to DeFi, as we are a DEX, but we think anyone in the DeFi space can learn a lot from the things people like CeFi. The reasons we’ve heard generally include security, regulatory oversight, and usability.

[2] From an interview with Lex Fridman

Krypton is dedicated to the idea that functional markets are fair markets, where you can get the trade you want at a cost that makes sense. Our academic roots are the foundation of our novel approach to solving the challenges of building a decentralized financial system, and we’re proud to offer a truly novel approach to exchange architecture with the Krypton DEX.