Cairo
The language used for writing smart contracts on StarkNet.
Setup
Debian
- From apt:
- python3.7+
- python3-dev
- python3-venv
- pypy3-dev
- libgmp3-dev
Nile
- Open project folder in terminal
- Source env or install nile in venv
- Run
nile node
- Open another terminal, source env
- Run
nile compile
- Run
nile deploy contracts/something.cairo --alias something
- https://github.com/OpenZeppelin/nile#call-and-invoke
Conditionals
There do not seem to be any else if
syntax. Also can't figure out &&
or ||
equivalent. Current solution for else if
is nested if/else
:
if x == y:
return (bit=1)
else:
if x == -1:
return (bit=1)
else:
return (bit=0)
end
end
Revoked References
Results from a function return statement are references. If:
- You call the same function more than once
- Store the value in different variables
- Try to use any variable besides the last one
There will be an error.
func double(a) -> (result):
if a == 0:
return (result=0)
else:
return (result=a * 2)
end
end
# This function will fail
func main{output_ptr : felt*}():
# Uncomment below line to automatically create local variables and resovle references
# alloc_locals
let (x) = double(2)
let (y) = double(4)
#
# This call will fail without alloc_locals
#
serialize_word(x + y)
return ()
end