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

Loading player…

Zigitrum: Fast and Simple Smart Contracts in Zig | Chris Cordle - Offchain Labs

Ethereum DenverThu, Oct 9, 2025, 12:00 AM

Speaker

Chris Cordle

Zig is known for being a simple, fast, and compact programming language often touted as a "better C". Here, we introduce a Zig smart contract framework for Arbitrum chains, powered by Stylus. Learn how to build smarter, smaller apps with Zig in a single afternoon! 🚀 Get Ready for ETHDenver 2025! 🚀 We're already hard at work preparing for next year's biggest Web3 event! Keep your eyes peeled for more info on ETHDenver 2025—it’s going to be epic! 🌟

Transcript

all right thanks everyone for joining us for this next talk uh pretty cool one especially if you're interested in alternative languages for writing your smart contracts it is zigat fast and simple smart contracts in Zig and it's going to be presented to us uh by Chris who's an Integrations engineer at offchain lab so let's give it up for Chris everyone thank you thank you thank you all right so uh my name is Chris by the way that's my socials Chrisco 512 I'm an integration engineer at offchain labs and I'm here to just explain to you how to write contracts that Target arbitrum in Zig and um what is Zig Zig is like a new word language it was uh created in 2016 which is for programming languages it takes a while to get maturity and so it's still considered a a young language but it's still it's quite robust 9 years in um but it is a general purpose language but uh it is suitable for smart contract Dev because it focuses on Simplicity explicitness and no hidden control flow which is really good in smart contracts because a lot of times you're really concerned with security and how your state flows and and what state changes you're making and making sure that everything's like predictable and testable so these are the things like Zig is really good for oh and that that's the ziguana that's the uh the official logo there uh so the main goals of Zig is to sort of like be you it's sometimes uh um it's it's sometimes called like a replacement for se but it's goal is to be like a simpler safer more maintainable SE so it's supposed to be more accessible and easier for beginners to learn and it provides a lot of utilities that make it easier to use in like C or C++ um at the same time it has like seamless interupt with c and C++ because it has like a built-in C compiler into the zig compiler so you can easily like import C libraries and incorporate them like cryptography libraries that been around for 10 15 20 years and see and pull them in very easily and you could just pull the source code in and compile with the zig compiler um it also has Fine gun control over memory and performance and um that can be useful in some smart contract uh context but basically like unlike say Russ where the memory sort of manag for you this does allow you to um control the memory more so but it at the same time it provides easier abstractions than C and C++ do for doing that so this is like just a basic hello world as you can see it's like really simple it's C like uh you got a public function main returns a void it's assigning a string it's using the debug print to print Hello World um pretty straightforward um so so yeah again to kind of go over some of the architecture benefits is like one of the things that it has is no hidden allocations memory management is explicit so you um you do use allocators to allocate memory but um there's some cool uh just simple abstractions for working with it that makes it less intimidating than like using Malik and C or whatever uh you might use in other languages to do so um it also has this concept of comp time for metaprogramming which which is really nice because um you're just programming in Zig and at compile time it will uh evaluate that expression and then sort of inline the result of that expression into your code so it's sort of easier to work with than macros and it's uh a powerful concept for doing meta programming which can be useful in smart contracts because a lot of times it helps to do things at comp time um if you can inline something and void the you know binary Belo or whatever it might be uh so it's a use very useful primitive and um and I use it in the the framework that'll go over in a minute um yeah it's a procedural like sytax it's got uh yeah built-in compilation cross compile from your machine to any Target you can compile from arm to x86 x86 to arm wum is a first class Target as well which makes it suitable for arbitrum stylus uh wum VM and uh yeah as I mentioned the C and C++ compiler built in so you can easily integrate C libraries in your code um yeah again reiterating uh there's no hidden control flow type casting is explicit um there's only safe and unambiguous type conversion allowed the compiler will um catch anything that's uh unsafe um as far as casting goes um there's no operator overloading you can't overload the plus sign to do something arbitrary um it that's just going to do regular energer addition there's no way to overload that behavior um so that's kind of nice because you're avoiding like unintended side effects which again in smart contracts you don't want unintended side effects anytime um yeah so kind of the yeah I think it's like this is sort of the pitch of Zig it's simpler to learn than rust so rust is great it's memory safe it's a beautiful language um it but it does have some complexities with dealing with the borrow Checker and dealing with lifetimes some of the generics are a little hard to grock uh for beginner programmers um so it you know it's simpler than Zig but it's it's as fast as C or sorry simpler than rust but it's fast as C so easier learning curve um yeah uh yeah this is just going over like some of the features of like how to integrate C or C++ uh libraries in your code but I'm going to skip over this part real quick um again like even though Zig is not as safe as rust it does have a lot of memory safety features built in and it gives you more gu uh guard rails than C or C++ does um it it does there's no garbage collector so the memory is managed with explicit allocation and a lot of times Zig libraries are functions such that you can pass in your own allocator you can bring your own allocator for your custom use case and as long as it uh matches a certain U uh API then then it will work just fine you just drop in allocators like the debug allocator for example when you're running tests a lot of times that's what you're going to pass in uh to your functions and it will catch use after free or double free errors however when you finally compile your code um you can strip out those checks there you know so that there's no runtime checks in your code to like make your binary real small and uh more performant so yeah the safety checks can be stripped out very easily but you can still catch them in testing um with that test allocator um yeah it's less strict than r you're not going to you know like it's rust is very strict about memory safety but it's sort of like a middle ground between C and rust um which I it might be a sweet spot for some people so um that's that's one of the interesting things about it um so Zig is growing very fast it's a relatively young language but it's it's got exponential growth um a lot of people are are picking up on it cuz it's U again like a simpler SE gives you um you know more performance than a go or Java would give you but U maybe easier to pick up than um rust for some devs um these are some of the products that are currently built with Zig uh the most popular one might be bun which is a very popular JS and typescript runtime um that's all written in in bun and in Zig has been around for a few years um tiger beetle which is a very high performance financial transaction database and ghosty which is uh a very good terminal emulator uh so why Zig for smart contracts why is it a good language for smart contract Dev well it's a again a simple procedural language so the control flow is very predictable it's easy to follow the high performance is there and it produces very compact binary sizes which remember on on like ethereum based blockchains we've got a limit of 24 kilobytes for how big a smart contract can be and that's not a lot of space in the modern era um and sometimes uh compilers take uh um that for granted and they don't like U you know with certain languages they can produce large binaries and that can be a problem for this limit environment that we have in um the blockchain space so it's also a very fast compiler so um it compiles in subc um speeds it's very very good so it's easy to work with um it also has out of the box support for arbitrary Precision integers which is fantastic up to 65,000 bits which is huge uh but you know like solidity works for 256bit integers which is weird for a lot of languages a lot of languages only support up to 64 bits maybe 128 bits but Zig supports 65,000 bits out of the box so you got like arbitrary math on large integers no problem and there's no external libraries needed to support that so that's that's a really nice feature of the language that supports uh such huge numbers um yeah uh yeah it's got arbitrary it's got cryptography built in the standard Library very useful like sha 3 and Kack 256 uh which is useful of course for matching solidity ABI specs which uses katak a lot uh yeah so the idea behind this framework that I'm going to show you which is just sort of like an early stage demo of a zig framework for opum Stylus is um that it has zero dependencies it just runs on the standard Library it's using a r a router based architecture almost like maybe a like a web server like an expressjs or something like that um and it supports middleware so you can just drop in middleware um to run before your routes um and and that's useful for things like oth or checking ownership and things like that in front of your handlers um also the goal is to like make it super easy and learnable and make it very testable so trying to stick with mostly functions uh composing other functions to make everything like really testable and and um uh predictable okay so let's go to the code I'm going to exit and go to my terminal and um let me zoom in because and we're going to open Vim okay oh yeah I'm going to go to the example I'm just going to cut right to the counter demo so I'm going to demonstrate how to how to work with um the counter um so this is kind of the framework and is it big enough let's see let's zoom in more here okay um yeah so we just import the the wax library and we set up a context which is going to contain all the VM variables uh including like block number message sender all these things that are just sort of global variables and solidity they're just going to be attached to this context object so that's what we're setting up here we're also setting up a router and we're passing in the data struct which is going to describe like the layout of our data on our smart contract so we've got a simple count with a uh 256bit integer and we're wiring up two routes count and increment and right here like this these are three parameters this is basically describing the text name of the route uh that can be anything arbitrary to like match the solidity ABI selector spec and then you can pass in middleware here in the middle which I'll show in a in a minute but for now let's just show the quick demo with count and increment and then here's the function definitions right here um this is just a basic in increment it receives a pointer to the context and then it's grabbing from the store on the context the store gets attached to it and then use basic Getters to get the value then you set it uh to increment that value and then uh this is a basic getter to get the count this little exclamation point is just like uh error Union so it's just saying that an error could be returned from this function um yeah so that's just how you you set up your return data and then this is just sort of boiler plate this is the user entry point this is where um stylus's VM actually calls into to execute the contract and um it passes in a link so this is just sort of like boiler plate that you would copy paste into any contract you'd build um but notice how it is basically we we call this dnit uh on the context right here with this defer keyword that's how Zig manages memory uh freeing so you just you call defer and basically that's going to run at the end of the block so it's like memor is being allocated whenever you're calling context. init and you're going to defer to the end of the block that it's going to call DN it to free that memory um at the end of the block so that's just how Zig makes it easier than just remembering to do Malik and and manage that um that defer keyword is really handy for that and then we're just passing in the routes in the context of the router and then to demonstrate this um we're going to go ahead and let's CD into that examples counter folder we're going to Zig build it and then we're going to CD to the zig out bin and that's got a little main.m file in it and right now I've got like the arbitrum stylus um Dev node running uh in my local machine um which we have a link to on our docs like an arbitrum docs. docs. arbitrum Doo um you can learn how to set up uh arbitrum Nitro Dev node to run smart contracts on your local machine that's a port stylus um it's very simple to get going as long as you have Docker and then um so we're going to go ahead and deploy that using we're going to use cargo stylus so this is a rust utility um that you will need to download in order to deploy but it's going to um I've got this all filled out already with my local variables for my uh local endpoint private key it sets up a private key whenever you run the the local Nitro node um that you can just use it it funds it with like thousands of e and you can just use it to deploy contracts locally um but that's all on our doc iio if you got any questions about that but we're just going to pass in this wasm file this is how this is this is the same tool you use to deploy rust contracts on stylus but you can also deploy any arbitrary wasm um and so we're just passing in that main.

