DEX on Matic Network with 0x

By Anurag Arjun

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

More from the Polygon Blog
Data Availability is Not Data Storage

Recall this grade school experience: you raise your hand and ask, “Can I go to the bathroom?” To which your teacher responds with “I don’t know. Can you?” Might seem far fetched, but this is a perfect entry point to understanding the difference between data availability and data storage. Let's bring this analogy close to […]

Read More
The Future is Now for Ethereum Scaling: Introducing Polygon zkEVM

We all know that Ethereum needs to scale, and we at Polygon believe that zero-knowledge (ZK) tech is the most promising pathway to get there. But that path has often seemed as if it would be long and winding. The conventional wisdom has been that the crypto space would need many years to develop Layer […]

Read More
Polygon Reaches First Sustainability Milestone by Achieving Network Carbon Neutrality

Polygon has made a major first step toward becoming carbon negative with the retirement of $400,000 in carbon credits representing 104,794 tonnes of greenhouse gasses, or the entirety of the network’s CO2 debt since inception.  The milestone comes after Polygon in mid-April released its Green Manifesto, part of its broader vision for sustainable development. The […]

Read More
Polygon Avail Launches on Testnet to Turn Monolithic Chains Modular

If we want the entire world to join Web3, blockchains will need to handle more transactions. Monolithic blockchains can’t scale because they’re asked to perform too many tasks (execution, settlement, and data availability) at once. But if chains were able to focus on just one part of the stack at a time, the entire ecosystem […]

Read More
Plonky2 is Now Open-Source

Earlier this year, Polygon announced Plonky2, a zero-knowledge proving system that represents a major breakthrough for ZK tech. Plonky2 offers two main benefits: incredibly fast proofs and extremely efficient recursive proofs. It’s a huge leap forward for the ZK space, and we’ve been blown away by the response from the developer community: people want to […]

Read More
The Complete Beginner’s Guide to NFTs on Polygon

Ever wanted to learn how to buy NFTs, or are you looking for a new platform to do it? Then you might want to consider Polygon.  Learning about NFTs can be a big jungle, but on Polygon, we make it simple to get started through our beginner-friendly ecosystem. Buy NFTs with low gas fees and […]

Read More
Polygon is Now Home to Over 37,000 DApps

More than 37,000 decentralized apps (dApps) have been built on Polygon, according to latest data from Alchemy, the world's leading Web3 development platform. That’s almost double the number in March and a fourfold increase since the start of the year. The number of monthly active teams, the most direct measure of developer activity on the […]

Read More
crossmenuchevron-down-circle