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

Loading player…

Stylus workshop: Proxy Update of the Logic Contract - ETHGlobal Brussels 2024

ETHGlobalWed, Oct 9, 2024, 12:00 AM

Join Jose FP from Arbitrum, for a workshop titled, “Stylus workshop: Proxy Update of the Logic Contract.” In this workshop we'll go through the basics of Stylus and what to take into account when upgrading the logic contract of a Proxy from a Solidity contract to a Rust contract. We'll show an example of how to upgrade a Solidity based ERC-20 contract behind a Proxy, to a Rust based ERC-20 contract. This workshop took place during ETHGlobal Brussels, a 3-day hackathon held in Brussels, Belgium from July 12 to 14, 2024. ETHGlobal Brussels brought together the most skilled web3 developers, designers and product builders from all around the globe for a weekend-long adventure to advance the Ethereum ecosystem! ---------------- Follow us: x.com/ethglobal Website: https://ethglobal.com Email us at: hello@ethglobal.com

Transcript

[Music] all right hello everyone my name is Jose Franco I ask for the microphone because I move a lot so um we are going to talk today about arbitrum stylus and specifically we're going to talk how to upgrade a contract that is written in solidity to to a rest contract right through proxies then first of all my name is Jose Franco I'm an inte integration engineer at offchain Labs here are my Twitter and telegram handles I don't write a lot on Twitter but you can contact me there and telegram of course and offchain Labs well we are the initial Builders of arbitrum one and Nova before they were transferred to the arbit DA and we are the developers of stylus and orbit so for this hackathons we have specific uh specific price tracks for stylus and orbit if you want to know more about specifically orbit we're going to learn about stylus today just go to our booth and we'll be more than happy to to help you all right these Workshop is divided into two parts the first one is just an introduction to stylus we're going to talk about what it is what it enables and what benefits it has and then we're going to show a simple counter counter contract reasen in rest right and the second one is a bit more advanced and we're going to talk about how to upgrade this uh solidity contract that you have behind the proxy to a rest contract what you have to to keep in mind when you do that right and we we will also see a a sample project so first of all what is stylus Stylus is an is an upgrade to the current uh development environment we have for smart contracts on evm chains right what that means is that um if you want to write a smart contract today in on an evm chain usually write that in solidity or in Viper as well right but that's your choice that's what you what you have to uh that's the choice that you have right so stylus body what it allows you is to uh write smart contracts in other languages like r c C++ and many other languages that will be um interoperate with the evm as well I mean so this the way this works is that stylus activates a new virtual machine different than the evm that executes what PLM bite code right and that virtual machine lives alongside the EVN and they can talk to each other right so if you have a stylus smart contract it is executed in the wasm virtual machine but you can from there you can call solidity contracts that live that are executed in the evm machine and the other way around okay they are fully interoperable um one of the main benefits that stylus brings is that we can access new developers e ecosystems right like the r one or the C++ one and these are old numbers this is from 2022 almost two years ago but probably the numbers have changed but the ratios are probably the same so we are now opening the development um space to new communities as well right so more benefits from stylus it increases speed and reduces cost this is generally true because wasm bite code is going to be more efficient than than and solidity uh bite code then you have to take into consideration um storage accesses and all that but gener generally speaking it is more efficient and then also big Improvement big Improvement in memory usage that brings also new use cases like Advanced cryptography generative art I was talking with a colleague yesterday and they were able to to implement this uh black shells algorithm that is Us in finance for example that is very very complex to do in solidity because yeah you end up with transactions that spend a lot of gas you can do that with stylus now and it reduces a lot the the usage of gas and those are yeah a few examples of what you can do so what we have today is that um besides the the the virtual machine we are also building an SDK for rust there is another one for C and C++ we are putting more effort into R so the SD is more is more advanced but actually you don't need an SDK to to write your smart contract because any language that can be compiled to wasum can use arbitrum stylus and actually we have examples of smart conract written in Zig and assembly script they don't use any SDK or anything it's just um a program that is compiled to wasm it is deployed on chain and yeah you run it that's it so the next steps for stylus and the and the current state actually we have an stylus in arbit sepolia so today you can you can deploy your stylus programs there and and run them and there is an AIP already in the governance for to activate it on arbit from one and Nova the onchain vote will come soon I don't know exactly when but probably soon and if the da vote for it we will enable that on on one and Nova all right um these are three resources that i' like to share with with people who want to start with stylus um don't worry about about searching for this because at the end of the presentation I'm going to show you a QR code that has all these resources all right so the first one is a quick start um this is in the public documentation it goes through how to set up your environment and how to work with a Hello World contract that is written in R how to deploy it how to use the cargo stylus tool that we that we use for that then we have the stylus by example that's a uh another different portal that contains information about how you can do certain things with the using the the r SDK for example how do you emit a log how do you handle errors how do you um I don't know encode function selectors stuff like that right and finally awesome Stylus is a repository of code that's been written by uh third party developers and we maintain there so you guys have examples of things that you can do with with stylus right all right so first example this is a a basic count counter contract it just has one variable that is a counter you can increment it you can change you can set a different uh count and all that so this is a perfect example to see um for a r smart contract um we are going to take a look at it now all right here it is so for those of you who are familiar with rast you will see that um the this the this structure that we have here is similar to what you get when you create a new cargo project actually the projects for EST stylos are created with cargo so we have our SRC folder and we have a main file and a Le file the main file we usually only use it to to be able to export the AI of the contract that we're going to write right so the main entry point for the contract and the the yeah the main file that contains the the contract code is the leap file and in here this contract is very basic it only well it's called a counter it only has one storage variable and I'm going to explain this now and these three functions get set count and increment right okay so um I'm going to explain these things by explaining what specific stylus macros we have here and the first one that we have is this solidity storage macro and this macro tells stylus that the next struct that it going to it's going to find um defines the the storage layout that our contract is going to have right so in this case we only have one variable when U2 56 variable named count and and that's it the next one that we have is entry point entry point defines well tells the stylus where the execution of the smart contract begins that means that if um whenever you call this contract you are telling stylus Hey whenever you receive a transaction you start executing code from here on right so usually these two macros go together solidity storage and entry point um the last one that we have here is external external tells a stylus that the next functions that it's going to find are public functions they are they they they can be called from outside the contract right if you want to implement any private contract you can do so by adding another inul impul counter above the external macro and those will be internal right okay and as I said these three functions well the get function only Returns the count the set count well sets another a different count uh a different number in the variable and increment well increments by one another thing to keep in mind here is that um when building the stylus when we compile the RAS code into into wasm we try to well we try to no we we change the we change the code and we change the function so so they are solidity ABI compatible okay what this means is that we want users to not care about what language the contract is written in but we want them to be able to to call them the same way they call solidity contracts right so for example we have here a set count function that is using rust syntax it's using uh snake case right but this is not the way solidity AI functions are written right so when compiling this program dsdk is going to change this so when you call this function you will have to use camel case the same way you do with solidity all right and that being said I think we have time to run to quickly run these this uh example we have this um I can answer questions later okay we have also this script folder that holds um sorry scripts to perform individual operations so if we call for example the deploy function here the deploy script oh sorry need to go to scripts deploy it's going to deploy our program we can take a look at it it actually uses the cargo stylist tool I don't want to go into details about this but you have that in the documentation and then uh so our contract is now deployed to this address and we can for example get count here I have this comment to help me with this we get the count the count is zero obviously because we just deployed the program we increment it yeah okay and then we get the count again and it's one so as you can see here we are using actually Foundry cast to call our stylus program the same way you would call A A A solidity program right and if we go to the set count script it's using cast send and as I said before it's using set count in camel case Okay so this is going to be important for what we're going to see later all right let's go back to the presentation here all right let's go to the second part now I'm going to talk a bit about upgradability of a smart contracts you probably know everything about proxies and all that but in case anyone hasn't worked with proxies let me just cover the basics to understand what we're going to do later so as you know smart contracts are immutable that means that when you deploy the bite code you you can not modify it right but obviously sometimes you want to have a you want to have upgradeable smart contracts because you want to be able to fix backs you want to be able to improve the logic of your contract so how do you how do you obtain upgradeability well one of the most common patterns that we use are proxies okay and proxies are just um smart contracts that deployed in front of a logic contract of the contract that has the the the business logic of your application so let's say that for example you want to have an you want to deploy an nc20 contract that is uh upgradeable you deploy the erc20 contract and in front of it you deploy the proxy your users are going to call the proxy they are only going to interact with the proxy and that contract is going to delegate call the erc20 contract um then if you want to upgrade the logic you just deploy a new erc20 and point the proxy to that one so whenever the users call the proxy contract the the the call will be forwarded to to the New Logic contract then what happened with the storage because we are delegate calling from the proxy we are using the storage space of the proxy contract right and this is important because otherwise we will use the storage space of the logic contract and if we change it well we lose all the information right so one thing to keep in mind when upgrading is that you need to keep the same storage layout as the previous logic contract otherwise you will be messing with the slots and I don't know if you change the balances mapping with the owner mapping you might be asking for the wrong information later all right however you can extend it if you keep the same storage layout and you add new variables below you can extend it and yeah just keep in mind that for the next one you have to keep the same the same storage layout and the other thing to keep in mind is that logic contracts shouldn't be initialized with a Constructor because if we are modifying the storage in the logic contract directly we are not modifying the storage from the proxy right so what we usually do is that we initialize the contract I don't know in the case of a near C20 we we set the name and the symbol for example from a special initial initialization function function that we call from the proxy so um oh yeah one thing to keep in mind is that I'm giving a very basic explanation of proxies so proxies are very complex and can yeah can can do a lot of things this is just the basics to see what we are going to see today um there's a lot of information on Google I'm more than happy to talk about proxies as well but yeah just keep in mind that these are much more complex so requirements of the pro pattern that we're going to use um the logic contracts these erc20 contracts need to be initialized from the proxy or through the proxy not with Constructor and we need to keep the same storage layout when we upgrade the logic contract right all right so let's get to it then we're going to upgrade a solidity erc20 contract to AR rest implementation this solidity erc20 contract is going to be behind the proxy right so the steps that we are going to follow more or less are first we are going to replicate the storage layout in the rast contract second we're going to write the same contract contract functions in the r contract we can modify the logic if we need but the signatures have to be the same and then to test we are going to simulate the the the update the update flow and add any necessary functionality we need there all right so for the solidity part of this code sample we are going to use open seing uh library right so for the proxy we are just using a transparent upgradeable proxy just simple as that for the erc20 contract we are going to use their implementation we are using here the the standard the non- upgradable implementation they have an upgradeable implementation for the erc20 but we want to keep the storage as simple as possible the upgradeable uses a spefic specific storage slot we want to to start the storage slot in in the slot zero or whatever the slot we we start from and we are also using the initializable um library to be able to initialize the contract through the proxy right so one thing to keep in mind here is that the initializable contract when you inherit from it you are adding two storage variables to special storage variables to your contract right and because we are inheriting from that one before the erc20 we are going to have those two storage variables first and then we will have all the storage variables for for the erc20 contract we need to keep that in mind when we build our R contract and then we have uh just the initial the initialize um function I'm not initializing the the token name and symbol here I'm just hardcoding those values because I want to make this simple and we have one mean function all right so let's move to rest then when we write our R smart contract the first thing we are going to do is to replicate our storage layout and here I want to show you a different macro that we can use as well which is so storage this one is different than the previous one the previous one was solidity storage and this is solid storage the difference between those is the syntax that you are going to use for for your for your variables right so for the previous one we use rust syntax and for this one we are using solidity syntax this is interesting especially for these use cases because if you are familiar with solidity if you are a solidity developer it's very easy to copy the storage layout that you have and paste it as is in your R smart contract if you are a Ras uh developer and you don't know anything about solidity it doesn't matter you just write the with rest in tax okay and here we are using also um a borrow macro because in this specific example we have our erc20 implementation is in a different file I wanted to show you a more complex uh example so in this case everything that is erc20 is in a different file and then we call um we call that that module from our first Le module we will see that in the code later um yeah so then we're going to write the same functions uh in our Ras contract we are using another special macro here this inherit macro that is going to take all the functions that we have in this erc20 module and they are going to add it to the to the to my token we are adding a new Burn function here that we didn't have in the in the solidity contract yeah that's it so three contracts proxy and erc20 in solid those are going to be deployed with Foundry and then we have the r contract the erc20 r contract that later we're going to uh deploy and change the proxy contract to point on that one all right so let's take a quick look at it oh bad time to be signed out uh it is this one yeah so this is the the QR code that I'm going to show at the end is the um is for this repository the rmy of this repository contains all these resources that I I talked about before uh in this project we have three main folders We have the proxy which is just a Foundry project that contains the the proxy code then we have the erc20 solidity that contains my token similar to what we saw before but with with a few things I had to add a Constructor here even though it doesn't do anything because I'm not using the upgradeable and yeah open seing requires to to have the Constructor the initialized that we saw before the mint and the hardcoded uh name and symbol and then we have the r contract here so the r contract has the main file the same as before just to export the AI and then we have the leave and the erc20 the erc20 has the what I said before the implementation of the erc20 standard right it uses the solid storage uh that handles solidity syntax so it has balances allowances total Supply then we have our events and errors that we can Emit and throw and then in here we have an inut of the erc20 that doesn't have the external uh macro that we saw before that means that these functions are internal they can only be called for within from within the contract right we have our transfer we have our mint burn all the internal functions that we need and then we have a different inut erc20 with the external macro so these are the functions that we can we can call from outside the contract right the name symbol decimals total Supply well everything that we need so notice that even though in in solidity we have transfer from in camel case here we are writing in snake case because we're writing our R contract so this is going to be compiled to wasm it's going to change the the way we call it so it's going to be compatible with with the proxy that we are using Okay users are not going to notice any difference there all right so from the leap file in the end the leave remember is the entry point for our contract so even though we have the erc20 file all calls are going to go through whatever we have in in Le first right we are using um the erc20 module that we just saw the solid storage is what we saw before uh remember that we had two variables um two storage variables that come from the initializable contract and so we are putting them first and then we are putting the we are borrowing the ones from the erc20 module and we have the mint function and the and the burn function that we are adding all right I think we can do a quick test here for the scripts folder we have the same thing here if you want to run it we can just deploy it one test that I like to do let's see if I can do everything with one hand uh deploy so deploy it's only going to deploy the the Sol DT contracts all right so we have the proxy here we will need both the the proxy contract and admin for later but for now let's get the token information here let's see all right so let's get the token information let's minan some tokens yeah I'm not sure I have time for the full demo demo but let's try yeah and let's get the balance all right so here for the balance I'm just just using the the deployer account all right and now what we are going to do if I can is to upgrade the contract with the update logic script for this one we need the the proxy contract and the proxy admin because we need to send the update call from the proxy admin address right copy and so this is going to deploy the RAS contract using cargo stylus and then it's going to call the the the update function of the proxy contract so now if we go to the get information we'll see that we have now the my token V2 so it's using the contract the rest contract and if we go to balance we should have the same balance as we had before so the the the first that were minted on initialization and the last uh 100 tokens that we meed with this all right so I don't have more time so I will just show you the QR code if this allows me to show you the QR code ah no well I'm going to yeah I'll do another thing I'll I'll write on Discord on the E Global server I will will send this presentation I will send you the link to the repository actually that contains everything and yeah you can grab it from there okay I will send it in the partner arbitrum channel of the E Global Discord and yeah thank you very much I don't know I think someone had a question before I have one minute left yeah for stylus stylus I think they are working on a on a library for R contracts um I can try to dig in some information and see uh what I can find I think they they actually publish it on Twitter I think if I remember correctly function soes your comp understand that this code does not change anything function or oh I see what you mean um I'm not 100% sure but I believe you don't mod if you don't modify the storage it's just going to Market as as viewer yeah it should the SDK the rest SDK understands yeah so the SDK probably when it compiles it to wasm H it will it will just mark it as as view okay get yeah well actually what you can do is that um well we had several macros that you can use and you put them in top of the of the function and then give you for example for a pay for a payable modifier we had a payable macro and you put it on top of your function and then it makes it payable for example right I for example Oh you mean for modifiers yeah okay okay okay sorry yeah I don't know I can find that out but yeah but I don't know if you have the same behavior as modifiers in in in R yeah the same yeah exactly yeah it's it's going to be compatible with solid if you want because I run out of time if you want we can we can talk about me in in our booth I don't know if you have time no okay so question yeah exactly you can go to our booth and we will talk or even outside here thank you very much [Music]

Automatic transcript — names and jargon may be misspelled.