Cairo

Introduction

Cairo is a Turing-complete programming language that allows blockchain developers to harnesss the power of STARKs. In order to build on top of Starknet, developers write Cairo smart contracts and every single operation then gets run inside of the Cairo VM that creates a STARK zero-knowledge proof of the computation performed on Starknet. Cairo is still in its infancy, but as more and more developers use it, features become standardized, and less breaking changes are introduced from version to version, it will become a robust and mature ecosystem. In this section we will go over resources where you can learn Cairo and how to become a good developer on Starknet.

How to learn Cairo?

There are a few resources that will help you get started with writing, testing and deploying Cairo smart contracts. If you have any questions when learning about Starknet and Cairo, feel free to ask anything on the official Starknet Discord. The resources will appear in the order that I think someone getting started with Cairo should take them. To get started with Cairo I recommend going over starknet-cairo-101 which is a fun game where you need to read Cairo contracts, understand them and then interact with deployed versions of them to claim points. There are different levels which get progressively harder and they are a lot of fun to go through. To get started I also recommend setting up your wallet (Argent X or Braavos) so that you can interact with the smart contracts on the Starknet testnet.

After you finish, I recommend going through starklings which are a series of programming challenges where you have a Cairo contract that is missing some pieces in order to be compiled. You will learn how to work with different primitives of the language. There are a few intricacies that stem from being a STARK provable language where starklings will be a lot of help.

For finding references to the language and its standard library, there is no better place than the Cairo documentation. In order to play with the language you can also write contracts in the Cairo Playground. The experience of the playground is similar to the Remix IDE used to write Solidity smart contracts, if you want to use Remix for testing Cairo smart contracts you can install the Starknet Remix plugin through the Remix plugin manager (bottom left of the Remix IDE, above the gear logo).

In order to compile, test, deploy and interact with your Cairo contracts locally you can install a variety of tools like the official cairo-lang Python library, the Nile framework, the Starknet Hardhat plugin and others. From what I’ve seen, most production codebases are using Nile which essentially wraps around the cairo-lang compiler and provides tooling for interacting with as well as managing Cairo contracts.

After you finish starknet-cairo-101 and starklings there are a few other tutorials and examples you can go through to get acustomed to reading and writing in Cairo. They are part of a series of starknet educational resources starknet-edu sourced by the Starkware team. Next up is starknet-erc721 which teaches you how to deploy and customize an NFT on Starknet. Afterwards you can learn how to deploy and ERC20 contract with starknet-erc20. The ERC20 and ERC721 are good examples to learn Cairo syntax on as they are contracts you will interac with frequently. They are part of the the OpenZeppelin Cairo contracts library which include things like token standards, reentrancy guard, access control, accounts and more.

Next up in the starknet-edu series we have the StarkNet messaging bridge repo which will illustrate how to do L1<>L2 messaging bridges in order to build cross-layer applications (protocols like Aave and MakerDAO are already building for a multichain/multirollup future - check out starknet-usd, aave-starknet-bridge and starknet-dai-bridge). And the last two challenges in the series are StarkNet debug and starknet-accounts.

You can also use the Warp transpiler to convert your code from Solidity to Cairo, although you still may need to tweak its output to work as intended in some cases.

After you’ve completed these tutorials the best way to keep learning is to try to build your own little pet project with Cairo. Something like a simple AMM contract, an NFT marketplace, or a Cairo version of the lil-web3 repo created by Miguel Piedrafita. In this way you’ll face issues that you will have to solve on your own, which is my experience one of the best ways to learn and solidify existing knowledge as you come to know what you don’t understand. Another great way to improve is to look at production code from projects building on top of Starknet with Cairo. We mentioned the OpenZeppelin Cairo contracts, but there are many other projects. The only issue with reading production code is that its usually a project with a lot of files, folders and very little documentation so it might be hard to understand what the code is doing without it. That’s why going over the OpenZeppelin contracts might be a better start as they are a library that is used quite often in the Starknet space.

To write good Cairo code, checkout these Cairo coding guidelines from @massilachab and @kalzakdev.

Cairo apps and tooling

Repos with Cairo code you can get inspired from:

To see a list of most Cairo projects, libraries and tools take a look at:

If you want to interact with Starknet programmatically and build dapps, checkout:

There are also additional resources which might be useful:

STARKs

As you get deeper and deeper into the language you might also want to read the Cairo whitepaper. Disclaimer: There is quite a bit of math involved, you do not need to understand most of it. Reading it as is still helps you understand how things work under the hood on a better level. Also if you are curious about the cryptography that goes under the hood of STARKs checkout Starkware’s STARK page.