February 20, 2025

On-chain liquidity as an orderbook

Traders are used to orderbooks.

But, most defi liquidity is in AMMs - and AMMs don't look like orderbooks. This alone stops many good traders from trading on-chain.

However, you can programmatically recast all on-chain liquidity into orderbook form and then trade on-chain as if it were an orderbook.

AMMs and Orderbooks are just different ways of arriving at a supply curve

Orderbooks consist of individual limit orders – which aggregate to an overall supply curve.

AMMs are pools of liquidity – which spread their liquidity into a supply curve by a continuous formula.

Orderbooks and AMMs both present strictly increasing trade supply curves.

How to turn AMMs into orderbooks

With limit orders you can draw any arbitrary increasing supply curve.

So if on-chain liquidity is a strictly increasing supply curve – then we can find a set of limit orders that draws it.

By running many simulations – you can accurately draw the incremental supply (price level demands) of an equivalent orderbook.

Since one pool is rarely representative of all on-chain liquidity, you can use a router to find the best price at any depth – and build an aggregate on-chain liquidity book:

The defi orderbook

With this you have interface very similar to an orderbook. For example, you can:

  • View the shape of defi liquidity, and make trade decisions based on it.
  • Check price levels for any given price tick.
  • Query for the price you get at a given depth, or depth you can get at a given price.

Building the on-chain orderbook with Tycho

Tycho makes it easy to create this orderbook: You can simulate trades over any liquidity pool (in micro-seconds per query) – and build up the defi orderbook.

Tycho also informs you which pools changed at the last block – and so you only need to rerun simulations for the pools and routes that changed.

Finally you can turn any market order on this book (at any depth or price) into call data that you can execute on-chain as well.

Build the orderbook and win a bounty

This orderbook interface does not exist yet. But many are waiting for it.

To kick it off we wrote some specs here: https://github.com/propeller-heads/tycho-x/blob/main/TAP-2.md

There's also a 10k USD bounty if you are the first to build it.

Contact Tanay (@tanay_j on tg) if you want go after the bounty.

Appendix

Caveat: Gas cost cause the supply curve to not strictly increase sometimes

Gas costs are a discrete cost that you don’t have in orderbooks (where all cost are pro-rata), and this can bend the supply curve to not be strictly increasing for small portions. Particularly:

  • At very small amounts you get a bad price (net gas). And
  • Whenever you add a pool (or tick) the price first worsens in a jump (because of the additional gas) and then can improve again, before increasing as normal. 

Keep Reading

Tycho: The open-source liquidity indexer

Integrating and simulating DEXs for solving is hard – Tycho makes it easy.
Read More

The next steps in DEX design

DEXs are still behind CEXs volume - but in the next iteration that might be the past.
Read More