DAPPCON 2019: Workshop - Getting Hands-on with 0x Mesh w/ Fabio Berger (0x)
DappCon 2019 · August 2019 · 36:41
Mesh addresses the engineering burden of sharing off-chain 0x orders between relayers through a gossip-style peer-to-peer network that revalidates orders and removes those no longer fillable. The workshop demonstrates a Docker node, JSON-RPC statistics, order-event streaming, submission and external-database syncing. Browser support still required bootstrap work at the time; persistence depended on someone pinning orders, and nodes could hold different order-book views because Mesh avoided consensus.
Speakers
Topics
From DappCon 2019, hosted by Dappcon Berlin.
Transcript
my name is Fabio I'm part of the Xerox team and today we're gonna be doing a workshop on one of our most exciting projects at the moment called Xerox mesh it's a peer-to-peer network for sharing Dex orders just a little bit of an overview of like the presentation and then the workshop we're gonna start with just a little bit of background on Xerox and also explain why we're building mesh what problem does it solve then we'll go into mesh what is it how does it work go into some use cases for mesh the tech stack that we're building on top of talk about one of the features that we're really excited about which is browser support so actually having a node the
node run in a browser tab and then we'll go and actually together spin up a mesh node connect to it connect to an order stream fetch orders from the network post in order to the network and actually use it cool so just a little bit of background on 0 X 0 X is a decentralized exchange protocol what we mean by protocol is that anyone who's building an exchange on top of 0x all they need to do is have the orders in their order books conform to the 0x order message format so once they do this they can then use this single shared audited set of smart contracts to perform settlement and there's a whole
bunch of developer tools that they can use to both interface with the smart contracts UI kits for building a Dex UI and of course mesh so since we've launched we've helped exchange a lot of value and we have a bunch of projects that are building on top of us so it's quite exciting cool so unlike other decentralized exchanges 0 X is not an automated market maker that's like you know swap bank or and it's also not fully on chain like eath to die or oasis so the way that 0x works is that the orders are created and shared off chain and then they are only sent on chain for settlement so
this actually lets you update your orders more frequently it lowers the gas cost of trading because you don't need to do a transaction to create an order or to update an order and we're storing the least amount of information possible on chain it also enables these order book style markets so these are markets that are similar to traditional financial markets where you have a bunch of bids and a bunch of ass on a book and they're constantly being updated by by the the makers of those orders and I guess you know our bed at Xerox is that this order book style market is actually going to out-compete the other models specifically when I compare it when we
compare it to market automated market makers both both it can support larger buys with lower slippage which is really nice and it doesn't rely on arbitrage to keep the price on a particular book in line with the rest of the market cool so all right 0x this is protocol you can use it it gives you a settlement it gives you tools but it doesn't specify how orders are stored and shared off chain and right now most orders are stored on real Ayres and a relayer is simply anyone who hosts a database of orders and helps
counterparties find each other but the question is okay so all real Ayres that are built on Xerox again they have the same order message format so what that means is you could take an order from one relayer and you can actually fill it on another and why would why would they want to share liquidity in this way because that way they can actually have deeper books and better prices for their customers but how should relay or share liquidity this is this is a big question and obviously the naive implementation would be every relayer just connects to the API every other relayer and like that they can pull all the orders but the problem with that is
that every single integration is custom takes a lot of engineering effort and if there's a new relayer that pops up he has to go to every single other relayer and be like hey add me I exist so this was actually one of the big reasons why we decided to build mesh we we just realized that it wasn't as easy as it should be to share orders so mesh 0x mesh it's a gossip style peer-to-peer network for sharing Xerox orders you can kind of think of it like BitTorrent but instead of downloading music and movies you're downloading cryptographic commitments to buy or sell a given asset it the the data on mesh is obviously also less persistent than on then on
BitTorrent so you know orders they become invalid they get cancelled they get filled and so they're constantly revalidating all these orders and then removing the ones that are no longer fillable so mesh uses the etherium network for two purposes the first is obviously to validate the Xerox orders make sure that they're still fillable the other is actually to protect against some resource exhaustion attacks so what we do is we actually link the the scarce asset a scarce asset that exists on the etherion blockchain to the scarce resources that exists in the network and in so doing we can prevent someone from
ddossing this peer-to-peer network and and just flooding it with a bunch of random orders so what are some of the benefits of mesh it's a completely decentralized off chain order propagation mechanism up until now you kind of had to use a relay you had to rely on some third party to host your orders you don't anymore you can just spin up a node and and and and join this network and start propagating orders the second dish this concept of shared liquidity this is something that doesn't really have a parallel to centralized exchanges literally being able to just seamlessly discover any order that exists out there and also share any order that you want with with everyone
else and so you know yeah this liquidity can kind of show up on a bunch of different surfaces over the Internet if you're an entrepreneur and you are building a defy application and you need liquidity you can just spin up a mesh node and tap into it and the last one is market formation so you know you have your project you have your core kind of supporters and early adopters but how do they start trading this new asset that you've created until now it's been mostly you have to pay a listing fee to buy Nantz you have to try and get it onto a centralized exchange but now you can actually have your users trade over a neutral permissionless network cool so
let's go into some use cases let's say you want to build the decks and what you can do is you can actually pull liquidity from mesh and solve this chicken and egg problem that exists when you're bootstrapping a two-sided market and already have a bunch of orders on your book even before you launch so when a user comes to your site you know they're not they're not confronted with a deserted UI with no orders instead they see this bustling UI and they're much more likely to stick around let's say you want to create a new asset instead of you know listing yourself on Finance or something like that why don't you list yourself and so with mesh it's very easy for you to create
some orders that your early adopters can take and you can spin up this open source decks UI and and and host your own exchange this is actually an example of a project called verasafe that did exactly that what if your market making mesh it's really nice because mesh actually combines all of these different exchanges and real ayres into a single format and so if you're a market maker you can just plug into mesh push your orders into mesh and they will surface on all of the real Ayres so instead of posting directly to a specific relayer you can just post it into mesh all the real Ayres will get it and even if a nuri layer or defy
application comes into existence your orders will already be discoverable by them so there's another really interesting aspect of mesh for market makers in particular and that's that without mesh you've always had this exchange which gets to decide what are the market mechanics and what are the rules under which you will trade with traders and you know typically this is either an order book style exchange or something like that but it doesn't let market makers innovate and kind of try and out-compete each other by using different strategies with mesh though market makers can connect directly to
traders and so what we're seeing is market makers starting to innovate in how they deliver orders so some examples are market makers that want to create a request for quote system we're kind of I guess similar to air swap you have this back and forth where you're deciding on a price and depending on who the counterparty is the market maker might give a better or worse price another example is just price matching by Nance so what you can do is say I will I will give you the price up by on by Nance at the point of settlement and and they craft their order such that it probably has a worse price than by Nance they'll they'll fill that and refund you the difference I think there's going to
be a ton of innovation in in this in this space and then lastly what if you just want to trade the newest coolest experimental assets that aren't listed anywhere yet right like imagine a project with a hundred and eleven people maybe it's called stable cat you know it's launching a new stable coin that's backed by crypto kitties super Wow and and this this actually goes a little bit into the browser support that I'm going to talk about later but what stable cat can do now with mesh is they can host an open source Dex UI on ipfs and actually have mesh running in the browser such that all of their early adopters get connected to one another and can
start trading and and this is kind of how a market can start to form so what is much built with mesh is written in golang it compiles down to wasum we use lip p2p for a lot of the networking stock specifically gossip sub-code emiliya DHT auto not and peer store for the persistence layer we're using level DB and we use go aetherium to interface with the etherium blockchain so now a little bit more about browser support not a lot of peer-to-peer networks work in the browser and there's there's several reasons for that but we're very excited about offering this as as a
feature because we think that it drastically lowers the barrier to entry for someone to get connected to the network just opening up a tab is much easier than downloading software running a node you know going to the console etc so how do we how are we achieving this we compile to wasum luckily Lippe to be DOS to obviously we have to think about the database layer how is that going to work in both browser and non browser environments we found a way to get level DB to work in both and that is why actually shimming the file system calls in the browser to using browser FS and browser FSI actually lets you decide if you want to store to local storage or to
index DB under the hood for lib p2p the pier store module is actually a stateful so it stores this information to disk but it can be configured to use level DB and then probably the most important part is supporting multiple transports so tcp only works non browser to non browser WebSocket can handle browser to non browser and web RTC is pretty much the only protocol that I know of that supports browser to browser communication so that literally means that there is no server between two browser tabs they're connected directly to one another right now let p2p has a transport called
WebRTC direct it actually only supports browser to non browser connections and that's because browser tabs you can't open up a socket to a browser tab there's no way to ping or dial a browser tab so the WebRTC connection actually needs to get bootstrapped and we're currently working on a specification to do that using a third node in the network and so yeah we were super close there's actually one more PR out to get to MVP browser support and then by the end of next month we're planning to have it completely finished and ready to go cool so that was kind of the
introduction part a little bit about mesh now we're gonna dive into actually spinning up a mesh node let's actually like you know let's let's get some let's connect to the order stream let's get some orders so what I did just to make it easier for you guys as I created these bit ly links and this one goes to just a gist on github that actually has an aetherium RPC endpoint already in there that we can use for the demo and you can literally all you need to have installed on your computer is docker so as long as you have docker running you should be able to then just copy paste this snippet and start running the node and I'm gonna do that here cool so here
is literally that same command so let's just start the node here cool so what we can see here is that saying found peer who speaks our protocol so those are all other mesh nodes in the network and now it's going to start connecting to those mesh nodes sorry I'll go back to here in case anyone still needs the link and then I guess the other thing that I wanted to just point out here is that six zero five five seven that's the JSON RPC port so this is I guess eight five four five is typically what it is for an ethereal node for so it's 6:05 five seven and then six zero five five eight that's actually the
the port that's used for the peer-to-peer networking yeah you were specifying an etherium network ID and an RPC URL and that's pretty much it yeah yeah yeah I can do that make this bigger yeah let me know we'll be like how where is everyone in the in the process yeah good it is anyone else still
getting their note up and running or okay cool so we have our note up which is exciting so now we're gonna connect to our mesh node using the JSON RPC interface and we have an official typescript client and a go client but you can also just use any JSON RPC client and for this demo we're going to use the typescript the typescript client so I created this this repo called mesh workshop demo and it's literally just a couple scripts that instantiate a client and then runs
a certain method on it and yeah follow it if you just go to that bitly link it'll send you to this and you can clone the repo and the the instructions are in the readme but let's maybe go to the the first call so the first call that we'll do is get stats and this just gets you a bunch of statistics about your mesh node so here you can see we just import the RPC client we instantiate it pointing it to localhost six zero five five seven we call get stats and we just print it out so if I run this here locally oh well the law of demos great let's see what's
going on okay okay nothing handler crashed that's not good okay I might try and spin this up again oh wow okay hang on sorry I think I might I was using the wrong I was using the wrong API key for Alcon alchemy so now we need a wait for the node to boot
up okay it's connecting to certain peer so if we call get stats now we'll see some statistics about the node so here you can see you know what pub/sub talking topic is it listening to what's its peer ID what's the latest block that it's processed the number of peers that it's connected to and the number of orders and since we just started the mesh node it doesn't have any orders yet we can call it again cool so we already have 220 orders that it pulled from the network and that number will will go up as it warms up cool so let's see that's the ghats get stats endpoint now let's
do something more exciting let's actually connect to the order stream so the order stream is interesting it's not just a stream of hey these are orders that have been found in the network it's also it also has events for whenever the state of that order changes so if the order expires if it gets filled if it gets canceled all of that you will receive four orders that you've previously seen and let's see so here we'll do the order stream cool so here we can see these order
events coming in it's a little bit hard to read but what we have here is the order and then we also have let's see it's hard to see here oh the kind and the kind oh yeah here time this says expired or added or filled and so whenever the state of the order changes we get a notification yeah maybe I can pull it out oh is that better cool yes so here is the stream of orders
that are being found on the network and then also when they expire or filled we get a notification cool so that's the order stream and and we'll get into this maybe a little bit later but you can you can then pipe that order stream into whatever database you want so what if you just want to get all the orders that are currently sitting in your mesh node there's a there's a there so function to that to do that as well just get orders async and if we do that I think I I think what I did here is I instead of printing them out because it would be
quite a lot it'll just say how many orders it found which in this case now is 456 so now let's add an order so if we want to post an order to the network this is what an order looks like it's literally just a JSON JSON object or a JSON blob you know it has all the all the fields so what are the assets that are being traded what what is the price what's the expiration here's a signature the signature you know can be just like a regular EC signature or it could be something more elaborate and what you can do then is just take this order and pass it to add orders async and this actually yeah it takes on a
race so you can send multiple in one in one go and I'm pretty sure I already added this one to the network so I wouldn't be surprised if we get an error instead of a success but let's see accept it okay cool nice so here we can see I just submitted this order to the network it was accepted this is how much of the order is still remaining to be filled obviously in in in base units kind of like in way I guess and the order hash etc if it was rejected we would see it in this array and it would have a reason for why it was rejected cool so what if you want to perform more advanced
queries oh no so I created this order already ahead of time and signed it so the signature is essentially signing all of this information and that way no one can change the information without having the signature change yeah so I didn't actually show the part of creating the 0x order but there's also we also have libraries that will help you do that so what if you want to do more advanced queries are JSON RPC interface you know it doesn't have a very expressive kind of querying language and a large part of that is that you know
the underlying DB level DB you know we've been optimizing it more for running the mesh node and not necessarily for kind of querying order books etc so the way that we envision people doing that is actually sinking mesh with an external database of their choice so you could sync it with a Postgres database if you want to use sequel for querying you could sync it with a with a graph QL database if that's how you like to kind of look at your data and we have on our documentation we have a step-by-step kind of instruction guide going through how you do that we also have already done it and that link up there will take
you to it we have we have another project called launch kit it's it's essentially both a Dex front-end and also a Dex back-end the backend we've taken it and we've added mesh to it so now you can easily spin up mesh node that also has an HTTP and a WebSocket interface that lets you query orders and it will give you these these methods so you know get a set pairs get orders order and these also have obviously query parameters where you can say I want all the orders where that are eath to die I want to get all the orders that are on a certain book and yeah but we hope that other people
will create other kind of mashups between mesh and and some database and some querying language that that people can use yeah no it doesn't so anyone can take that order from the mesh node and submit it directly to the etherion blockchain yes yes exactly so in order to send the transaction so there's different models there's models where the taker submits
it and there are other models where the relayer submits it if it's we call the the one where the relayer submits at the matching model if it's a matching model then yes you have to send it back to the relayer but if it's an open order book model or an open order book order then anyone can submit it yes yes yes so that's like trade collisions that's that's an issue if you're doing open order book there's a third model that we've been working on called the coordinator model and there you have to you go to a coordinator and you say I
want to fill this order and they will give you a signature to allow you to fill it and so they can only give you the signature and not someone else so that there isn't a race condition they just return to you a signature that lets you submit it on chain yeah because because we have this so the zero access more contracts are highly extensible so you can add whatever logic you want in front of an order filler yes exactly exactly yeah cool so if you do use get back-end with mesh kind of get this bonus because you
can use the the Dex front ends so this is an open-source front-end that we built that anyone can use it's you know it talks to an SRA end point and so yeah you can essentially have all your mesh orders show up on a Dex UI or you can also have them show up kind of in a collectibles UI so Xerox also supports NF TS so if you if you want to create a marketplace or kind of like more of an eBay style market you can do that as well yeah so it's on it's on github and it's called launch kit so if you just do github ZRX launch kit you'll
you'll see it cool so I also just wanted to show the browser interface so if you're using mesh in the browser you actually import a different package it's called mesh browser and it's it's similar to the other interface except obviously you know we're passing in the the configs here as a parameter and then you know you can start the mesh node and you can also listen to to order events so this this is how you would use it in the browser you would obviously not be using the command line and you wouldn't be using json-rpc either that doesn't that doesn't make sense cool did did did everyone get - did
everyone get to Runnings did anyone get to run some of the examples yeah cool sweet awesome yeah so some of them were already sharing but it was very ad hoc right it was like you know I'm gonna pull orders from radar and maybe like bamboo or like your er C decks but there wasn't exactly yeah yeah yep yeah exactly exactly so essentially
everyone is more liquid but kind of what what changes is you know as a Dex as someone who's building a relay er you know your business model is no longer predicated around putting a moat around your liquidity and charging access to it but rather you know targeting X like creating a market for X asset in Y market and and and and and like being the best at that yeah yeah yeah that's a that's a really good
that's a really good question so and and that's actually also part of the reason why we don't want to be too prescriptive with the querying language that we're giving people because depending on whether you are querying you know NFT Zoar fungible tokens you want different you know query parameters and ways of sorting and organizing the information so it so you know at the end of the day everything can be represented as these orders so just where was it you know I want to sell this for that at this price and and that information right is
generic whether or not you're trading a non fungible or a fungible token what what changes is how you want to organize that information and kind of query it so the SRA you know we built it when Xerox only had support for ERC 20 and then we had to also add support for for NF T's and it was already very hard to do that in a way that kind of made sense so what the way that I imagine things rolling out is there will be different api's and kind of different databases that are going to be used for NF T's versus non fungible z-- fungible sorry does that make sense yeah yeah exactly and that's and that's
exactly why we built to you eyes and not one because this doesn't work for for NF T's but this does yeah cool sweet so that's all that I had prepared is a little bit faster than I thought obviously you know the docs for mesh are online we have our github we're kind of a lot of the action happens and we're always on discord so feel free to join us there and and talk to us and yeah happy to answer any more questions about mesh or just 0x in general so it's gonna be sword in indexdb on in
your browser and indexdb the orders from other mesh nodes yes yes yes exactly and so there I think it's the right type of incentive right if you have an order that you want to you want to have on the order book you need to have someone who's pinning it so to speak right like in ipfs so you know if you're I think I think the people who will be running mesh nodes are real Ayres market makers like intense traders for for consumers like kind of us yes you can use a browser node and I think that can be
useful for kind of boost wrapping these early markets but if you want your order to persist that's kind of a service that a relayer can provide to you right so if you submit your order through a relay err they will persist it even when you go offline no exactly and I guess another point on that is that we're not trying to reach consensus about the state of the order book so everyone will have a slightly different view of of the order book but a that already kind of happens now depending on your latency and connection to even a centralized exchange and -
yeah it would take a really big performance hit if you were trying to like reach consensus at every stage yeah yeah yeah so that's definitely part of our long-term plans is to genera size mesh so that it can be used for arbitrary orders so like for us it would be interesting because you know we have different versions of the protocol we'd like to keep you know one network running for v2 and then another one start another one for v3 but you know
that could also then let other people who are not even building on Xerox use mesh so I would say today no right we're trying to get something out the door and so we're we're doing that work we're essentially making that happen faster by not worrying about making it generic but in the future yes absolutely you could do that I think the hardest piece will be you will have to write your own validation logic right for your orders yeah but we are adding that we're adding a way for people to add custom validation on top of the base validation but what would you would need it's like purely custom but sorry well once we do that though we can make the base
validation just be like part of a middleware stack of a validation and then you can just pull it out so yes there's absolutely a way to get there and we'd love if you're interested in kind of making that happen okay cool cool sweet any more questions awesome thanks so much for coming I really appreciate it [Applause]