New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Offchain Labs 🛠️ Arbitrum Smart Contracts in Rust

ETHGlobalWed, Oct 9, 2024, 12:00 AM

Join us for an Offchain Labs workshop! From November 17-19 2023, ETHGlobal welcomed developers from around the globe to take part in shaping the future of Ethereum at ETHGlobal Istanbul. Over $600,000 USD in prizes were given away at the prestige Istanbul Congress Center... thanks for an amazing weekend hackers! ----------------- Follow us: https://x.com/ETHGlobal?s=20 Check us out: https://ethglobal.com/ Email us at: hello@ethglobal.com

Transcript

[Music] today we'll be talking about arbitrum stylus which is a new project that uh what we call evm plus we are supercharging evm development by allowing developers to write equivalent smart contracts in Rust C C++ and a lot of other languages that support web assembly uh you can even add support for your own language uh and in we'll walk you as well on a workshop on how to actually get started and how to get your first stylus contract deployed and uh and just basically see how easy it is to get something that is just evm you know interoperable and and just works out of the box so without further Ado uh you know we know that rollups are awesome and they settle basically their state to ethereum uh users interact with them transactions get packaged up and rolled up into a batch and get posted back to L1 and rups have different implementations of how they actually choose to package this information and how what they actually prove um over regarding their state transition function arbitrum today uh you know works by essentially taking a taking the ethereum state transition function adding a few modifications and converting that into web assembly B code which then gets proven on ethereum so at the end of the day what we do is that we actually you know we actually prove that every transaction as it was executed on arbitrum gets confirmed on ethereum and can be and if there's a fault in a single up code execution we can prove that there was a flaw right there and why do we choose web assembly for this so web assembly was a choice for arbitrum because it's a very simple binary format that has an extensive Community behind it uh so many languages also compil to wasum and it's a big part of the web standards committees uh we also argue that it's excellent for fraud proving on l2s based on the way it works um you know being a stack-based machine uh providing op codes that can be constrained in certain ways gives us really nice properties that have that have power arbitrum for many for for quite a long time and of course you know JavaScript has always been the deao language of the web uh with many other flavors coming in over the years uh but most importantly that has that narrative has begun to shift to a certain extent with web assembly being introduced today in production you can run applications that are written in Rust C and C++ and run them in perhaps the most adversarial environment that exists out there today which is web browsers uh web browsers today are battle testing production applications that are running web assembly code and you know with with significant results and speed to boot uh we see applications such as figma we see applications that rely on heavy rendering and processing that are actually using rust underneath the hood and running in the browser as web assembly so we asked ourselves the question hey can we do more than just execute evm smart contracts on arbitrum well it turns out that you know with l2s you can prove General computation that is of course deterministic I should add um and as arbitrum state gets settled to ethereum using fraud proofs and we're already using web assembly why not just extend that to support user provided web assembly as well and a quote that we like to mention about why how impactful web assembly is is Solomon hikes actually the creator of Docker said in 2008 that if wasum as a technology along with wasi which is a standard library for wasm had existed back in 2008 they wouldn't have been a they wouldn't have needed to create Docker that's how important it is and that's how important wasm on the server is for the future of computing all right so introducing arbitrum stylus Stylus is a nextg programming environment for arbitrum chains you can write smart contracts in many different languages but that's not only the main selling point Main selling point is that we don't have to make a compromise or a choice as a developer between evm or wasm instead one chain multiple languages one chain multiple VMS it's fully composable it's not a new L2 and we're calling this evm plus we have one single state transition um we use the same state tree as ethereum in the same database and we combine two VMS being the evm and interpreted evm and web assembly runtime using a waser server backend and we combine the two there's nothing else like it some of the numbers so Stylus is significantly faster um it's not just a it's not just like hey you can use a different language and solidity but actually we can Benchmark 73x uh compute performance against catac this translates directly into saving and slashing user fees it also enables things that just weren't previously possible um such as you know Advanced cryptography that would otherwise overflow the gas limit on evm chains uh gaming Ai and L1 pre-compile research instead of waiting for many years for ethereum to ship a new pre-compile you can program it in Rust and ship it on arbitrum not only that but we can estimate the amount of developers that exist in other languages that can now come into developing on an evm chain thanks to the availability what's really special about Stylus is that for the most part you can write pure rust so we're not we're not we're not introducing our own required DSL where we say like hey no it's like a weird flavor of rust that you know has to work in a certain way um no we provide an SDK but anyone can build their own if you want to interact with the evm or do things that are evm related you can say give me the block number give me the block Tim stamp you have this low-level kind of CIS call like API to get things done you can call contracts you can interact with storage and you know just as the JavaScript World developed a huge flavor of Frameworks you can build your own um if you don't like the stylus SDK that we provide Nothing is Stopping You it's not enshrined into the protocol at the very lowest level stylus contracts actually are just btes in and bites out so if you want to write basically a pure computation on the blockchain that just takes in a byes input and outputs a byes output you can do that and it's a simple as doing this you want to add support for new programming languages so Zig is all the rage these days being a spiritual successor to see um almost any language that doesn't have a runtime can you know can can be can be used with stylus U adding support for Zig for example took me around 16 lines of code and I'm running Zig smart contracts today on stylus as a team we provide first class support for rust and c and we have sdks that are available to use for hackers here um and we'll do also a walkr on how to program some of these test net is live so the test net is basically uh is a chain that branches off arbitrum sepolia and uh you can Bridge into it we have a quick start guide that guides you through the process uh it's as simple as getting testnet ether bridging it over takes a few minutes and after that you can you can go free to go wild with just a simple amount of test net eth you can do an insane amount of things compute deployments on stylus just because it is that cheap compared to running evm related code even on the test net of course question is when um test net is out main net sooner than you think for it to be deployed on arbitrum one in Nova uh it would have to go through the Dow for approval and we are currently working on both an audit and getting in basically getting to that point um we have all the evm contract interoperability so you can have a solidity smart contract that's talking to a rust smart contract via an ABI interface and they don't even need to know anything about each other right that's all abstracted underneath the hood and you have endless possibilities right zip hash and C for example here is implemented deployed on stylus extremely cheap to execute um to give some numbers in the time the evm can do seven ad instructions we can do a full round of hash and we can do it a lot cheaper uh memory wise for memory operations stylus can be 100 to 500x cheaper on memory than evm related operations simply because the memory model is that inefficient in the evm and yes uh so you can contact us here uh but before you know but before we wrap this up uh I'm going to walk you all through a live demo of how to get started okay all right so the first thing is we provide a tool called cargo stylus which if folks here have used rust cargo is your go-to command for doing everything in the rust ecosystem and having this plugin for cargo is very simple comes with four commands you can initialize a new project you can check that it compiles and will Deploy on chain you can actually deploy it on chain and the coolest part you can export ABI so you can write your contract in Rust and Export a solidity ABI that then your other contracts in solidity can call into and do a lot of cool things and we'll talk about how you can do that so cargo stylist that's the first thing you're going to install going to do cargo install cargo stylus and I'll save you all the long rust compile times of course here um but once you get that installed you'll have access to all the tools you need so you have all the different abis all the different commands here and there are a few others that weren't included above so you can even replay transactions in a debugger which is really cool you can use traditional debugging tools uh with stylus contracts because you're not limited by the evm you can trace transactions and and Trace exactly kind of certain things that occurred in the execution giving you a lot of interesting Powers as a developer the next thing that you want to be aware of is the stylus hello world repo so the hello world repo is our minimal it's not the most minimal entry point but it gives you a solidity equivalent counter implementation so basically you have a counter contract you can increment you can set the number and fetch the number and we're going to be implementing this today cool so let's get started so I clone the stylish hello world repo here on the left comes to you with a few different things um it comes to you with the main. RS it comes to you the lip. RS and interesting things you can do and we're going to actually implement it from scratch so inside of this uh inside of the this example. RS what I'm going to do is I'm going to be importing the style SDK I'm going to import the Prelude which includes pretty much everything you're going to need here and I'm also going to I'm also going to include the uh let's see I'm going to include some primitive types such as uh u u uh or just all the alloy Primitives so we use the alloy Library which is being created by the ethers RS team and the Alo library has extremely optimized data structures and Primitives for use in ethereum applications it's really cool because they have they have they have uh awesome features just constantly generics uh they're super fast and uh they work out with stylist right out of the box for folks that are wondering if you need to depl De develop smart contracts uh with the standard library or not both work you can use the rust standard library or you can choose to deploy contracts without the standard library and both will function with stylus so the first thing we can do is I could program this basically um you know in pure rust by declaring strs and declaring all these things but for solidity developers the SDK provides an easy way to kind of uh accommodate and and get more used to the rust syntax so we provide a really uh an a macro called Soul storage in this in this uh storage you can Define solidity strs so you can type solidity syntax in here so in this case um I'm going to be doing a I'm going to be doing a struck called counter and I'm going to be adding a value called number which is a un 256 I'm going to also be marking this as an entry point for my application cool so this entry point means that hey this struct is basically the entry point for transactions that are coming in to this program and I need to then Implement a few methods for this to occur so I'm going to be implementing the counter struct and I'm going to mark it as being basically the implementation of the solidity storage struct next I want a few methods on this so to as a hello world example we're going to we need to be able to modify the number value that is contained inside the struct underneath the hood this is all using the ethereum storage model the solidity storage model specifically um and using the ethereum state tree so you have access to all those goodies that you're familiar with so we're going to be implementing a method here on this counter struct it's going to be called a set number and it's going to take a mutable reference to self so we're able to leverage also Russ uh Russ memory safety here um and common patterns in developing applications I'm going to take in a number it's going to be a un 256 from the Alo library and it's going to return a result um cool so it's going to return a result where the first return type the success case can be anything in this case it's going to be void it's going to be the unit type so I'm not going to return anything if I set a if I set a transaction and it can return an error which is raw bytes so if if you want to return a custom error you can return it as raw bytes uh and the caller can can then interpret it as they want cool so in this case I have an n and I want to be able to update the number value here so I'm going to do self. number and there's and and numbers in the SDK uh values that are SDK values have you have access to a lot of very powerful uh methods on them you can do load which loads it from Storage so essentially does an s store the cool thing is that Stylus is optimized to not do an s store every single time you load instead we use a caching model where if you load something once it'll be cached until you then flush to storage so you get a lot more control over storage access patterns in your contracts compared to doing something in solidity without some without some you know some underlying changes that you would need to do in this case the number has a set method that I can then modify and this will actually modify it in solidity storage inside of arbitrum the set method will return I believe a result um or let's see here no it just it just it just sets it and that's it so that's basically it for our mutating function um we also want a getter for our number so we're going to be returning a result type that's u56 and it's also uh the error case is just a bunch of bytes and in this case I'm going to do self.

