Journey through Cairo I — Setting up Protostar and ArgentX for local development

Darlington Nnam
7 min readAug 29, 2022

--

Having been following the numerous scaling solutions available for Ethereum in the past few months, i recently got keenly interested in Starknet, the Layer 2 scaling solution created by the team at Starkware.

Whilst the race around scaling Ethereum has mostly been focused on EVM compatibility in order to support the already thriving ecosystem around Ethereum, the guys at Starkware, has taken a quite bolder move to create a totally new VM with a native language for writing smart contracts Cairo.

Whilst this has its numerous advantages, as they get zk-compatibility from ground up, it poses a lot of challenges mostly surrounding Developer’s adoption. Getting started in a totally new ecosystem requires a lot, from learning new languages to getting accustomed to the relatively new development tools, which for most solidity developers, is time intensive and risky.

I recently decided to explore Cairo for Starknet development, and for the next few months, i’d be documenting my Cairo Journey to help out other persons looking to get started in the space.

CAIRO

One of the most popular sayings used within the starknet ecosystem is “CAIRO IS HELL”, and they honestly might not be wrong, cause having spent the past few weeks going through the official documentation, won’t be exaggerating to say Cairo is about the hardest programming language i’ve encountered thus far. But…wait a minute, let’s remember at the time of this writing Cairo in itself is relatively new, and according to the cofounder of Cairo, the language was initially created for in-house use and there was no initial intention of letting it out into the public, but currently there’s a lot of works going into v1.0, mostly targeted at a better development experience, so things will definitely get better.

Now what is Cairo?

Cairo is the first Turing-complete language for scripting provable programs for general computations.

In the next few chapters of this article, we are going to be diving deep into some basic fundamentals of Cairo, and getting our hands dirty, writing smart contracts on Starknet, but for today, lets focus on setting up our local development environment, and deploying our first contract!

PROTOSTAR FOR LOCAL DEVELOPMENT

There are quite a few frameworks available for local Cairo development, amongst which we have Protostar, from the team at Software Mansion, Nile from the Openzeppelin team, and Hardhat.

Tried setting up both Protostar and Nile, and i think i found Protostar easier to setup and get started with, but there’s just a little caveat…There is currently no support for Windows, so windows users might have to look at setting up Windows Subsystem for Linux(WSL) or try out the Nile framework.

To setup Windows Subsystem for Linux check this tutorial here.

To get started with Protostar, you should have a git executable (>= 2.28) to be specified in the $PATH.

Installation

To install on both Windows and Linux:

  1. run the following command:
curl -L https://raw.githubusercontent.com/software-mansion/protostar/master/install.sh | bash

2. Restart your terminal

3. Run protostar -v to confirm installation. If properly installed, you should see your protostar version and Cairo-lang version like this:

Initializing a new Project

Protostar similar to Truffle for solidity development, can be installed once and used for multiple projects.

To initialize a new Protostar project:

  1. run the following command:
protostar init

2. It would then request for the project’s name and the library’s directory name, you’d need to fill in this, to successfully initialize your project.

Project Structure

As can be seen from the image above, initializing a new Protostar project, creates a folder containing 3 new folders and a protostar.toml file.

lib folder — is the folder required to contain all your project dependencies.

src folder — is the folder required to contain your contract’s source codes.

test folder — is the folder required to contain your test files.

protostar.toml is the file which contains your command configurations. According to the official docs, it can be used to avoid passing arguments every time you run a command. Protostar searches for an argument value in the ["protostar.COMMAND_NAME"] section, for example:

# ...["protostar.build"]
cairo-path = ["./lib/cairo_contracts/src"]

Within the src folder, we already have a boilerplate contract basic.cairo, created for us by default, which we are going to be deploying in this tutorial, but to deploy and interact with our contracts, we need a wallet…ArgentX

basic.cairo

