DEX on Matic Network with 0x

A possible approach to the integration of Matic Network with the 0x protocol

We are very excited to introduce Matic Network, and you can read more about us in our previous article we published last week, or better, just read our whitepaper.

An important use case for Matic Network will be transactions on decentralized exchanges — that will be enabled in a future release of Matic. Here is how Matic Network will work with the 0x protocol to enable instant, decentralized and low-cost trades.

Decentralized exchanges

Decentralized exchanges are the next important progression from the current centralized system of exchanges. Key reasons are the following:

  • No central party which can be hacked (Mt. Gox, Bitfinex, etc.)
  • No custodian for customer funds
  • No central party which a government could potentially target
  • User controls her own security

In spite of the huge benefits, however, there are usability problems with decentralized exchanges today.

As referenced in this article,

“The primary downsides to many existing decentralized exchanges are that they are illiquid, slow, expensive, and inoperable with one another. For those operating order books on the blockchain, every new order or adjustment to an order goes through the blockchain. This means that they rely entirely on block times and incur network transaction fees for every single transaction.”

Source: A beginner’s guide to 0x

According to the 0x whitepaper, 0x is “an open protocol for decentralized exchange on the Ethereum blockchain.” 0x is creating a protocol using Ethereum smart contracts for anyone in the world to operate a decentralized exchange.

0x tries to solve for these inefficiencies by creating a standard protocol of orders being relayed off of the blockchain and only having them brought back onto the blockchain when the order needs to be settled.

Problems

While the 0x protocol is a fantastic solution to the problems faced by existing decentralized exchanges, it still has to contend with scalability issues faced by the Ethereum blockchain overall. Even though the orders are relayed off-chain, the settlement transaction still needs to be on the Ethereum blockchain, which can take time and/or high transaction fees.

Here’s where Matic Network comes in.

Matic Network

As mentioned in our previous article, Matic Network is a scaling solution for Ethereum, and we support token transfers to start with. It strives to solve the scalability and usability issues while not compromising on decentralization and leveraging the existing Ethereum developer community and ecosystem. Matic Network leverages an adapted version of Plasma to provide scalability and superior user experience to DApps/user functionalities built on top of Ethereum.

Current 0x workflow

Here is the 0x order and trade workflow from their whitepaper for reference:

Figure: Off-chain order relay, on-chain settlement diagram.

The current workflow steps are as follows:

  1. Maker approves the decentralized exchange (DEX) contract to access their balance of Token A.
  2. Maker creates an order to exchange Token A for Token B, specifying the desired exchange rate, expiration time (beyond which the order cannot be filled), and signs the order with their private key.
  3. Maker broadcasts the order over any arbitrary communication medium (most probably, a Relayer — see https://0xproject.com/wiki#List-of-Projects-Using-0x-Protocol)
  4. Taker discovers the order and decides that they would like to fill it.
  5. Taker approves the DEX contract to access their balance of Token B.
  6. Taker submits the makers signed the order to the DEX contract.
  7. The DEX contract authenticates makers signature, verifies that the order has not expired, verifies that the order has not already been filled, then transfers tokens between the two parties at the specified exchange rate.

0x and Matic Network

Let’s see how 0x protocol will work on Matic Network —

Figure: 0x workflow with Matic Network integration

The workflow will be as follows:

  1. Both Maker and Taker deposits funds on the Matic network. Both users can now transfer tokens to anyone they want instantly with negligible fees (see link for more details). The next steps are exactly similar to the current 0x workflow given above — from the Relayer perspective, only the network and contract address details will change. From an end user perspective, there is no change at all. Even Step 1 can be abstracted away for the end user by a smart user interface on the Relayer side.
  2. Maker approves the decentralized exchange (DEX) contract to access their balance of Token A on the Matic chain.
  3. Maker creates an order to exchange Token A for Token B, specifying the desired exchange rate, expiration time (beyond which the order cannot be filled), and signs the order with their private key.
  4. Maker broadcasts the order over any arbitrary communication medium.
  5. Taker discovers the order and decides that they would like to fill it.
  6. Taker approves the DEX contract to access their balance of Token B on the Matic chain.
  7. Taker submits the makers signed the order to the DEX contract.
  8. The DEX contract authenticates makers signature, verifies that the order has not expired, verifies that the order has not already been filled, then transfers tokens via Matic between the two parties at the specified exchange rate.

Steps 2–8 can be repeated for as many trades as needed without any extra steps. All trades on the Matic Network will occur instantly and have significantly low transaction fees.

9. Once a user is ready, they can withdraw remaining tokens to the main chain by establishing proof of remaining tokens on Root contract (contract deployed on Ethereum chain).

Summary

Introducing Matic Network into the workflow, makes sense right out of the box. To borrow an analogy from the “centralized” world, users can deposit their funds, and conduct trades instantly and with lower transaction fees. However, instead of a typical stockbroker, a user has full control of funds, and can withdraw them back as and when needed. We ensure that the workflow is secure, without compromising on the 0x usability vision. Nothing changes from the user perspective — we will explain the UI part in a later post, which will abstract the interaction between 0x and Matic.


Let us know what you think.

We will keep updating this article as needed. Stay tuned.

Stay in touch with us by following us on Twitter. If you have any questions, feedback, feature requests or need help, please contact us on Telegram: https://t.me/maticnetwork