Nethermind - Workshop Crash Course on Cairo - ETHGlobal Singapore 2024
ETHGlobal·Wed, Oct 9, 2024, 12:00 AM
Join us for a workshop with Alex Metelli titled, "Nethermind: Workshop Crash Course on Cairo" Learn how to setup a Cairo development environment and deploy your first smart contract on Starknet This workshop took place at ETHGlobal Singapore 2024, a 3-day hackathon held from September 20 to 22, 2024, that 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] okay hello everyone uh in this Workshop we're going to try to uh build a simple smart contract with kairo and uh we will try to deploy on stet also I will show you the tool chain required to start developing in kairo so uh there is a repository on the nethermind GitHub uh with all the code that we're going to use in this workshop and all the bash commands that you're going to need uh so we just need to uh start by um installing ASDF which is a pocket manager and it's going to be very useful to manage the version of scarb and uh stack and Foundry scarb is a package manager for kaido and uh stack and Foundry is the uh Foundry as in solidity but for for Kyo specifically so the first uh common we're going to need is to install ASDF of course I have everything installed already so um so okay so I'm not going to need to install anything anymore right okay uh since SDF is already installed it's going to tell me it's complaining next uh we add a plug-in with SDF so first plugin we add is carb it's going to complain again because carb is already on my system and we install the latest scar with um ASDF PL um scar install we can um select whatever version of Scar we want in this case we use the latest right and we do the same with Stark and Foundry which is the test uh suit for kaido same and okay latest boundary to install okay and we're going to need star cly which is um a library um to uh deploy declare and deploy uh KIRO contracts okay so this is the tools these are the tool that we're going to need uh to develop and right uh now we can create uh Kyo project we can do it by using uh SM Forge G start found in it and a package name and we can call it it Global okay this command we create a repository with the dependency and some boiler plate code uh used for examples let's open it okay so as you can see we have a basic Source folder which uh is supposed to contain all the our smart contracts a test folder with the test H this is very important SC toml is uh basically defining the package and the dependency it's very similar to rust as you can notice uh so we have uh the name of the package the version the Edition is basically uh a set of um it's it's like a preload so um it's a set of library that you can you have included in uh in your package and uh from the Cairo um code libraries you can select this one which is the most complete or you can select a lighter one which is 247 but in that case you will need to import to import manually uh the library that are missing and then we have St version uh in the dependency and for the dev dependencies we have a St Foundry version 30 okay uh that's a section with the scrapes and we want to also add uh tool version file that is used by ASDF uh and here we can Define scar version in this case 2.8.2 and Stark net Foundry Foundry uh is 0.30 uh in case we want to use a different scar version with a different compiler we can just change this uh values and ASDF will select automatically uh the version that we are going to use it's very easy and it's the best best thing that you can do usdf uh so let's try scar version 8.2 and SN Forge version okay let's have look to the Contra uh in the boiler plate so um to define a contract we need to Define an interface first which is a which is called trade so we adust the function headers we use T contrast state which is a generic uh contrast State type and here we got two function increase balance St explanatorium get balance very easy um the next part is the actual Contra so we add stack contract decorator we Define a mod module name I'm going to change this to alow heit Global and okay we got the storage in this case balance and the type is felt and here is the actual implementation we add The Decorator AI anded V zero if we don't do this this function will not be visible will not be external basically so you cannot they will not be able to to call them so we we Define Ed zero and H we call the implementation allow it Global and this is an implementation of the generic uh trait above so the interface basically and here we use the Contra state which is uh the state of the contract and we need to pass it as an argument to our implementation here we got two function uh the increase balance so basically just increase the balance and here we do self which is contrast State balance the storage right the New Balance here and get balance is basically reading from self uh the balance if you see the difference here how the contast state is passed when you have writing function so function that they change State you need to pass the contast state as a reference so it's it's going to be mutable cont State when you just have a read function which they don't uh modify the state you just pass as um as a um pointer basically contate as a pointer right to compile we simply do scar build SC build right this will compile our contract in the Target folder and they and you're going to have two files one is the Sierra and the other one is the bite code uh right this the Contra class which is actually the bite code we're going to we're going to deploy and this is the Sierra let's format this this is human read s is a intermediate representation um of the actual bite code and um yeah anyway okay we got this to run the test so here there's already a couple of tests that the um init command has created as you can see we are going to import uh all the dependencies uh here we change the name okay not same um so we want to import the dispatcher and dispatcher trait which uh um used to be to initialize our contract after it's deployed uh same here okay uh this is the function to deploy we import a Foundry dependency so to declare and U the trade for the contract class which allow us to deploy and declare so on stet before you deploy a contract you need to declare the class um on chain uh so as you can see uh here we declare the class of the our contract and we deploy after we deploy the the contract class basically uh you see in this test we Mark as test we deploy the contract uh we create a dispatcher which is uh basically um the element that we allow us to to call uh the deploy contract and here we we perform a call dispatcher get balance and and this is a test um you can also have a failing test to run the test simply as and Forge test this a mismatch version here all right yeah uh there we go that's why okay that was a mismatch version so ASDF wasn't reading the correct Foundry version for the typo so it's compiling the contract okay oh yes that's a problem because we changed the uh contract name right so the contract now is called hello it global so we need to change the name here okay one another failed oh yeah here too need to change this one as well so the reason why I changed the name of the contract is because uh you canot R declare the same contract twice so most likely this uh contract has been declared on SEO already so if I don't change something it's going to try to declare a bite code that has been declared already and it's going to fail okay test it fast let's go ahead and start deploying uh to deploy first thing we need a an RPC URL in this case I'm using blast API which you can find as a free RPC Service uh I recommend you to use the 0.
7 version which is uh the latest RPC if you try to use a different one is going to fail because star C is uh using RPC 0.7 so uh we export stpc environment variable uh so we don't we don't have to add it all the time okay uh so to deploy we first need a signer uh which is basically a key store to um to use with st clay okay s sign store new and this will create a Json file no password in this case I don't need that and here we we got our key store you can uh deploy the key you can save the key store everywhere you want it doesn't really matter so you got basically signing Keys here um uh second we need to export the key store as and viable okay and okay uh second thing we need to deploy an account so the the RPC here is uh pointing to seia we going to need to declare an account first and deploy it on sepolia to um to be able to deploy our contract so we is's asking our key Store password no password okay we got an account Json here we got public key you you you want to keep these files uh of course uh hien um normally don't push them to GitHub uh okay so we got an account and now we need to deploy the account okay right to be able to deploy the account we need to found it first so uh I already have um some it on uh on seia so I'm going to send some funds to my new account which is not deployed yet um 0 one right it's too much actually okay if you need some it there's a there's a few foret around you can find them just Google stet foret uh so it's going to tell you uh the account is already deployed uh do you still want to send the fs yes all right transaction is gone through yep okay so uh we're going back to right so now it's been founded we press enter let's try again mm yeah yeah you see transaction failed let's try again okay almost time with the time she'll be fine now all right okay the account is deployed and it's going to tell you here it's status change from uh not deploy to deployed and we know that it's deployed right now we got an account we can start by declaring the contract uh so uh we do CL Stark Li declare Target Dev and we have so what we need to deare is a contra class um uh L yeah uh it Global hello it Global cont Json um declaring classes requires oh sorry I need to export the account [Music] um yeah we need to Define account uh count Jon right the CL the class is declared and we're going to get back a class Ash which we are going to use to deploy uh start CL deploy the class S as a parameter and account again okay the Contra should be deployed now we can go to Voyager which is the block Explorer and we swap to seum and we enter the new deployed account address there we go okay uh now we can read the contract here so get balance our get balance function uh let square that it's going to be probably zero yep uh you can change the format of the data and to write we connect our wallet I suggest you to to create a wallet with uh with an Argent uh or Bravos to to be able to use this let's increase the balance amount 45 54 transaction is exactly like um any ethereum block Explorer basically okay should be good let's go back on read query and our balance has been updated that that's all um any questions got two minutes more yeah all good okay guys thank you very much I hope this was helpful and I'll see you around [Music]
Automatic transcript — names and jargon may be misspelled.