diff --git a/.gitignore b/.gitignore index eb5a316..2f7896d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -target +target/ diff --git a/cairo_program/src/bool.cairo b/cairo_program/src/bool.cairo index cafad1e..aa231ac 100644 --- a/cairo_program/src/bool.cairo +++ b/cairo_program/src/bool.cairo @@ -3,7 +3,7 @@ fn main() { let result: bool = is_adult(18); println!("bool result = {}", result); - let is_even_result: bool = is_even(17); + let is_even_result: bool = is_even(17); println!("result = {}", is_even_result); } @@ -23,7 +23,7 @@ fn is_adult(x: u8) -> bool { fn is_even(x: u8) -> bool { if x % 2 == 0 { return true; - } + } return false; } diff --git a/cairo_program/src/bytearray.cairo b/cairo_program/src/bytearray.cairo index 0c7f306..d356eac 100644 --- a/cairo_program/src/bytearray.cairo +++ b/cairo_program/src/bytearray.cairo @@ -2,4 +2,4 @@ fn main() { let text: ByteArray = "We are writing Cairo in this session and it's going to be fun"; println!("full text here: {}", text); -} \ No newline at end of file +} diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo index 9501eca..3ee235f 100644 --- a/cairo_program/src/integer.cairo +++ b/cairo_program/src/integer.cairo @@ -7,6 +7,19 @@ fn main() { let sub_result: u8 = sub_num(10, 5); println!("sub result is: {}", sub_result); assert(sub_result == 5, 'invalid sub logic'); + + // Test subtraction that should panic (10 - 15 = negative) + // sub_num(10, 15); // This would panic + + let mul_result: u8 = mul_num(3, 4); + println!("mul result is: {}", mul_result); + assert(mul_result == 12, 'invalid mul logic'); + + let div_result: u8 = div_num(20, 4); + println!("div result is: {}", div_result); + assert(div_result == 5, 'invalid div logic'); + // Test division by zero - should panic +// div_num(10, 0); // This would panic } // addition logic @@ -14,7 +27,19 @@ fn add_num(x: u8, y: u8) -> u8 { x + y } -// subtraction logic +// subtraction logic with negative check fn sub_num(x: u8, y: u8) -> u8 { + assert(y <= x, 'negative result not allowed'); return x - y; } + +// multiplication logic +fn mul_num(x: u8, y: u8) -> u8 { + x * y +} + +// division logic with division by zero check +fn div_num(x: u8, y: u8) -> u8 { + assert(y != 0, 'division by zero'); + x / y +} diff --git a/cairo_program/src/lib.cairo b/cairo_program/src/lib.cairo index acc7644..de0053b 100644 --- a/cairo_program/src/lib.cairo +++ b/cairo_program/src/lib.cairo @@ -1,5 +1,6 @@ // mod hello_world; // mod short_string; -// mod integer; +mod integer; // mod bool; -mod bytearray; \ No newline at end of file +// mod bytearray; + diff --git a/starknet_contracts/snfoundry.toml b/starknet_contracts/snfoundry.toml index c06aab7..83cc417 100644 --- a/starknet_contracts/snfoundry.toml +++ b/starknet_contracts/snfoundry.toml @@ -1,3 +1,5 @@ +[sncast.default] +account = "cairoTest1" # Visit https://foundry-rs.github.io/starknet-foundry/appendix/snfoundry-toml.html # and https://foundry-rs.github.io/starknet-foundry/projects/configuration.html for more information diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index f1b82bb..ee4abef 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -4,6 +4,8 @@ pub trait ICounter { /// Increase count. fn increase_count(ref self: T, amount: u32); + /// Decrease count. + fn reduce_count(ref self: T, amount: u32); /// Retrieve count. fn get_count(self: @T) -> u32; } @@ -12,17 +14,40 @@ pub trait ICounter { #[starknet::contract] mod Counter { use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; + use starknet::{ContractAddress, get_caller_address}; #[storage] struct Storage { count: u32, + owner: ContractAddress, + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.owner.write(owner); } #[abi(embed_v0)] impl CounterImpl of super::ICounter { fn increase_count(ref self: ContractState, amount: u32) { + // Only owner check + assert(self.owner.read() == get_caller_address(), 'Caller is not the owner'); + + assert(amount != 0, 'Amount cannot be 0'); + // Read current count and add the amount + let current_count = self.count.read(); + self.count.write(current_count + amount); + } + + fn reduce_count(ref self: ContractState, amount: u32) { + // Only owner check + assert(self.owner.read() == get_caller_address(), 'Caller is not the owner'); + assert(amount != 0, 'Amount cannot be 0'); - self.count.write(self.count.read() + amount); + // Read current count and subtract the amount + let current_count = self.count.read(); + assert(current_count >= amount, 'Insufficient count'); + self.count.write(current_count - amount); } fn get_count(self: @ContractState) -> u32 { diff --git a/target/cairo-language-server/2.18.0_proc_macro.cache b/target/cairo-language-server/2.18.0_proc_macro.cache index 9a48e13..4227ca4 100644 Binary files a/target/cairo-language-server/2.18.0_proc_macro.cache and b/target/cairo-language-server/2.18.0_proc_macro.cache differ