%lang starknetfrom starkware.cairo.common.math import assert_nnfrom starkware.cairo.common.cairo_builtins import HashBuiltin@storage_varfunc balance() -> (res : felt):end@externalfunc increase_balance{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}(amount : felt):with_attr error_message("Amount must be positive. Got: {amount}."):assert_nn(amount)endlet (res) = balance.read()balance.write(res + amount)return ()end@viewfunc get_balance{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (res : felt):let (res) = balance.read()return (res)end@constructorfunc constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():balance.write(0)return ()end

INSTALLING AND SETTING UP ARGENT X

At the time of writing this article, Metamask, which is the most popularly used wallet in EVM development, and which you are most likely more familiar with, does not support Starknet yet, but we have two other wallets ArgentX and Braavos, which can be used for interacting with native Starknet contracts. For today’s tutorial, we will be going over setting up ArgentX.

ArgentX — Account Abstraction

Amongst the breaking changes in Starknet as compared to EVM-based chains, is the implementation of Account Abstraction, which defined on a high level, refers to the abstraction of the concept of accounts and related mechanisms from the core protocol of the chain. So while identifying users is key to the system’s operation, we look to allow flexibility and extensibility in aspects relating to how accounts are verified and operate.

In simple words, there are no Externally owned accounts on Starknet but every account is a smart contract with built-in multicall..

For deeper details on Account Abstraction, check out my article here.

Installing ArgentX

To install ArgentX:

  1. On chrome, visit the Chrome extension marketplace here, for other browsers, checkout their extension marketplaces.
  2. Add the extension to your Chrome browser.
  3. Following the instructions, create a new password, then create a new account.

You’d notice unlike other wallets like Metamask implementing Externally owned account model, creating a new account or wallet on ArgentX requires deploying a new contract. This is due to Account Abstraction, cause as we said earlier, every account is a smart contract.

Once we are done with setting up ArgentX, we can now deploy our first ever smart contract on Starknet.

Deploying to Starknet With Protostar

Like we noticed earlier, initializing a new Protostar project, created a boilerplate contract basic.cairo, which we are going to be deploying.

To deploy our contract on Starknet:

  1. We need to build our contract. To do this, run:
protostar build

2. We’d run the Protostar deploy command, passing in the test network:

protostar deploy ./build/main.json --network alpha-goerli
  • the deploy command is an in-built command from protostar.
  • the ./build/main.json specifies the path to our compiled file.
  • the --network variable is used to specify the network we are deploying to.

For more info on protostar deploy commands, check out the docs here.

Once deployed, we’d get our contract address and transaction hash outputted on the screen, but how do we view and interact with this contract?

Voyager

Similar to Etherscan on Ethereum, Voyager is the block explorer for StarkNet. Developed by an in-house team at Nethermind, it provides a window for anyone to interact with their Starknet contracts.

Deployments at Starknet, might take a while, so do not fret, if after going to check your contract on Voyager, you get an error that says “Sorry, we are unable to locate this contract.”

Since we deployed to the Goerli testnet, we’d need to switch from Mainnet to Goerli, at the top right part of Voyager.

After waiting a while, we can now see our contract reflect on Voyager here.

We could also read and write to our contract from Voyager, but we’d need some test ether for gas fees, which you can get from Starknet’s faucet here.

Conclusion

If you got to this point, congratulations! you just successfully deployed your first contract to Starknet, and are on your way to scaling your L1 dapps.

But a quick one, just before you go:

Did you notice that, unlike when we deploy to Ethereum and other EVM-based chains, we didn’t particularly need a signer object to pay the gas fees for our contract deployment?

Yeap, that’s right! we didn’t particularly deploy our contract from our Argent wallet, and since Starknet is still in Alpha, gas fees are not yet particularly mandatory for contract deployments, but this will definitely change in the future!

If you gained some knowledge from this, then endeavour to connect with me on the following socials, especially on Twitter, where i share my little findings on Cairo!

Twitter: https://twitter.com/0xdarlington

LinkedIn: https://www.linkedin.com/in/nnamdarlington

Github: https://github.com/Darlington02

--

--