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.
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
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:
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:
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.
Flow of actions
Above you can see a diagram roughly showing a trade flow:
approve(addr, tokenAmount)
and later on do the actual transfer.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.
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 APIs allows programs to easily access and trade across multiple sources of liquidity at the same time. This means that for example:
These APIs are optionally implemented by relayers.
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.