Now that there are hundreds of blockchain based cryptocurrencies, and more being added every month, the need to exchange these assets is compounding. With the emergence of smart contracts, it is possible for two or more parties to exchange blockchain assets without the need for a trusted third party. A decentralized exchange is an important advancement from the ecosystem of centralized exchanges for a few key reasons: decentralized exchanges can provide stronger security guarantees to end users since there is no longer a central party which can be hacked, run away with customer funds or be subjected to government regulations. Hacks of Mt. Gox, Shapeshift and Bitfinex have demonstrated that these types of systemic risks are evident. The decentralized exchange will abolish these risks by allowing users to transact trustlessly – without a middleman – and by placing the burden of security onto individual users rather than onto a single custodian.

0x is an open protocol for decentralized exchange on the Ethereum blockchain. It is delibrated to serve as a basic building block that may be combined with other protocols to drive increasingly sophisticated dApps. 0x uses a publicly available system of smart contracts that can act as shared infrastructure for a variety of dApps. In the long run, open technical standards are bound to win over closed ones, and as more assets are being tokenized on the blockchain each month, we will see more dApps that require the use of these different tokens. As a result, an open standard for exchange is critical to supporting this open economy.

A hybrid implementation, which we refer to as “off-chain order relay with an on-chain settlement,” combines the orderliness of state channels with the near-instant settlement of on-chain order books. In this approach, cryptographically signed orders are broadcasted off of the blockchain; an interested counterparty may inject one or more of these orders into a smart contract to execute trades trustlessly, directly on the blockchain. Friction costs are minimized for market makers because they can signal intent off chain and transactions only occur when value is being transferred. This approach is extended by allowing anyone to act as the exchange and by making the protocol application-agnostic.

When Will Warren and Amir Bandeali co-founded the 0x Project in October of 2016, they did so in hopes of a world that allows for every asset’s representation as part of the Ethereum blockchain. Ideally, the included assets would have everything from digital game items to stocks to gold and fiat currencies. With so much tokenization, there will be thousands of different types of tokens, requiring a trustless exchange for users. While the co-founders appreciate the developments of decentralized exchanges, they saw an opportunity for improvement. With 0x, they hope to address the inefficiencies of decentralized cryptocurrency exchanges as well as the inability of various exchanges to work together.

 

SMART CONTRACT

The exchange protocol is implemented within an Ethereum smart contract that is publicly accessible and free to use (no additional costs are imposed on users beyond standard gas costs). It is written in the Solidity programming language and contains two relatively simple functions: fill and cancel. The entire contract is approximately 100 lines of code and it costs approximately 90k gas to fill an order.

 

Signature Authentication

The exchange smart contract is able to authenticate the order originator’s (Maker’s) signature using the ecrecover function, which takes a hash and a signature of the hash as arguments and returns the public key that produced the signature. If the public key returned by ecrecover is equal to the maker address, the signature is authentic.

address publicKey = ecrecover( hash, signature( hash ) );

if ( publicKey != maker ) throw;

 

Fills & Partial Fills

The exchange smart contract stores a reference to each previously filled order to prevent a single order from being filled multiple times. These references are stored within a mapping; a data structure that, in this case, maps a 32 byte chunk of data to a 256 bit unsigned integer. Passing the parameters associated with an order into the Keccak SHA3 function produces a unique 32 byte hash that may be used to uniquely identify that order (the odds of a hash collision, finding two different orders with an identical hash, are practically zero). Each time an order is filled, the mapping stores the order hash and the cumulative value filled.

A Taker may partially fill an order by specifying an additional argument, valueFill, when calling the exchange smart contract’s fill function. Multiple partial fills may be executed on a single order so long as the sum of the partial fills does not exceed the total value of the order.

 

Expiration Time

An order’s expiration time is specified by the Maker at the time the order is signed. The expiration time is an unsigned integer value that represents the absolute number of seconds since the unix epoch. This value cannot be changed once it has been signed. Time within the Ethereum virtual machine is given by block timestamps that are set each time a new block is mined. Therefore, the expiration status of an order does not depend upon the time at which a Taker broadcasts their intention to fill an order, instead it depends upon the time at which the fill function is being executed in the EVM by a miner. A miner cannot set the block timestamp of the current block to be earlier than the timestamp of the previous block.

 

Canceling Orders

An unfilled and unexpired order may be canceled by the associated Maker via the exchange smart contract’s cancel function. The cancel function maps an order’s hash to the order’s maximum value (valueA), preventing subsequent fills. Canceling an order costs gas and, therefore, the cancel function is only intended to serve as a fallback mechanism. Typically, Makers are expected to avoid on-chain transactions by setting their order expiration times to match the frequency with which they intend to update their orders. One issue with this approach is that it can create situations where a Maker attempts to cancel their order at roughly the same time a Taker is attempting to fill that same order. One of the two parties transactions will fail, wasting gas, depending upon the sequence in which the two transactions are mined. Uncertainty regarding the sequence in which transactions are mined could lead to undesirable outcomes at times. This uncertainty could increase if the Ethereum blockchain were to experience a significant backlog of pending transactions.

 

ZRX TOKENS

Another aspect of the 0x Project is the unique 0x Ethereum token, known as ZRX. This token is how users pay Relayers’ trading fees. It is also a decentralized form of governance for the 0x protocol’s upgrade system. Essentially, those who own ZRX have input in the protocol and improvements to make that is proportional to the amount owned.

There is a fixed supply of one billion ZRX, and the token launch was recent, on August 15, 2017. 50 percent of the tokens were released during the launch with 15 percent retained by 0x, 15 percent going to the developer fund, 10 percent going to the founding team, and 10 percent going to the advisors and early backers. The tokens allocated for founders and advisors as well as staff members will be released over the course of four years, while those bought during the launch event were liquid immediately.

 

CONCLUSION

The 0x seems like a sensible step from decentralized cryptocurrency exchanges, effectively taking care of many of the flaws those exchanges contain. Due to its versatility and the ease of availability of the protocol, it is not far-fetched that 0x will grow quickly, providing the basis for other similar exchange systems using the Ethereum blockchain.

 

manasa on Emailmanasa on Githubmanasa on Linkedinmanasa on Twitter
manasa
Blockchain research analyst at Nvest Labs
Graduated as a Computer Science engineer from VTU in 2017. Currently pursuing my Masters in Software Engineering from University of Visvesvaraya College of Engineering (2017-19 batch). Interning as Blockchain research analyst at Nvest Labs.
WhatsApp chat