StarkNet Cairo 101
Get started with Cairo with this simple tutorial. Complete the puzzles/exercises, get tokens and learn about StarkNet smart contracts!
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:
|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
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
A mandarin version is available 中文版本请查看这里here.
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
[0x61cc0eb883afd4e33b127b2443d2ea6dd5afb1d3584223f0017332296539d5b](https://goerli.voyager.online/contract/0x61cc0eb883afd4e33b127b2443d2ea6dd5afb1d3584223f0017332296539d5b). 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 Voyager to your account smart contract. Voyager is the block explorer for StarkNet (the equivalent of Etherscan for Ethereum) and allows you to browse the state of the blockchain, view all transactions and their status. By connecting Voyager 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 exercices & 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:
For this tutorial, we'll interact with our contracts through Voyager, StarkNet's block explorer. Please make sure to connect Voyager 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 Voyager!
- Access your transactions with a URL of the format: https://goerli.voyager.online/tx/your-tx-hash.
- Access a contract with a URL of the format: https://goerli.voyager.online/contract/your-contract-address
- Access read/write functions of the contract with the "read/write contract" tab in Voyager.
Addresses of the exercises and contracts
|Topic||Contract code||Contract on Voyager|
|Points counter ERC20||Points counter ERC20||Link|
|Storage variables, getters, asserts||Ex02||Link|
|Reading and writing storage variables||Ex03||Link|
|Recursions level 1||Ex08||Link|
|Recursions level 2||Ex09||Link|
|Privacy on StarkNet||Ex13||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 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.
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.
Reusing this project
- Clone the repo on your machine.
- Set up the environment following these instructions.
- Install Nile.
- Test that you can compile the project.