Image for post
Image for post

Off-chain trading with Augur and 0x

A guide to Veil’s order book

Graham Kaemmer
Feb 6, 2019 · 7 min read

This post is a technical rundown of some of the mechanics that allow Veil to work. It’s intended for readers who understand some Ethereum concepts like ERC20 and transactions, have a rough idea what 0x and Augur are, and are curious to know more about what Veil is doing behind the scenes. We’ll also assume that you’ve read Paul’s Guide to Augur Market Economics, particularly the sections about outcome shares and complete sets.

We’ll cover the following:

  • The unique trading mechanics of prediction markets on Augur
  • Why Veil has “two” orders books, and why they are mirror images of each other
  • How Veil uses 0x orders to let people trade positions in Augur markets

Trading in Augur Markets

Augur is a decentralized prediction market protocol. It’s a set of smart contracts that let Ethereum users create, trade in, report the outcomes of, and claim their winnings in prediction markets. For a longer introduction to Augur, check out the Guide to Augur Market Economics. I will focus here on Augur markets with two outcomes (LONG and SHORT)—it will simplify some of the trading logic, and Veil currently only supports such markets.

As a refresher, here’s how an Augur market works at its most basic level:

  1. A market is created with two outcome ERC20 tokens.
  2. Anybody can use ETH to buy a complete set (1 LONG share and 1 SHORT share), split the set into distinct shares, and trade the shares with other users.
  3. When the market expires, the reporters assign a value to shares of each token (such that the value of a complete set remains 1 ETH).

While a market is open, the value of its tokens are determined by traders who make and fill orders. The prices are generally based on the tokens’ expected values at the market’s expiration. Because shares will never be redeemable for more than 1 ETH each, the market prices of the two tokens are always somewhere between 0 and 1.

Let’s take an example: Jill is willing to pay 0.6 ETH for a LONG share. That means that she thinks the LONG share will be worth more in the future, either because it will be redeemable for more than 0.6 at the end of the market, or because someone else will want to buy it later for 0.7 or 0.8 ETH. If another trader, Jack, is willing to sell a LONG share for 0.6 ETH, then Jill and Jack’s orders can match. Great.

In this situation, buying a LONG share for ETH works just like buying most tokens on most exchanges: Jill wants to buy a token at 0.6 ETH, Jack wants to sell that token at 0.6 ETH, so a trade can be executed between them.

However, let’s consider another example where Jack doesn’t actually own any shares, but he does want to buy a SHORT share for 0.4 ETH. In this case, Jill and Jack can still trade, but it takes an extra step. A third party can step in and spend 1 ETH to buy a complete set — 1 LONG share and 1 SHORT share. Then, it can sell the LONG share to Jill for 0.6 and the SHORT share to Jack for 0.4, totaling the 1 ETH that was spent on the complete set.

With the third party buying complete sets, Jill’s order for LONG at 0.6 matches Jack’s order for SHORT at 0.4. In fact, all buys for LONG tokens at price X match with buys for SHORT tokens at price 1-X.

With a little bit more mental energy, we can also conclude that sell orders for LONGs and SHORTs match the same way. Imagine that Jill is willing to sell a LONG token for 0.6 and Jack is willing to sell a SHORT token for 0.4. Now the third party can take 1 ETH, fill both of their orders, and then sell the complete set back to Augur to get back the 1 ETH.

This leads us to the odd but important property of prediction markets with two outcomes: assuming a third party is willing to buy and sell complete sets on traders’ behalf, the order books for LONG and SHORT shares are mirror images of each other. Or in other words, there is only one order book and two ways to look at it. A LONG buy at 0.6 can match either a LONG sell at 0.6 or a SHORT buy at 0.4. A SHORT sell at 0.2 can match either a SHORT buy at 0.2 or a LONG sell at 0.8. Creating an order in one order book always creates an opposite order in the other.

