Skip to content
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
target
target/
4 changes: 2 additions & 2 deletions cairo_program/src/bool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -23,7 +23,7 @@ fn is_adult(x: u8) -> bool {
fn is_even(x: u8) -> bool {
if x % 2 == 0 {
return true;
}
}
return false;
}

2 changes: 1 addition & 1 deletion cairo_program/src/bytearray.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
27 changes: 26 additions & 1 deletion cairo_program/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,39 @@ 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
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
}
5 changes: 3 additions & 2 deletions cairo_program/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// mod hello_world;
// mod short_string;
// mod integer;
mod integer;
// mod bool;
mod bytearray;
// mod bytearray;

2 changes: 2 additions & 0 deletions starknet_contracts/snfoundry.toml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
27 changes: 26 additions & 1 deletion starknet_contracts/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
pub trait ICounter<T> {
/// 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;
}
Expand All @@ -12,17 +14,40 @@ pub trait ICounter<T> {
#[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<ContractState> {
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 {
Expand Down
Binary file modified target/cairo-language-server/2.18.0_proc_macro.cache
Binary file not shown.