Cairo1 advances and vision
ETHCC 6 · July 2023 · 16:16
Shahar Papini presents Cairo 1 features, programming patterns, examples and plans for further development.
Speakers
Topics
From ETHCC 6, hosted by Ethereum France.
Transcript
[Music] Okay. So, I can start. Yeah. Okay. Hi there. So, I'm Shahar. I'm from Starware and I'm working on the compiler team of Kairo 1 and I'm here to talk to you about the advances and new stuff on Cairo. But I only have 15 minutes. So I decided to focus on uh the developer experience and how smooth it is to use Caro today with all the latest features and I'm going to do live coding session doing some kind of toy problem and showing you from scratch how uh how
smooth and easy it can be to write in Cairo. So uh the first thing we we need to do uh to write Cairo is uh install scarb scar is the project manager for cairo h it's similar for it's it's like car cargo is for rust or npm is for node then scarb is for cairo there is a this documentation we'll I'll share later about how to stall it's oneliner So assume we have scar installed. So how do we start a new project in carro? We do scar in it and voila we have a new project. What
did we get here? We get a scarab tommo which is a configuration with dependencies and everything and a simple kyro file. H we get some dummy code which is Fibonacci. We don't want Fibonacci. By the way, I'm planning to do a tic-tac-toe game. So Let's see how it will go. First things first, do you know that Kai carro is a general language for ZK proof for making ZK proof for your programs not just for stark net. Uh so to use starknet we need to uh follow what scar says about activating scaret which is adding this
line and this dependencies. This is the but the only thing we need online this part is all the package dependencies which you know from other project managers. All right. So the first thing we want is to make a new contract. We can use the contract snippet we have built in in the uh extension and try it. We give it the name. Let's say tic tactoe.
I think this is how it's spelled. And as you see, we need to annotate a model with this with this contract. This is belongs to the stocknet plugin which adds all the things you need to make it a contract. And we get this storage which is all the storage we have for our contract. Okay. H next thing I want to make a game. So I prepared a nice um package that can use called kyolit. You can add packages not just from local path. You can add it from github repositories and eventually we have from registry or everything else. And there are a lot of
public packages already ex existing but I have my dummy one. is in this local path. Okay, now that I got my package, let's see what we have inside it. Let's try to use it. Let's do some completions. And we can see we have our K lit here and we'll complete some more. And we can see we have this I game interface inside. This is what we are interested in. So we can import it and let's see what it has inside. I do go to definition. I I see what it has here. And the I game
is a an interface for simple game has you need to say what turn is and you can apply move and I would want to make this is battery. All right. Okay. Everything's good. H yeah, we want to implement this for our contract. So let's see how we can do that. We make an implementation and now we have its name. It's called game implementation of let's do completions I game. And what do we need to give it? If we do like controlclick and put the mouse over, we can see we need to give it a T state and
T move. The state is the contract state which we get by being a contract. It's already generated by the contract thing and we need to also provide it with a move to make pretty soon. And this is how we make an implementation. Now we get uh some diagnostics from the language server. Let's see. First of all, it says none all trait items are implemented because I do need to implement the turn and apply move. and it says it doesn't know what move is. So let's start defining the move.
Let's call it a position. I got some copilot completion there. And I can have an alias type and complete and I get I'm defining this move. All right, this error is done. And now I I need to implement the rest of the things. As you can see, I have things I want you to see is like I have completions, I have diagnostics, I have light highlighting, I have cool stuff.
Okay. So let's quickly implement all this thing. This state becomes this. Now I have this H side type which is required here. It exists inside my package. So I'll import it as well. Let's see what I have here. I have I have this side. Okay. Let's see what what else I have here. Have her. Okay. Not yet.
Let's move it here. All right, move is move and result I need to give it the correct result. I can do this by h using as it's alo new feature as game result. So I imported this result and I'll call it game result. can change names of things and here I'll make some implementation okay you can see we have kairo is type safe so I'm getting this diagnostic that say h we expected you to return a result and we got nothing we
got units so uh let's h quickly return some result and get completion error and I need to uh have the error game error here. So I'm implementing game error. I imported game error and let's get completions. I have these kinds of things. Okay, let's say for now it's a bad move and put it in both. Okay, and I get another error. What is this error? It says value not dropped.
If you're familiar with linear types, then carro has linear types inside. What does it mean? It means that values can't just be disposed of, can be created unless you specifically say they can be. And the way we say it in Cairo is using a drive. This line adds an implementation for drop which means we can drop this value. Linear types have a lot of useful applications especially with enforcing invariance in a blockchain. I don't have time to show examples for this right now but know it is possible. Okay.
So this should compile. I'm running scar test and everything passes. All the zero tests passes but it compiles. How can I make a test? H we can make an inline test inside this module. We have this snippet. It makes a test. All right, let's call it test game. H let's uh create a a contract state for testing. This is done by H. Let's complete. Let's see. We have contract state for testing.
It's this is autogenerated by the contract as well. All right. We also need to be mutable. This how to define mutable in caro. And we can start calling stuff. Let's call turn. And uh let's do a unwrap which makes sure the the result we got is okay and not error. As you can see this completion added the trait we needed to call the the method. Let's save and let's see what happens when we test.
Yeah, we got an error. Why did we get an error? It says on the unwrap unwrap failed because we returned an error. Not an okay. So, let's just do some very simple thing. Let's return. Okay. And we need to return side. Let's return side x. And now our test passes. Okay. Let's see make some more interesting logic than just returning a constant value. How do I make a storage variable in caro? So inside the stored strct I can add something like churn and this case I would want it to be let's
say game result of side. Why did I do game result? Because I wanted to also represent the case that the game ended. So either it's okay and we are in progress and we have it's someone's turn or the game ended and and someone already won. So I'll do something like this. And here on turn instead of returning this value I I read from my storage variable as you can see I just did self dot and it completes me that I have the turn variable and it also completes that it has a read method. If I want to see the signature, I do controllclick and put my mouse on this method. And I can see it's
a signature. In this case, it doesn't get anything except self. So I'll just save it. And this seems to work. And let's run test. And everything runs. It's okay. Good. And let's uh do something uh really simple. Let's uh in apply move check that move is inbound. You can do move.x if move.x is greater than two and 0 one and two are okay. So three is bad and let's do some copout completions.
I'm getting an error otherwise I'm returning an okay and we need to add semi semicolon here and let's make sure uh this is what happens indeed so let's do some apply move I'm returning a I'm giving a position of let's say x 10 y so make it invalid and unwrap app and let's see our test it fails if you give it something valid.
Yeah, it passes. Okay. H So, uh very nice as you can see it's a quick h feedback on your programming. H if we were to complete this tic tactoe, we'd probably need something like a mapping. Let's add a mapping. We have a snippet for that, too. Uh here, let's make a board from position and make it to like option site to make sure something is filled. H All right.
Uh I don't have time to complete everything. Erh, I just wanted to show you that the basic flow of programming contract in caro that we have a very nice highlighting completions quick testing easy to like it's one line install instead of setting up a project. You can publish it and we have got definitions and controlclick and see stuff. Also we can look at the autogenerated code. For example, this function was autogenerated. We can control click and see entire generated code for our contract and we can see a standard library things like in here for example. H okay.
So that was like a a very quick guide on how to start writing Kairo on your own. Uh thank you. Do you have any questions? Yes. blah blah blah. Okay, thank you. Nice presentation. Uh question is uh do you think that for developer experience is good to change Cairo syntax already few months because you change uh two times until now and probably to end of this year will be two more times. H well we did uh change the contra syntax but the reason
for that was because the first version was compatible to carro zero the old version and now we have the more mature version of contract syntax we want to have. So this is not planned to change and we are supporting it we commit to support it six months in advance so that it will be compatible. So uh we will also introduce other ways other additions like components but the basic syntax won't be broken. Yes. Any other question? Any other question? No. Okay. Thank you.
[Applause] [Music]