The New Syntax of Cairo 1 Smart Contracts — From tag v1.0.0-alpha.6 to v2.1.0-rc0 of starknet-compile

El Mathe
Coinmonks
Published in
3 min readJul 18, 2023

--

Embark on a seamless journey of smart contract development with Cairo 1, as it evolves to resemble Rust, offering enhanced features, simplified syntax, and effortless deployment in the thriving Starknet ecosystem.

Photo by MEUM MARE: https://www.pexels.com/photo/hieroglyphs-around-ancient-god-carving-17004827/

It’s 2023, and Cairo is becoming increasingly popular for writing Layer 2 smart contracts. Before 2022, writing a Cairo smart contract was challenging due to the complexity of learning it (neither Assembly nor Python 😊).

This year, Cairo has improved and has become much like Rust. In many articles from basecampers before June 2023, to install Cairo, you were advised to use the tag v1.0.0-alpha.6, and everything worked smoothly.

However, six months after this tag, the framework evolved, and several corrections were made, especially with the tag v2.0.0 and beyond. There is a risk of encountering the following error when trying to deploy your code:

“Got BadRequest while trying to access https://alpha4.starknet.io/gateway/add_transaction. Status code: 400; text: {“code”: “StarknetErrorCode.INVALID_CONTRACT_CLASS”, “message”: “Compiled versions older than 1.1.0 are not supported. Got an old version.”}.

I recommend upgrading to “v2.1.0-rc0.”

In this short article, I’ll talk about some changes in your workflow.

How to Upgrade from the “v1.0.0-alpha.6” tag to “v2.1.0-rc0”

First, check the version you have by executing the command “starknet-compile — version.”

If you downloaded the Cairo source code from GitHub (https://github.com/starkware-libs/cairo), there is no need to upgrade to “v2.1.0-rc0” since the main branch as of July 18, 2023, is already on this tag. However, it’s always interesting to be on a specific version to ensure compatibility with other tools when changes are significant.

If you cloned “Cairo” into the folder “~/.cairo,” as many do, access it and then enter the command “git checkout tags/v2.1.0-rc0.”

Once you are on this branch, rebuild your tools with “cargo build — release.”

The Smart Contract Structure

In the alpha 1 tag, a smart contract looked like this:

#[starknet]
mod Asset{
struct Storage{
balance : felt252
}

#[constructor]
fn constructor(){
balance::write(10);
}

#[event]
fn BalanceIncreased(amount : felt252){}

#[view]
fn get_balance() -> felt252{
balance::read()
}

#[external]
fn increase_balance(ref self : ContractState, amount : felt252){
let new_balance = balance::read() + amount;
balance::write(balance::read() + amount);
BalanceIncreased(new_balance);
}

In the new configuration, it should look like this:

#[starknet::interface]
trait IAssetContract<TContractState>{
fn get_balance(self : @TContractState) -> felt252;
fn increase_balance(ref self : TContractState, amount : felt252);
}

#[starknet::contract]
mod Asset{
#[storage]
struct Storage{
balance : felt252
}

#[constructor]
fn constructor(ref self : ContractState){
self.balance.write(10);
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event{
BalanceIncreased : BalanceIncreased
}

#[derive(Drop, starknet::Event)]
struct BalanceIncreased{
amount : felt252
}

#[external(v0)]
impl Asset of super::IAssetContract<ContractState>{
fn get_balance(self : @ContractState) -> felt252{
self.balance.read()
}

fn increase_balance(ref self : ContractState, amount : felt252){
self.balance.write(self.balance.read() + amount);
self.emit(Event::BalanceIncreased(BalanceIncreased{amount}));
}
}
}

Let’s talk about some changes:

Compilation and Deployment of the Smart Contract

I encountered some issues deploying smart contracts on Starknet in recent days, but with this explanation, you’ll be able to resolve them.

In the previous version of Starknet, you didn’t need to specify “single file” when compiling a single file. Now, when you build, don’t forget to mention it like this:

starknet-compile src/smartcontract.cairo output/smartcontract.json --single-file

After the build, declare the smart contract (as in the previous version). Simply use the command:

starknet declare --contract output/smartcontract.json --account account_name

Once you have the hash class of your code, you can easily deploy it on Starknet. Remember, since the recent Starknet update, the speed has reached new dimensions, so don’t hesitate to test it out.

In conclusion, Starknet and Cairo are two rapidly evolving technologies. Therefore, some things might change and other rules might be adopted. If there are any concepts unclearly expressed in this text, we can discuss and correct them. I thank FeedTheFed for their brilliant article on the new version of Cairo, as well as the Starknet Congo community for the questions that led me to research and write this article.

Thank you.

--

--

El Mathe
Coinmonks

Building privacy on Web3 using ZKP and WASM. Experience in Rust, Cairo and Solidity @elielmathe on Twitter and LinkedIn