paint-brush
0x: deMystified (V2)by@hossein.kazemi
739 reads
739 reads

0x: deMystified (V2)

by Hossein KazemiJune 1st, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Decentralised exchanges and the technology behind them is one of the hottest topics in the <a href="https://hackernoon.com/tagged/blockchain" target="_blank">blockchain</a> and <a href="https://hackernoon.com/tagged/cryptosphere" target="_blank">cryptosphere</a> of which 0x is the most talked about.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coins Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - 0x: deMystified (V2)
Hossein Kazemi HackerNoon profile picture

Decentralised exchanges and the technology behind them is one of the hottest topics in the blockchain and cryptosphere of which 0x is the most talked about.

The acquisition of Paradex by Coinbase is a great sign that decentralised exchanges and in particular 0x are getting more and more important.

Note: This article is based on the soon-to-be-released V2 of the 0x protocol.

Terminology

Knowing the following will help you better understand this article:

Relayer: In 0x terminology, an exchange is called a relayer. Relayers have some big differences with exchanges that I will be describing further in this article.

ERC-721: Is a somewhat new token standard on the Ethereum blockchain. In ERC-721, each token is completely unique and non-interchangeable with other tokens. They are called Non-Fungible Tokens (NFT).

Maker: A user that creates an order

Taker: A user that browses the orders and executes and order

What is 0x?

0x is an open protocol for trading standard tokens (ERC-20, ERC-721, etc.) on the Ethereum blockchain. It is designed to be used in two main ways:

  1. Point-to-Point exchange of tokens: Allowing two parties to directly exchange tokens between each other using just about any communication medium they prefer (like Telegram, Facebook, etc.)
  2. Building a decentralized exchange (relayer): Allowing anyone to build and operate an exchange with Ethereum based standard tokens. In this article I will be focusing on the this use-case.

In 0x protocol, orders (on a standard format) are transported off-chain over any arbitrary medium, massively reducing gas costs and reducing blockchain bloat. Relayers help broadcast orders and collect a fee each time they facilitate a trade. Anyone can build a relayer.

To achieve this, 0x provides:

  1. A set of smart contracts in order to facilitate the trading between tokens
  2. A set of developer tools with which one can build a relayer and work with the smart contracts.
  3. A messaging standard for orders**.**

The simplest example of a Relayer is a web application allowing users to create, discover and fill orders. The Relayer must build out a UI and host a backend database to provide this functionality.

Order flow in 0x

Flow of actions

Above you can see a diagram roughly showing a trade flow:

  1. Before anything happens, Maker should give permission (allowance) to 0x Smart Contract to withdraw fund. In Ethereum it is possible to call approve(addr, tokenAmount) and later on do the actual transfer.
  2. Then Maker creates an order on the relayer. This order has a specific format and contains quite a few fields which need to be filled in order for a trade to take place. You can check 0x’s documentation for a complete list. After the order is created, the relayer publishes this order in the order book which is hosting.
  3. A Taker sees the order and decides to fill the order. The relayer then fills in the necessary fields in the order and uses the 0x’s APIs to execute the order.
  4. Through 0x’s APIs, the orders is passed to the set of smart contracts and the funds are moved within the Maker’s and Taker’s wallet.

0x Smart Contracts

Now let’s dive deeper in step 4 of above flow and see what 0x’s Smart Contracts are and what they do. As mentioned before, we describe the soon to be release V2 of the protocol here:

0x Smart Contracts Architechture

0x smart contracts have a modular architecture which allow adding and supporting new token standards without having to modify the smart contracts and force developers and users to upgrade.

Exchange Smart Contract is in the heart of the architecture and responsible for filling and canceling trades. There is not much information released regarding the AssetProxy Dispatcher part of the smart contract, but it seems to be simply a router that routes the flow to the corresponding asset proxy smart contract.

Proxy Smart Contracts are responsible to transfer tokens in a trade. Note that in this architecture each type of token has its own proxy contract. 0x V2 will be released first with ERC-20 and ERC-721 support. Imagine that soon you will be able to trade your CryptoKitties with CryptoBots! Checkout Rarebits and OpenSea.

Remember that in step 1 we mentioned that users will give permission (allowance) to 0x Smart Contract so they can transfer tokens? Proxy smart contracts are the ones which receive this permission.

Wrapped ETH (WETH)

The whole code for 0x Smart Contracts is about trading standard tokens on Ethereum. But what if you want to trade ETH with another ERC-20 token. ERC-20 token standard was released after ETH so ETH is not really an ERC-20 compliant token therefore can’t be used directly in 0x’s Smart Contracts.

To solve this problem, ETH is “wrapped”. If you happen to try to trade on a relayer, first you need to swap your ETH with WETH. This will cost you gas of course.

As wrapping ETH is an extra step, 0x team have developed a Smart Contract called Forwarding. With the Forwarding contract, users can simply send ETH and the orders they want to fill, and the forwarding contract will wrap the ETH and fill the orders in one single transaction, eliminating the need for WETH for takers.

Forwarding Contract for WETH

Standard Relayer API

Standard Relayer APIs allows programs to easily access and trade across multiple sources of liquidity at the same time. This means that for example:

  • Relayers can pull orders from the order books of other relayers in order to bootstrap liquidity on their own platform and attract traders.
  • Traders can easily make markets and trade across multiple relayers at the same time.
  • As new relayers enter the market, traders can immediately interact with them without changing their existing software or tools.
  • If one relayer shuts down, dApps that rely on liquidity can easily talk to a different relayer or set of relayers so that operations run uninterrupted.
  • Applications can reliably pull data from multiple relayers.

These APIs are optionally implemented by relayers.

0x Connect

0x Connect is a Javascript library that makes it easy to interact with relayers that conform to the Standard Relayer API. Functionality includes getting supported token pairs from a relayer, getting orders filtered by different attributes, getting individual orders specified by order hash, getting orderbooks for specific token pairs, getting fee information, and submitting orders.

Overall with the release of V2 of the 0x protocol we should see a wider adoption of this protocol within the cryptosphere in the coming months.

In the next articles I will be going through another decentralised exchange. This time a NEP-5 based exchange called Switcheo following cross chain networks.

Follow me here on Medium or on twitter: https://twitter.com/hossein761 to stay up to date.

DISCLAIMER: The sole purpose of this article is for education. What is mentioned in the above article is no investing advice and no trading advice. If you trade based on the findings of the above article you are the one responsible for any loss or damage.