Writing Smart Contracts for a Sharded Blockchain – Kendall Cole
Ethereum Denver·Fri, Oct 9, 2020, 12:00 AM
Speaker
This talk is from ETHDenver, the world’s largest Web3 BUIDLathon and Community Gathering. ETHDenver’s content is crypto and blockchain focused. Subscribe to our channel to learn more + connect on Twitter: https://twitter.com/EthereumDenver. Speaker: Kendall Cole of NEAR Abstract: Learn how to write, deploy and interact with Rust, TypeScript, and Solidity smart contracts on NEAR, a sharded, Proof-of-Stake public blockchain with a WebAssembly-based runtime. This talk was recorded Friday, February 14, 2020. ETHDenver 2020’s content themes included DeFi; DAOs and Token Engineering (TE); Gaming and Usability; Protocols and Interoperability; and Public Good and Social Impact. Industry experts also weighed in on topics like decentralization, privacy, identity, non-fungible tokens, scaling, governance, diversity, and arts and culture in blockchain technology. The 2020 event was February 14-16 in Denver, Colorado and hosted 2000+ blockchain enthusiasts, developers and community members from 50 countries and 50 U.S. states. 🔴 SUBSCRIBE ETHDenver’s YouTube https://www.youtube.com/channel/UCgz9NU06t_FkT0Py1Vadczw 💡 LEARN ETHDenver’s Official Website https://www.ethdenver.com/ 👋 CONNECT ETHDenver Twitter https://twitter.com/EthereumDenver 📖 READ ETHDenver News on Medium https://medium.com/ethdenver 🛍️ SHOP ETHDenver Official Merchandise bufficorn.com 💬 CHAT Telegram https://t.me/Ethdenver Discord https://discord.gg/jteMn5c 📱 FOLLOW LinkedIn https://www.linkedin.com/company/ethdenver Instagram https://www.instagram.com/ethdenver/ Facebook https://www.facebook.com/ETHDenver/
Transcript
alright we'll go ahead and get started hey everyone my name is Kendall and I work on near protocol so near protocol is a proof of fake started watching and yeah we're gonna talk a little bit today about how to write smart contracts for it so just give you guys a little quick overview of like what we're gonna do start off just gonna introduce a little bit more about NIR and how it works and we're going to set up a little dev environment we made it super simple for you guys it's just gonna be one click and spin up a web IDE then we're gonna write our first smart contract in assembly script which is basically a strict subset of typescript that compiles to web assembly we're gonna deploy this contract and an example front end so you guys can kind of see how it works and then we're gonna write a second smart contract in rust that is basically going to interact with this assembly script contract and as a bonus which I think we will have a little bit of time for since we're starting early we're gonna deploy a solidity smart contract to near using truffle show you guys how you could maybe combine near with your hacks this weekend alright so yeah just a little quick overview of what Mir is so near is a public proof of stake Sharda blockchain and we have a web assembly based runtime that currently has full support for rust and assembly script we'll explain what that means in a minute but you know really anything that compiles to web assembly could in fact work on Mir so we'll just cover a couple of like key concepts that makes me a bit different from say like aetherium for instance or other blog chains so one of the first concepts we have kind of a somewhat unique account system conceptually at least this account system is basically like a smart contract wallet or like the etherium name service so the way it works each account has a name like a human readable name so that's like 2 to 64 characters it's gonna have a balance in near it can have contract code or not and if it does have contract code it's gonna have a key value storage for this contract and then probably the concept that you guys are most familiar with so we have each account is going to have access keys and these are actually public key private key pairs and so what's cool is that each account can have basically like no no access keys which means it's kind of like locked so it has smart contract code it's basically a locked smart contract and that code will never change or it can have you know dozens of access keys and you can interchange these keys whenever you see fit so we actually have like a add and remove key action that you can take on your account so here's just like a little bit of like a visualization on this you can see full access keys like this account has three full access keys and we can remove those as needed and then if you see that little ID says new corgis that's actually the name of a smart contract and so this is using what's called a limited access key and so a limited access key can actually be scoped to just one smart contract so basically it can be used to call any function of that contract or it can even be scoped to just a single function in a single smart contract and then you can set like an allowance for that function or for that key so in this case it has like a very small little near balanced call the near court use contract so yeah the next really important concept as I mentioned we have a web assembly runtime so basically you just compile your smart contracts to web assembly and then you deploy the wasum file and so what do we mean by a full support is that for russ and assembly script we basically have some pre-written smart contracts they give you some like powerful functions like you're familiar with in solidity you have the message and from message you can get the sender you can get the value that was attached that's like that's basically supported and will actually go through an example of this in just a second so the third concept to cover before we actually get coding the way that contracts work on shards is actually like it's kind of unique to mirror so despite the fact that that we are a shard of watching there is actually still just one blockchain and every single across account or contract interaction is going to be considered a set and the reason for this is that we don't actually have same shard guarantees in there and the reason for that is that we use this concept called re sharding and so what this means is that contracts and accounts can actually shift shots so the reason we reserved this like right to basically have contract shift shards is that if a contract is for instance like growing very large has like a lot of activity or even state it can even grow to like maintain its own shot so there's some like interesting patterns that can arise from s and then probably the most important thing when it comes to reasoning about your smart contracts to know that while calls actually can be returned in a later block they're always going to be atomic so you never have to worry about like you know if say you know the first part of your contract call succeeds and the second part fails you don't have to worry about there being any consisting to BC there it is basically going to roll back the transaction so just a little visualization here on the left you can see like you know pretty standard like beacon chain design very similar to what like the eve to current spec is going for and then on the right you can see the way that we're doing things so Night Shade is kind of the name of our charting system and so basically what happens is that each shard is going to produce a chunk and then all the chunks of like the so a chunk from each shard is then going to be contained into a block so we actually do still just have one watching you don't have to track a different blockchain for a different shard so yeah just a little summary here so there is there going to be a DES miss to be between shards couple links here if you're interested in learning more do you want to see like a very detailed breakdown of how cross shot transactions work in there and you check out this link near AI slash X chart and if you're interested in learning more about how we handle data availability you can check out near that AI slash nitrate alright so now we'll go ahead and actually get to writing some smart contracts so if you're interested in following along yourself you can just go to this website right here and that's basically gonna spin up like take you to a github repository and in that github repository you're gonna see a blue button that says open in git pod and that's the the one-click web IDE that we're gonna be using today so what we're gonna be building is just like a very simple messaging app so this is basically you can see right here so there's gonna be two types of messages there's going to be like a standard post and is gonna be a premium post and his premium post is gonna demonstrate how you can basically charge for certain function calls in there it's really just to show you guys kind of how how our smart contracts work so here's the environment right here this is basically what you'll see if you just click on that open and get pod button and yeah it's kind of cool just like we have a docker container it's basically gonna be the play that all already have all the dependencies installed so that makes things like nice and quick so probably the first thing that cover here is just like the contract structure fortunately this everything by the way that we're gonna be doing today will be a Miss readme so if you want to follow along later feel free to do that it doesn't render so well here it's kind of dark but the contract structure basically the the structure of our our project so we have contracts and under contracts you're gonna see two folders they'll be assembly and rust so that's basically gonna be alright will write the assembly script contracts in this folder and the rust ones in here so if we look at the assembly script folder where you have three files here the two of which are the most important there's gonna be this like main type script this is we're actually gonna write most of the code and then we have this model dot type script and this is like if you're familiar with typescript which is where it's gonna find some of the types and then for the front end this is basically some example code we won't really be going into too much depth today but this may not javascript bio is kind of the most relevant this one's just gonna show you kind of like how you can actually interact with smart contracts from the front end so yeah let's start with just this model that TS file so here were actually just going to define like the the one type that we're going to be using today so we'll call this posted message and we're gonna have a just like a few different attributes here so we'll have a sender which would be the string is actually give me like the the account that is sent the message we're gonna have just like the body of the message which we'll call text and then because we're going to designate between premium messages and non premium messages was defined this boolean here and if we go over to this you can see we're just gonna like import this type from the model file and here's where things get kind of interesting if you see here this is like where we're using what's called near runtime typescript and so that is actually like that helper library that we were talking about before so what we have here is context and context you can kind of treat as a combination of like message and block if you're familiar with salinity this is where you can get like the sender and this is where you can get like the block pipe for instance and then we're inter we'll be importing one more one more object here which is called persistent vector and this is basically like a helper data structure that we have for you guys it's essentially like wrapping our storage like our storage parameters and so if anything that you define in this persistent vector will basically automatically be treated as something that should be stored on the blockchain so this concept is also very similar to solidity patterns where you have like memory data structures and then like storage data structures this is here we're just like defining or like importing like a like a UN to 128 so I mean bignum is not actually a library that we wrote so this kind of shows you you can actually use some external libraries in your your typescript contracts it's sort of dependent on the library itself you know there's gonna be some things and won't be supported but it's definitely worth trying out alright so as you can see here we just like defining like the one data structure they were actually interested in in this smart contract so that's gonna be messages so we're just gonna instantiate a new persistent vector and that persistent vector is going to be made up of different posted messages the only thing that's actually a little bit near specific here is we have this here like M and that's basically just to make sure that the key value store is unique so we'll just make this one end all right so now actually getting to writing some functions so the first thing we're gonna do is going to be add message and so this is actually gonna be what we call like a change method so this is actually going to change the state of the blockchain so yeah I mean this is like pretty standard type script here so we're just like gonna take it's gonna take a single input which is text it's not going to return anything and then we're just gonna like define this message here so the type is going to be posted message here's where we actually use context so as you can see we do context that sender so that's gonna pull the account of whoever it was that was actually like sending this message so basically whoever called this function will be the context that sender and text and then it's like premium this is this is just gonna check if this should be a premium message and so the way we're gonna do that we're using context again and we're gonna pull this like attach deposit and this is very analogous to the like message value from solidity so we're just gonna check if it's higher than with this space through the exert 0.1 year we actually do have some good helper libraries that we didn't use in here that are like making it so you don't have to know like basically which is called that at so near its like similar to quake so you don't actually have to know this if you use some of our helper libraries and then here we're actually gonna push it to messages so as you can see we defined messages here we're gonna push this message and so this actually store it to the chain so that's that's the first function and then basically the read function this is what we call like a view method and this is again like familiar with solidity with space which using like a view method and solidity so we're gonna actually gonna return an array here and it's gonna be a array of these posted messages so I mean you're probably thinking like you know why return array versus persistent vector basically because persistent vector is actually gonna be storing something to the chain whereas like array is just an in-memory data structure and so since we only care about it to return the result we don't actually need to store it anywhere so we're just returning meteorite I mean here yeah we're kind of using like a defined constant up here which is message limit just so that we only return ten messages so not particularly interesting like here we actually create this array with the messages that we returned and just gonna iterate over it it's basically just like only returning the most recent 10 and we return the result here all right yeah I mean that's actually just the first smart contract so now the fun part we get to deploy this so we actually tried to make this as simple as possible if you look at our documentation you can see like a step by step breakdown of how this works but we have like a nice little helper script here which is just going to be dev so dev is actually just going to deploy this for you and so it's gonna do a few things it's going to create a new account for your smart contract it just is going to be like a dev one so it's just gonna like pick one for you it's then going to deploy the contract to this account and then it's gonna like call the first contract function and also run our front-end so we just type NPM run dev here give it one second we can kind of see it's building the contracts so this is actually building it's gonna create a web assembly file in this out folder here called main so it's gonna be the same as the what we call it like the the tiger file and here you can see it's starting the deployment and we are deployed done deploying so I deploy to dev one five eight whatever and now we have our fun and running so yep using git pod we can just open the browser and we can actually see this application so here we are this is now live we can login with the mirror wallet get redirected in here so we can see near guestbook is requesting taxes our account check more info we can see what the actual contract is can even open this up in our block Explorer and see there's basically created and a contract was deployed and sorry back here we'll just authorize this so this is actually just like basically creating a limited access key on our account just allow that and we're back so now we can actually select between the two different message types so we'll just check this one out first one to post the message and there we go and what's cool and as I mentioned the the limited acts exceeds key pattern obey basically what we did is we added a key that was scoped to just this add message function on the contract that we deployed and we added that to our account so my account is called Denver demo and so basically what that means like we have this we actually have the private key of that limit access key stored on our front end here and you can kind of dig into the front-end and see how that's working and so what this means is like users don't actually have to go and sign another transaction when they want to actually interact with the smart contract but if we do want them to do that we can use the second function here premium post and see how that's going to work so this will actually like redirect us back to the smart contract this is basically getting pulled by default from the URL that's why it's like kind of so ugly here and as you can see this is actually requesting that we're gonna transfer the 0.1 there and it's gonna be calling this function here so this is like the premium message that we're paying for it so what we usually do is if if you want to actually require the transfer of Mir in a smart contract function we require you to get an explicit signature from the user and that's just to protect the users from accidentally sending you a ton of mirror without really knowing what they're doing so we'll go ahead and allow that see transferring here and transfer successfully we'll be redirected back and bam there's our payment message cool so to recap here what have we done we've basically written an assembly ship smart contract with two different calls we've deployed the contract to the test network and we then interacted with it in two different ways so now we are going to write a rough smart contract that's going to talk to this contract and it's actually going to use two different types of the cross contract calls so we'll go back here we'll go to rust folder here so as you can see we have some like rust Pacific stuff here got like the cargo files most of this is like the main one we've got this little build script here which is basically going to like use like the web assembly project and it's going to deploy or basically build this file and then output it here and it's gonna out put it in this folder here that's like a result folder so I actually went ahead and built it just because it does take quite a few seconds to build a rust mark contract but we have like a nice little helper function here that's just MPM run builds colon rust and that'll go ahead and build your Ross contract so we'll actually go over what this rust contract is so you can kind of see how things are gonna work if you're interested in using rust alright so rust works extremely well we're extremely excited about it that being said like we have a lot of helper functions to make things easier and there is a little bit of like kind of setup required like a couple of boilerplate inputs for you to get started so the first thing you have to do is import this like what's called borscht it's basically like a in library and then kind of the main thing and this is like very similar to like our near runtime typescript library that we had you import in the typescript file this is basically includes a lot of helper functions so environment is where you're going to be able to use like contacts to actually get like who sent the transaction how much they sent and like block information for our cross contract call we've got this like external contract a little like object here and so this yeah will actually go through how this is going to work but this is basically used to wrap like the interface for the contract that you want to interact with then nearby engine itself kind of like a self referential import and then a promise and we'll talk about how the problems is work in just a second you also have to import this like global alligator and then most importantly you have to define like destruct and there's basically like the name of your crop of your smart contract we're gonna call a cross contract to keep it simple make sure you tag it with your bind gen and this little kind of messy pattern here we're gonna try to like hide this but for now you have to basically define this once and then you don't have to worry about serialization anymore and yeah here's probably the one of the more interesting little functions so this is going to be our external contract so we can call it guestbook and as you can see here this is actually the same methods that we defined before and it's gonna have the same inputs with a little bit of like rust magic so this one is mutable so that's how you can tell that it's gonna be a sort of like create function and is actually gonna be like mutating State on the blockchain and it's gonna take in again the same input as as the other function yeah sorry about that thank you for pointing that out by the way it's getting kind of into let me yeah is that is that better all right cool thanks guys yeah all right so it's just to kind of show this again so these are basically the exactly Matt matching the functions that we defined in this typescript contract since we're gonna be calling it there so it's gonna be add message which is this mutable method that's already have that like mute stirring there and it's actually gonna be taking in the same kind of data structure so it's just gonna be a string here which is text and then the same thing for get messages and what's kind of cool is I mean the data structures are actually a little bit different between a russ and typescript but they're still gonna be compatible in this cross contract which is kind of cool so this is a the get message which is actually gonna return this vector of strings and that's similar to the array that we returned here this array of posts the messages and again I mean interesting that it doesn't actually require any sort of like knowledge of what this is going to be and then will actually implement the the calls here so just again you have to tag it with your gives basically tag like each implementation with nearby gen we'll call it a cross contract cuz it's implementing this struct here and we're just gonna do two functions to kind of just demonstrate how things work so the first one is this add message and so in add message we're actually gonna do a very simple call so it's me calling from guestbook we're gonna be calling add message it's gonna take in text text is actually the only argument if you remember up here after that we do have to add a few more additional arguments the most important one being dislike account ID this is actually going to be the account ID of the contract that we're calling which maybe remember is probably that like ugly dev dev dev contract name then this is gonna be the amount of near that's done with it and this is gonna be the gas so that's ad message and then if we want to actually use a promise and this is how you would do like more like complex cross contract calls we'll call this add and return messages so same kind of thing it's gonna take in just like this text this text text string and also the account ID and it's actually going to return a promise and so if you can see here so the first one is this exactly the same we're just like calling ad message but then we can use because it is a promise we're gonna use like you then to basically define what comes next and here we're actually gonna then call the get messages function like same kind of pattern doesn't actually take anything in so we don't use text there and this is gonna return itself so we're actually gonna be able to then print out basically the messages that were returning so yeah that's the Russ contract now let's go through how we can actually deploy it and interact with it so let's go back to here that's running all right so we already built it on here but if you're following along you can run this NPM run build - Russ and that'll go ahead and build the contract and then there's a few things we have to do this I'm gonna just walk you through how you actually do like a like from scratch contract to fly and so if you're trying to follow along here we have these instructions here so we already built the contract now we're gonna do near login so there's actually going to login to like the near account that I'm gonna deploy this from I just click this here second opens this here story's gonna authorize near shelves basically have access to our account so we can deploy contracts confirm that and we're back alright so now we've stored that there next up we're gonna do we're gonna create an account so this is gonna be a V account that we're gonna deploy our smart contract goo so we'll just keep it simple Denver one two three I have to basically let it know which account I want actually used to find this new account alright we've now created Denver one two three now we need to deploy the contract so this is a little bit ugly let's actually open up another like file here txt file is why we have the helper scripts to make it a little bit simpler alright so this is the account ID that we're going to be deploying it to so we just created at Denver one two three we already have the web assembly file that we're gonna be pointing it to it's mentioned before it's just gonna be this one right here it's called yes but web assembly file master account is the one that I've been using so this 8 Denver demo and now what a ploy does bad boy hey boom starting the deployments and we're done alright so now we've deployed it and now as you see here we can actually call it we're just gonna call it for the command line just to give you guys an idea of how it works so we'll just call this second function here this add and return messages so we want to call it Denver one two three we'll put in the account ID that we deployed that first contract to that's this one and [Music] let's give it this message alright did you go and if we pull up I think this is damn there we go so we see that it actually sent the message to our assembly scripted smart contract for all of us restored and it also returned to us all of the messages that we've seen before so now we have deployed a rough smart contract that interacts with our webassembly contract and used the cross contract call and a promise in to return the result of that call so the sky's the limit will done you can do there the promise structure is actually like pretty interesting you can actually just touch on that real quick because I think it's worth showing you guys you can use it basically as like a factory for contracts within a contract so let me just pull up you can see here this is actually where so what we do just within this call using the promise we're gonna create a new account ID we're going to then transfer it a bit of mere add a full access key and then deploy the contract and if we wanted to go a step further we could even then call methods on the smart contract after that so it really is like pretty powerful little function we're working on the documentation to make it a bit better but you know if you have any questions and you're interested in more any more about this please come talk to us alright cool so yeah that is pretty much what we had for this specific demo so I'll just go back to the slides here and we'll talk about some of like just one more thing that I think is probably worth mentioning so we just finished the EVM support for fournier and so the way this basically works we actually it's we deployed the basically like rust implementation of the EVM compiler there was written by parity as a smart contract to the near blockchain so it's cool using truffle and using this custom web 3 provider we have you can actually deploy your contract two-bit like basically your EVM bytecode to this contract and then interact with it as though you would in a normal application and what's also cool so then if you use this web 3 provider in like web 3 gjs instead of like using meta mask for instance you can use the near wallet that we just demonstrated to interact with your solidity or viper or anything that compiles to evm bytecode smart contract so I'll go ahead and just show you how you can act like how simple the deploy is basically and then point you to the repo of where you can use this yourself if you're interested so I've actually been yeah like kind of demoing this a little bit but it really is as simple as like just doing truffle migrate setting that network is near and wait for this to deploy you see any migrations [Music] all right we are deployed now if I can remember where I have the Explorer so this is our block Explorer I'd finished it Explorer dot near protocol comm here we go so we can see Denver DBM is actually the contract and then we just called it a few seconds ago ago we called deploy code and call a contract is basically calling the like in it or like the constructor of the contract because we don't by default call instructors unless you specify that so yeah pretty simple we can actually look a little bit here's actually the code this is basically just setting up your so this is all you have to do basically to set up your truffle file we have a little example which I'll give you the link for in just a second if you want to see this yourself and you have to basically use that veneer provider set the network as near and you can go ahead and deploy it so nice and simple and then to interact with it yet just change the provider to the near provider on your front end and your in your JavaScript code and you're pretty much good to go so yeah if you'd actually like to do this yourself near Dada a I slashed near - EVM or come talk to us we're super excited about this and like happy - happy to talk you through it and just one last little point here and then we actually have a couple minutes left so I could take a question or two if nobody has one but we do have some bounties this weekend so we have three bounties for you guys so if you use the cross contract calls that we just demonstrated in rust for the best implementation of that we're going to give away $1500 we are interested in kind of like technical like the technical implementation here so if you tried to like use any advanced patterns that are basically in the examples that we have that's kind of like what we're looking for there if you really like to talk more about this just find anybody wearing the near t-shirt another one if you deploy and this was actually super simple and we're hoping we get a lot of submissions areas we just think it's quite a fun one to do you can deploy any solidity or Viper contract Amir and use the near wallet with that EVM support we have we're also gonna give away $1500 there and never we're really interested in just kind of the most like user-friendly solution because we're pretty excited about the way that our wallet works and we'd love to see what you guys can do with it and then the last one in this one we'll kind of see so we have a nearer to a theorem bridge that we're super excited about and it's it is ready but we don't have very good examples up just yet we're actually working on them as we speak so hopefully we'd be done by this evening when we like further announce the bombings but if you use this in any way and it's very simple way to do this is you're actually going to be able to transfer assets between if there am and near that's my way the simplest way to use it if you just use those there's newly transferred assets in your your near smart contracts or in your solidity smart contracts then that technically makes you eligible so we're definitely just adding creativity here and love to see you guys come up with yeah and those are our bounties thanks guys we'll be around all weekend so please come talk to us if you'd like to learn more about near or and just it engaging with our bounties thanks [Music] you [Music]
Automatic transcript — names and jargon may be misspelled.