If you encounter an issue while following this tutorial, see Troubleshooting.
Introduction
Welcome to the second installment of the Deploy your first contract guide! 🥇 Starknet contracts are a special superset of Cairo programs that are run by the Starknet sequencer, and as such, have access to Starknet’s state. This installment of the series will therefore walk you though generating and understanding Scarb’s defaultHelloStarknet
contract, which will be used throughout the following installments.
To learn more about Starknet smart contracts, see the Cairo book.
Creating HelloStarknet
Scarb’s default HelloStarknet
contract can be generated by simply running:
Starknet Foundry (default)
test runner. If successful, this should create a new hello_starknet
directory with the following structure:
Understanding HelloStarknet
For the purpose of this tutorial, you can ignore all files in the hello_starknet
directory other than hello_starknet/src/lib.cairo
, which holds the contract’s code:
HelloStarknet
is a simple contract for managing balance. Specifically:
- The contract is defined by encapsulating state and logic within a module annotated with the
#[starknet::contract]
attribute. - The logic that the contract exposes to the outside world is represented by its interface trait, annotated with the
#[starknet::interface]
attribute. Here, our contract defines and publicly exposes the functionsincrease_balance
andget_balance
. - The state is defined within the
Storage
struct, which is always initialized empty. Here, our struct contains a single field calledbalance
of typefelt252
. - The logic itself is defined in the implementation block and annotated with the
#[abi(embed_v0)]
attribute to expose the implementations to the outside world. Here,increase_balance
uses thewrite
method to increasebalance
byamount
andget_balance
uses theread
method to return the value ofbalance
.
HelloStarknet
contract’s storage will be recorded in Starknet’s history.
To review examples of more advanced contracts, see Starknet By Example.