0x Smart Order Routing

An overview of 0x's Smart Order Routing.

May 19, 2020

Under the Hood

More liquidity means better pricing for the end-user. With Swap API, we prioritize liquidity to maximize returns on quotes, even if it means having to venture outside of the 0x ecosystem. With the advent of the 0x Protocol’s ERC-20 bridge asset proxies, we now have the ability to fill against other on-chain decentralized exchanges (DEXes) through the 0x Exchange contract. Requesting a quote from Swap API will not only scan limit orders from the protocol’s networked liquidity, but will also check on-chain DEXes such as Uniswap, Curve, Oasis, and Kyber. The API’s order routing algorithm will then use this information to split your fills up across the different sources to maximize the overall return on your swap.

The Problem

There’s a lot of diversity among existing DEX models, so liquidity can look very different between them. Some exchanges offer better rates but at high slippage while others offer worse rates for lower slippage. Below are real-world quotes taken from 2 DEXes to illustrate this common scenario.

A sampling of increasing ETH->DAI quotes from Oasis and Kyber.

What’s more, these markets continue to evolve with trading activity, making it difficult for a typical user to know where to get the best rate at any given moment. So simply providing the overall best source for a swap would be valuable, but Swap API goes a step further by also splitting your swap across different DEXes.

Larger area under curve = better output.

Because slippage often increases non-linearly with fill size, this approach allows us to keep the overall rate of a swap near optimum by incurring less slippage. The generated quote can then be passed into the 0x Exchange contract, which will perform all the trades in a single, atomic transaction.

The Process

Echoing the principles of the 0x Protocol, we rely on a mix of on-chain and off-chain components when generating swap quotes. Quotes are generated off-chain (via Swap API), to be consumed on-chain. In a high level overview of this process, we have three distinct phases: Sampling, Optimization, and Settlement.

Sampling

We start by pulling relevant native orders from the 0x Mesh network and our exclusive market maker partners. We then use a custom contract to collect many quotes from each DEX for increasing fill sizes, up to the taker’s desired token amount. Critically, this contract also allows us to batch operations into a single RPC call. This keeps the API highly responsive.

Pooling all liquidity sources into an offline model.

Optimization

Both native orders and DEX samples are then converted to “fill paths,” which are just directed acyclic graphs where each node represents a small fill on a DEX. The optimal solution is the (valid) combination of these fills that results in the greatest output token amount for a given input token amount. Swap API will intelligently merge each successive path with the next until we end up with a single, unified path with the highest overall return rate, accounting for fees/gas.

Merging quotes from different sources together to maximize the area under the curve.

Settlement

The final optimized path is then converted into individual 0x orders. DEX fills are wrapped in “bridge orders,” which are orders we generate just-in-time to instruct the 0x Exchange contract to perform a trade against a DEX during settlement. Swap API encodes the entire call to the Exchange contract (which includes the orders to fill) as a hex blob which the taker can simply sign and submit to the blockchain. The Exchange contract then will fill each order sequentially until the required number of tokens have been bought or sold. During this process, gas tokens will also be burned to reduce the final transaction cost to the taker.

Joe Taker executing an on-chain swap for tokens A -> B, crafted by 0x-API.

What’s Next

We’re continuously working on ways to improve the performance of the quote service to provide the best pricing for users. Here are a few, related things on our immediate roadmap.

  • Last-minute, on-chain routing to adjust, and potentially improve, quotes for market movements that can occur between submission and when the transaction has actually been mined.
  • A new, custom settlement contract, tailored specifically for efficiently filling these quotes. This should lead to more accurate fills and ultimately reduce fees.
  • Composable transformations on tokens. This would, for example, allow a user to perform a swap from cUSDC → ETH, and vice versa, where cUSDC is automatically wrapped/unwrapped before the trade.
  • Expanded RFQ program, which gives takers direct access to market makers so they can provide better-priced orders prior to filling them.
  • Meta-transaction support, to remove or defer the requirement that the taker must pay for gas with ETH.

Ready to start building?
Create a 0x account and get started, for free, in less than five minutes.

Contents

Subscribe to newsletter

By submitting you're confirming that you agree with our Terms and Conditions.
Yay! You’re signed up.
Oops! Something went wrong, but it's not your fault.

Up next

Fundamentals: Smart Contract Wallets

Oct 15, 2024

Compliance made easy with 0x Address Screening

Sep 26, 2024

0x's next-gen pricing engine is now live

Sep 25, 2024

Introducing state-of-the-art Buy/Sell Tax support

Sep 4, 2024

Take control of your balance sheet with 0x v2

Aug 15, 2024

0x v2 bug bounty program

Jul 30, 2024

What does the "best price" in DeFi really mean?

Jul 23, 2024

Eliminate allowance risk with Permit2

Jul 17, 2024

Introducing 0x's next-gen pricing engine

Jul 15, 2024

0x Dev Digest: May 2024

May 31, 2024

Frame spotlight: Paycaster

May 21, 2024

Frame spotlight: Airstack

May 9, 2024

Power up your Farcaster Frames with 0x swaps

May 2, 2024

0x Dev Digest: April 2024

Apr 30, 2024

Introducing 0x Trade Analytics

Mar 12, 2024

Coinbase Case Study

Jan 30, 2024

Introducing gasless swaps and approvals with Gasless API

Jan 22, 2024

