StarknetAstro

StarknetAstro

(BootCamp1) Cairo Setup

image

Note! This tutorial is outdated due to changes in the Cairo language. For the 1.10 syntax, please refer to this article: https://starknetastro.xlog.app/Starknet_Shanghai_Workshop_DAY1#

Recommended Learning Materials:

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.

Install Rust#

It is recommended to install rust using rustup (docs). rustup allows you to switch between rust versions and upgrade.

~ $ 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 also download the installation package for various systems here:

https://forge.rust-lang.org/infra/other-installation-methods.html#rustup

Install Cairo#

Enter the following command in the terminal to clone the latest Cairo repo from Github:

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 supported by starknet at this stage is v1.0.0-rc0.

cd ./cairo
git checkout tags/v1.0.0-rc0

Afterwards, we can 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

Execute .cairo Files#

Let's start by writing a test Cairo file. Create a new file in the current directory.
The file name is: hellostarknetastro.cairo

The content is:

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)

Compile .cairo Files#

Cairo comes with some examples that we can compile with the following command:
First, create a folder in the cairo root directory 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, which can be further compiled into Cairo assembly (casm) files that can be executed directly on the Cairo-VM.

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 need to compile plain Cairo code to casm unless there are specific requirements.

Install Python#

The old Cairo-CLI requires Python 3.9. To avoid conflicts with existing installations, similar to rust, we recommend using 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 if it was installed successfully:

python3.9 --version

Or simply install Python 3.9 directly:
https://www.python.org/downloads/release/python-3915/

Optional: Install 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

Then, I recommend creating a virtual environment. Let's create a new 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

At this point, you should 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 if it was installed successfully:

(venv) camp1 $ starknet --version

Output:

starknet 0.11.1
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.