Variables
There are 3 types of variables in Cairo contracts:
- Local
- declared inside a function
- not stored on the blockchain
- Storage
- declared in the Storage of a contract
- can be accessed from one execution to another
- Global
- provides information about the blockchain
- accessed anywhere, even within library functions
Local Variables
Local variables are used and accessed within the scope of a specific function or block of code. They are temporary and exist only for the duration of that particular function or block execution.
Local variables are stored in memory and are not stored on the blockchain. This means they cannot be accessed from one execution to another. Local variables are useful for storing temporary data that is relevant only within a specific context. They also make the code more readable by giving names to intermediate values.
Here's a simple example of a contract with only local variables:
#[starknet::interface]
pub trait ILocalVariablesExample<TContractState> {
fn do_something(self: @TContractState, value: u32) -> u32;
}
#[starknet::contract]
pub mod LocalVariablesExample {
#[storage]
struct Storage {}
#[abi(embed_v0)]
impl LocalVariablesExample of super::ILocalVariablesExample<ContractState> {
fn do_something(self: @ContractState, value: u32) -> u32 {
// This variable is local to the current block.
// It can't be accessed once it goes out of scope.
let increment = 10;
{
// The scope of a code block allows for local variable declaration
// We can access variables defined in higher scopes.
let sum = value + increment;
sum
}
// We can't access the variable `sum` here, as it's out of scope.
}
}
}
#[cfg(test)]
mod test {
use super::{
LocalVariablesExample, ILocalVariablesExampleDispatcher,
ILocalVariablesExampleDispatcherTrait
};
use starknet::{SyscallResultTrait, syscalls::deploy_syscall};
#[test]
fn test_can_deploy_and_do_something() {
let (contract_address, _) = deploy_syscall(
LocalVariablesExample::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
let contract = ILocalVariablesExampleDispatcher { contract_address };
let value = 10;
let res = contract.do_something(value);
assert_eq!(res, value + 10);
}
}
Storage Variables
Storage variables are persistent data stored on the blockchain. They can be accessed from one execution to another, allowing the contract to remember and update information over time. See Storage for more information.
To write or update a storage variable, you need to interact with the contract through an external entrypoint by sending a transaction.
On the other hand, you can read state variables for free, without any transaction, simply by interacting with a node.
Here's a simple example of a contract with one storage variable:
#[starknet::interface]
pub trait IStorageVariableExample<TContractState> {
fn set(ref self: TContractState, value: u32);
fn get(self: @TContractState) -> u32;
}
#[starknet::contract]
pub mod StorageVariablesExample {
// All storage variables are contained in a struct called Storage
// annotated with the `#[storage]` attribute
#[storage]
struct Storage {
// Storage variable holding a number
value: u32
}
#[abi(embed_v0)]
impl StorageVariablesExample of super::IStorageVariableExample<ContractState> {
// Write to storage variables by sending a transaction
// that calls an external function
fn set(ref self: ContractState, value: u32) {
self.value.write(value);
}
// Read from storage variables without sending transactions
fn get(self: @ContractState) -> u32 {
self.value.read()
}
}
}
#[cfg(test)]
mod test {
use super::{
StorageVariablesExample, StorageVariablesExample::valueContractMemberStateTrait,
IStorageVariableExampleDispatcher, IStorageVariableExampleDispatcherTrait
};
use starknet::{SyscallResultTrait, syscalls::deploy_syscall};
use starknet::testing::set_contract_address;
#[test]
fn test_can_deploy_and_mutate_storage() {
let (contract_address, _) = deploy_syscall(
StorageVariablesExample::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
let contract = IStorageVariableExampleDispatcher { contract_address };
let initial_value = 10;
contract.set(initial_value);
assert_eq!(contract.get(), initial_value);
// With contract state directly
let state = StorageVariablesExample::contract_state_for_testing();
set_contract_address(contract_address);
assert_eq!(state.value.read(), initial_value);
}
}
Global Variables
Global variables are predefined variables that provide information about the blockchain and the current execution environment. They can be accessed at any time and from anywhere!
In Starknet, you can access global variables by using specific functions from the Starknet core library.
For example, the get_caller_address
function returns the address of the caller of the current transaction, and the get_contract_address
function returns the address of the current contract.
#[starknet::interface]
pub trait IGlobalExample<TContractState> {
fn foo(ref self: TContractState);
}
#[starknet::contract]
pub mod GlobalExample {
// import the required functions from the starknet core library
use starknet::get_caller_address;
#[storage]
struct Storage {}
#[abi(embed_v0)]
impl GlobalExampleImpl of super::IGlobalExample<ContractState> {
fn foo(ref self: ContractState) {
// Call the get_caller_address function to get the sender address
let _caller = get_caller_address();
// ...
}
}
}
#[cfg(test)]
mod test {
use super::GlobalExample;
use starknet::{SyscallResultTrait, syscalls::deploy_syscall};
#[test]
fn test_can_deploy() {
let (_contract_address, _) = deploy_syscall(
GlobalExample::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap_syscall();
// Not much to test
}
}