Cairo

The language used for writing smart contracts on StarkNet.

Setup

Debian

Nile

  1. Open project folder in terminal
  2. Source env or install nile in venv
  3. Run nile node
  4. Open another terminal, source env
  5. Run nile compile
  6. Run nile deploy contracts/something.cairo --alias something
  7. 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:

  1. You call the same function more than once
  2. Store the value in different variables
  3. 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