Bitcoin ETFs have arrived

Jan 12, 2024

Building in the open: 0x pricing update

Dec 11, 2023

Matcha leverages Gasless API to bring users the most frictionless trading experience in DeFi

Nov 30, 2023

0x Dev Digest: October 2023

Nov 2, 2023

Monetize crypto trading in your app with Swap API

Oct 26, 2023

0x Dev Digest: September 2023

Oct 3, 2023

A comprehensive analysis of RFQ performance

Sep 26, 2023

Unlock optimal trades in Swap API with 0x RFQ liquidity

Sep 20, 2023

0x Dev Digest: August 2023

Aug 31, 2023

Portal launches swaps in its white label MPC wallet powered by 0x

Aug 16, 2023

0x Swap API is now live on Base

Aug 9, 2023

Introducing paid plans for Swap API

Jul 24, 2023

Decreasing Frictions in DeFi hackathon recap

Jul 12, 2023

0x's pricing principles

Jul 3, 2023

0x Dev Digest: June 2023

Jun 30, 2023

App spotlight: tastycrypto

Jun 27, 2023

App spotlight: 31Third

Jun 22, 2023

0x 101: Intro to gasless API

Jun 13, 2023

Inspiration for building with Swap API

Jun 8, 2023

0x Dev Digest: May 2023

May 31, 2023

Fundamentals: What are gas fees?

May 25, 2023

Decreasing Frictions in DeFi Hackathon

May 12, 2023

Swap API liquidity management

May 18, 2023

0x 101: Getting started with Swap API

May 16, 2023

0x 101: Intro to Swap API

May 9, 2023

0x 101: Intro to 0x Orders

May 4, 2023

0x 101: Intro to 0x Protocol

Apr 27, 2023

A new home for 0x Protocol

Apr 24, 2023

Say hi to the new 0x

Apr 20, 2023

0x Year in Review 2022

Jan 5, 2023

0x Swap API expands to Arbitrum

Sep 22, 2022

Managed liquidity

Apr 23, 2020

App spotlight: Taho

Mar 8, 2022

Fundamentals: What is a Layer 2 chain?

Apr 19, 2023

Fundamentals: What is the difference between quoted, executed, and adjusted prices?

Apr 19, 2023

Fundamentals: What is price impact?

Apr 19, 2023

Fundamentals: What is slippage?

Apr 19, 2023

Fundamentals: What is an automated market maker (AMM)?

Apr 19, 2023

Fundamentals: What is market making?

Apr 19, 2023

Fundamentals: What is a DEX aggregator?

Apr 19, 2023

Fundamentals: What is liquidity?

Apr 19, 2023

Fundamentals: What is a decentralized exchange (DEX)?

Apr 19, 2023

0x at ETHDenver 2023

Mar 21, 2023

The 0x Mission and Values

Jun 7, 2018

Announcing support for new testnets

Aug 10, 2022

0x Swap API expands to Binance Smart Chain

Mar 17, 2021

Scaling DeFi — Layer One

Sep 1, 2021

Introducing Slippage Protection

Jul 14, 2022

San Francisco Blockchain Week 2022 Recap

Nov 21, 2022

0x Swap API adds new liquidity sources

Jul 11, 2022

Review of slippage performance

Sep 14, 2022

Phuture case study

Oct 20, 2022

Measuring the impact of hidden DEX costs

Apr 14, 2022

Market making in DeFi

Aug 12, 2021

Introducing 0x Labs

Jun 22, 2020

Introducing 0x Explorer

Oct 26, 2022

Growing DeFi with professional market makers

Aug 26, 2020

0x 101: How to Access 0x Data

Apr 25, 2023

GameStop chooses 0x Swap API

Jul 8, 2022

Build on Base with 0x

Feb 23, 2023

A comprehensive analysis on DEX liquidity aggregators’ performance

Oct 1, 2020

Announcing 0x Swap API v1

Oct 1, 2020

Access all DEX liquidity through 0x Swap API

Jan 28, 2020

Announcing the 0xpo Summit 2022

Aug 24, 2022

0x Protocol, a preview of what’s to come

Mar 16, 2023

Update to our Privacy Policy

Mar 14, 2023

Price Impact Protection has arrived

Dec 14, 2022

0x Limit Orders Go Multi-Chain

Dec 23, 2021

0x Labs raises $70M Series B led by Greylock to continue expanding Web3’s core exchange infrastructure

Apr 26, 2022

0x launches Tx Relay API in beta, with Robinhood Wallet as first partner

Mar 1, 2023

0x Labs raises $15M Series A to bring decentralized exchange markets to a global audience

Feb 5, 2021

App spotlight: Zerion

Jun 22, 2022

App spotlight: Matcha

Nov 24, 2021

App spotlight: DexGuru

Jun 17, 2021

App spotlight: DEXTools

Sep 23, 2021

App spotlight: DODO

Aug 10, 2021

App spotlight: DappRadar

Dec 16, 2021

App spotlight: DeFi Saver

Jul 20, 2021

0x + Brave partner to make crypto and DeFi more accessible to everyone

Jul 7, 2021

0x Swap API now supports 0x Protocol v4

Mar 1, 2021

0x Swap API is now available on Fantom

Oct 26, 2021

0x Swap API is now live on Optimism

Jan 11, 2022

0x Swap API is now available on Polygon

May 31, 2021