Image for post
Image for post
The order books for LONG and SHORT tokens are the same, but mirror images

Augur’s built-in, on-chain order matching system provides this property of a single order book. The Augur trading contract acts as the “third party” in the example above. Any time somebody creates a buy order for a LONG token at price X on Augur, it means somebody can immediately buy a SHORT token at price 1-X.

However, to use the Augur order book you need to submit an Ethereum transaction every time you want to create or cancel an order. This costs gas and time, and reduces the appeal both to market makers (who like creating and canceling lots of orders) and small-time traders (who don’t want to pay a large fixed gas fee with every trade). Veil set out to solve this problem using 0x.

Off-chain orders with 0x

At its core, 0x is a smart contract that executes orders. There’s actually quite a bit of complexity involved, but at a high level it plays a simple role: if two people agree off-chain that they want to exchange some shares of one token for some shares of another token, they can submit a signed order to 0x, which will move both shares in one atomic transaction. Orders are created and signed by a “maker”, and then later executed on-chain by a “taker”.

Augur’s built-in order book doesn’t accept off-chain 0x orders, so Veil itself must be the “third party” that buys and sells complete sets and executes trades. To act as the third party, Veil is the “taker” on all Veil orders. If we let users take each others’ trades directly, we’d lose the ability to match LONG orders with SHORT orders — we’d end up with two order books instead of one for each market. We’d also require that users actually go to Augur to purchase complete sets and that they pay the gas to send transactions to 0x, both of which would make it harder to start trading.

Let’s say Jill and Jack are using Veil. Once again, Jill wants to buy a LONG share for 0.6 ETH. When Jill signs a Veil buy order for 1 LONG share at 0.6 ETH/share, it doesn’t immediately match any other orders. It’s placed on Veil’s order book both as a LONG buy at 0.6 and a SHORT sell at 0.4.

Jack comes along looking to take a SHORT position. He sees that he can get 1 SHORT share for 0.4 ETH, so he signs another Veil buy order for that amount.

Immediately upon the creation of Jack’s order, the two orders match and are removed from the order book (in fact, Jack’s order never appears in the order book at all). The Veil matching engine now does two things: it buys 1 complete set from Augur for 1 ETH, and then it executes both orders by submitting a transaction to 0x. When the transaction finishes, Jill has 1 LONG token and Jack has 1 SHORT token, and Veil’s ETH balance is unaffected. From Jill’s and Jack’s perspectives, it appears that somebody sold them a token, even though both traders only created buy orders.

In this scenario, we can think of Jack as the taker because he takes liquidity from the order book. Even though Veil is listed as the “taker” on both 0x orders, Jill’s order is never executed until Jack’s order matches. It’s also worth pointing out that Veil always executes matching orders in a batch (using 0x’s batchFillOrKill method) to ensure that they execute atomically.

Conclusion

To reiterate:

  1. Augur lets you buy and sell complete sets of shares for ETH.
  2. By buying and selling complete sets on traders’ behalf, Veil can share orders for LONG and SHORT tokens, resulting in a single order book for both.
  3. Veil, not traders, executes trades on-chain, so Veil pays all the gas fees.

If you’re trading a lot, or if you’re market making, it’s important to understand that Veil markets have only one order book, despite having two tokens. For example, a market maker can add liquidity by creating either buy orders or sell orders. This is important because you only need ETH to start market making — just create buy orders for both LONG and SHORT. If you take a big position on one side, you can offer to sell those shares instead of buying more.

We’ll write a more in-depth guide to market making on Veil in the future. In the meantime, check out our sample tradebot that is capable of (very naively) market making on Veil scalar markets. It adds liquidity by creating buy orders on both sides, and only requires Veil Ether to get started.

We hope this post gives you a better idea of how the Veil order book works, and why it’s different from other decentralized exchanges. If you still have any questions don’t hesitate to reach out to us on Twitter or Discord.

Veil

Markets on everything, by everyone.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store