number. getet cool I probably also want one more method which is increment so basically take the current number that's in storage add one to it and just and just return so it's going to take a mutable reference to it's going to take no arguments and it's also not going to return anything it's just going to increment the number so what I'm going to do here is I will do let mute num equals self. number. getet and I'm going to do self. number.

set num plus u 256 from one and this should return a result by itself I believe uh let's see actually no it does not so I think we're safe okay cool so we have our we have our getter we have our Setter we have our increment function uh let's see what we can do with this so the stylus hello world repo already comes with this EX example pre-filled out so I was just writing it here from scratch uh but in the actual in the actual uh in the actual Library you'll have this already implemented for you so we have access to it right away so you have the increment the set method and also the entry point let's see computer is pretty slow for some reason um uh is there a does anyone have an adapter for us oh thanks so much thank you so much yeah yeah yeah all right thank you guys for bearing with us here perfect all set thank you okay so back to it um so here we have a few cool we have we have some comments here added on uh you can also declare declare a custom Global allocator for your application so you don't need to use uh you know the normal allocator there are some allocators in Rust that are optimized for web assembly so we do this here uh by default but you can choose to do things however you wish so now that we have our we have our contra implemented what do we actually do with it how do we actually get it on chain how do we actually do interesting things with it so with the cargo stylist tool you can check that it's going to verify on chain oh it seems here I dedic I duplicated my ex I I implemented the example twice so cool so you can see here that if I do cargo stylist check what it's going to do is it's going to try to compile my code to WM it's going to tell me how big it's going to be on chain and it's going to try to deploy it to the test net without without actually sending a mutating call so it's going to check that it's going to succeed the checks if that works then you're good to go so it looks like my application succeeded the checks what I'm going to do next is I'm going to do cargo stylus deploy okay cargo stylist deploy awesome so I'm going to pass in my private key path uh to the test net and I'm going to be deploying it so stylist programs have two steps for deployment so the first one is just getting the code onchain so getting the code on chain is the first step the next step is actually doing an onchain activation so you have to basically activate the wasam code to be usable on chain this is an optimization that we do uh so if somebody already activated the same code as you um you know the stylus uh the protocol will use that already activated wasam code on chain and uh it'll just work so it's a way for us of D duplicating execution um and I basically deploy my contract it's live that's awesome it's deployed to this address okay let's save it here and we're going to be exporting this this uh exporting this address as an mvar right here I'm going to use it a little bit later so now that our contract is on chain it's on the test net um I want to be able to interact with it I want to call it I want to I want to talk to it right so uh using the cargo stylus tool once again you can do cargo stylus export ABI see what happens there there we go so your rust smart contract here's a solidity interface for it and this means that from any other solidity contract you can make a call to it you can you can transact with it using your favorite tools so we have the ABI which is really cool um let's actually try to interact with it so let's make a transaction how can we do that well you can use any kind of ethereum tooling that you're familiar with in this example we love maintaining our same development stack as rust so we have an example here in Rust that shows you how to call your contract using the ether's library which is a very popular library for ethereum development and rust um here I declared the ABI for my contract and um and basically I have methods to be able to then interact with it using ethers ABI gen so it's really cool um let's see here looks like I'm lagging a little bit but you can run this example you can do cargo run example and actually let me see yeah we're going to do cargo run example counter what it's going to do is it's going to run it's actually going to use the ether's library and rust to interact with my contract so the ether's library made a eth call and the counter value is zero and I'm going to try to increment it so it's sending a transaction it incremented the new counter value is one if I interact with it again on the test net the value is one I'm incrementing it again the value is going to become two cool so we have a new counter value of two um so this is just using standard ethereum rust tooling to interact with your program that you wrote in Rust uh via solidity interface which is really powerful so aside from this toy example what other cool things can you do so we have the stylus SDK which is written in Rust and recall that this is just a nice thing that we provide but you don't need to use it you can write your own framework on on top of this because once again the API to the evm is so low level that you can choose to implement it any way that suits your needs the SDK is meant to provide an experience that is easier for solidity developers to become more familiar with it and has access to already all kinds of things for you including storage layouts um and includes an example erc20 token in Rust that exports the correct solidity interface for it uh you can declare events as well using our macros uh you can declare errors as well in Rust as enums and get all the power that rust enums have to offer and essentially just go wild um you can also execute this and we have examples on how to how to interact with it okay aside from that there's an awesome stylus repo on on GitHub where you can see some of the projects that have already been built along with some guides and useful information so Unis swap V2 for example with the same ABI interface written in Rust um we have cryptography examples so most recently p256 verification uh written in C we have BLS uh signature verification written in C as well and extremely fast and executing cheaply on stylus um so you can do all kinds of crazy things you can verify BLS signature um and also nfts uh projects that we are interested to see are more ethereum standards ported over into stylus um and seeing things such as proxy patterns um seeing uh seeing just other examples of crazy ideas that folks might have you can even take existing rust crates and just deploy them almost right away in vanilla and turn them into smart contracts just add an entry point um add a way to get data or set data and there you go and uh let's see so yeah docs are available of course on the typical rust tooling so docs. RS for the stylus SDK you'll find information on all our different modules including uh you know VM affordances message sender uh storage you know TX origin like everything you might want cross contract calls you know uh you can do anything and uh you can navigate to arbitrum doio stylus uh to just learn more and have access to all this information um we are offering a $4,000 Bounty specifically for stylus at the hackathon um and we'll be active on our Discord so uh please join in into the arbitrum Discord if you have any stylist questions and uh let's see and once again uh my information is here so uh the lead developer Rachel who is not with us today but I'm Raul Jordan you can find me on telegram at Cipher Punk or R Jordan offchain labs.com and yeah we're just really excited to see what uh what folks might build so thank [Applause] you all right question yeah oh yes yes yes sure so Rachel has the sources on this information um but I believe we did some Gathering of the number the estimated number of solidity developers versus rust devs versus C++ devs yeah I should have I should have prefaced that U but yeah we estimated around 20K solidity devs uh 3 million rust devs 12 million C++ yeah yeah great question so of course um wasm Mar contracts uh you know have different gas pricing than the evm model at the end of the day when you're interacting with the russ smart contract using traditional tooling you'll be pay you'll be paying gas the same as ethereum gas right uh but underneath the hood the pricing there's a translation between evm gas and stylus gas we call stylus gas Inc actually so there's there's a ratio of of of gas to Ink um that we price accordingly based on how we think how much cheaper is a wasum addition versus a interpreted evm Edition and we price things accordingly like that um so yeah basically have a different VM yes we're using the waser VM uh and there and you know contracts are still stored in the state tree for ethereum um they interact via the same you know they run in the same in the state transition function and they are also fraud proven like uh ethereum transactions yeah so example you used uh example to prepare a solidity interface yes and can you provide one or two examples how we can use this interface sure sure so the example that I gave here in the um in the stylus hello world repo uh here is the I'm using the ether's library in Rust uh and the ether's library in Rust um allows you to basically you can paste in an interface like a solidity interface for something and use the ethers library to interact with it so here I pasted in the interface from the contract that we just wrote which here's a solidity interface and I can call it I get rust bindings to it I can do I can do number set number increment um You can call it from other solidity smart contracts you can call it using cast or using a metamask transaction uh just like you would talk to any ethereum smart contract you can talk to your Russ smart contract yeah all right any other other questions so you can use third part R modules you can you can you can use rust crates and uh you know go wild try it out those that support the standard Library those that don't uh right now we don't support floating point but that will be coming in the next few months it's a little bit difficult but we're working on it uh so anything anything that's deterministic is fair game yeah okay thank you all thank you sh

Automatic transcript — names and jargon may be misspelled.