Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.

Commit 0d79abb

Browse files
Nonnyjoejulio4
andauthored
feat: Cairo cheat sheet (#104)
* implemented match and loop * added array, felt and casting * added events and struct * updated felt syntax * resolved all corrections * chore: revisions * Update type_casting.md --------- Co-authored-by: julio4 <jules.doumeche@gmail.com>
1 parent 88f5545 commit 0d79abb

19 files changed

+344
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Code generated by scarb DO NOT EDIT.
2+
version = 1
3+
4+
[[package]]
5+
name = "cairo_cheatsheet"
6+
version = "0.1.0"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "cairo_cheatsheet"
3+
version = "0.1.0"
4+
5+
[dependencies]
6+
starknet = ">=2.3.0"
7+
8+
[[target.starknet-contract]]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[starknet::contract]
2+
mod arrayExample {
3+
#[storage]
4+
struct Storage {}
5+
6+
7+
#[external(v0)]
8+
#[generate_trait]
9+
impl external of externalTrait {
10+
fn createArray(self: @ContractState, numOne: u32, numTwo: u32, numThree: u32) -> bool {
11+
let mut Arr = ArrayTrait::<u32>::new();
12+
Arr.append(numOne);
13+
Arr.append(numTwo);
14+
Arr.append(numThree);
15+
16+
let ArrLength: usize = Arr.len();
17+
assert(ArrLength == 3, 'Array Length should be 3');
18+
19+
let first_value = Arr.pop_front().unwrap();
20+
assert(first_value == numOne, 'Both values should match');
21+
22+
let second_value = *Arr.at(0);
23+
assert(second_value == numTwo, 'Both values should match too');
24+
25+
//Returns true if an array is empty, then false if it isn't.
26+
Arr.is_empty()
27+
}
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[starknet::contract]
2+
mod feltExample {
3+
use starknet::{ContractAddress, get_caller_address};
4+
5+
#[storage]
6+
struct Storage {
7+
userName: LegacyMap::<ContractAddress, felt252>,
8+
}
9+
10+
#[external(v0)]
11+
#[generate_trait]
12+
impl external of externlalTrait {
13+
fn storeName(ref self: ContractState, name: felt252) -> felt252 {
14+
self.userName.write(get_caller_address(), name);
15+
16+
let welcomeMsg: felt252 = 'Welcome to StarknetByExample';
17+
welcomeMsg
18+
}
19+
20+
fn viewName(self: @ContractState, Add: ContractAddress) -> felt252 {
21+
self.userName.read(Add)
22+
}
23+
}
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mod array_example;
2+
mod mapping_example;
3+
mod felt_example;
4+
mod loop_example;
5+
mod match_example;
6+
mod struct_example;
7+
mod type_casting_example;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[starknet::contract]
2+
mod loopExample {
3+
#[storage]
4+
struct Storage {}
5+
6+
#[external(v0)]
7+
#[generate_trait]
8+
impl external of externlalTrait {
9+
fn gatherEvens(ref self: ContractState, maxLimit: u32) -> Array<u32> {
10+
let mut i: u32 = 0;
11+
let mut Arr = ArrayTrait::new();
12+
loop {
13+
if i == maxLimit {
14+
break;
15+
};
16+
if (i % 2 == 0) {
17+
Arr.append(i);
18+
}
19+
i += 1;
20+
};
21+
22+
return Arr;
23+
}
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#[starknet::contract]
2+
mod mappingContract {
3+
use starknet::ContractAddress;
4+
5+
#[storage]
6+
struct Storage {
7+
studentsName: LegacyMap::<ContractAddress, felt252>,
8+
studentsResultRecord: LegacyMap::<(ContractAddress, felt252), u16>,
9+
}
10+
11+
#[external(v0)]
12+
#[generate_trait]
13+
impl external of externalTrait {
14+
fn registerUser(
15+
ref self: ContractState, studentAdd: ContractAddress, studentName: felt252
16+
) {
17+
self.studentsName.write(studentAdd, studentName);
18+
}
19+
20+
fn recordStudentScore(
21+
ref self: ContractState, studentAdd: ContractAddress, Subject: felt252, score: u16
22+
) {
23+
self.studentsResultRecord.write((studentAdd, Subject), score);
24+
}
25+
26+
fn viewStudentName(self: @ContractState, studentAdd: ContractAddress) -> felt252 {
27+
self.studentsName.read(studentAdd)
28+
}
29+
30+
fn viewStudentScore(
31+
self: @ContractState, studentAdd: ContractAddress, Subject: felt252
32+
) -> u16 {
33+
// for a 2D mapping its important to take note of the amount of brackets being used.
34+
self.studentsResultRecord.read((studentAdd, Subject))
35+
}
36+
}
37+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#[starknet::contract]
2+
mod matchExample {
3+
#[storage]
4+
struct Storage {}
5+
6+
#[derive(Drop, Serde)]
7+
enum Colour {
8+
Red,
9+
Blue,
10+
Green,
11+
Orange,
12+
Black
13+
}
14+
15+
#[derive(Drop, Serde)]
16+
enum Coin {
17+
Penny,
18+
Nickel,
19+
Dime,
20+
Quarter,
21+
}
22+
23+
#[external(v0)]
24+
#[generate_trait]
25+
impl external of externlalTrait {
26+
fn value_in_cents(self: @ContractState, coin: Coin) -> felt252 {
27+
match coin {
28+
Coin::Penny => 1,
29+
Coin::Nickel => 5,
30+
Coin::Dime => 10,
31+
Coin::Quarter => 25,
32+
}
33+
}
34+
35+
fn specified_colour(self: @ContractState, colour: Colour) -> felt252 {
36+
let mut response: felt252 = '';
37+
38+
match colour {
39+
Colour::Red => { response = 'You passed in Red'; },
40+
Colour::Blue => { response = 'You passed in Blue'; },
41+
Colour::Green => { response = 'You passed in Green'; },
42+
Colour::Orange => { response = 'You passed in Orange'; },
43+
Colour::Black => { response = 'You passed in Black'; },
44+
};
45+
46+
response
47+
}
48+
49+
fn quiz(self: @ContractState, num: felt252) -> felt252 {
50+
let mut response: felt252 = '';
51+
52+
match num {
53+
0 => { response = 'You failed' },
54+
_ => { response = 'You Passed' },
55+
};
56+
57+
response
58+
}
59+
}
60+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#[starknet::contract]
2+
mod StructExample {
3+
use starknet::{ContractAddress, get_caller_address};
4+
5+
#[storage]
6+
struct Storage {
7+
userData: data
8+
}
9+
10+
#[derive(Drop, starknet::Store)]
11+
struct data {
12+
Add: ContractAddress,
13+
Age: u8
14+
}
15+
16+
#[external(v0)]
17+
#[generate_trait]
18+
impl StoreStructImpl of IStoreStructContract {
19+
fn store_struct(ref self: ContractState, age: u8) {
20+
let newStruct = data { Add: get_caller_address(), Age: age };
21+
self.userData.write(newStruct);
22+
}
23+
24+
fn read_struct(self: @ContractState) -> (ContractAddress, u8) {
25+
let lastUser = self.userData.read();
26+
let add = lastUser.Add;
27+
let age = lastUser.Age;
28+
(add, age)
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)