Note! This tutorial is outdated due to changes in the Cairo language. Please refer to the 1.10 syntax in this article: link
Recommended Learning Resources:
-
Documentation:
- Starknet Astro Boot Camp: The documentation for this Boot Camp.
- cairo-book: A book written by community members, inspired by the rust-book. It is currently one of the most comprehensive resources available.
- Cairo documentation on Github: The official documentation, still in progress.
- Documentation summary by Argent X: A documentation summary by Argent X, an inspirational brother from Africa.
-
Code Examples:
- starklings interactive tutorial: An interactive tutorial that runs in a local environment. One of the assignments for Starknet Basecamp.
- starknet-cairo-101: A tutorial for Cairo contracts. One of the assignments for Basecamp.
- awesome-cairo: A compilation of many open-source Cairo projects, including excellent implementation examples.
- OpenZeppelin's contract repository
- Cairo core library source code
Minimum Installation Options:
System: curl, git
IDE: VSCode or any editor you prefer (just not the default notepad on Windows)
MacOS: homebrew
Optional:
If you want to deploy Cairo contracts to testnet and mainnet, you will also need to install the following support:
Account abstraction wallet: Braavos or Argent X
CLI tool for Cairo 0.x.
Installing Rust#
It is recommended to install Rust using rustup (docs). rustup allows you to switch between Rust versions and perform upgrades.
~ $ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Afterwards, restart the command line terminal to verify the successful installation, or execute the following command in the terminal:
~ $ source "$HOME/.cargo/env"
Verify the version:
~ $ rustup --version
rustup 1.25.2 (17db695f1 2023-02-01)
If you don't have curl installed, you can download the installation packages for various systems here.
Installing Cairo#
Clone the latest Cairo repo from Github by entering the following command in the terminal:
git clone https://github.com/starkware-libs/cairo/ ./cairo
Note that some versions of alpha may not be supported by starknet, so we need to specify a specific tag. The stable version currently supported by starknet is v1.0.0-rc0.
cd ./cairo
git checkout tags/v1.0.0-rc0
We can now build the entire Cairo:
cargo build --all --release
Test if it was successful:
cargo run --bin starknet-compile --help
or
./target/release/starknet-compile --version
Executing .cairo Files#
Let's start by writing a test Cairo file. Create a new file in the current directory.
File name: hellostarknetastro.cairo
Content:
use debug::PrintTrait;
fn main() {
'Hello, StarknetAstro!'.print();
}
Execute the following command:
cargo run --bin cairo-run -- hellostarknetastro.cairo
or use the pre-compiled release to execute:
target/release/cairo-run hellostarknetastro.cairo
The terminal will output something similar to:
[DEBUG] Hello, StarknetAstro! (raw: 105807143882116536446217580363080108601441594273569)
Compiling .cairo Files#
Cairo comes with some examples that we can compile using the following command:
First, create a folder in the root directory of Cairo to save the output:
mkdir output
Then use cargo to compile:
cargo run --bin cairo-compile examples/fib.cairo output/fib.json
or use:
target/release/cairo-compile examples/fib.cairo output/fib.json
Here, we are actually outputting intermediate code called Sierra. If we want to output a file that can be executed directly on the Cairo-VM, we need to further compile Sierra into Cairo assembly (casm) files.
cargo run --bin sierra-compile -- output/fib.json output/fib.casm
or
target/release/sierra-compile -- output/fib.json output/fib.casm
Of course, in general, we only need to compile Cairo contracts to casm when we need to deploy them to starknet. We don't usually need to compile pure Cairo code to casm without any special requirements.
Installing Python#
The old Cairo-CLI requires Python 3.9. To avoid conflicts with the already installed version, similar to Rust, it is recommended to use the python version management tool pyenv to install Python.
MacOS:
brew update
brew install pyenv
or
curl https://pyenv.run | bash
Then
pyenv install 3.9
pyenv global 3.9
Linux:
curl https://pyenv.run | bash
Then
pyenv install 3.9
pyenv global 3.9
Verify the successful installation:
python3.9 --version
or simply install Python 3.9 here.
Optional: Installing Cairo 0.x CLI#
This CLI is used to deploy starknet contracts. We need to install GMP environment support first.
Linux:
sudo apt install -y libgmp3-dev
MACOS:
brew install gmp
Next, I recommend creating a virtual environment. Let's create a folder first:
mkdir -p starknetastro/camp1/
cd starknetastro/camp1/
Then create a Python virtual environment:
python3.9 -m venv venv
Activate the virtual environment:
source venv/bin/activate
You should now see (venv) at the beginning of the terminal. Upgrade PIP in the virtual environment:
(venv)camp1 $ pip install --upgrade pip
Install the CLI:
Linux:
(venv) camp1 $ pip install cairo-lang
MACOS (M1 chip):
(venv) camp1 $ CFLAGS=-I`brew --prefix gmp`/include LDFLAGS=-L`brew --prefix gmp`/lib pip install cairo-lang
Verify the installation:
(venv) camp1 $ starknet --version
Output:
starknet 0.11.1