Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom errors #25

Open
wants to merge 6 commits into
base: starknet-contracts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions assignments/solutions_3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
fn main() {
subtract();
multiply();
divide();
}

fn subtract() -> u16{
let a = 10_u16;
let b = 20_u16;

let subt = a - b;
println!("The answer is: {}" sub);

sub
}

fn multiply() -> u32 {
let (a, b): (u32, u32) = (13, 10);

let mult = a * b;
println!("The product is: {}", mult);

mult;
}

fn divide() -> u8{
let a: u8 = 14;
let b: u8 = 2;

let div = a / b;
println!("The answer is: {}" div);

div;
}
1 change: 1 addition & 0 deletions src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod Errors {
pub const ZERO_ADDRESS: felt252 = 'ZERO ADDRESS!';
pub const SAME_ADDRESS: felt252 = 'CANNOT BE SAME ADDRESS!';
pub const ZERO_VALUE: felt252 = 'CANNOT BE ZERO_VALUE!';
pub const EMPTY_STRING: felt252 = 'CANNOT BE EMPTY STRING!';
}
7 changes: 7 additions & 0 deletions src/intro_to_bytearray.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// for any string exceeding 31 chars, use ByteArray
pub fn run() -> ByteArray {
let name_of_school: ByteArray = "Cairo Bootcamp 3 2024 in Kaduna is holding as a hybrid class";
println!(" our school desc: {}", name_of_school);
name_of_school
}

8 changes: 8 additions & 0 deletions src/intro_to_felt.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Please note - Security considerations for felt252
// never use felt252 to perform arithmethic operations
// the max char length for felt252 is 31
pub fn run() -> felt252 {
let x = 'SAY GM!';
println!("x value here: {}", x);
x
}
11 changes: 11 additions & 0 deletions src/intro_to_u16.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::utils::low_to_high;
//converting from low to high
pub fn run(x: u8, y: u8) -> u16 {
let result: u16 = low_to_high(x) + low_to_high(y);
result
}

//converting from high to low
fn high_to_low(x: u16) -> u8 {
x.try_into().unwrap()
}
8 changes: 8 additions & 0 deletions src/intro_to_u8.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// integers are basically divided into 2:
// uint - u8, u16, u32, u64, u128, u256, usize
// int - i8, i16, i32, i64, i128, i256
pub fn run(x: u8, y: u8) -> u8 {
// x + y // implicit return
assert(x + y <= 255, 'exceeds');

}
9 changes: 7 additions & 2 deletions src/student_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod StudentRegistry {
use starknet::storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map
};
use crate::errors::Errors::{NOT_ADMIN, ZERO_ADDRESS};
use crate::errors::Errors::{NOT_ADMIN, ZERO_ADDRESS, EMPTY_STRING, ZERO_VALUE, SAME_ADDRESS};

#[storage]
struct Storage {
Expand Down Expand Up @@ -51,7 +51,12 @@ mod StudentRegistry {
) {
// validation to check if student account is valid address and not a 0 address
assert(!self.is_zero_address(_account), ZERO_ADDRESS);
assert(_age > 0, 'age cannot be 0');
let _admin = self.admin.read();
assert(_account != _admin, SAME_ADDRESS);

assert(_age > 0, ZERO_VALUE);
assert(_name != 0, EMPTY_STRING);
assert(_xp != 0, ZERO_VALUE);
let student = Student {
name: _name, account: _account, age: _age, xp: _xp, is_active: _is_active
};
Expand Down
4 changes: 4 additions & 0 deletions src/utils.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// util function to convert u8 to u16
pub fn low_to_high(x: u8) -> u16 {
x.into()
}