Cairo language preview
1. Starknet contract
attributes, traits, and storage
horizon-dark
#[starknet::contract]
mod Counter {
#[storage]
struct Storage {
count: u128,
}
#[external(v0)]
fn increment(ref self: ContractState, amount: u128) {
let current = self.count.read();
self.count.write(current + amount);
}
} atom-one-dark
#[starknet::contract]
mod Counter {
#[storage]
struct Storage {
count: u128,
}
#[external(v0)]
fn increment(ref self: ContractState, amount: u128) {
let current = self.count.read();
self.count.write(current + amount);
}
} github-dark
#[starknet::contract]
mod Counter {
#[storage]
struct Storage {
count: u128,
}
#[external(v0)]
fn increment(ref self: ContractState, amount: u128) {
let current = self.count.read();
self.count.write(current + amount);
}
} dracula
#[starknet::contract]
mod Counter {
#[storage]
struct Storage {
count: u128,
}
#[external(v0)]
fn increment(ref self: ContractState, amount: u128) {
let current = self.count.read();
self.count.write(current + amount);
}
} nord
#[starknet::contract]
mod Counter {
#[storage]
struct Storage {
count: u128,
}
#[external(v0)]
fn increment(ref self: ContractState, amount: u128) {
let current = self.count.read();
self.count.write(current + amount);
}
} github
#[starknet::contract]
mod Counter {
#[storage]
struct Storage {
count: u128,
}
#[external(v0)]
fn increment(ref self: ContractState, amount: u128) {
let current = self.count.read();
self.count.write(current + amount);
}
} 2. Generic function
felt252, integer types, and pattern matching
horizon-dark
fn fib(n: felt252) -> felt252 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
} atom-one-dark
fn fib(n: felt252) -> felt252 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
} github-dark
fn fib(n: felt252) -> felt252 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
} dracula
fn fib(n: felt252) -> felt252 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
} nord
fn fib(n: felt252) -> felt252 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
} github
fn fib(n: felt252) -> felt252 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
} 3. Macros and syscalls
panic!/assert!/array!, and Starknet syscalls
horizon-dark
fn withdraw(ref self: ContractState, amount: u128) {
let caller = get_caller_address();
let contract = get_contract_address();
let now = get_block_timestamp();
assert!(amount > 0, "amount must be positive");
let mut history: Array<felt252> = array![];
if caller == contract {
panic!("cannot withdraw to self");
}
history.append(now.into());
} atom-one-dark
fn withdraw(ref self: ContractState, amount: u128) {
let caller = get_caller_address();
let contract = get_contract_address();
let now = get_block_timestamp();
assert!(amount > 0, "amount must be positive");
let mut history: Array<felt252> = array![];
if caller == contract {
panic!("cannot withdraw to self");
}
history.append(now.into());
} github-dark
fn withdraw(ref self: ContractState, amount: u128) {
let caller = get_caller_address();
let contract = get_contract_address();
let now = get_block_timestamp();
assert!(amount > 0, "amount must be positive");
let mut history: Array<felt252> = array![];
if caller == contract {
panic!("cannot withdraw to self");
}
history.append(now.into());
} dracula
fn withdraw(ref self: ContractState, amount: u128) {
let caller = get_caller_address();
let contract = get_contract_address();
let now = get_block_timestamp();
assert!(amount > 0, "amount must be positive");
let mut history: Array<felt252> = array![];
if caller == contract {
panic!("cannot withdraw to self");
}
history.append(now.into());
} nord
fn withdraw(ref self: ContractState, amount: u128) {
let caller = get_caller_address();
let contract = get_contract_address();
let now = get_block_timestamp();
assert!(amount > 0, "amount must be positive");
let mut history: Array<felt252> = array![];
if caller == contract {
panic!("cannot withdraw to self");
}
history.append(now.into());
} github
fn withdraw(ref self: ContractState, amount: u128) {
let caller = get_caller_address();
let contract = get_contract_address();
let now = get_block_timestamp();
assert!(amount > 0, "amount must be positive");
let mut history: Array<felt252> = array![];
if caller == contract {
panic!("cannot withdraw to self");
}
history.append(now.into());
}