m there and it's going to run and it's going to give us a couple of uh data points so the the contract came out to 1.4 kilobytes compressed and this is the contract address so if we want to call it I'm going to set a quick local variable here and then I'm going to use foundaries uh cast tool to just kind of invoke um invoke increment here and that that incremented the counter and I think if I just call cast call it should um return one if I call it increment again call again it's two okay so the counter is working perfect it deployed fine it was nice and Compact and um really sort of simple and straightforward to do okay so let's like go over just a slightly more advanced use case we got like four minutes to go but um just kind of show like sort of the direction the the framework's taken with this like um middleware pattern is like this is just like a modified example of what we just saw where it's still a counter but it's an ownable counter so now it has an owner address and it's got this claim ownership function which we're just using to like um the contract is going to be initialized with an owner of zero address and then the first person to call claim ownership this is not how you would do it in production but just for this basic demo we're going to um we're going to just allow anybody to claim but it's running on our local so it doesn't really matter um and then once call and claim ownership will basically make you owner and then we're going to guard this increment function with this only owner middleware and the only owner middleware just looks like this it gets a context pointer and a next function pointer and the next function pointer is what you use to invoke the next function in the chain you can chain any number of middleware together and it's just checking the current sender versus the current owner and then it reverts if it's not the right owner um and then this is the claim ownership it'll revert if it's already been claimed but otherwise it just lets you claim ownership of the contract okay so with that in mind we're going to go ahead and build this one let's CD out into this example um and I think we're back here CD into counter owner and then we're going to Zig build this and then we're going to CD Zig out Ben cargo stylus or cargo yeah stylus deploy main. wasm okay this one's a little bigger 2.3 kilobytes so it's got those extra functions and and utilities included and we're going to go ahead and contract set this variable up here and then we're going to cast call or we're going to try to cast actually yeah the Count's currently zero and then if we cast send increment it should revert because it only allows the owner to call that so if we call the owner function real quick which we should expect an address back and the owner is just a quick getter and yeah right now the zero address owns it so we're going to go ahead and call the claim motorship function to claim ownership of the contract and now if we call own ER where that's the demo account that arbitrum Nitro sets up for you and then um now we should be able to call the increment function right so we should just be able to increment the counter now and get through that middleware check and that looked like it went through and if we call the count now expecting a un 256 back um we should be able to get that count and that's there we go so now we're able to just arbitrarily call increment as the owner and get that so we got a working demo with like very simple like middleware pattern that sort of like um maybe takes the place of modifiers and solidity but um it's it's it gives you a little bit more control over the flow because you control when the next function is called and you can even arbitrarily Define this in the middle of your middleware and it'll it'll bubble back up when when the Handler is completed so very cool interesting uh uh you know maybe new language to try on arbitrium and um yeah there's a lot of excitement about Zig so hope you can try it out um there is if you can you want to scan this QR code uh you can check out the repo um it's currently my personal repo but we'll probably have it on offchain Labs uh soon um and there's also a link to Doc arbitrum Doo where we can get even more details about the full arbitrum stack and we do have a a telegram stylist group um which is a lot of cool hackers in there that are working on both you know rust and um rust and various languages for arbit from stylus so it's like uh just at t.me arbitror stylist check that out and uh appreciate your time thanks for coming out [Applause]

Automatic transcript — names and jargon may be misspelled.