Arbitrum 🛠Rust Smart Contracts for Solidity Devs
ETHGlobal·Mon, Oct 9, 2023, 12:00 AM
This workshop will walk developers through creating an ERC-20 contract in Rust with lessons learned from Solidity. Some of the most skilled web3 developers, designers and product builders from all around the globe came together for a weekend-long hackathon at ETHGlobal New York 2023. ----------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com
Transcript
foreign [Music] today we're going to do a workshop where you're going to be able to deploy your own rust ERC 721 that is we're going to make nfts together um using the rest programming language and arbitrum stylus um so if you actually want to follow along and and deploy this nft which you're going to see um is pretty cool and straightforward you'll need to go to this link so there's this GitHub repo and if you follow along with this you'll be able to actually create here in ft and use it to do a bunch of fun stuff so this Workshop kind of has two parts to it in the beginning here while we're all getting um the things installed on that GitHub page setup so that's just like the rest programming language cargo stylus the tool we use to actually um you know deploy stylus programs and some free tesna ether um so you know if you're if you're low on test and Eve we have that too all of that is on this page here so this is on the offchain labs account there is a stylus Workshop nft repo and we'll look at this together so while everyone's setting that up we're going to look at an example ERC 721 implementation together to try and learn a little bit more about like how rust and stuff works from a perspective of a solidity developer and what actually working in it in a in an arbitrim setting looks like and then um at the at the end we'll just uh work on nfts together so okay so this repos where you want to go I'll show the link again later but it's on the off chain labs to search stylus Workshop nft that'll take you to this repo here and it's very it's kind of what you expect from a git repo you just clone this thing and okay good you can see this um I'm gonna jump into code together but just real quick so you're going to install rest you're going to install cargo stylus and like here's the commands for it and then you're going to get some free Tesla ether from this faucet um so you won't have to like buy anything you can just do it um okay so yeah you'll just go through this repo um we hope to later that you know if this will be of educational value just looking at this code together but um let's do a little Deep dive together with it so I'm going to open it up in my editor um you're going to find that when you look at this nft project there is this Source directory and various files in it there's only a few the first one which will be of particular interest to anybody who's actually you know done a lot of work with solidity before um is this 721.rs so dot RS is the extension for rust and you'll actually see as we scroll through this together that what it means to write an ERC 721 in stylus using rust is actually quite similar to what it means in solidity you'll find that every program begins with a set of storage definitions and you'll notice that using the stylus SDK that is the special rust library that off chain Labs provides you're actually able to declare the various pieces of State using solidity syntax so you'll notice here that this ERC 721 this is the object that implements that actually implements the interface that um provides that nft standard you'll notice that it has a few pieces of state it has a mapping of un256s to addresses those are owners of various token IDs it's got approvals and balances and all of that good stuff right all the stuff you would expect from that standard so from the perspective of somebody actually using um stylus you just declare the various pieces of state that you want oftentimes by just copying and pasting whatever solidity definitions that you like into your code and you use this special macro called Soul storage for it um definitely feel free to read the code in further detail later to um you know really grock like all the ins and outs of it but you're just going to notice just from a high level like looking at this together we've got events that are declared much the same way as in salinity we've got errors um and all of that good stuff and what's kind of cool about rust is the way the methods work um is similar so there's this uh there's this external macro that we often apply and that actually allows you to declare each of the methods of your um of your smart contract and have them be exposed to the world in the same way a solidity smart contracts methods are marked external so when you look at these rust definitions like for example this name or this symbol or this balance of right this will this should look eerily similar to what you would see in solidity right well here's the cool thing you can actually call it from solidity because under the hood the stylus SDK uses the same solidity ABI every contract written in Rust really just like looks like a solidity contract and I say rest here but of course the stylus SDK has other languages too and so what that means actually by the way is that without any modifications to a DEX say like uni swapper or pick your favorite right um You can actually deploy and list rust smart contracts like rust tokens rust nifties whatever um to these standard protocols without those protocols having to have any kinds of modifications so you just write your code in whatever language you like and then from the perspective of the chain everything just looks like solidity um and you know different methods do different things like transferring the nft around or approving other accounts to be able to spin the nft on your behalf that's a pretty dangerous thing actually um as numerous exploits have shown but the really cool part about this is like this standard this erc721 standard that you've that you're very familiar with if you've ever worked with nfts before can be replicated in different languages and all that really looks like is just providing definitions for each of those mechanisms uh from each of those methods in in roster in whatever language we're thinking of and you'll actually notice that the stylus SDK that um you know you can download on that page has all of the affordances you would expect to have in an evm environment so for example you're actually able to emit logs and you're able to well you can't really do this as easily insulated but you can print to the console and stuff there's all sorts of really nice things that you're able to do like inspect your message sender or transfer value around how payable methods a lot of great stuff so I strongly recommend reading the um ERC 721 file under the source directory for a bit of a nice parallel side by side but there's actually more that we're able to do because this is rust so um if you actually go to the art.rs file you're going to see that together we're going to make some nfts here so because this is rust which has like really nice low level types and stuff um you can actually do things like import an existing image library and create pngs from your application on chain that's right the nft that we're going to be making today is entirely on chain so there's no like hash that it stores and some other website that's trusted presents the data or whatever all of this stuff happens on chain is fully generative and um you know just you know has all that censorship resistance all those great properties that you would have of a fully on-chain product and what's cool about it is instead of having to like say in solidity right like do a bunch of like low level assembly code you just use familiar programming paradigms and like nice high level constructs when when working on your stuff okay so from the purpose of today's tutorial and you'll see this also on the GitHub repo there is a function that we're going to modify together to create new nfts so there's this generate nft function and it takes us an input the token ID so that's like which you know every every new nft has a unique identifier and that identifier you can use to generate different kinds of Art and you'll see that in this example project you're able to generate an image and then make modifications to that image in a procedural manner so here we're drawing a gradient we draw a line on it then we draw another line that goes from this point to that point we draw an ellipse and let me just actually show you what that looks like so what's really cool about this is because this is rust and you have access to the console we can actually print our nft out so take a look at this together those steps that I showed earlier we have well from the top left corner to the bottom right we have the gradient Line This was that this was that draw gradient and then we have various lines to to make the eyeballs in this picture and then the ellipse to make the smiley face and what's so cool about this is it shows that the future of actually like working on blockchain applications is one where all the familiar tooling that you already have is much more easily accessible right like if we're able to have languages like Russ we get test cases from rust and stuff like that and I'm actually using that to generate different versions of this nft you'll notice that in the code right now the only way it varies is the color it uses for the smiley face but hey you can notice here that with different identifiers I can each time print this out so what's really cool about that too is it means that like debugging and working on your nft is actually really straightforward because um you actually have access to like a really nice console where you can like do all of the things that you'd expect in tracing and all of that good stuff so um you know you can just use a simple command which you'll see in the GitHub repo to print your nft out and by the way uh here's actually a website that's showing them um I change the internet's kind of a problem but I'll get it to reconnect but um the the cool thing here is like when you guys using that GitHub repo to pull your nft it's going to show up on this webpage so if someone wants to draw something and edit that code together you're going to be able to actually change what's on this screen so that's kind of fun and how this is literally working is it's just reading the blockchain looking for any deployments of this contract um let me actually show you what it looks like to deploy it okay so um assuming I have internet we're going to make sure you actually have this really cool tool called cargo stylus that we use um the installation instructions are there but it's it's really simple it's just a single line but what cardio stylus lets you do is um well you can check and deploy transactions so um you know when you're programming your code it's often good to like run test cases to make sure things work and with this cargo stylus check tool does is it will actually go and make sure that if we were to go to play this to the blockchain that everything works and um if you go and you've you know made some changes to your nft you can run um actually I need a private key file so let me uh find that previously in my history you can actually run cargo stylus deploy and then point that to a private key somewhere and it will then go and deploy and then activate your nft on chain and what's so cool about that is in just like one little step right using these very familiar rust tools cargo is like the standard rest tool um you know we're able to with stylus have just some minor some minor extensions and then suddenly bam you've got all this great on chain goodness so um I think our goal today is this now that we've kind of like taken a look at various um you know we've looked together uh how the code works I invite everybody to take a big deeper dive or whatever um let's actually edit these nfts together so um you know we can just take this function this generate nft function and make whatever modifications we want so for example maybe I want to add another line right um all you got to do to do that is in your editor um you know you add another line that says line and then pick another position to start drawing from so maybe I'll start from the coordinate 810 and draw all the way to the coordinate I guess um 15 16. that sounds fun that'll be nice and diagonal and you can pick what color you want to draw it in right so maybe I'll pick another maybe we'll pick another hex color um I like Let's do let's do actually a nice stylus pink so the stylus hex code is e306 6E so if that should draw a pink line if uh if our reasoning was right and so well let's just test it let's just make sure nft works and hey there it is there's our pink line right and what's so fun about that is there's this like really nice like iterative feedback you can do when you're working and you're working rust um like don't even wrong in the solidity world we have hard hat we have well truffles unsetted but like you have all this like great stuff right and um you know you'll actually find that like printing to the console or doing things like that or kind of high friction you know it's it's not really super well supported um and you have to use these kind of awkward function signatures well in in this Paradigm um you can just like do all the familiar stuff that you're used to and by the way that includes with like uh debugging and all the familiar tools we're soon to release the ability to do GDB on the blockchain so you'll be able to like when you're in the middle of your smart contract if something goes wrong you might want to like go jump in and see what exactly went wrong well you can then fire up ddb this is a very common debugger that people use in rest or C Etc and just go see for yourself um what exactly went wrong and step through it and use all that without having to um like learn new to learn new tooling and stuff okay so um I think since we've looked at this together let's uh I'm I'm just we're just gonna go around the room and um help anyone who wants to set up an nft do so and uh yeah get these deployed and shown up here so that's part one of the talk Let's do let's collaborate
Automatic transcript — names and jargon may be misspelled.