Tagged

Setting up a Cairo development environment

The ultimate development environment with Protostar, Argent X and Starkscan

Darlington Nnam
Jan 24, 2023

It's a popular opinion that better development tools accelerate a developer’s productivity, but the arduous task lies in finding that perfect development environment. 

If it’s your first time experimenting with Cairo and StarkNet, you’re probably on the hunt for this perfect tool to make your developer journey much smoother. 

That’s why we’ve written this article!  

We’ll help you find and set up everything you need to build in StarkNet.

Cairo?  — What’s that?

If it’s your first time encountering StarkNet/Cairo, there’s no need to feel lost. We’ll explain everything you need to know to get started. 

StarkNet is a type of Ethereum Layer 2 scaling solution known as a ZK rollup. ZK rollups, like StarkNet, enable decentralized applications to achieve unlimited scale for their computation — without compromising Ethereum’s decentralization and security, thereby solving the Scalability Trilemma.

Cairo is StarkNet’s native language and the first Turing-complete language for scripting provable programs (where one party can prove to another that a certain computation was executed correctly) for general computations.

What is a Development Environment?

A development environment is a workspace with a set of processes and programming tools to help developers efficiently build, test, and debug an application or program.

Similar to how we have a plethora of options for developing on Ethereum with solidity (Truffle, Foundry, Hardhat, etc), we also have a good amount of options for Cairo development on StarkNet. Amongst them are Protostar (developed by the Software Mansion Team), StarkNet CLI (official development framework), Nile (from the OpenZeppelin Team), and Hardhat (which is a plugin developed by ShardLabs).

But for today’s tutorials, we will mostly play around with Protostar while using Argent X for wallet interactions and Starkscan for surfing the network.

What is Protostar?

Protostar is a development environment/toolchain for developing and testing Cairo contracts on StarkNet. 

We’ve found Protostar a lot easier to set up and use among all development frameworks. There is just a single caveat. Protostar currently has no support for Windows, so windows users might have to look at setting up Windows Subsystem for Linux(WSL) in order to use Protostar or try out the other options mentioned earlier.

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

Installing Protostar

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 the installation. If properly installed, you should see your protostar and Cairo-lang version like this:

Protostar properly installed on your machine

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 the project’s name and the library’s directory name. You’d need to fill in this to initialize your project successfully.

Initialize your StarkNet project properly

Project Structure

New Protostar project

As 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"]

Having successfully set up Protostar, we can now easily write, compile and deploy contracts to StarkNet, but wait a minute…we would need a wallet to effectively interact with these deployed contracts, right? That’s where Argent X comes in to save the day!

Argent X, the wallet for StarkNet developers 

Argent X is the most popular and only open-source wallet on StarkNet

Available as a browser extension across different browsers’ web stores, Argent X helps you create, manage, and connect accounts to decentralized applications built on StarkNet.

Installing Argent X is similar to any other wallet in crypto. However, if creating a new wallet is a new process, check out our guide on how to create an Argent X wallet. 

Once you’ve set everything up, we will deploy and interact with a StarkNet contract.

Deploying Contracts using Protostar

Thanks to Development frameworks like Protostar, deploying Cairo contracts can be done using a few commands from the terminal.

For this tutorial, we will deploy the boilerplate contract with Protostar found in src/basic.cairo

After the 0.10.3 upgrade to the StarkNet network, deploying a contract requires declaring first, then invoking the Universal Deployer Contract passing in the declared hash as a constructor parameter. 

To proceed with deployment, we will first need to compile/build the contract:

Protostar build

Building projects contracts                                                                                                                                                                

Class hash for contract "basic": 0x745f2d517414cc07d74fb162fca62492f11eb589189ab74c12c7a535ebac2ec
12:00:26 [INFO] Execution time: 2.16 s

Then we will move unto declaring the contract:

protostar declare ./build/basic.json --network testnet --account-address 0x046c5877EE8d297107Bd8E507Fb97b8cAB4ECD6C83892AD92EA40100412b4976 --max-fee auto

And finally, we can deploy using Protostar’s deploy command:

protostar deploy [classhash] --network testnet --account-address 0x046c5877EE8d297107Bd8E507Fb97b8cAB4ECD6C83892AD92EA40100412b4976 --max-fee auto --inputs 10

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

Become familiar with Starkscan

Similar to Etherscan on Ethereum, Starkscan is a block explorer for StarkNet, which provides a window for anyone to interact with their StarkNet contracts.

To find and interact with our Contract on StarkNet, we can simply copy the outputted contract address and head over to testnet.starkscan.co to search for the Contract.

Starkscan homepage

Once the Contract has been deployed on the network, we should be able to make read/write calls to it using our Argent X wallet on Starkscan.

Writing to a StarkNet Contract

Writing to a Contract changes the Contract’s state and, as such, requires a wallet to sign and pay for transactions. 

To connect your Argent X wallet to Starkscan, follow these simple steps:

1. First, click on the “Connect to Wallet” button in the “Write Contract” section.

Connecting your StarkNet Argent X wallet to Starkscan

2. Select “Argent X”

Selecting Argent X as your StarkNet wallet on Starkscan

3. Finally, approve by clicking on “Connect”

Approving Starkscan in Argent X

You should be able to make write calls to your StarkNet contracts.

Reading a StarkNet contract

Read calls are made to view functions. They don’t alter or modify the Contract storage and can be performed without a wallet connection. For example, we could easily call the get_balance function without connecting our wallet.

A Cairo contract on Starkscan

Conclusion

Having reached this point, we’re confident you now have a StarkNet development Environment set up, and can easily compile, declare, deploy and interact with your Cairo Contracts.

If you have any questions as regards this, reach out to me @0xdarlington, I’d love to help you build on StarkNet with Argent X.

For more developer resources, follow us across our socials:

Twitter — @argentHq

Engineering Twitter — @argentDeveloper

LinkedIn — @argentHq

Youtube — @argentHQ

Interested in the topic? Join us!

We’re always looking for outstanding engineers to help us pioneer better UX and security in crypto. We work remotely across Europe.

Argent Careers

Related Blogs

Getting started with StarkNet

The StarkNet ecosystem is exploding and here's how you can get involved

Understanding The Universal Deployer Contract And Deploying your Contracts through Argent X

Deploying contracts on Starknet using the Universal deployer contract

Part I: WTF is Account Abstraction

Learn why it’s a game changer for crypto's adoption

Own It

We use 🍪 cookies to personalise your experience on Argent. Privacy Policy

Accept

HQ London, made with ❤️ across Europe