The 0x Token - Specs, Terminology and more - DeMystified

in #blockchain6 years ago

1_SLGuroFc79pjKvMznIVWpA.png

0x: deMystified

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:

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.)
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:

A set of smart contracts in order to facilitate the trading between tokens
A set of developer tools with which one can build a relayer and work with the smart contracts.
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

orderflow0x.png

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

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.
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.
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.
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.

oxsmartcontracts.png

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.

Sort:  

Very informative!
Thanks for sharing and steem on :)

On point! "Exchange Smart Contract is in the heart of the architecture and responsible for filling and canceling trades." I love this part. :) Great article.

currently I'm having a hard time convincing myself to invest again on a project after what happened to me,and currently right now I'm trying to invest again slowly and i think most of the projects that i stumbled upon are usually normal or worse,a scam but 0x seems to be different both in fundamentals and it's integrity with it's end users.

Not in my end, I will invest 0x now and what happened yesterday was a plus point after it was listed on kucoin.

Yes it's just listed on kucoin but It will not change their development, their technology is not good enough to be used by their users. So this project is not really worth to invest for me.

Coin Marketplace

STEEM 0.19
TRX 0.12
JST 0.027
BTC 60441.03
ETH 3344.59
USDT 1.00
SBD 2.48