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
1 change: 0 additions & 1 deletion cairo_program/src/bool.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fn is_adult(x: u8) -> bool {
return true;
}


// determine even numbers
fn is_even(x: u8) -> bool {
if x % 2 == 0 {
Expand Down
43 changes: 38 additions & 5 deletions cairo_program/src/integer.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
use core::num::traits::OverflowingMul;

#[executable]
fn main() {
let result: u8 = add_num(5, 6);
println!("the sum of x & y is: {}", result);
assert(result == 11, 'invalid sum logic');
let add_result: u8 = add_num(5, 6);
println!("the sum of x & y is: {}", add_result);
assert(add_result == 11, 'invalid sum logic');

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(255, 1);
println!("mul result is: {}", mul_result);
assert(mul_result == 255, 'invalid mul logic');

let div_result: u8 = div_num(20, 0);
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
return x + y;
}

// subtraction logic
fn sub_num(x: u8, y: u8) -> u8 {
return x - y;
assert!(!(y > x), "negative result not allowed");
x - y
}

// multiplication logic
fn mul_num(x: u8, y: u8) -> u8 {
let (result_, overflowed_) = x.overflowing_mul(y);

assert!(!(overflowed_), "multiplication overflowed");

result_
}

// division logic with division by zero check
fn div_num(x: u8, y: u8) -> u8 {
assert!(!(y == 0), "Cannot be Zero");
x / y
}

4 changes: 2 additions & 2 deletions cairo_program/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// mod hello_world;
// mod short_string;
// mod integer;
mod integer;
// mod bool;
mod bytearray;
// mod bytearray;
5 changes: 5 additions & 0 deletions starknet_contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ edition = "2024_07"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html


[dependencies]
starknet = "2.18.0"
cairo_execute = "2.18.0"

[cairo]
enable-gas = true

[dev-dependencies]
snforge_std = "0.56.0"
Expand Down
28 changes: 28 additions & 0 deletions starknet_contracts/src/arithmetic.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::num::traits::OverflowingMul;

// addition logic
pub fn add_num(x: u32, y: u32) -> u32 {
return x + y;
}

// subtraction logic
fn sub_num(x: u8, y: u8) -> u8 {
assert!(!(y > x), "negative result not allowed");
x - y
}

// multiplication logic
fn mul_num(x: u8, y: u8) -> u8 {
let (result_, overflowed_) = x.overflowing_mul(y);

assert!(!(overflowed_), "multiplication overflowed");

result_
}

// division logic with division by zero check
fn div_num(x: u8, y: u8) -> u8 {
assert!(!(y == 0), "Cannot be Zero");
x / y
}

33 changes: 31 additions & 2 deletions starknet_contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// Interface representing `HelloContract`.
/// This interface allows modification and retrieval of the contract's storage count.

mod arithmetic;
#[starknet::interface]
pub trait ICounter<T> {
/// Increase count.
Expand All @@ -10,21 +12,48 @@ pub trait ICounter<T> {

/// Simple contract for managing count.
#[starknet::contract]
mod Counter {
pub mod Counter {
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::{ContractAddress, get_caller_address};
use super::arithmetic::{add_num};

#[storage]
struct Storage {
count: u32,
owner: ContractAddress,
}

#[constructor]
fn constructor(ref self: ContractState, owner: ContractAddress) {
self.owner.write(owner); // no .into() needed }
}

#[generate_trait]
impl PrivateImpl of PrivateTrait {
fn assert_only_owner(self: @ContractState ) {
assert(get_caller_address() == self.owner.read(), 'Caller not the Owner');
}
}



#[abi(embed_v0)]
impl CounterImpl of super::ICounter<ContractState> {
fn increase_count(ref self: ContractState, amount: u32) {

// Only owner
self.assert_only_owner();
assert(amount != 0, 'Amount cannot be 0');
self.count.write(self.count.read() + amount);

// Read current count and add the amount
let current_count = self.count.read();
let new_count = add_num(current_count, amount);
self.count.write(new_count);

assert(new_count == current_count + amount, 'Count increase failed');
}


fn get_count(self: @ContractState) -> u32 {
self.count.read()
}
Expand Down