Reach language - Kwame Bryan - [Toronto Ethereum Developers Meetup]
ETH Toronto Talks · 30 Jun 2021 · 23:49
Researcher and cypherpunk Kwame Bryan presents Reach, a language he positions as a viable alternative to Solidity for writing smart contracts. He dives into the code with a game of Morra and discusses the merits of Reach as a lighter weight way to build compared with Solidity. The talk was given at the Toronto Ethereum Developers Meetup.
Speakers
Topics
From ETH Toronto Talks, hosted by ETH Toronto.
Transcript
yeah great uh my name is kwame i am a uh well tim actually gave me a new title i'm a researcher but i'm i'm just kind of just a hacker i i go around i love finding new tech um i got involved with ethereum in 2014 at the uh toronto bitcoin expo and um and i kind of stuck with ethereum just kind of hacking around in the early stages um my background is actually in games i worked at a company called jammed mobile in montreal they became ea mobile and i worked on the game called bejeweled um and if you've heard of master game mechanics that's kind of my background which really plays well into blockchain based games um uh
simultaneous action selection um which is kind of my my background and um right now i i work with reach um that's a dap programming language um i don't know if anybody's heard of it but there's been some some press um in the space and it's a blockchain agnostic um framework right now it does um play really well with the ethereum world and uh that's what i'm gonna basically showcase so i'm just gonna run through some code and kind of give you an idea of how uh dabs are built in reach and and uh hopefully um it'll give you an idea or maybe just give you some um an opportunity to look at something different other than solidity not saying that solidity is bad
i'm just saying if you want an alternative there's other ones out there okay so i'm going to share my screen and make sure and hopefully you guys see in um visual studio code with uh with a host okay so i'm just going to really just go through the code and kind of demonstrate how adapt's actually built out so this is our index.rsh this is your reach file and you can just see like an import directive in the top here now you'll see that we have this all players this is just kind of like an object that we can associate with
a player one and player two now player one and player two in reach are defined as participants and participants in in consensus really so this is kind of how um and a participant would be billed out so you can see we have a player one this will have a wager of type unsigned int and it's being composed of this um you know general interface so we can see that the player two is going to accept a wager it's going to be a function we see we have a tuple which is just an unsigned int at this point and it's going to return a bool um and you'll know in blockchain games that doing things in coin flips and yes and no they're just
really efficient uh okay so here we have like which would be considered like um you know a main enter our main entry point as you can see we have our reach side app and then we define the participants which is their string name which we bind in our front end and you'll see that we have an object for player one and then we have the player two with its own object now this is where reach is becomes really simple if you think of it in terms of you know alice does this and then bob does that so here we have our a dot only and this is called a um a local step right a local consensus
step where we declassify and um oh good so we have um this is actually the ide for for reach and i'm doing some edits in it and you can see here we have some post issues so we have inside the editor which you can it'll define what an interact object is so as you're kind of coding through you're getting a lot of hints as you uh work through the your code so what we're basically defining here is that alice is going to set a wager we're going to declassify this we're going to set it into this variable name const and then when we publish this when we send this out to the blockchain what do we want to do we want to publish a wager and then we'll pay a transaction for this to be deployed on the
blockchain we access this um function called commit which basically connects this information to the chain so if you're designing kind of like maybe like a construct of a you know erc20 you could really just do it with defining an interface um alice who would deploy it and then just deploy it out so here we have bob and bob dot only means that this is something only bob can do and whatever's inside it is his local step so here we use the classify the classified just means that um this this private information will be opened up so that it can be um published to the network in on bob's behalf we interact with the his uh function and then we pass in the
wager that alice set that's kind of how this works and then bob is going to pay the wager and here we just have a variable for the outcome and this is this might be different for people who've never used this it's called a loop invariant and a loop invariant is basically you're defining what the outcome of your loop is going to be before it happens so while you're going through this loop um and here where this this is a game of mora if anybody's played the game of maura uh rock paper scissors diplomas diplomacy the same type of game where you have um uh you know modulo arithmetic for say uh three outcomes right and you have like
true and false you have something in the middle you don't remember the one that's in the middle you just kind of do the two and you kind of do modular arithmetic to the term a winner of a certain outcome because it doesn't matter if it's million of coin flips it's still gonna be true and false and that's so that's kind of how that's defined so here in this outcome we have the finger and the prediction and these are private local steps so anything that's defined with an underscore is a private local step um you'll notice here that when they're when we do our publish we're kind of uh declassifying which information and then this salt here is just a standard library function that does randomization for you salts your
your information um we kind of continue down we go it's a lot of the same steps that's the one thing that's cool about reach is that if you understand the um concept of gaining information declassifying it publishing it to the network it's really just similar steps that you do it's like alice does this publish it to network bob receives that does something with it publish it to the network and every time that we're going through and we're committing and we have our publishing pays that's where our transactions are happening so if you're looking at as it and developing adapt you're you're not just building a contract and then going okay well let's put some ui to it
you're you're building up if that makes sense like you start off with with a with a contract and a front end with little complexity and then you build them up in parallel um as your um as it scales up okay so what i'm going to do here um as you kind of go through you can see here that at the last part we have this assertion assertions are how we do testing in reach so when you write an assert it creates a theorem and it uses a verification engine to determine if you know all these simulations will result in true and if there's anything any money left on the table or anything left
it will it will fail it won't even won't even run so there's no opportunity to have money on left on the table when you write and reach it's really safe so you can see here that we have this assert balance right or this wager and it's the same at the top so we're making sure that everything that's happening in this game loop because this is actually a game in this game loop results and making sure that in this coin flip if someone says puts out a wager and they accept it there can't be anything left on the table uh here we just have some regular programming for a ternary operator and as i said before we're really based on this whole you know zero one in this game world if you're building games in the blockchain you're
dealing with with that kind of true truth and false and then we hear here we have our transfer function so we uh this is basically for alice right times the wager to alice or we transfer the wagers which are up 0 1 which would be bob to bob and then here we have the interact so this each interact is where we run that display winner outcome so we get the outcome and then we display who the winner is and that's basically it that is all you need for a game of mora now i'm going to go in here and in the build and if you guys can see can you guys see the build information
here it should be the same screen i mean in index main dot mjs this is the back end so this is what is actually compiled from your reach program so you can see here this is this is a lot of what we we're used to right um the one thing you'll notice is that i am scrolling a lot right and this this is what we would this is the generated uh solidity code um from a reach program so if you look at the simple complexity um you know well it's simple because i've
been writing it more but if you can see the simplicity of this anyone can really generalize what is happening in this program where if you're in a solidity program you could probably get lost pretty quickly a lot of complexity another thing that is generated when we actually generate our file is a little front end so i'm just going to sorry i don't know if i'm breaking up here is just going to um run out again maybe you could try turning off your
camera because you have been breaking up just now a little bit yeah okay thank you all right hopefully that's uh that's better thanks um oh perfect okay so in here we have our index.mjs and this is just the um a front-end file and when you run um let me just uh do this here i'm just gonna go let's do this it should just bring it up all right so inside we have all these um different uh options that we have but the the main one that you get when you
start working with reach how do we it got removed ah oh there it is no sorry uh this region that uh this region that sets up your um first uh you know index rsh file and the front and in the front end for it which is really cool um because when you start building this up as soon as you get to the the point where you want to start putting in ui as a developer i'm not i'm not a the greatest ui guy but i want to have a console-based application where i can um go through these processes so what i'm going to do here is i'm going to clear this out and i'm gonna run this game of maura so i'm gonna compile it and then
you know what i really should have tested this because i added this uh posty so you can create guest in the ide but um lou we could see your screen running but we can oh okay here you go am i still there yes now you are okay uh i think i do think my internet went out and i'm really i apologize guys no worries okay so you're gonna see here that we have in our front end it's gonna ask for our player one and i'll just say yes and i'll create an account and and yes and deploy the contract and i'm
going to say yes i want to deploy the contract and i can copy this in and in a perfect world you'd want to use a relay or even like a hash lock but um i'll just say no and i'll just play the contact information okay and i'll just say 500 and accept the wager bs and this in this game of mora i'll just say two what's the prediction for i'll kind of just do the same thing just to show okay so now the game is kind of like in this game loop now and i'll just say two four oh three and i'll just say two four and you can see that they
um the player two won in this game and the transfer of funds went through and um in the the next level um you would actually have i have another project that's called uh scaffold reach ui which actually has a where you create an nft and it has like the react front end for it but in the base uh constructs of how reach works you you start with this index.rsh you have this really simple index of mds file and then you scale up your contract and your ui in succession which i think is really cool um and if you've if you have a lot of other toolings and you've been in the space for a long time um you know we've gotten used to truffle and we've gotten used to hard hat
and um but if you're really new this is one of the biggest pain points it's just kind of building a simple dap and having it work and this has everything for you and if you get the ide the reach ide and you can start playing around with reach and um i'll throw in the link to the doc site that runs you through a tutorial of actually creating rock paper scissors and um yeah so i mean uh hope you guys think it's interesting take a peek um that's all i got that's amazing kwame thank you i always love um like coding sessions in these talks because
i'm not a coder myself and i'm not a technical person myself at all so it's just nice to dig into the code and i think everyone else probably thinks the same way too um based on the response um we got one question here from phil phil do you want to just ask directly yourself yeah sure um just wanted to know um what are the limitation of reach uh because you said that everyone is still using solidity and it was really cool demo to see all the code produce versus what you type it looks like the future what are the limitations right now that makes that people don't take the leap yet maybe um i well i mean breach is really new first of all um i think it might need
some support from maybe like a consensus or you know something like that that says yeah you know what this is actually um an alternative to it and i'm not trying to say that um we should stop using solidity because there's some stuff that has to be done in solidity but a lot of this higher level stuff where you know transferring of funds like do we really need to write trans our own transfer you know functions or balance of functions like this is kind of boilerplate stuff and we do get this from consensus um but uh i guess what i'm getting at is we learn how to write the code but most of the time uh we get to this point in solidity where all this stuff is just boilerplate
and um a lot of the mistakes are just happening from you know people copying and pasting other stuff right so um an integrated development environment for a new person to adapt development um is perfect because this is actually creating a dap it's not just building a smart contract right we're in solidity or something else you just build smart contracts this is actually building a dap right and my uh experience is more of like front-end html5 games that kind of stuff so um now with a lot of the l2 scaling solutions like coming back into this is like well i want to build games but you know um that wasn't really the case so not a lot of people really thought about building dabs they just thought about
building contracts and simple interfaces for it but now we're building games right we're transferring you know real world assets so it's um we need a framework and um leon was talking was doing did some really cool stuff with uh you know the unity right like that's the future right um no one's what i could see about macbook city but what is the where do you draw the line between a smart contract and adapt that's unfair for me wow that's uh that's a that's really good uh i mean a smart contract can basically run on its own like if you think of an erc20 for just building a you know a token um you don't need you don't need adapt for a token you just
write a smart contract you use remix you pop it up you inject your web3 you hit whatever chain you want to hit or you know um you know version of eth or whatever you have in your metamask right um with with this solution you're thinking about well how is the user going to interface with this thing as you're building it out so now when you build your ui like my biggest problem in dap development is that i write a contract and then i'm thinking about the ui and i'm like oh geez right yeah like i think 10 like 90 of your time is going to be building a ui right and it's the ui that that takes the longest in in
in this space well i should speak personally um the front end takes the longest like 10 percent of my time it's writing a contract and 90 of my time is writing it's right in the ui so if i can build it up in a text-based way and then it's like okay you have like this the way that it was defined here if you gave that to you know a really slick um front-end developer they would have everything they need right um they're just like all right cool where this this uh console part is that you know i and where this player is i can i can write the code to logically work myself around that if you just gave them a big huge smart contract if you're like what all right so i mean that that's i guess that's the difference
um i'm thinking of how you build up dapps from a uh maybe a full stack you know blockchain developer if there's such a thing cool and um jimmy um did you want to mute yourself and ask your question um um yeah i mean i think leon answered it just it was about the you made a comment about it being blockchain agnostic and i was wondering if you could expand on that uh yeah so this plot reaches blockchain agnostic it supports right now um of course all the evms um right out of the box um no no changing anything there um it this technology was originally um spearheaded and funded by
a blockchain called al grand um they are like a zero knowledge type of um you know blockchain and um vitalik was hanging out them at bitcoin miami so obviously they're probably talking as we know that vitalik loves his uh zero knowledge stuff so it was originally a developed by them and um it really it works really well with ethereum so i i um i saw the project and right now they have uh they're in talks to do stuff with cosmos um there's some other other talks i think the in um ada um the cardinal project and um yeah so as long as these pair chains car start going out and these op codes start being released
then we can start working around it and doing it but as i said before i don't work for reach i just i stumbled on the technology by buying some algo and i was like wow this is pretty cool makes it makes it really easy for me to build a dap so i'm all for it that was it awesome well thank you super interesting stuff uh mark uh your hands are raised yeah thanks comey that's really interesting so i had a question since the reach is being transpiled or compiled to solidity is there any question about the efficiency of the solidity code um you know that's a really good question and
jay would probably be you know better to um you know answer that question at the compiler level um but i do know that there is optimized like gas optimizations that breach compiler can do right um so like you uh a lot of developers know that if we write more efficient code that is a lot more efficient on the evm um but who really wants to go and do that do the thought of it like if we can have the compiler really optimizing our code like like a good llvm right like everybody everybody writes better code now because you know a group of you know people actually put you know effort into right-wing developers wanting efficient code to create llvm
so um this kind of the same thing here but like it's a really good question and you know i'm going up into hyperbole but um the compiler does do some optimizations but i can't say for sure that it would be more efficient to write in reach because of that trans transpilation um that there there has to be some friction but the optimization should balance that okay thank you thanks cool and one more question in the chat um does that mean you're looking for front end devs am i am i looking for front end devs i i think so or if um if jza
if you wanna yeah if there's if there's people that like want to uh you know help out um i am trying to you know put put some stuff together and look for i actually want to interface uh reach's um standard liability with pure web um so yeah i'm kind of putting some really some stuff together and if there's people out there that want to help build um blockchain games um that's my that's my background and um yeah i'd love to you know pollinate and and definitely um if you want to pass me your information i'll definitely link link up with you for sure that'd be great appreciate it then yeah you guys can always use the chat but um
i think that's that's it for all the questions um kwame thank you for for uh stopping by and giving us your talk that was yeah that was great i love honestly watching people dig into the code