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: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ target
snfoundry_trace/
coverage/
profile/
.DS_Store
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
# Cairo Bootcamp 4.0
# Cairo Bootcamp 4.0

## Setting Up Starknet Dev Environment with Starkup
- Cairo toolchain installation - tool for installing all the Starknet essentials for development.
- Starkup supports the installation of the following tools:
- Scarb - the Cairo package manager
- Starknet Foundry - the Cairo and Starknet testing framework
- Universal Sierra Compiler - compiler for any ever-existing Sierra version
- Cairo Profiler - profiler for Cairo programming language & Starknet
- Cairo Coverage - utility for coverage reports generation for code written in Cairo programming language
- CairoLS - vscode extension
- [Starkup Github link](https://github.com/software-mansion/starkup)

Install `starkup` using:
```sh
curl --proto '=https' --tlsv1.2 -sSf https://sh.starkup.sh | sh
```














20 changes: 1 addition & 19 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,5 @@
version = 1

[[package]]
name = "session_1"
name = "cohort_4"
version = "0.1.0"
dependencies = [
"snforge_std",
]

[[package]]
name = "snforge_scarb_plugin"
version = "0.39.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:61ae2333de26d9e6e4b535916f8da7916bfd95c951c765c6fbd36038289a636c"

[[package]]
name = "snforge_std"
version = "0.39.0"
source = "registry+https://scarbs.xyz/"
checksum = "sha256:31f118f54e5d87393e50c07e8c052f03ff4af945a2b65b56e6edbd3b5b7fd127"
dependencies = [
"snforge_scarb_plugin",
]
51 changes: 7 additions & 44 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,52 +1,15 @@
[package]
name = "session_1"
name = "cohort_4"
version = "0.1.0"
edition = "2024_07"

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

# [executable]
[dependencies]
starknet = "2.11.2"

[dev-dependencies]
snforge_std = "0.39.0"
assert_macros = "2.11.2"

[[target.starknet-contract]]
sierra = true

[scripts]
test = "snforge test"

[tool.scarb]
allow-prebuilt-plugins = ["snforge_std"]

# Visit https://foundry-rs.github.io/starknet-foundry/appendix/scarb-toml.html for more information
[cairo]
enable-gas = false

# [tool.snforge] # Define `snforge` tool section
# exit_first = true # Stop tests execution immediately upon the first failure
# fuzzer_runs = 1234 # Number of runs of the random fuzzer
# fuzzer_seed = 1111 # Seed for the random fuzzer

# [[tool.snforge.fork]] # Used for fork testing
# name = "SOME_NAME" # Fork name
# url = "http://your.rpc.url" # Url of the RPC provider
# block_id.tag = "latest" # Block to fork from (block tag)

# [[tool.snforge.fork]]
# name = "SOME_SECOND_NAME"
# url = "http://your.second.rpc.url"
# block_id.number = "123" # Block to fork from (block number)

# [[tool.snforge.fork]]
# name = "SOME_THIRD_NAME"
# url = "http://your.third.rpc.url"
# block_id.hash = "0x123" # Block to fork from (block hash)

# [profile.dev.cairo] # Configure Cairo compiler
# unstable-add-statements-code-locations-debug-info = true # Should be used if you want to use coverage
# unstable-add-statements-functions-debug-info = true # Should be used if you want to use coverage/profiler
# inlining-strategy = "avoid" # Should be used if you want to use coverage

# [features] # Used for conditional compilation
# enable_for_tests = [] # Feature name and list of other features that should be enabled with it
[dev-dependencies]
cairo_test = "2.11.2"
# cairo_execute = "2.11.3"
34 changes: 34 additions & 0 deletions assignments/task_3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Cairo Assignment 1

## Objective

This assignment aims to help students understand basic arithmetic operations and logical conditions in Cairo covering the primitive data types. Each function should be modular, reusable, and well-commented.

## Tasks
Implement the following:
### 1. Arithmetic Operations
- **Subtraction:** performs subtraction of two numbers.
- **Multiplication:** performs multiplication of two numbers.
- **Division:** performs division of two numbers (ensure handling of division by zero).

### 2. Even or Odd Sum
- Implement a function that takes two arguments, computes their sum, and determines whether the sum is even or odd.

### 3. Odd Number Checker
- Implement a function that checks if a given number is odd.

### 4. Basic Logic Functions
- **Number Sign Checker:** Implement a function to check if a number is positive, negative, or zero.
- **Maximum of Two Numbers:** Implement a function to return the maximum of two numbers.


## Guidelines
- Each function should be modular and well-commented.
- Use meaningful variable names and snake_casing
- Ensure type safety by specifying argument and return types correctly.
- Handle edge cases such as division by zero.
- Test your functions within the `main` function before submission such that it logs the appropriate `stdout` on the terminal

## Submission
Submit your completed Cairo code by creating a PR

122 changes: 77 additions & 45 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,54 +1,86 @@
/// Interface representing `HelloContract`.
/// This interface allows modification and retrieval of the contract count.
#[starknet::interface]
pub trait ICounter<TContractState> {
/// Increase contract count.
fn increase_count(ref self: TContractState, amount: felt252);
/// Increase contract count by one
fn increase_count_by_one(ref self: TContractState);

/// Decrease contract count.
fn decrease_count(ref self: TContractState, amount: felt252);

/// Decrease contract count.
fn decrease_count_by_one(ref self: TContractState);
/// Retrieve contract count.
fn get_count(self: @TContractState) -> felt252;
}

/// Simple contract for managing count.
#[starknet::contract]
mod Counter {
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};

#[storage]
struct Storage {
count: felt252,
fn main() {
let multiply = multiplication(2, 10);
let div = division(0, 2);
let checker = sign_checker(6);
let odd = is_odd(3);
let max = max_num(6, 7);
let even_or_odd = even_odd_sum(3, 6);
let sub = subtraction(10, 2);

println!("multiplication is equal to {}", multiply);
println!("checker is equal to {}", checker);
println!("odd is equal to {}", odd);
println!("max is equal to {}", max);
println!("even_or_odd is equal to {}", even_or_odd);
println!("sub is equal to {}", sub);

match div {
Option::Some(value) => println!("answer is {}", value),
Option::None => println!("this is an error operation"),
}
}

#[abi(embed_v0)]
impl CounterImpl of super::ICounter<ContractState> {
fn increase_count(ref self: ContractState, amount: felt252) {
assert(amount != 0, 'Amount cannot be 0');
self.count.write(self.count.read() + amount);
}

fn increase_count_by_one(ref self: ContractState) {
self.count.write(self.count.read() + 1);
}
// this function subtracts between two numbers
pub fn subtraction(x: u128, y: u128) -> u128 {
return x - y;
}

// this function multipli_es two numbers
pub fn multiplication(x: i16, y: i16) -> i16 {
return x * y;
}

fn decrease_count(ref self: ContractState, amount: felt252) {
assert(amount != 0, 'Amount cannot be 0');
self.count.write(self.count.read() - amount);
}
// this function divides two numbers
pub fn division(x: i16, y: i16) -> Option<i16> {
if y == 0 {
return Option::None;
} else if x == 0 {
return Option::None;
} else {
return Option::Some(x / y);
}
}

fn decrease_count_by_one(ref self: ContractState) {
self.count.write(self.count.read() - 1);
}
// thi function checks if a number is positive, negative, or equal to zero
pub fn sign_checker(x: i16) -> ByteArray {
let num = x;
if num > 0 {
return "positive";
} else if num < 0 {
return "negative";
} else {
return "zero";
}
}

fn get_count(self: @ContractState) -> felt252 {
self.count.read()
}
// this function picks the maximum number between two numbers
pub fn max_num(x: i16, y: i16) -> i16 {
if x > y {
return x;
} else {
return y;
}
}

// pub is used to make a mod or function public andvisible to parent functions
// pub mod even_or_odd_sum;

// this function determines whether a value is odd or not
pub fn is_odd(x: i16) -> bool {
return x % 2 != 0;
}


// this function checksif the result of the sum of two numbers is even or odd
pub fn even_odd_sum(x: i16, y: i16) -> ByteArray {
let add = x + y;
let odd = is_odd(add);
let res = true;
if odd == res {
return "odd";
} else {
return "even";
}
}