Cairo Mini Series

Extropy.IO
5 min readJul 30, 2023

--

Hello there, welcome to this Cairo mini-series where we will guide you step by step through the installation process, building and deploying your first Cairo project, testing your contract, and sharing some good practices.

In this article, we will walk you through the process of installing everything you need to get set up and ready to build your first Cairo project.

Installation

Scarb installation

Nowadays, there are only three things needed to get started building your first Cairo contract. Scarb is the first element required for building our contract.

Visit their installation page and follow the provided steps. It is recommended to install it via asdf as this will help us switch seamlessly to a upgrade/downgrade scarb whenever we need.

For now, please install scarb v0.5.1 in order to declare and deploy contracts on Starknet.

asdf plugin add scarb https://github.com/software-mansion/asdf-scarb.git
asdf install scarb 0.5.1
asdf global scarb 0.5.1
asdf reshim scarb

Once you have completed the installation, we can check if it was successful.

Run the following command:

scarb --version

You should receive the version of Scarb along with the version of Cairo.

scarb 0.5.1 (798acce7f 2023-07-05) cairo: 2.0.1 (https://crates.io/crates/cairo-lang-compiler/2.0.1)

Cairo comes bundled with Scarb, so there is no need to install it separately anymore.

Language Server Installation

Next, to ensure everything works with Scarb, we just need to either visit the VS Code marketplace or search for Cairo 1.0 in your VS Code extensions tab and install it.

Once the extension is installed, Scarb should automatically detect it, and the language server should start functioning properly.

Starkli CLI

Last but not least, we need to install a Command Line Interface (CLI) to declare and deploy our smart contracts.

If you haven’t heard about Starkli, it is similar to the cairo-lang, but Starkli is written in Rust and is a blazing fast CLI tool. Furthermore, it comes with new features that we will explore later in this series.

For more information, you can check out Starknet Community Call #48, where Jonathan presents the new CLI tool written in Rust.

Now, let’s proceed with installing the Starkli CLI tool.

Go to the Starkli repository and follow the provided installation steps.

Once installed, run the following command to verify if the installation was successful:

starkli --version
>>
starkli 0.1.8 (d9bb4f7)

If the installation has been successful, you should see the installed version of Starkli displayed.

Cairo installation (optional)

Before we create your first project, since scarb has integrated Cairo, there is no longer a need to install Cairo separately. It is integrated with scarb and works out of the box.

However, if you wish to use for example, an unreleased version of Cairo for development purposes, you can follow the installation process below:

Installing Cairo with a script -> here

Installing Cairo manually -> here

Creating your first project

Now that we are all set up, let’s create your first project with scarb.

scarb new hello_world
cd hello_world

Once created, let’s delve into the project structure.

.
├── Scarb.toml
└── src
└── lib.cairo

By default, scarb new will generate a Scarb.toml file, known as the manifest.

This file contains metadata necessary for compiling the package. Additionally, it creates a src folder that includes the lib.cairo file with an example Cairo code.

Now that we have the initial project structure, let’s make some modifications to the Scarb.toml. To enable the compilation of Starknet contracts, we need to add the following line to our Scarb.toml:

[[target.starknet-contract]]

In addition, we will have to declare the dependency of the Starknet version. We do this by adding the following line:

[dependencies]
starknet = ">=2.0.1"

In the end your Scarb.toml should look like this:

[package]

name = "hello_world"
version = "0.1.0"

[dependencies]
starknet = ">=2.0.1"

[[target.starknet-contract]]

There are other exciting things you can do here, such as adding external dependencies, defining custom scripts or profiles, and much more. If you are interested in learning more, you can refer to the Cheatsheet for additional information.

Creating our contract

Let’s create a new file called contract.cairo in our src folder.

Once created, we can delete all the code from the lib.cairo file and import the module that we will create in contract.cairo.

Your lib.cairo file should look like this:

mod contract;

Let’s implement our first contract. If you need to catch up on the new syntax of Cairo, you can refer to our article titled

Starknet v0.12 Quantum Leap

where we covered the syntax changes.

Here’s an example of how your contract.cairo file should look:

#[starknet::interface]
trait IHelloWorld<TContractState>{
fn hello_world(self: @TContractState) -> felt252;
}

#[starknet::contract]
mod contract{

#[storage]
struct Storage{
hello_world: felt252,
}

#[constructor]
fn constructor(ref self: ContractState){
self.hello_world.write('Hello World!');
}

#[external(v0)]
impl HelloWorld of super::IHelloWorld<ContractState>{
fn hello_world(self: @ContractState) -> felt252 {
self.hello_world.read()
}
}
}

Now that we have our smart contract, let’s try to build it with scarb and generate the Sierra JSON output.

Run the following command in your terminal:

scarb build

If everything worked correctly, you should notice a new folder that was created by scarb called target.

There we will have our sierra file that was generated by the compiler. Currently, by default, scarb generated 3 different files within the target/dev folder.

The one that we will declare and deploy is the hello_world_contract.sierra.json file.

Declaring and deploying your contract

Now, for the final step in our process.

We will deploy the smart contract using starkli.

If you haven’t set up your account with starkli, please follow the steps below:

starkli signer keystore new demo-key.json
starkli account oz init demo-account.json --keystore ./demo-key.json
starkli account deploy demo-account.json --keystore ./demo-key.json

Now, you should receive an address where you need to send some test funds to deploy your account.

If you need test funds, you can request them from here. Once you have sent the test funds, you can proceed with deploying your account.

Now that we are prepared, it’s time to declare your contract. Run the following command:

starkli declare target/dev/hello_world_contract.sierra.json --account demo-account.json --keystore ./demo-key.json --compiler-version 2.0.1 --network goerli-1 --watch

Now that you finished declaring your contract, you should receive a hash address. Use this and replace the HASH in the following line and run the command.

starkli deploy HASH --account demo-account.json --keystore ./demo-key.json --network goerli-1 --watch

And that’s it! We have successfully deploy our contract on Testnet.

Congratulations! Our newly deployed contract can be found here.

Additional resources:

To learn more about Cairo and other related tools, you can explore the following resources:

Cairo Book — https://book.cairo-lang.org/

Cairo-by-Example — https://cairo-by-example.com/

Starknet-by-Example — https://starknet-by-example.voyager.online/

Cairo Workshop by David Barreto — https://www.youtube.com/watch?v=7Yfsm7V9R4A&ab_channel=Topology

If you want to learn more about zero knowledge proofs

Entry level — try the Zero Knowledge Bootcamp from Encode.club

Advanced level — Advanced ZKP Course from Extropy

--

--

Extropy.IO

Oxford-based blockchain and zero knowledge consultancy and auditing firm