Skip to content

Instantly share code, notes, and snippets.

@soos3d
Last active October 29, 2022 10:34
Show Gist options
  • Save soos3d/cc8fec752e93e03e2db2ead11f52b827 to your computer and use it in GitHub Desktop.
Save soos3d/cc8fec752e93e03e2db2ead11f52b827 to your computer and use it in GitHub Desktop.
Simple Cairo contract to store and retrive a variable.
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin
// Store a variable
// Storage vars are by default not visible through the ABI. They are similar to "private" variables in Solidity.
// This variable is a felt and is called my_value_storage
// From within a smart contract, it can be read with my_value_storage.read() or written to with my_value_storage.write()
@storage_var
func my_value_storage() -> (my_value_storage: felt) {
}
// Declaring getters
// Public variables should be declared explicitly with a getter
// Function to return the variable's value.
@view
func get_my_stored_value{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
my_stored_value: felt
) {
let (my_stored_value) = my_value_storage.read();
return (my_stored_value,);
}
// Set the variable's value.
@external
func set_my_stored_value{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
my_value: felt,
) {
my_value_storage.write(my_value);
return ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment