diff --git a/assignments/solutions_3.md b/assignments/solutions_3.md new file mode 100644 index 0000000..63cf698 --- /dev/null +++ b/assignments/solutions_3.md @@ -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; +} \ No newline at end of file diff --git a/src/errors.cairo b/src/errors.cairo index 524d2ad..a3e441c 100644 --- a/src/errors.cairo +++ b/src/errors.cairo @@ -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!'; } diff --git a/src/intro_to_bytearray.cairo b/src/intro_to_bytearray.cairo new file mode 100644 index 0000000..7b53a29 --- /dev/null +++ b/src/intro_to_bytearray.cairo @@ -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 +} + diff --git a/src/intro_to_felt.cairo b/src/intro_to_felt.cairo new file mode 100644 index 0000000..d3ca719 --- /dev/null +++ b/src/intro_to_felt.cairo @@ -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 +} \ No newline at end of file diff --git a/src/intro_to_u16.cairo b/src/intro_to_u16.cairo new file mode 100644 index 0000000..41dca72 --- /dev/null +++ b/src/intro_to_u16.cairo @@ -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() +} \ No newline at end of file diff --git a/src/intro_to_u8.cairo b/src/intro_to_u8.cairo new file mode 100644 index 0000000..1e91f2e --- /dev/null +++ b/src/intro_to_u8.cairo @@ -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'); + +} \ No newline at end of file diff --git a/src/student_registry.cairo b/src/student_registry.cairo index 9a405a3..ac53c74 100644 --- a/src/student_registry.cairo +++ b/src/student_registry.cairo @@ -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 { @@ -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 }; diff --git a/src/utils.cairo b/src/utils.cairo new file mode 100644 index 0000000..831bbd3 --- /dev/null +++ b/src/utils.cairo @@ -0,0 +1,4 @@ +// util function to convert u8 to u16 +pub fn low_to_high(x: u8) -> u16 { + x.into() +} \ No newline at end of file