EN | On-chain chess en Rust con Arbitrum Stylus | MERGE Madrid 2025
MERGE Madrid Events·Sun, Nov 9, 2025, 12:00 AM
Transcript
All right. Hello everyone. Um welcome to this workshop on arbitum stylus. So we are going to talk today about um what is arbitum stylus and we're going to see an example of something that can can be achieved with the stylus that is not easily doable in in solidity or with the AVM. Right?
So first of all my name is Jose Franco. I'm a blockchain engineer at offchain labs. Um here are my Twitter and Telegram handles. And offchain labs are the initial builders of arbitum one and arbitrum Nova before they were transferred to the DAO. And we are also the developers of Stylus the the orbit stack for creating new chains um bold time boost and a whole lot of technologies that are available on arbitum chains.
So this workshop is divided into two into two parts. The first one is a bit more uh product oriented so to speak. Uh we're going to talk about what is stylus for those who who don't know what it is. And we're going to see an example of a a small um counter contract that is written in Rust. And then we are going to talk about uh um the main problem that we want to to talk about in this in this talk the the onchain chess engine.
Okay. What is the the challenge that we have to to solve and and we're going to see an an example implementation and a and a demo if we have time. All right. So what is a stylus? Stillus is an is an upgrade to the traditional um de smart contract development um environment available on EVM chains.
What that means? It means that if you want to write a smart contract in an EVM chain, you usually do it in solidity or in Viper. But those are like the two options that you have, right? So arbitum stylus allows you to write smart contracts in other languages like Rust, C, C++ and and many more languages. And this is thanks to a new virtual machine uh that is a wasome virtual machine executes was programs and lives alongside the EVM and it's fully interoperable with uh with each other.
That means that um solidity contracts can call stylus contracts and the other way around. Um, stylus give us um many benefits and some of them are like we have we can now access a broader uh development ecosystem and on board uh web to developers that means that for example for a Rust developer or a C++ developer if they wanted to write a smart contract or do something on the blockchain they had to learn a new language they had to learn solidity to do that. Well, that's not the case anymore. they can use the language they know and write smart contracts as powerful or even sometimes more powerful than than solidity contracts. You have access also to general purpose language tooling that like the crates repository if you're writing in Rust and um you can access new use cases like advanced cryptography, generative art um yeah and and complex computation like the example that we're going to see today here.
What tools do we have available for RA for Stylus. First of all is um we have a Rust SDK. This is the the the SDK that we've put more effort in um besides the the actual virtual machine of course. So we have the Ras SDK, we have proof of concept SDKs for C and C++ but actually you don't need these SDKs if you want to write a contract. You can do you can use any other language that compiles down to Wasom and write a contract.
We have examples of also of contracts written in assembly script in SIG and and I'm sure there are other contracts written in in in other languages. And of course the stylus is available in all arbitr chains and and new orbit chains that are created today, they have stylus um um available uh at the beginning. some resources that we have uh if you want to start coding in Styus. The first of all, the first one is the the quick start is a page that we have in the portal documentation in the documentation portal. It goes uh it shows you how to set up your environment to start writing contracts in Rust.
And then we have stylus by example which is uh which mimics solidity by example and it shows you like a lot of code snippets and examples of things you can do with the with the SDK. The documentation that we have today is mainly focused on Rasa smart contracts but we also have documentation in the in the portal for other languages. Okay. Okay, let me show you just a a small example of a contract that is written in Rust. So you know uh more or less what it looks like.
If you are familiar with Rust um you know that you can use a tool like cargo for example and you just do like cargo new whatever and you create a new project. So in this case we you can do the same you just use cargo to create the project and what we are seeing here is a snippet of the of the lib file that is created when you start a library project right so we have here that we are importing the the SDK and then we have the stall storage macro um this starts to get a bit more technical for those who are not technical focused um so we have the solid storage macro where we uh define the storage of our contract in this case we only have like one storage variable which is a UN 256 it's a number and that's inside a public strct that's called counter and this is what we are going to to to call our contract okay so once we have defined our storage we can start writing the functions to access and manipulate that storage right in this case we have three public functions that goes inside an impulse block for the same uh strct that we um defined earlier. We have three functions here number, set number and mold number and it just manipulates uh the storage variable that we have or just returns uh return it like in the in the first case. Okay, this as you can see is pure rust in here. We are not using solidity and something that I didn't mention before is that is that this storage this storage variable the syntax that we are using here is solidity but you can use RAS syntax also if you want.
And finally we have testing. Um when you write a RAS program you usually put the unit test inside the same file where the functionality exists. Well the same goes for stylus contracts. So you just put the the the the unit test in the same file and just verify that that all the functionality that is within that library or within that contract works as as expected. So let's move to the second part which is a bit more interesting.
Um are there any chess players here by the way? Any chess players? No one. One. Okay.
So we have one. So he he'll know what what I'm talking about. All right. So when we want to put um when we want to work on a on a chess engine uh just a chess engine without putting the putting it uh on chain we have like a complex problem to solve. So u for those who don't know chess has different different types of pieces and each piece moves in a certain way like for example we have the knight the that moves that hops over other pieces and moves in in an L-shaped movement.
We have the pawn who moves just one square forward unless it is the first move then it can move two twice or if it is capturing a piece it can move diagonally. So we have like a lot of different movements that the piece that the different pieces can do and we have to somehow um verify that every time a piece moves it's doing it in a in a legal way so to speak it is a legal move right um that means that for example um I don't know a pawn is not moving like three squares forward or if your king is in check you cannot like move any other piece you have to defend your king stuff like that Right? That's a lot of computation that you have to done in your contract in your program. Now if you want to put that on chain you have a limitation there because if you are doing a lot of computation that costs a lot of gas and uh when you're using for example solidity which is not um as performant as was for this kind of computation you run into a problem that you might reach the limit of the of the gas that you can spend and that doesn't work. So the problem that we've seen is that I haven't seen any solidity um complete chess engine.
Um that means uh a chess engine that is completely written in solidity. I've seen a few a few things but they are never complete and are usually for this uh for this because of this and with wasome we can do that because the the well the let's say that the engine that runs wasome programs is more prepared for this kind of computation. Okay. Um, I'm not going to go deep into this because this this is getting very technical, but in the end, this is like a smart solution to to solve the problem of how you calculate whether a move is legal or not. Okay.
Um, then so in this case, if you want to write a chess engine, you can just write everything yourself and do all the calculations, write all the contracts, all the code code yourself or you can use a library of from someone who has already solved this problem. And this is what we're going to try to do here. Uh there are certain things that you have to you have to keep in mind when you are choosing a library to to write your for your contract. One of them is that um uh you shouldn't use the the standard library that comes with Rust because it is too big and you're going to reach the the limits um the contract limit of chain. uh there cannot be uh parallelization uh no randomization either because we are on a deterministic environment deterministic and sequential uh environment which is the blockchain and for now you cannot use any floats but we are working we're trying to work around that that limitation okay so it turns out that we have a solution we have a crate that exists in in crates io that actually implements a full chess engine that is written in ra okay And we have uh we have integrated this engine inside a smart contract and this is a complete engine.
it it takes everything into account even yeah I don't know if you are castling if you're doing complex movements of chess everything that you can imagine it can it can support it and we are going to see a demo now I'm going to show you a bit of code for those who are not engineers sorry about that but I'll try to make it um I'll try to make it uh uh friendly for you guys okay so here I have my environment And can you see the code? Well, yeah. Okay. So, this is a a a cargo project actually. And this is the the lip file that that comes when you write the library.
Um we are importing here the chess engine, the stylus SDK. We have a few um a few constants. uh I didn't explain it before but uh for those of you who want a bit more technical in the end you are putting all the board all the information in a in a 256 bit integer you have 64 squares and you have like um three bits for the piece and one bit for a color and you multiply all of that and you have like uh 256 bits. So in the end you are you're putting all the information inside one storage variable right and this is like just some constants to to and to just facilitate um what we are reading for that for from that storage right we have our our storage layout here our contract is called stylus test we have total games spending games and the games mapping each game has this information player one player two what the status it is in if it is um if this still playing if you can um I don't know if it is finished because someone won or something like that what um color is it uh what color has to move now and who is the victor if the game has finished right and then we have the board state as I said before it everything fits into a 256 bit and we save that in the board state then we have a few public functions that Okay, I'm not going to go very deep into them, but just to show you that we have this function play move. Uh, you just say, okay, this is the the the game that we want to move in.
Uh, you go from this row to this column to this row and to this column and the game and the contract calls the play move function from the crate that we imported and just verifies if it is a legal move or not. Okay. Um, so I want to show you now uh just what it looks like and I'm going to run the the the demo for you guys. So first of all, I'm going to deploy the contract. I have a local chain that is running that I'm going to show you now.
So the local chain is just running here. Okay. So in here we just deploy the contract at this address. You'll notice that the contract is a bit above the the size limit of of of chains which is 24 kilobytes. This is 25 kilobytes.
In this case is fine because I'm running a local chain. But this is because we are using something to display the board and we are going to see that now to display the board in the node logs which is something that you are not going to do in production. If you remove those functions that's actually the the the feature that we have here. If you remove that feature, the contract size goes down to 21 kilobytes and that fits perfectly on a chain, right? So, we are going to take this this contract.
I'm going to put it here. Don't worry about this private key because it's all over our documentation. So, nobody's going to steal any funds. And then we are going to um I think this was uh get total uh yeah, we're going to join the game here. We're going to join twice because we need two players.
And then this is what I want to show you which is this is going to show the board in our um node logs. Of course, as I said, you will have on a production environment, you will have a a user interface to show this board, but in this case, just for this demo, um we chose to do this here. Um, and then we're going to run a set of moves here, which is uh which is plays the the scholars made. For those of you who are Spanish and familiar with chess, this is the mater. So this has now make all those moves and in the end it says, okay, the game status is finished.
Black is the victor because black is the player who moved uh who who did the mate actually. And we see that all the movements are now recorded uh in the note logs, right? Um yeah. So that's it. Uh if you can if you can change to the presentation now.
Can you can you change to the presentation? Yeah, don't worry. Okay. So that's what I wanted to show you. Um if you want to to to play with this demo with this stylus chest, you have the repository here available.
You can also scan the QR code and it will take you there. Uh and again if you have any doubt or you want to talk about Stylus, we have a telegram channel. You can go to our discord. Uh we have there the documentation portal as well. And yeah, and that's it.
Thank you very much. Sorry, I'll leave this on. Thank you very much and uh hope you enjoy it. Thank you.
Automatic transcript — names and jargon may be misspelled.