0x 101: Intro to 0x Orders
Take a look under the hood to see how a transaction is executed through 0x Protocol.
0x Protocol is the trusted open source protocol for the settlement of decentralized exchange of tokenized value. It is the foundational settlement layer of the 0x infrastructure tech stack and a core Web3 building block. 0x Protocol reliably executes orders created by off-chain services such as 0x’s Swap API in a non-custodial manner.
In this article, we take a look under the hood to see how a transaction is executed through 0x Protocol.
Makers, Takers, and 0x Orders
For any given trade, there are two actors participating in the transaction: the “Maker”, short for market maker, and the “Taker”, the actor who wants to take the order the Maker is making. A trade between these two parties happens via an order.
For a transaction to be executed through 0x Protocol, a 0x order is required. A 0x order is a message passed through 0x Protocol to facilitate the trade of an ERC20 token for an ERC20, ERC721, or ERC1155 token.
What exactly is an order? An order is a packet of data that contains the order’s details, such as what assets are being traded, the Maker’s and Taker’s addresses, and at least one valid signature. When two entities agree on the terms of a trade, the 0x order is submitted to the respective blockchain and settled via 0x Protocol smart contracts.
The 0x Protocol supports different order types, including Limit Orders, whose structure is shown below:
0x order types
There are currently three types of orders in 0x Protocol:
- Limit Orders: These are standard 0x orders, which encode a possible trade between a Maker and Taker at a fixed price, and are primarily used for off-chain orderbook liquidity. Limit Orders facilitate ERC20 to ERC20 trades. (Note: A limit order in this context is not the same as a limit order in traditional finance)
- RFQ Orders: These are a stripped down version of standard Limit Orders that support the settlement of off-chain liquidity sourced from professional market makers via the 0x Request-for-Quote (RFQ) System. These orders are fielded just-in-time, directly from market makers. RFQ orders facilitate ERC20 to ERC20 trades.
- NFT Orders: NFT orders in 0x Protocol are optimized for swapping ERC20 tokens for either ERC721 or ERC1155 tokens.
How does a 0x trade work?
Let’s take a look at the illustration below to see how a 0x order works. This flow applies to ERC20 to ERC20 trades and ERC20 to NFT trades.
- A Maker creates a 0x order, which is a json object that adheres to a standard order message format. This order indicates what kind of asset the Maker is looking to trade, such as ERC20, 721, or 1155.
- The order is hashed and the Maker signs the order to cryptographically commit to the order they authored.
- 0x Swap API aggregates liquidity across all available sources to surface the best price to the Taker.
- The Taker fills the 0x order by submitting the order and the amount they will fill it for to the blockchain.
- 0x Protocol smart contracts contain settlement logic to verify the Maker’s digital signature and that all the conditions of the trade are satisfied. If so, the given assets are atomically swapped between Maker and Taker. If not, the trade is reverted.
Unlike other decentralized exchanges that function entirely on-chain, 0x does not store orders on the blockchain; instead, orders are stored off-chain and only trade settlement occurs on-chain. This unique feature of off-chain relay, on-chain settlement makes 0x highly flexible and gas-efficient because only settlement information needs to be stored on-chain.
Get started building with 0x
In this article, we discussed the structure of 0x Orders, how they encode trade details between a Maker and Taker, and how orders are shared and swapped via the “off-chain relay, on-chain settlement” paradigm.
0x offers a suite of fully integrated APIs to help you easily tap into this trading liquidity. Check out our guide below to learn how to get started building with Swap API: