Skip to content

BlockDweller/starknet-cairo-101

 
 

Repository files navigation

StarkNet Cairo 101

Get started with Cairo with this simple tutorial. Complete the puzzles/exercises, get tokens and learn about StarkNet smart contracts!

Introduction​

Disclaimer

​Don't expect any benefit from using this other than learning some 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 to understand its syntax. You do not need to code or install anything on your machine to follow and complete it.​

​ It will take you some time in order to get started (doing the first two exercises) into the tutorial. Hang on! Once there, things will flow more easily. You're learning!​

Where am I?

This workshop is the first in a series aimed at teaching how to build on StarkNet. Checkout out the following:

Topic GitHub repo
Learn how to read Cairo code (you are here) Cairo 101
Deploy and customize an ERC721 NFT StarkNet ERC721
Deploy and customize an ERC20 token StarkNet ERC20
Build a cross layer application StarkNet messaging bridge
Debug your Cairo contracts easily StarkNet debug
Design your own account contract StarkNet account abstraction

Providing feedback & getting help

Once you are done working on this tutorial, your feedback would be greatly appreciated!

Please fill out 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.

Are you interested in following online workshops about learning how to dev on StarkNet? Subscribe here

Contributing

This project can be made better and will evolve as StarkNet matures. Your contributions are welcome! Here are things that you can do to help:

  • Create a branch with a translation to your language
  • Correct bugs if you find some
  • Add an explanation in the comments of the exercise if you feel it needs more explanation
  • Add exercises showcasing your favorite Cairo feature

Languages

  • A Spanish version is available here.
  • A Portuguese version is available here.
  • A Korean version is available here.

Getting started

Creating a smart contract wallet

To complete the tutorial you need to collect points. These points will be owned by a smart contract wallet 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). These wallet solutions are similar to what Metamask is for Ethereum and allow users to initiate transactions and interact with applications on StarkNet.
  • Follow the instructions to install the extension and deploy a smart contract account (it could take about 5 minutes to be deployed). Please note that in StarkNet there is only one type of accounts - smart contract accounts (this is called Account Abstraction), in contrast to Ethereum where there are wallets and smart contracts. In other words, every wallet on StarkNet is a smart contract and there is no distinction between them and other smart contracts. Therefore, to create a new wallet, you need to deploy a transaction that publishes your smart contract wallet to the network.
  • Make sure you are on the Goerli testnet network.
  • The tutorial's points are held in contract 0x5c6b1379f1d4c8a4f5db781a706b63a885f3f9570f7863629e99e2342ac344c (Starkscan link, Voyager link).
  • Click on "Add Token" in your installed wallet and the contract address to get your points balance to show up there! A new token called SC101 (starknet-cairo-101) will appear in your wallet.
  • Connect the block explorer (Starkscan or Voyager) to your account smart contract. These are block explorers for StarkNet (the equivalent of Etherscan for Ethereum) and allow you to browse the state of the blockchain, view all transactions and their status. By connecting a block explorer to your wallet, you will be able to broadcast your transactions through your wallet.
  • To execute transactions on the Goerli StarkNet testnet you'll need testnet ETH to pay for gas. To get some, go to the faucet and follow the instructions. It could take several minutes, but you should receive some L2 Goerli ETH in your wallet that you can use to execute transactions on the testnet.

Solving exercises & Getting points​

Each exercise is a separate smart contract. It contains code that, when executed correctly, will distribute points to your address. ​ 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 ​ ​

Using a block explorer

For this tutorial, we'll interact with our contracts through Starkscan or Voyager, StarkNet's block explorers. Please make sure to connect the block explorer to your account contract! This will allow you to broadcast your transactions through your wallet.

When looking for a contract/transaction, always ensure you are on the Goerli version of the block explorer!

​ ​

Addresses of the exercises and contracts

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

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 a block explorer! ​

  • Go to the ERC20 counter on Voyager or Starkscan, in the "read contract" tab.
  • Enter your address in the "balanceOf" function.

You can also check your overall progress here. ​

Transaction status

​ You can also check the status of your transaction with the following URL https://alpha4.starknet.io/feeder_gateway/get_transaction_receipt?transactionHash= or https://alpha4.starknet.io/feeder_gateway/get_transaction?transactionHash=, where you can append your transaction hash. ​ ​

Reusing this project

  • Clone the repo on your machine.
  • Set up the environment following these instructions.
  • Test that you can compile the project.
starknet-compile contracts/ex01.cairo

About

Learn how to read Cairo code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Cairo 87.5%
  • Python 12.2%
  • Other 0.3%