/starknet-cairo-101

Learn how to read Cairo code

Primary LanguageCairo

StarkNet Cairo 101

Get started with Cairo with this simple tutorial. Complete the puzzles/exercises, get tokens and learn about StarkNet smart contracts! A mandarin version is available 中文版本请查看这里here

Introduction

Disclaimer

​ Don't expect any kind of benefit from using this, other than learning a bunch of cool stuff about StarkNet, the first general purpose validity rollup on the Ethereum Mainnnet. ​ StarkNet is still in Alpha. This means that development is ongoing, and the paint is not dry everywhere. Things will get better, and in the meanwhile, we make things work with a bit of duct tape here and there! ​

How it works

Complete the exercises and get tokens! This workshop is a set of smart contracts deployed on StarkNet Alpha on testnet. Each smart contract is an exercise/puzzle - which outlines a feature of the Cairo Smart contract language. Completing the exercise will credit you with points, in the form of an ERC20 token. ​ This workshop focuses on reading Cairo code and StarkNet smart contracts, in order to understand its syntax. You do not need to code or install anything on your machine in order to follow and complete it. ​ Getting started (doing the first two exercises) will take you some time, in order to get into the tutorial. Hang on! Once there, things will flow more easily. You're learning! ​ This workshop is the first in a serie that will cover broad smart contract concepts (writing and deploying ERC20/ERC721, bridging assets, L1 <-> L2 messaging...). Interested in helping writing those? Reach out! ​

Providing feedback

Once you are done working on this tutorial, your feedback would be greatly appreciated! Please fill this form to let us know what we can do to make it better. ​ And if you struggle to move forward, do let us know! This workshop is meant to be as accessible as possible; we want to know if it's not the case. ​ Do you have a question? Join our Discord server, register and join channel #tutorials-support ​

Getting started

Creating an account contract

In order to complete the tutorial you need to collect points. These points will be owned by a smart contract wallet, that you need to deploy.

  • The easiest way to set one up is to use Argent X (download the chrome extension or check their repo) or Braavos (download the chrome extension).
  • Follow the instructions to install the extension and deploy an account contract
  • Make sure you are on the Goerli testnet network
  • The tutorial's points are held in contract 0x074002c7df47096f490a1a89b086b8a468f2e7c686e04a024d93b7c59f934f83. Click on "add token" in your installed wallet to get your points balance to show up there!
  • Connect Voyager to your account contract! This will allow you to broadast your transactions through your wallet. ​

Using voyager

For this tutorial we'll interact with our contracts through Voyager, StarkNet's block explorer.

-> Connect Voyager to your account contract! This will allow you to broadast your transactions through your wallet.

When looking for a contract / transaction, always make sure you are on the Goerli version of Voyager!

Getting points

​ ​ Each exercise is a separate smart contract. It contains code that, when executed properly, will distribute points to your address. Since there is no way currently to send a transaction easily through your account contract, you'll have to specify your address for each call. ​ Points are distributed by the function distribute_points() while the function validate_exercise records that you completed the exercise (you can get points only once). Your goal is to:

Graph ​ ​ ​

Checking your progress

Counting your points

​ Your points will get credited in your installed wallet; though this may take some time. If you want to monitor your points count in real time, you can also see your balance in voyager! ​

  • Go to the ERC20 counter in voyager, in the "read contract" tab
  • Enter your address in the "balanceOf" function

You can also check your overall progress here

Transaction status

​ You sent a transaction, and it is shown as "undetected" in voyager? This can mean two things: ​

  • Your transaction is pending, and will be included in a block shortly. It will then be visible in voyager.
  • Your transaction was invalid, and will NOT be included in a block (there is no such thing as a failed transaction in StarkNet). ​ You can (and should) check the status of your transaction with the following URL https://alpha4.starknet.io/feeder_gateway/get_transaction_receipt?transactionHash= , where you can append your transaction hash. ​

Exercises & Contract addresses

Contract addresses

Topic Contract code Contract on voyager
Points counter ERC20 Points counter ERC20 Link
General syntax Ex01 Link
Storage variables, getters, asserts Ex02 Link
Reading and writing storage variables Ex03 Link
Mappings Ex04 Link
Variable visibility Ex05 Link
Functions visibility Ex06 Link
Comparing values Ex07 Link
Recursions level 1 Ex08 Link
Recursions level 2 Ex09 Link
Composability Ex10 Link
Importing functions Ex11 Link
Events Ex12 Link
Privacy on StarkNet Ex13 Link

​ ​

Contributing

Help is welcome

This project can be made better, and will evolve in the coming weeks. Your contributions are welcome! Here are things that you can do to help:

  • Correct bugs if you find some
  • Add explanation in the comments of the exercise if you feel it needs more explanation
  • Add exercises showcasing your favorite Cairo feature ​

Reusing this project

  • Clone the repo on your machine
  • Set up the environment following these instructions
  • Install Nile.
  • Test that you are able to compile the project
nile compile