@event - Represents an event
@storage_var - Represents a contract state variable (i.e. mapping, address, etc.)
@constructor - A constructor which runs once at deployment
@view - Used to read from storage_vars
@external - Used to write to storage_vars
@l1_handler - Used to process a message sent from an L1 contract
# 1. Structs
struct MyStruct:
member my_name : felt
member my_age : felt
end
----------------------------
# 2. Event
# we can pass structs to events
func NewStruct(new_struct : MyStruct):
end
----------------------------
# 3. Storage
@storage_var
func My_Struct(struct_id : felt) -> (struct-i : MyStruct):
end
----------------------------
# 5. Storage getters
@view
func my_struct(struct_id : felt) -> (my_info : MyStruct):
return My_Struct.read(struct_id)
end
----------------------------
# 7. Non-Constant Functions
@external
func new_struct(id : felt, name : felt, age : felt):
let my_struct = MyStruct(name, age)
My_Struct.write(id, my_struct)
NewStruct.emit(my_struct)
return ()
end
# = 448378203247
let hello_string = 'hello'
%lang starknet
%builtins range_check
# import to use alloc
from starkware.cairo.common.alloc import alloc
# view function that returns a felt
@view
func array_demo(index : felt) -> (value : felt):
# Creates a pointer to the start of an array.
let (my_array : felt*) = alloc()
# sets 3 as the value of the first element of the array
assert [felt_array] = 3
# sets 15 as the value of the second element of the array
assert [felt_array + 1] = 15
# sets index 2 to value 33.
assert [felt_array + 2] = 33
assert [felt_array + 9] = 18
# Access the list at the selected index.
let val = felt_array[index]
return (val)
end
%lang starknet
%builtins pedersen range_check
from starkware.cairo.common.cairo_builtins import HashBuiltin
# Function that receives an array as parameter, so it actually receives the array length and # the array itself
@external
func array_play(array_param_len : felt, array_param : felt*) -> (res: felt):
# read first element of the array
let first = array_param[0]
# read last element of the array
let last = array_param[array_param_len - 1]
let res = first + last
return (res)
end
# Account struct
struct Account:
member isOpen: felt
member balance: felt
end
# Mapping named "accounts_storage" that holds the account details for
# each user using his address as key
@storage_var
func accounts_storage(address: felt) -> (account: Account):
end
#bad case
@external
func bad_normalize_tokens{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (normalized_balance : felt):
let (user) = get_caller_address()
let (user_current_balance) = user_balances.read(user)
let (normalized_balance) = user_current_balance / 10**18
return (normalized_balance)
end
#bad case
@view
func bad_get_nonce{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}() -> (nonce : felt):
let (user) = get_caller_address()
let (nonce) = user_nonces.read(user)
user_nonces.write(user, nonce + 1)
return (nonce)
end
@external
func bad_claim_tokens{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr}():
#can be zero
let (user) = get_caller_address()
let (user_current_balance) = user_balances.read(sender_address)
user_balances.write(user_current_balance + 200)
return ()
end