From 6684a83eb36c9e88a85e19ca4d0d51463b189733 Mon Sep 17 00:00:00 2001 From: Iwueseiter <156322726+Iwueseiter@users.noreply.github.com> Date: Mon, 30 Sep 2024 09:03:18 +0000 Subject: [PATCH 1/4] solution_3 --- assignments/solutions_3.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 assignments/solutions_3.md 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 From 6a87a2bce23ffaaa8cab8b94db40ce1650d2c629 Mon Sep 17 00:00:00 2001 From: Iwueseiter <156322726+Iwueseiter@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:06:09 +0000 Subject: [PATCH 2/4] modularized code --- src/intro_to_bytearray.cairo | 7 ++++++ src/intro_to_felt.cairo | 8 +++++++ src/intro_to_u16.cairo | 11 ++++++++++ src/intro_to_u8.cairo | 8 +++++++ src/lib.cairo | 41 +++++++++++------------------------- src/utils.cairo | 4 ++++ 6 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 src/intro_to_bytearray.cairo create mode 100644 src/intro_to_felt.cairo create mode 100644 src/intro_to_u16.cairo create mode 100644 src/intro_to_u8.cairo create mode 100644 src/utils.cairo 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..0774c22 --- /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'); + x + y +} \ No newline at end of file diff --git a/src/lib.cairo b/src/lib.cairo index 0d1ac3d..ac4b9a7 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,31 +1,14 @@ -mod session_4; - -fn main() { - let sum_1: u8 = intro_to_uint(); - let sum_2: u8 = sum_u8(2, 2); - - println!("result_1 {} | result_2 is {} ", sum_1, sum_2); - - // run grade logic - session_4::main(); -} +mod intro_to_felt; +mod intro_to_u16; +mod intro_to_bytearray; +mod intro_to_u8; +pub mod utils; -// intro to felt -fn intro_to_felt() -> felt252 { - let my_first_char: felt252 = 'GM'; - my_first_char -} - -// intro to u8 -fn intro_to_uint() -> u8 { - let x: u8 = 10; - let y: u8 = 20; - x + y -} - -// addition logic -fn sum_u8(x: u8, y: u8) -> u8 { - x + y -} - +fn main() { + intro_to_felt::run(); + intro_to_bytearray::run(); + intro_to_u16::run(7, 7); + intro_to_u8::run(8, 8); + +} \ No newline at end of file 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 From 3eee977be8dd42e6ec82b2312ab83d4e0e59c691 Mon Sep 17 00:00:00 2001 From: Iwueseiter <156322726+Iwueseiter@users.noreply.github.com> Date: Tue, 1 Oct 2024 12:27:02 +0000 Subject: [PATCH 3/4] fix bug# --- src/intro_to_u8.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/intro_to_u8.cairo b/src/intro_to_u8.cairo index 0774c22..1e91f2e 100644 --- a/src/intro_to_u8.cairo +++ b/src/intro_to_u8.cairo @@ -4,5 +4,5 @@ pub fn run(x: u8, y: u8) -> u8 { // x + y // implicit return assert(x + y <= 255, 'exceeds'); - x + y + } \ No newline at end of file From 98db78d2c0c72a780d2183c3265ef982d1cc6b5f Mon Sep 17 00:00:00 2001 From: Iwueseiter <156322726+Iwueseiter@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:23:23 +0000 Subject: [PATCH 4/4] feat:(student_registry) implement custom error --- src/errors.cairo | 1 + src/student_registry.cairo | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) 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/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 };