From fd1ddb7f43b849f9786487885cd0d4213fb7291a Mon Sep 17 00:00:00 2001 From: chukwudiikeh Date: Wed, 29 Apr 2026 07:29:49 +0000 Subject: [PATCH 1/7] Fix Cairo program compilation errors and add lib target --- IMPLEMENTATION_EXPLAINED.md | 581 +++++++++++++++++++++++++++++++ cairo_program/Scarb.lock | 2 +- cairo_program/Scarb.toml | 4 +- cairo_program/src/integer.cairo | 34 +- cairo_program/src/lib.cairo | 2 +- starknet_contracts/Scarb.lock | 5 + starknet_contracts/Scarb.toml | 1 + starknet_contracts/src/lib.cairo | 47 ++- 8 files changed, 659 insertions(+), 17 deletions(-) create mode 100644 IMPLEMENTATION_EXPLAINED.md diff --git a/IMPLEMENTATION_EXPLAINED.md b/IMPLEMENTATION_EXPLAINED.md new file mode 100644 index 0000000..4a2ffa8 --- /dev/null +++ b/IMPLEMENTATION_EXPLAINED.md @@ -0,0 +1,581 @@ +# Cairo Counter Contract - Line by Line (Explain Like I'm 5) + +--- + +## File 1: `cairo_program/src/integer.cairo` + +This file is like a toolbox with 4 basic math tools. + +--- + +### Addition Function + +```cairo +pub fn add_num(x: u32, y: u32) -> u32 { +``` +**Line 1:** "Hey everyone, I'm making a tool called add_num!" +- `pub` = everyone can use this tool (not private) +- `fn` = I'm making a tool (function) +- `add_num` = the tool's name +- `x: u32, y: u32` = give me two whole numbers (like 5 and 3) +- `-> u32` = I'll give you back one whole number + +```cairo + x + y +``` +**Line 2:** Add the two numbers like 5 + 3 = 8, and give back the answer + +```cairo +} +``` +**Line 3:** "I'm done making this tool!" + +--- + +### Subtraction Function + +```cairo +pub fn sub_num(x: u32, y: u32) -> u32 { +``` +**Line 1:** "Hey everyone, I'm making a tool called sub_num!" +- Give me two numbers, I'll give you back one number + +```cairo + assert(x >= y, 'Result cannot be negative'); +``` +**Line 2:** "Wait! Let me check something first!" +- `assert()` = like a safety guard that says "STOP!" if something is wrong +- `x >= y` = is the first number bigger than or equal to the second? (like: is 10 ≥ 3?) +- If not (like 3 - 10), yell: 'Result cannot be negative' +- Why? You can't have -7 apples! Our numbers don't do negatives. + +```cairo + x - y +``` +**Line 3:** Okay, safe to subtract! Do 10 - 3 = 7 and give back the answer + +```cairo +} +``` +**Line 4:** "I'm done making this tool!" + +--- + +### Multiplication Function + +```cairo +pub fn mul_num(x: u32, y: u32) -> u32 { +``` +**Line 1:** "Hey everyone, I'm making a tool called mul_num!" + +```cairo + let result = x.checked_mul(y); +``` +**Line 2:** "Let me try to multiply these numbers carefully..." +- `let result =` = make a box to hold the answer +- `x.checked_mul(y)` = multiply like 5 × 3, but check if the answer is TOO BIG +- If it works: put the answer in a gift box labeled "Some(15)" +- If too big: put nothing in the box, label it "None" +- Example: trying to count 4 billion × 2 = way too many to count! + +```cairo + assert(result.is_some(), 'Multiplication overflow'); +``` +**Line 3:** "Did I get an answer, or was it too big?" +- `result.is_some()` = is there a number in the box? +- If the box is empty (None), yell: 'Multiplication overflow' (too big!) + +```cairo + result.unwrap() +``` +**Line 4:** "Open the gift box and take out the number!" +- `unwrap()` = open the box and grab the number inside +- Safe because we already checked the box isn't empty + +```cairo +} +``` +**Line 5:** "I'm done making this tool!" + +--- + +### Division Function + +```cairo +pub fn div_num(x: u32, y: u32) -> u32 { +``` +**Line 1:** "Hey everyone, I'm making a tool called div_num!" + +```cairo + assert(y != 0, 'Division by zero'); +``` +**Line 2:** "Wait! You can't divide by zero!" +- `y != 0` = is the second number NOT zero? +- If it IS zero, yell: 'Division by zero' +- Why? If you have 10 cookies and try to share them among 0 friends... that makes no sense! + +```cairo + x / y +``` +**Line 3:** Okay, safe to divide! Do 10 ÷ 2 = 5 and give back the answer + +```cairo +} +``` +**Line 4:** "I'm done making this tool!" + +--- + +## File 2: `cairo_program/src/lib.cairo` + +```cairo +pub mod integer; +``` +**This line:** "Hey, I have a toolbox called 'integer' and everyone can use it!" +- `pub` = everyone can borrow my tools +- `mod integer` = the toolbox is in the file called `integer.cairo` +- This lets the smart contract use our math tools + +--- + +## File 3: `starknet_contracts/src/lib.cairo` + +This is like building a special calculator that lives on the blockchain! + +--- + +### Interface Definition (The Menu) + +```cairo +#[starknet::interface] +``` +**Line 1:** "I'm making a menu that shows what buttons my calculator has!" + +```cairo +pub trait ICounter { +``` +**Line 2:** "The menu is called ICounter" +- `pub` = everyone can see this menu +- `trait` = this is a list of buttons (not the actual buttons yet) + +```cairo + fn increase_count(ref self: T, amount: u32); +``` +**Line 3:** "Button 1: Make the number bigger" +- `ref self: T` = this button can change what's on the screen +- `amount: u32` = you tell me how much to add + +```cairo + fn reduce_count(ref self: T, amount: u32); +``` +**Line 4:** "Button 2: Make the number smaller" + +```cairo + fn multiply_count(ref self: T, amount: u32); +``` +**Line 5:** "Button 3: Multiply the number" + +```cairo + fn divide_count(ref self: T, amount: u32); +``` +**Line 6:** "Button 4: Divide the number" + +```cairo + fn get_count(self: @T) -> u32; +``` +**Line 7:** "Button 5: Just look at the number (don't change it)" +- `self: @T` = this button only LOOKS, can't change anything +- `-> u32` = tells you what number is on the screen + +```cairo +} +``` +**Line 8:** "That's all the buttons on my menu!" + +--- + +### Contract Definition + +```cairo +#[starknet::contract] +``` +**Line 1:** "Now I'm building the ACTUAL calculator (not just the menu)!" + +```cairo +mod Counter { +``` +**Line 2:** "My calculator is called Counter" + +--- + +### Imports (Getting Tools) + +```cairo + use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; +``` +**Line 3:** "I need tools to save and read numbers from the blockchain!" +- `StoragePointerReadAccess` = tool to READ saved numbers +- `StoragePointerWriteAccess` = tool to WRITE/SAVE numbers + +```cairo + use starknet::{ContractAddress, get_caller_address}; +``` +**Line 4:** "I need tools to know WHO is using my calculator!" +- `ContractAddress` = like a home address, but for blockchain accounts +- `get_caller_address` = tells me who just pressed a button + +```cairo + use cairo_program::integer::{add_num, sub_num, mul_num, div_num}; +``` +**Line 5:** "I'm borrowing the 4 math tools we made earlier!" +- Brings in add, subtract, multiply, and divide from our toolbox + +--- + +### Storage (The Calculator's Memory) + +```cairo + #[storage] +``` +**Line 6:** "Here's what my calculator remembers forever:" + +```cairo + struct Storage { +``` +**Line 7:** "I have a memory box with 2 things in it:" + +```cairo + count: u32, +``` +**Line 8:** "Thing 1: The number on the screen (called 'count')" + +```cairo + owner: ContractAddress, +``` +**Line 9:** "Thing 2: The address of who owns this calculator" +- Like writing your name on your calculator so others can't use it + +```cairo + } +``` +**Line 10:** "That's everything I remember!" + +--- + +### Constructor (First Time Setup) + +```cairo + #[constructor] +``` +**Line 11:** "This runs ONE TIME when someone first creates the calculator!" + +```cairo + fn constructor(ref self: ContractState, owner: ContractAddress) { +``` +**Line 12:** "Setup function - tell me who the owner is!" +- `ref self: ContractState` = I can write to memory +- `owner: ContractAddress` = give me the owner's address + +```cairo + self.owner.write(owner); +``` +**Line 13:** "Write the owner's name in permanent marker!" +- `self.owner` = the owner spot in memory +- `.write(owner)` = save the owner's address to the blockchain forever + +```cairo + } +``` +**Line 14:** "Setup complete!" + +--- + +### Internal Helper (Security Guard) + +```cairo + #[generate_trait] +``` +**Line 15:** "Make a special helper tool automatically" + +```cairo + impl InternalImpl of InternalTrait { +``` +**Line 16:** "Here are my private helper tools:" + +```cairo + fn only_owner(self: @ContractState) { +``` +**Line 17:** "Security guard tool - checks if you're the owner!" +- `self: @ContractState` = I only need to LOOK at memory, not change it + +```cairo + assert(get_caller_address() == self.owner.read(), 'Caller is not the owner'); +``` +**Line 18:** "Are you the owner? Let me check your ID!" +- `get_caller_address()` = who are you? +- `self.owner.read()` = who is the owner? +- `==` = are these the same person? +- If NO, yell: 'Caller is not the owner' and kick them out! + +```cairo + } +``` +**Line 19:** "Security check done!" + +```cairo + } +``` +**Line 20:** "All helper tools defined!" + +--- + +### Public Functions (The Actual Buttons!) + +```cairo + #[abi(embed_v0)] +``` +**Line 21:** "These buttons can be pressed from outside the calculator!" + +```cairo + impl CounterImpl of super::ICounter { +``` +**Line 22:** "Now I'm building the REAL buttons from the menu we made earlier!" + +--- + +### Increase Count Button + +```cairo + fn increase_count(ref self: ContractState, amount: u32) { +``` +**Line 23:** "The '+' button - makes the number bigger!" + +```cairo + self.only_owner(); +``` +**Line 24:** "STOP! Security guard, check their ID!" +- Calls the security guard we made earlier +- If not the owner, they get kicked out here + +```cairo + assert(amount != 0, 'Amount cannot be 0'); +``` +**Line 25:** "You can't add zero! That's silly!" +- `amount != 0` = is the amount NOT zero? +- If it IS zero, yell: 'Amount cannot be 0' + +```cairo + let current = self.count.read(); +``` +**Line 26:** "Let me check what number is on the screen right now..." +- `let current =` = make a box called 'current' +- `self.count.read()` = read the number from the calculator's memory + +```cairo + let new_count = add_num(current, amount); +``` +**Line 27:** "Use my add tool to calculate: current + amount" +- Calls our `add_num` function from the toolbox +- Example: if screen shows 10 and you add 5, new_count = 15 + +```cairo + self.count.write(new_count); +``` +**Line 28:** "Write the new number on the screen and save it forever!" +- `self.count.write()` = save the new number to blockchain memory + +```cairo + } +``` +**Line 29:** "'+' button is done!" + +--- + +### Reduce Count Button + +```cairo + fn reduce_count(ref self: ContractState, amount: u32) { +``` +**Line 30:** "The '-' button - makes the number smaller!" + +```cairo + self.only_owner(); +``` +**Line 31:** "Security guard, check their ID!" + +```cairo + let current = self.count.read(); +``` +**Line 32:** "What number is on the screen?" + +```cairo + let new_count = sub_num(current, amount); +``` +**Line 33:** "Use my subtract tool: current - amount" +- Calls our safe `sub_num` function +- If the answer would be negative (like 3 - 10), it will yell an error! + +```cairo + self.count.write(new_count); +``` +**Line 34:** "Write the new number on the screen!" + +```cairo + } +``` +**Line 35:** "'-' button is done!" + +--- + +### Multiply Count Button + +```cairo + fn multiply_count(ref self: ContractState, amount: u32) { +``` +**Line 36:** "The '×' button - makes the number bigger by multiplying!" + +```cairo + self.only_owner(); +``` +**Line 37:** "Security guard, check their ID!" + +```cairo + let current = self.count.read(); +``` +**Line 38:** "What number is on the screen?" + +```cairo + let new_count = mul_num(current, amount); +``` +**Line 39:** "Use my multiply tool: current × amount" +- Calls our safe `mul_num` function +- If the answer is too big (overflow), it will yell an error! + +```cairo + self.count.write(new_count); +``` +**Line 40:** "Write the new number on the screen!" + +```cairo + } +``` +**Line 41:** "'×' button is done!" + +--- + +### Divide Count Button + +```cairo + fn divide_count(ref self: ContractState, amount: u32) { +``` +**Line 42:** "The '÷' button - makes the number smaller by dividing!" + +```cairo + self.only_owner(); +``` +**Line 43:** "Security guard, check their ID!" + +```cairo + let current = self.count.read(); +``` +**Line 44:** "What number is on the screen?" + +```cairo + let new_count = div_num(current, amount); +``` +**Line 45:** "Use my divide tool: current ÷ amount" +- Calls our safe `div_num` function +- If you try to divide by zero, it will yell an error! + +```cairo + self.count.write(new_count); +``` +**Line 46:** "Write the new number on the screen!" + +```cairo + } +``` +**Line 47:** "'÷' button is done!" + +--- + +### Get Count Button (Just Look!) + +```cairo + fn get_count(self: @ContractState) -> u32 { +``` +**Line 48:** "The 'look' button - just shows you the number!" +- `self: @ContractState` = I only LOOK, I don't change anything (notice the `@`) +- `-> u32` = I'll tell you what number is on the screen +- NO security guard! Anyone can look! + +```cairo + self.count.read() +``` +**Line 49:** "Here's the number on the screen!" +- Just read and tell you the number, that's it! + +```cairo + } +``` +**Line 50:** "'Look' button is done!" + +```cairo + } +``` +**Line 51:** "All buttons are built!" + +```cairo +} +``` +**Line 52:** "Calculator is complete!" + +--- + +## Configuration Files (Setup Instructions) + +### `cairo_program/Scarb.toml` + +```toml +name = "cairo_program" +``` +**What we did:** Changed the name to `cairo_program` +- Like naming your toolbox so others can find it +- The smart contract needs to know what to call it when borrowing tools + +### `starknet_contracts/Scarb.toml` + +```toml +cairo_program = { path = "../cairo_program" } +``` +**What we did:** Told the calculator where to find the toolbox +- `cairo_program` = the toolbox name +- `path = "../cairo_program"` = go up one folder, then into cairo_program folder +- Like giving directions: "The tools are in the garage!" + +### `cairo_program/src/lib.cairo` + +```cairo +pub mod integer; +``` +**What we did:** Opened the toolbox door +- `pub` = everyone can borrow these tools +- Without this, the tools would be locked inside and nobody could use them! + +--- + +## How Everything Works Together (Story Time!) + +1. **You create the calculator** → You become the owner, screen shows 0 +2. **You press the '+10' button** → Screen now shows 10 +3. **You press the '×3' button** → Screen now shows 30 +4. **You press the '÷5' button** → Screen now shows 6 +5. **Your friend presses 'look'** → They see 6 (anyone can look!) +6. **Your friend tries to press '+5'** → ❌ Security guard says "You're not the owner!" and kicks them out +7. **You press '-10'** → Screen now shows -4... WAIT! ❌ The subtract tool yells "Result cannot be negative!" and stops you + +The calculator is safe because: +- Only YOU can change the numbers (owner protection) +- It won't let you make mistakes (error checking) +- Everyone can see the number, but only you can change it +- All the numbers are saved on the blockchain forever! diff --git a/cairo_program/Scarb.lock b/cairo_program/Scarb.lock index b4ff584..f297123 100644 --- a/cairo_program/Scarb.lock +++ b/cairo_program/Scarb.lock @@ -2,5 +2,5 @@ version = 1 [[package]] -name = "cairo_6" +name = "cairo_program" version = "0.1.0" diff --git a/cairo_program/Scarb.toml b/cairo_program/Scarb.toml index b6c900a..b69787b 100644 --- a/cairo_program/Scarb.toml +++ b/cairo_program/Scarb.toml @@ -1,10 +1,12 @@ [package] -name = "cairo_6" +name = "cairo_program" version = "0.1.0" edition = "2025_12" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html +[lib] + [executable] [cairo] diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo index 9501eca..b5539a6 100644 --- a/cairo_program/src/integer.cairo +++ b/cairo_program/src/integer.cairo @@ -1,20 +1,28 @@ -#[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 sub_result: u8 = sub_num(10, 5); - println!("sub result is: {}", sub_result); - assert(sub_result == 5, 'invalid sub logic'); -} +use core::num::traits::CheckedMul; // addition logic -fn add_num(x: u8, y: u8) -> u8 { +pub fn add_num(x: u32, y: u32) -> u32 { x + y } // subtraction logic -fn sub_num(x: u8, y: u8) -> u8 { - return x - y; +pub fn sub_num(x: u32, y: u32) -> u32 { + assert(x >= y, 'Result cannot be negative'); + x - y +} + +// multiplication logic +pub fn mul_num(x: u32, y: u32) -> u32 { + let result = x.checked_mul(y); + assert(result.is_some(), 'Multiplication overflow'); + match result { + Option::Some(val) => val, + Option::None => panic!("Multiplication overflow"), + } +} + +// division logic +pub fn div_num(x: u32, y: u32) -> u32 { + assert(y != 0, 'Division by zero'); + x / y } diff --git a/cairo_program/src/lib.cairo b/cairo_program/src/lib.cairo index acc7644..fcf903f 100644 --- a/cairo_program/src/lib.cairo +++ b/cairo_program/src/lib.cairo @@ -1,5 +1,5 @@ // mod hello_world; // mod short_string; -// mod integer; +pub mod integer; // mod bool; mod bytearray; \ No newline at end of file diff --git a/starknet_contracts/Scarb.lock b/starknet_contracts/Scarb.lock index 6c8c64a..6975e6c 100644 --- a/starknet_contracts/Scarb.lock +++ b/starknet_contracts/Scarb.lock @@ -1,6 +1,10 @@ # Code generated by scarb DO NOT EDIT. version = 1 +[[package]] +name = "cairo_program" +version = "0.1.0" + [[package]] name = "snforge_scarb_plugin" version = "0.56.0" @@ -20,5 +24,6 @@ dependencies = [ name = "starknet_contracts" version = "0.1.0" dependencies = [ + "cairo_program", "snforge_std", ] diff --git a/starknet_contracts/Scarb.toml b/starknet_contracts/Scarb.toml index 29cf20f..d915936 100644 --- a/starknet_contracts/Scarb.toml +++ b/starknet_contracts/Scarb.toml @@ -7,6 +7,7 @@ edition = "2024_07" [dependencies] starknet = "2.18.0" +cairo_program = { path = "../cairo_program" } [dev-dependencies] snforge_std = "0.56.0" diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index f1b82bb..756f591 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -4,6 +4,12 @@ pub trait ICounter { /// Increase count. fn increase_count(ref self: T, amount: u32); + /// Decrease count. + fn reduce_count(ref self: T, amount: u32); + /// Multiply count. + fn multiply_count(ref self: T, amount: u32); + /// Divide count. + fn divide_count(ref self: T, amount: u32); /// Retrieve count. fn get_count(self: @T) -> u32; } @@ -12,17 +18,56 @@ pub trait ICounter { #[starknet::contract] mod Counter { use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; + use starknet::{ContractAddress, get_caller_address}; + use cairo_program::integer::{add_num, sub_num, mul_num, div_num}; #[storage] struct Storage { count: u32, + owner: ContractAddress, + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.owner.write(owner); + } + + #[generate_trait] + impl InternalImpl of InternalTrait { + fn only_owner(self: @ContractState) { + assert(get_caller_address() == self.owner.read(), 'Caller is not the owner'); + } } #[abi(embed_v0)] impl CounterImpl of super::ICounter { fn increase_count(ref self: ContractState, amount: u32) { + self.only_owner(); assert(amount != 0, 'Amount cannot be 0'); - self.count.write(self.count.read() + amount); + let current = self.count.read(); + let new_count = add_num(current, amount); + self.count.write(new_count); + } + + fn reduce_count(ref self: ContractState, amount: u32) { + self.only_owner(); + let current = self.count.read(); + let new_count = sub_num(current, amount); + self.count.write(new_count); + } + + fn multiply_count(ref self: ContractState, amount: u32) { + self.only_owner(); + let current = self.count.read(); + let new_count = mul_num(current, amount); + self.count.write(new_count); + } + + fn divide_count(ref self: ContractState, amount: u32) { + self.only_owner(); + let current = self.count.read(); + let new_count = div_num(current, amount); + self.count.write(new_count); } fn get_count(self: @ContractState) -> u32 { From 9e6bf0295d3b9bd4fc0f332233da6406ce533cc6 Mon Sep 17 00:00:00 2001 From: chukwudiikeh Date: Wed, 29 Apr 2026 07:49:17 +0000 Subject: [PATCH 2/7] Complete assignment: add if statement to subtraction, implement mul/div with error handling, add onlyOwner modifier to state-changing functions --- cairo_program/src/integer.cairo | 17 ++++++++++------- starknet_contracts/src/lib.cairo | 2 ++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo index b5539a6..206a79e 100644 --- a/cairo_program/src/integer.cairo +++ b/cairo_program/src/integer.cairo @@ -5,24 +5,27 @@ pub fn add_num(x: u32, y: u32) -> u32 { x + y } -// subtraction logic +// subtraction logic with if statement to check for negative result pub fn sub_num(x: u32, y: u32) -> u32 { - assert(x >= y, 'Result cannot be negative'); + if x < y { + panic!("Result cannot be negative"); + } x - y } -// multiplication logic +// multiplication logic with overflow handling pub fn mul_num(x: u32, y: u32) -> u32 { let result = x.checked_mul(y); - assert(result.is_some(), 'Multiplication overflow'); match result { Option::Some(val) => val, Option::None => panic!("Multiplication overflow"), } } -// division logic +// division logic with zero check pub fn div_num(x: u32, y: u32) -> u32 { - assert(y != 0, 'Division by zero'); + if y == 0 { + panic!("Division by zero"); + } x / y -} +} \ No newline at end of file diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index 756f591..342d044 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -58,6 +58,7 @@ mod Counter { fn multiply_count(ref self: ContractState, amount: u32) { self.only_owner(); + assert(amount != 0, 'Amount cannot be 0'); let current = self.count.read(); let new_count = mul_num(current, amount); self.count.write(new_count); @@ -65,6 +66,7 @@ mod Counter { fn divide_count(ref self: ContractState, amount: u32) { self.only_owner(); + assert(amount != 0, 'Amount cannot be 0'); let current = self.count.read(); let new_count = div_num(current, amount); self.count.write(new_count); From cf73f1e7a7e13e023bbabdaaf1f9bded4a8f04c5 Mon Sep 17 00:00:00 2001 From: chukwudiikeh Date: Thu, 30 Apr 2026 00:28:05 +0000 Subject: [PATCH 3/7] Update Cairo programs and Starknet contracts --- IMPLEMENTATION_EXPLAINED.md | 535 +++++++------------------------ cairo_program/src/integer.cairo | 53 +-- starknet_contracts/src/lib.cairo | 2 +- 3 files changed, 149 insertions(+), 441 deletions(-) diff --git a/IMPLEMENTATION_EXPLAINED.md b/IMPLEMENTATION_EXPLAINED.md index 4a2ffa8..3602337 100644 --- a/IMPLEMENTATION_EXPLAINED.md +++ b/IMPLEMENTATION_EXPLAINED.md @@ -1,128 +1,112 @@ -# Cairo Counter Contract - Line by Line (Explain Like I'm 5) +# Cairo Bootcamp 6 - Implementation Explained --- ## File 1: `cairo_program/src/integer.cairo` -This file is like a toolbox with 4 basic math tools. +This file contains basic arithmetic operations with safety checks. --- -### Addition Function +### Main Function ```cairo -pub fn add_num(x: u32, y: u32) -> u32 { +#[executable] +fn main() { + let x: u32 = 10; + let y: u32 = 5; + + println!("Addition: {} + {} = {}", x, y, add_num(x, y)); + println!("Subtraction: {} - {} = {}", x, y, sub_num(x, y)); + println!("Multiplication: {} * {} = {}", x, y, mul_num(x, y)); + println!("Division: {} / {} = {}", x, y, div_num(x, y)); +} ``` -**Line 1:** "Hey everyone, I'm making a tool called add_num!" -- `pub` = everyone can use this tool (not private) -- `fn` = I'm making a tool (function) -- `add_num` = the tool's name -- `x: u32, y: u32` = give me two whole numbers (like 5 and 3) -- `-> u32` = I'll give you back one whole number -```cairo - x + y -``` -**Line 2:** Add the two numbers like 5 + 3 = 8, and give back the answer +**What it does:** Demonstrates all four arithmetic functions with sample values (10 and 5). + +--- + +### Addition Function ```cairo +fn add_num(x: u32, y: u32) -> u32 { + x + y } ``` -**Line 3:** "I'm done making this tool!" + +**What it does:** Takes two unsigned 32-bit integers and returns their sum. --- ### Subtraction Function ```cairo -pub fn sub_num(x: u32, y: u32) -> u32 { -``` -**Line 1:** "Hey everyone, I'm making a tool called sub_num!" -- Give me two numbers, I'll give you back one number - -```cairo - assert(x >= y, 'Result cannot be negative'); -``` -**Line 2:** "Wait! Let me check something first!" -- `assert()` = like a safety guard that says "STOP!" if something is wrong -- `x >= y` = is the first number bigger than or equal to the second? (like: is 10 ≥ 3?) -- If not (like 3 - 10), yell: 'Result cannot be negative' -- Why? You can't have -7 apples! Our numbers don't do negatives. - -```cairo +fn sub_num(x: u32, y: u32) -> u32 { + if x < y { + panic!("Result cannot be negative"); + } x - y -``` -**Line 3:** Okay, safe to subtract! Do 10 - 3 = 7 and give back the answer - -```cairo } ``` -**Line 4:** "I'm done making this tool!" + +**What it does:** Subtracts y from x, but panics if the result would be negative (since u32 can't represent negative numbers). --- ### Multiplication Function ```cairo -pub fn mul_num(x: u32, y: u32) -> u32 { -``` -**Line 1:** "Hey everyone, I'm making a tool called mul_num!" +fn mul_num(x: u32, y: u32) -> u32 { + match x.checked_mul(y) { + Option::Some(val) => val, + Option::None => panic!("Multiplication overflow"), + } +} -```cairo - let result = x.checked_mul(y); -``` -**Line 2:** "Let me try to multiply these numbers carefully..." -- `let result =` = make a box to hold the answer -- `x.checked_mul(y)` = multiply like 5 × 3, but check if the answer is TOO BIG -- If it works: put the answer in a gift box labeled "Some(15)" -- If too big: put nothing in the box, label it "None" -- Example: trying to count 4 billion × 2 = way too many to count! +7% > fn mul_num(x: u32, y: u32) -> u32 { + match x.checked_mul(y) { + Option::Some(val) => val, + Option::None => panic!("Multiplication overflow"), + } -```cairo - assert(result.is_some(), 'Multiplication overflow'); -``` -**Line 3:** "Did I get an answer, or was it too big?" -- `result.is_some()` = is there a number in the box? -- If the box is empty (None), yell: 'Multiplication overflow' (too big!) +> # Explaining mul_num Line by Line (ELI5 Style) -```cairo - result.unwrap() -``` -**Line 4:** "Open the gift box and take out the number!" -- `unwrap()` = open the box and grab the number inside -- Safe because we already checked the box isn't empty +Line 1: fn mul_num(x: u32, y: u32) -> u32 { +This says: "I'm creating a function called mul_num. It takes two numbers (x and y), both are u32 (unsigned 32-bit integers, meaning whole numbers from 0 to 4,294,967,295). The function will give back one number (-> u32)." -```cairo -} -``` -**Line 5:** "I'm done making this tool!" +Line 2: match x.checked_mul(y) { +This is the important part. checked_mul tries to multiply x and y. But here's the trick: instead of just doing the math, it checks if the answer is too big to fit in a u32. If it fits, great! If it doesn't fit (overflow), it returns a special "nothing" value. The match +keyword says: "Let me look at what happened and decide what to do next." ---- +Line 3: Option::Some(val) => val, +This says: "If the multiplication worked and gave us a real answer, that answer is wrapped in Some(val). Just unwrap it and give back val." Think of it like opening a present — the answer is inside the box. -### Division Function +Line 4: Option::None => panic!("Multiplication overflow"), +This says: "If the multiplication was too big and failed, we get None (nothing). When that happens, crash the program and show the error message 'Multiplication overflow'." panic! is like hitting the emergency stop button. -```cairo -pub fn div_num(x: u32, y: u32) -> u32 { -``` -**Line 1:** "Hey everyone, I'm making a tool called div_num!" +Line 5: } +End of the match statement. -```cairo - assert(y != 0, 'Division by zero'); +In simple terms: This function multiplies two numbers safely. If the answer is too big, it stops and tells you instead of giving you a wrong answer. ``` -**Line 2:** "Wait! You can't divide by zero!" -- `y != 0` = is the second number NOT zero? -- If it IS zero, yell: 'Division by zero' -- Why? If you have 10 cookies and try to share them among 0 friends... that makes no sense! -```cairo - x / y -``` -**Line 3:** Okay, safe to divide! Do 10 ÷ 2 = 5 and give back the answer +**What it does:** Multiplies two numbers safely. Uses `checked_mul` to detect overflow and panics if the result is too large for u32. + +--- + +### Division Function ```cairo +fn div_num(x: u32, y: u32) -> u32 { + if y == 0 { + panic!("Division by zero"); + } + x / y } ``` -**Line 4:** "I'm done making this tool!" + +**What it does:** Divides x by y, but panics if y is zero (division by zero is undefined). --- @@ -130,452 +114,159 @@ pub fn div_num(x: u32, y: u32) -> u32 { ```cairo pub mod integer; +mod bytearray; ``` -**This line:** "Hey, I have a toolbox called 'integer' and everyone can use it!" -- `pub` = everyone can borrow my tools -- `mod integer` = the toolbox is in the file called `integer.cairo` -- This lets the smart contract use our math tools + +**What it does:** Exposes the `integer` module publicly so other crates can use it. The `bytearray` module is private. --- ## File 3: `starknet_contracts/src/lib.cairo` -This is like building a special calculator that lives on the blockchain! +This file contains a smart contract that uses the arithmetic functions from `cairo_program`. --- -### Interface Definition (The Menu) +### Interface Definition ```cairo +/// Interface representing `HelloContract`. +/// This interface allows modification and retrieval of the contract's storage count. #[starknet::interface] -``` -**Line 1:** "I'm making a menu that shows what buttons my calculator has!" - -```cairo pub trait ICounter { -``` -**Line 2:** "The menu is called ICounter" -- `pub` = everyone can see this menu -- `trait` = this is a list of buttons (not the actual buttons yet) - -```cairo + /// Increase count. fn increase_count(ref self: T, amount: u32); -``` -**Line 3:** "Button 1: Make the number bigger" -- `ref self: T` = this button can change what's on the screen -- `amount: u32` = you tell me how much to add - -```cairo + /// Decrease count. fn reduce_count(ref self: T, amount: u32); -``` -**Line 4:** "Button 2: Make the number smaller" - -```cairo + /// Multiply count. fn multiply_count(ref self: T, amount: u32); -``` -**Line 5:** "Button 3: Multiply the number" - -```cairo + /// Divide count. fn divide_count(ref self: T, amount: u32); -``` -**Line 6:** "Button 4: Divide the number" - -```cairo + /// Retrieve count. fn get_count(self: @T) -> u32; -``` -**Line 7:** "Button 5: Just look at the number (don't change it)" -- `self: @T` = this button only LOOKS, can't change anything -- `-> u32` = tells you what number is on the screen - -```cairo } ``` -**Line 8:** "That's all the buttons on my menu!" + +**What it does:** Defines the public interface for the Counter contract with five operations (increase, decrease, multiply, divide, and retrieve). --- ### Contract Definition ```cairo +/// Simple contract for managing count. #[starknet::contract] -``` -**Line 1:** "Now I'm building the ACTUAL calculator (not just the menu)!" - -```cairo mod Counter { -``` -**Line 2:** "My calculator is called Counter" - ---- - -### Imports (Getting Tools) - -```cairo use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; -``` -**Line 3:** "I need tools to save and read numbers from the blockchain!" -- `StoragePointerReadAccess` = tool to READ saved numbers -- `StoragePointerWriteAccess` = tool to WRITE/SAVE numbers - -```cairo use starknet::{ContractAddress, get_caller_address}; -``` -**Line 4:** "I need tools to know WHO is using my calculator!" -- `ContractAddress` = like a home address, but for blockchain accounts -- `get_caller_address` = tells me who just pressed a button - -```cairo use cairo_program::integer::{add_num, sub_num, mul_num, div_num}; -``` -**Line 5:** "I'm borrowing the 4 math tools we made earlier!" -- Brings in add, subtract, multiply, and divide from our toolbox - ---- -### Storage (The Calculator's Memory) - -```cairo #[storage] -``` -**Line 6:** "Here's what my calculator remembers forever:" - -```cairo struct Storage { -``` -**Line 7:** "I have a memory box with 2 things in it:" - -```cairo count: u32, -``` -**Line 8:** "Thing 1: The number on the screen (called 'count')" - -```cairo owner: ContractAddress, -``` -**Line 9:** "Thing 2: The address of who owns this calculator" -- Like writing your name on your calculator so others can't use it - -```cairo } ``` -**Line 10:** "That's everything I remember!" + +**What it does:** +- Imports storage access tools and caller identification +- Imports the four arithmetic functions from `cairo_program` +- Defines storage with a `count` (the current value) and `owner` (who can modify it) --- -### Constructor (First Time Setup) +### Constructor ```cairo #[constructor] -``` -**Line 11:** "This runs ONE TIME when someone first creates the calculator!" - -```cairo fn constructor(ref self: ContractState, owner: ContractAddress) { -``` -**Line 12:** "Setup function - tell me who the owner is!" -- `ref self: ContractState` = I can write to memory -- `owner: ContractAddress` = give me the owner's address - -```cairo self.owner.write(owner); -``` -**Line 13:** "Write the owner's name in permanent marker!" -- `self.owner` = the owner spot in memory -- `.write(owner)` = save the owner's address to the blockchain forever - -```cairo } ``` -**Line 14:** "Setup complete!" + +**What it does:** Runs once when the contract is deployed. Sets the owner address. --- -### Internal Helper (Security Guard) +### Security Helper ```cairo #[generate_trait] -``` -**Line 15:** "Make a special helper tool automatically" - -```cairo impl InternalImpl of InternalTrait { -``` -**Line 16:** "Here are my private helper tools:" - -```cairo fn only_owner(self: @ContractState) { -``` -**Line 17:** "Security guard tool - checks if you're the owner!" -- `self: @ContractState` = I only need to LOOK at memory, not change it - -```cairo assert(get_caller_address() == self.owner.read(), 'Caller is not the owner'); -``` -**Line 18:** "Are you the owner? Let me check your ID!" -- `get_caller_address()` = who are you? -- `self.owner.read()` = who is the owner? -- `==` = are these the same person? -- If NO, yell: 'Caller is not the owner' and kick them out! - -```cairo } -``` -**Line 19:** "Security check done!" - -```cairo } ``` -**Line 20:** "All helper tools defined!" + +**What it does:** Provides a reusable security check that ensures only the owner can call certain functions. --- -### Public Functions (The Actual Buttons!) +### Public Functions ```cairo #[abi(embed_v0)] -``` -**Line 21:** "These buttons can be pressed from outside the calculator!" - -```cairo impl CounterImpl of super::ICounter { -``` -**Line 22:** "Now I'm building the REAL buttons from the menu we made earlier!" - ---- - -### Increase Count Button - -```cairo fn increase_count(ref self: ContractState, amount: u32) { -``` -**Line 23:** "The '+' button - makes the number bigger!" - -```cairo self.only_owner(); -``` -**Line 24:** "STOP! Security guard, check their ID!" -- Calls the security guard we made earlier -- If not the owner, they get kicked out here - -```cairo assert(amount != 0, 'Amount cannot be 0'); -``` -**Line 25:** "You can't add zero! That's silly!" -- `amount != 0` = is the amount NOT zero? -- If it IS zero, yell: 'Amount cannot be 0' - -```cairo let current = self.count.read(); -``` -**Line 26:** "Let me check what number is on the screen right now..." -- `let current =` = make a box called 'current' -- `self.count.read()` = read the number from the calculator's memory - -```cairo let new_count = add_num(current, amount); -``` -**Line 27:** "Use my add tool to calculate: current + amount" -- Calls our `add_num` function from the toolbox -- Example: if screen shows 10 and you add 5, new_count = 15 - -```cairo self.count.write(new_count); -``` -**Line 28:** "Write the new number on the screen and save it forever!" -- `self.count.write()` = save the new number to blockchain memory - -```cairo } -``` -**Line 29:** "'+' button is done!" - ---- -### Reduce Count Button - -```cairo fn reduce_count(ref self: ContractState, amount: u32) { -``` -**Line 30:** "The '-' button - makes the number smaller!" - -```cairo self.only_owner(); -``` -**Line 31:** "Security guard, check their ID!" - -```cairo let current = self.count.read(); -``` -**Line 32:** "What number is on the screen?" - -```cairo let new_count = sub_num(current, amount); -``` -**Line 33:** "Use my subtract tool: current - amount" -- Calls our safe `sub_num` function -- If the answer would be negative (like 3 - 10), it will yell an error! - -```cairo self.count.write(new_count); -``` -**Line 34:** "Write the new number on the screen!" - -```cairo } -``` -**Line 35:** "'-' button is done!" - ---- -### Multiply Count Button - -```cairo fn multiply_count(ref self: ContractState, amount: u32) { -``` -**Line 36:** "The '×' button - makes the number bigger by multiplying!" - -```cairo self.only_owner(); -``` -**Line 37:** "Security guard, check their ID!" - -```cairo + assert(amount != 0, 'Amount cannot be 0'); let current = self.count.read(); -``` -**Line 38:** "What number is on the screen?" - -```cairo let new_count = mul_num(current, amount); -``` -**Line 39:** "Use my multiply tool: current × amount" -- Calls our safe `mul_num` function -- If the answer is too big (overflow), it will yell an error! - -```cairo self.count.write(new_count); -``` -**Line 40:** "Write the new number on the screen!" - -```cairo } -``` -**Line 41:** "'×' button is done!" ---- - -### Divide Count Button - -```cairo fn divide_count(ref self: ContractState, amount: u32) { -``` -**Line 42:** "The '÷' button - makes the number smaller by dividing!" - -```cairo self.only_owner(); -``` -**Line 43:** "Security guard, check their ID!" - -```cairo + assert(amount != 0, 'Amount cannot be 0'); let current = self.count.read(); -``` -**Line 44:** "What number is on the screen?" - -```cairo let new_count = div_num(current, amount); -``` -**Line 45:** "Use my divide tool: current ÷ amount" -- Calls our safe `div_num` function -- If you try to divide by zero, it will yell an error! - -```cairo self.count.write(new_count); -``` -**Line 46:** "Write the new number on the screen!" - -```cairo } -``` -**Line 47:** "'÷' button is done!" - ---- -### Get Count Button (Just Look!) - -```cairo fn get_count(self: @ContractState) -> u32 { -``` -**Line 48:** "The 'look' button - just shows you the number!" -- `self: @ContractState` = I only LOOK, I don't change anything (notice the `@`) -- `-> u32` = I'll tell you what number is on the screen -- NO security guard! Anyone can look! - -```cairo self.count.read() -``` -**Line 49:** "Here's the number on the screen!" -- Just read and tell you the number, that's it! - -```cairo } -``` -**Line 50:** "'Look' button is done!" - -```cairo } -``` -**Line 51:** "All buttons are built!" - -```cairo } ``` -**Line 52:** "Calculator is complete!" - ---- -## Configuration Files (Setup Instructions) +**What it does:** +- `increase_count`: Adds amount to count (owner only, amount must be non-zero) +- `reduce_count`: Subtracts amount from count (owner only) +- `multiply_count`: Multiplies count by amount (owner only, amount must be non-zero) +- `divide_count`: Divides count by amount (owner only, amount must be non-zero) +- `get_count`: Returns current count (anyone can call) -### `cairo_program/Scarb.toml` - -```toml -name = "cairo_program" -``` -**What we did:** Changed the name to `cairo_program` -- Like naming your toolbox so others can find it -- The smart contract needs to know what to call it when borrowing tools - -### `starknet_contracts/Scarb.toml` - -```toml -cairo_program = { path = "../cairo_program" } -``` -**What we did:** Told the calculator where to find the toolbox -- `cairo_program` = the toolbox name -- `path = "../cairo_program"` = go up one folder, then into cairo_program folder -- Like giving directions: "The tools are in the garage!" - -### `cairo_program/src/lib.cairo` - -```cairo -pub mod integer; -``` -**What we did:** Opened the toolbox door -- `pub` = everyone can borrow these tools -- Without this, the tools would be locked inside and nobody could use them! +Each state-changing function: +1. Checks that the caller is the owner +2. Validates input (amount != 0 where applicable) +3. Reads the current count +4. Calls the appropriate arithmetic function +5. Writes the new count to storage --- -## How Everything Works Together (Story Time!) - -1. **You create the calculator** → You become the owner, screen shows 0 -2. **You press the '+10' button** → Screen now shows 10 -3. **You press the '×3' button** → Screen now shows 30 -4. **You press the '÷5' button** → Screen now shows 6 -5. **Your friend presses 'look'** → They see 6 (anyone can look!) -6. **Your friend tries to press '+5'** → ❌ Security guard says "You're not the owner!" and kicks them out -7. **You press '-10'** → Screen now shows -4... WAIT! ❌ The subtract tool yells "Result cannot be negative!" and stops you - -The calculator is safe because: -- Only YOU can change the numbers (owner protection) -- It won't let you make mistakes (error checking) -- Everyone can see the number, but only you can change it -- All the numbers are saved on the blockchain forever! +## How It All Works Together + +1. **cairo_program** provides safe arithmetic functions with overflow/underflow checks +2. **starknet_contracts** uses these functions in a smart contract +3. The contract stores a count value on the blockchain +4. Only the owner can modify the count +5. Anyone can read the current count +6. All operations use the safe arithmetic functions to prevent errors diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo index 206a79e..cc0b1c0 100644 --- a/cairo_program/src/integer.cairo +++ b/cairo_program/src/integer.cairo @@ -1,31 +1,48 @@ -use core::num::traits::CheckedMul; +use core::num::traits::{CheckedMul, CheckedAdd, CheckedSub, CheckedDiv}; -// addition logic -pub fn add_num(x: u32, y: u32) -> u32 { - x + y +fn main() { + let x: u32 = 10; + let y: u32 = 5; + + println!("Addition: {} + {} = {}", x, y, add_num(x, y)); + println!("Subtraction: {} - {} = {}", x, y, sub_num(x, y)); + println!("Multiplication: {} * {} = {}", x, y, mul_num(x, y)); + println!("Division: {} / {} = {}", x, y, div_num(x, y)); } -// subtraction logic with if statement to check for negative result -pub fn sub_num(x: u32, y: u32) -> u32 { - if x < y { - panic!("Result cannot be negative"); +fn add_num(x: u32, y: u32) -> u32 { + match x.checked_add(y) { + Option::Some(val) => val, + Option::None => panic!("Addition overflow"), + } +} + +fn sub_num(x: u32, y: u32) -> u32 { + if y > x { + panic!("Subtraction underflow:); + } + match x.checked_sub(y) { + Option::Some(val) => val, + Option::None => panic!("Subtraction underflow"), } - x - y } -// multiplication logic with overflow handling -pub fn mul_num(x: u32, y: u32) -> u32 { - let result = x.checked_mul(y); - match result { +fn mul_num(x: u32, y: u32) -> u32 { + match x.checked_mul(y) { Option::Some(val) => val, Option::None => panic!("Multiplication overflow"), } } -// division logic with zero check -pub fn div_num(x: u32, y: u32) -> u32 { - if y == 0 { +fn div_num(x: u32, y: u32) -> u32 { + if y == 0 { panic!("Division by zero"); } - x / y -} \ No newline at end of file + if y > x { + panic!("Result would be less than 1"); + } + match x.checked_div(y) { + Option::Some(val) => val, + Option::None => panic!("Division error"), + } +} diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index 342d044..a2f8c1e 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -9,7 +9,7 @@ pub trait ICounter { /// Multiply count. fn multiply_count(ref self: T, amount: u32); /// Divide count. - fn divide_count(ref self: T, amount: u32); + fn divide_count(ref self: T, amount: u32);` /// Retrieve count. fn get_count(self: @T) -> u32; } From 1acf99987f165cab270629ef360faeaeaadf48db Mon Sep 17 00:00:00 2001 From: chukwudiikeh Date: Wed, 6 May 2026 12:59:51 +0000 Subject: [PATCH 4/7] Fix Cairo syntax errors and add executable attribute --- cairo_program/Scarb.toml | 1 + cairo_program/src/integer.cairo | 18 ++++++++---------- cairo_program/src/lib.cairo | 2 +- starknet_contracts/src/lib.cairo | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/cairo_program/Scarb.toml b/cairo_program/Scarb.toml index b69787b..ead2619 100644 --- a/cairo_program/Scarb.toml +++ b/cairo_program/Scarb.toml @@ -8,6 +8,7 @@ edition = "2025_12" [lib] [executable] +name = "cairo_program" [cairo] enable-gas = false diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo index cc0b1c0..7d7ac7a 100644 --- a/cairo_program/src/integer.cairo +++ b/cairo_program/src/integer.cairo @@ -1,5 +1,6 @@ -use core::num::traits::{CheckedMul, CheckedAdd, CheckedSub, CheckedDiv}; +use core::num::traits::{CheckedMul, CheckedAdd, CheckedSub}; +#[executable] fn main() { let x: u32 = 10; let y: u32 = 5; @@ -10,16 +11,16 @@ fn main() { println!("Division: {} / {} = {}", x, y, div_num(x, y)); } -fn add_num(x: u32, y: u32) -> u32 { +pub fn add_num(x: u32, y: u32) -> u32 { match x.checked_add(y) { Option::Some(val) => val, Option::None => panic!("Addition overflow"), } } -fn sub_num(x: u32, y: u32) -> u32 { +pub fn sub_num(x: u32, y: u32) -> u32 { if y > x { - panic!("Subtraction underflow:); + panic!("Subtraction underflow"); } match x.checked_sub(y) { Option::Some(val) => val, @@ -27,22 +28,19 @@ fn sub_num(x: u32, y: u32) -> u32 { } } -fn mul_num(x: u32, y: u32) -> u32 { +pub fn mul_num(x: u32, y: u32) -> u32 { match x.checked_mul(y) { Option::Some(val) => val, Option::None => panic!("Multiplication overflow"), } } -fn div_num(x: u32, y: u32) -> u32 { +pub fn div_num(x: u32, y: u32) -> u32 { if y == 0 { panic!("Division by zero"); } if y > x { panic!("Result would be less than 1"); } - match x.checked_div(y) { - Option::Some(val) => val, - Option::None => panic!("Division error"), - } + x / y } diff --git a/cairo_program/src/lib.cairo b/cairo_program/src/lib.cairo index fcf903f..56cad6d 100644 --- a/cairo_program/src/lib.cairo +++ b/cairo_program/src/lib.cairo @@ -2,4 +2,4 @@ // mod short_string; pub mod integer; // mod bool; -mod bytearray; \ No newline at end of file +// mod bytearray; \ No newline at end of file diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index a2f8c1e..342d044 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -9,7 +9,7 @@ pub trait ICounter { /// Multiply count. fn multiply_count(ref self: T, amount: u32); /// Divide count. - fn divide_count(ref self: T, amount: u32);` + fn divide_count(ref self: T, amount: u32); /// Retrieve count. fn get_count(self: @T) -> u32; } From a2393f138f97be7402fd798b7bdb37d93e36ad96 Mon Sep 17 00:00:00 2001 From: chukwudiikeh Date: Thu, 21 May 2026 16:51:52 +0000 Subject: [PATCH 5/7] Update restricted_token contract --- starknet_contracts/src/restricted_token.cairo | 249 ++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 starknet_contracts/src/restricted_token.cairo diff --git a/starknet_contracts/src/restricted_token.cairo b/starknet_contracts/src/restricted_token.cairo new file mode 100644 index 0000000..2424de6 --- /dev/null +++ b/starknet_contracts/src/restricted_token.cairo @@ -0,0 +1,249 @@ +use starknet::ContractAddress; + +#[starknet::interface] +pub trait IERC20 { + fn get_name(self: @TContractState) -> felt252; + fn get_symbol(self: @TContractState) -> felt252; + fn get_decimals(self: @TContractState) -> u8; + fn get_total_supply(self: @TContractState) -> u256; + fn balance_of(self: @TContractState, account: ContractAddress) -> u256; + fn allowance( + self: @TContractState, owner: ContractAddress, spender: ContractAddress, + ) -> u256; + fn transfer(ref self: TContractState, recipient: ContractAddress, amount: u256); + fn transfer_from( + ref self: TContractState, + sender: ContractAddress, + recipient: ContractAddress, + amount: u256, + ); + fn approve(ref self: TContractState, spender: ContractAddress, amount: u256); + fn increase_allowance(ref self: TContractState, spender: ContractAddress, added_value: u256); + fn decrease_allowance( + ref self: TContractState, spender: ContractAddress, subtracted_value: u256, + ); + fn burn(ref self: TContractState, amount: u256); + fn revoke(ref self: TContractState, account: ContractAddress); + fn update_transfer_limit(ref self: TContractState, new_limit: u256); + fn get_transfer_limit(self: @TContractState) -> u256; + fn mint(ref self: TContractState, recipient: ContractAddress, amount: u256); +} +#[starknet::contract] +pub mod erc20 { + + use core::num::traits::Zero; + use starknet::get_caller_address; + use starknet::ContractAddress; + use starknet::storage::{ Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,StoragePointerWriteAccess,}; + + #[storage] + struct Storage { + name: felt252, + symbol: felt252, + decimals: u8, + total_supply: u256, + balances: Map::, + allowances: Map::<(ContractAddress, ContractAddress), u256>, + admin: ContractAddress, + transfer_limit: u256, + revoked: Map::, + } + + #[event] + #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] + pub enum Event { + Transfer: Transfer, + Approval: Approval, + Burn: Burn, + Revoke: Revoke, + LimitUpdated: LimitUpdated, + } + #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] + pub struct Transfer { + pub from: ContractAddress, + pub to: ContractAddress, + pub value: u256, + } + #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] + pub struct Approval { + pub owner: ContractAddress, + pub spender: ContractAddress, + pub value: u256, + } + #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] + pub struct Burn { + pub from: ContractAddress, + pub value: u256, + } + #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] + pub struct Revoke { + pub account: ContractAddress, + } + #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] + pub struct LimitUpdated { + pub new_limit: u256, + } + + mod Errors { + pub const APPROVE_FROM_ZERO: felt252 = 'ERC20: approve from 0'; + pub const APPROVE_TO_ZERO: felt252 = 'ERC20: approve to 0'; + pub const TRANSFER_FROM_ZERO: felt252 = 'ERC20: transfer from 0'; + pub const TRANSFER_TO_ZERO: felt252 = 'ERC20: transfer to 0'; + pub const BURN_FROM_ZERO: felt252 = 'ERC20: burn from 0'; + pub const MINT_TO_ZERO: felt252 = 'ERC20: mint to 0'; + pub const TRANSFER_LIMIT_EXCEEDED: felt252 = 'ERC20: transfer limit exceeded'; + pub const ACCOUNT_REVOKED: felt252 = 'ERC20: account revoked'; + pub const UNAUTHORIZED: felt252 = 'ERC20: unauthorized'; + } + + #[constructor] + fn constructor( + ref self: ContractState, + owner:ContractAddress, + recipient: ContractAddress, + name: felt252, + decimals: u8, + initial_supply: u256, + symbol: felt252, + ) { + self.name.write(name); + self.symbol.write(symbol); + self.decimals.write(decimals); + self.admin.write(owner); + self.transfer_limit.write(10000_u256); + self.mint(recipient, initial_supply); + } + + #[abi(embed_v0)] + impl IERC20Impl of super::IERC20 { + fn get_name(self: @ContractState) -> felt252 { + self.name.read() + } + + fn get_symbol(self: @ContractState) -> felt252 { + self.symbol.read() + } + + fn get_decimals(self: @ContractState) -> u8 { + self.decimals.read() + } + + fn get_total_supply(self: @ContractState) -> u256 { + self.total_supply.read() + } + + fn balance_of(self: @ContractState, account: ContractAddress) -> u256 { + self.balances.read(account) + } + + fn allowance(self: @ContractState, owner: ContractAddress, spender: ContractAddress,) -> u256 { + self.allowances.read((owner, spender)) + } + + fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) { + let sender = get_caller_address(); + self._transfer(sender, recipient, amount); + } + + fn transfer_from(ref self: ContractState,sender: ContractAddress,recipient: ContractAddress,amount: u256,) { + let caller = get_caller_address(); + self.spend_allowance(sender, caller, amount); + self._transfer(sender, recipient, amount); + } + + fn approve(ref self: ContractState, spender: ContractAddress, amount: u256) { + let caller = get_caller_address(); + self.approve_helper(caller, spender, amount); + } + + fn increase_allowance(ref self: ContractState, spender: ContractAddress, added_value: u256) { + let caller = get_caller_address(); + self.approve_helper(caller, spender, self.allowances.read((caller, spender)) + added_value,); + } + + fn decrease_allowance(ref self: ContractState, spender: ContractAddress, subtracted_value: u256,) { + let caller = get_caller_address(); + self.approve_helper(caller, spender, self.allowances.read((caller, spender)) - subtracted_value); + } + + fn burn(ref self: ContractState, amount: u256) { + let caller = get_caller_address(); + assert(caller == self.admin.read(), Errors::UNAUTHORIZED); + assert(amount.is_non_zero(), Errors::BURN_FROM_ZERO); + self.balances.write(caller, self.balances.read(caller) - amount); + self.total_supply.write(self.total_supply.read() - amount); + self.balances.write(Zero::zero(), self.balances.read(Zero::zero()) + amount); + self.emit(Event::Burn(Burn { from: caller, value: amount })); + } + + fn revoke(ref self: ContractState, account: ContractAddress) { + assert(get_caller_address() == self.admin.read(), Errors::UNAUTHORIZED); + self.revoked.write(account, true); + self.emit(Event::Revoke(Revoke { account })); + } + + fn update_transfer_limit(ref self: ContractState, new_limit: u256) { + assert(get_caller_address() == self.admin.read(), Errors::UNAUTHORIZED); + self.transfer_limit.write(new_limit); + self.emit(Event::LimitUpdated(LimitUpdated { new_limit })); + } + + fn get_transfer_limit(self: @ContractState) -> u256 { + self.transfer_limit.read() + } + + fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) { + assert(get_caller_address() == self.admin.read(), Errors::UNAUTHORIZED); + self._mint(recipient, amount); + } + } + + #[generate_trait] + impl InternalImpl of InternalTrait { + fn _transfer( + ref self: ContractState, + sender: ContractAddress, + recipient: ContractAddress, + amount: u256, + ) { + assert(sender.is_non_zero(), Errors::TRANSFER_FROM_ZERO); + assert(recipient.is_non_zero(), Errors::TRANSFER_TO_ZERO); + assert(!self.revoked.read(sender), Errors::ACCOUNT_REVOKED); + let limit = self.transfer_limit.read(); + assert(amount <= limit, Errors::TRANSFER_LIMIT_EXCEEDED); + self.balances.write(sender, self.balances.read(sender) - amount); + self.balances.write(recipient, self.balances.read(recipient) + amount); + self.emit(Transfer { from: sender, to: recipient, value: amount }); + } + + fn spend_allowance( + ref self: ContractState, + owner: ContractAddress, + spender: ContractAddress, + amount: u256, + ) { + let allowance = self.allowances.read((owner, spender)); + self.allowances.write((owner, spender), allowance - amount); + } + + fn approve_helper( + ref self: ContractState, + owner: ContractAddress, + spender: ContractAddress, + amount: u256, + ) { + assert(spender.is_non_zero(), Errors::APPROVE_TO_ZERO); + self.allowances.write((owner, spender), amount); + self.emit(Approval { owner, spender, value: amount }); + } + + fn _mint(ref self: ContractState, recipient: ContractAddress, amount: u256) { + assert(recipient.is_non_zero(), Errors::MINT_TO_ZERO); + let supply = self.total_supply.read() + amount; + self.total_supply.write(supply); + let balance = self.balances.read(recipient) + amount; + self.balances.write(recipient, balance); + self.emit(Event::Transfer(Transfer {from: Zero::zero(), to: recipient, value: amount},),); + } + } +} \ No newline at end of file From 38edd9adb3b380c55eb61553a8c014fdeb72b79c Mon Sep 17 00:00:00 2001 From: chukwudiikeh Date: Thu, 21 May 2026 16:54:04 +0000 Subject: [PATCH 6/7] Remove IMPLEMENTATION_EXPLAINED.md --- IMPLEMENTATION_EXPLAINED.md | 272 ------------------------------------ 1 file changed, 272 deletions(-) delete mode 100644 IMPLEMENTATION_EXPLAINED.md diff --git a/IMPLEMENTATION_EXPLAINED.md b/IMPLEMENTATION_EXPLAINED.md deleted file mode 100644 index 3602337..0000000 --- a/IMPLEMENTATION_EXPLAINED.md +++ /dev/null @@ -1,272 +0,0 @@ -# Cairo Bootcamp 6 - Implementation Explained - ---- - -## File 1: `cairo_program/src/integer.cairo` - -This file contains basic arithmetic operations with safety checks. - ---- - -### Main Function - -```cairo -#[executable] -fn main() { - let x: u32 = 10; - let y: u32 = 5; - - println!("Addition: {} + {} = {}", x, y, add_num(x, y)); - println!("Subtraction: {} - {} = {}", x, y, sub_num(x, y)); - println!("Multiplication: {} * {} = {}", x, y, mul_num(x, y)); - println!("Division: {} / {} = {}", x, y, div_num(x, y)); -} -``` - -**What it does:** Demonstrates all four arithmetic functions with sample values (10 and 5). - ---- - -### Addition Function - -```cairo -fn add_num(x: u32, y: u32) -> u32 { - x + y -} -``` - -**What it does:** Takes two unsigned 32-bit integers and returns their sum. - ---- - -### Subtraction Function - -```cairo -fn sub_num(x: u32, y: u32) -> u32 { - if x < y { - panic!("Result cannot be negative"); - } - x - y -} -``` - -**What it does:** Subtracts y from x, but panics if the result would be negative (since u32 can't represent negative numbers). - ---- - -### Multiplication Function - -```cairo -fn mul_num(x: u32, y: u32) -> u32 { - match x.checked_mul(y) { - Option::Some(val) => val, - Option::None => panic!("Multiplication overflow"), - } -} - -7% > fn mul_num(x: u32, y: u32) -> u32 { - match x.checked_mul(y) { - Option::Some(val) => val, - Option::None => panic!("Multiplication overflow"), - } - -> # Explaining mul_num Line by Line (ELI5 Style) - -Line 1: fn mul_num(x: u32, y: u32) -> u32 { -This says: "I'm creating a function called mul_num. It takes two numbers (x and y), both are u32 (unsigned 32-bit integers, meaning whole numbers from 0 to 4,294,967,295). The function will give back one number (-> u32)." - -Line 2: match x.checked_mul(y) { -This is the important part. checked_mul tries to multiply x and y. But here's the trick: instead of just doing the math, it checks if the answer is too big to fit in a u32. If it fits, great! If it doesn't fit (overflow), it returns a special "nothing" value. The match -keyword says: "Let me look at what happened and decide what to do next." - -Line 3: Option::Some(val) => val, -This says: "If the multiplication worked and gave us a real answer, that answer is wrapped in Some(val). Just unwrap it and give back val." Think of it like opening a present — the answer is inside the box. - -Line 4: Option::None => panic!("Multiplication overflow"), -This says: "If the multiplication was too big and failed, we get None (nothing). When that happens, crash the program and show the error message 'Multiplication overflow'." panic! is like hitting the emergency stop button. - -Line 5: } -End of the match statement. - -In simple terms: This function multiplies two numbers safely. If the answer is too big, it stops and tells you instead of giving you a wrong answer. -``` - -**What it does:** Multiplies two numbers safely. Uses `checked_mul` to detect overflow and panics if the result is too large for u32. - ---- - -### Division Function - -```cairo -fn div_num(x: u32, y: u32) -> u32 { - if y == 0 { - panic!("Division by zero"); - } - x / y -} -``` - -**What it does:** Divides x by y, but panics if y is zero (division by zero is undefined). - ---- - -## File 2: `cairo_program/src/lib.cairo` - -```cairo -pub mod integer; -mod bytearray; -``` - -**What it does:** Exposes the `integer` module publicly so other crates can use it. The `bytearray` module is private. - ---- - -## File 3: `starknet_contracts/src/lib.cairo` - -This file contains a smart contract that uses the arithmetic functions from `cairo_program`. - ---- - -### Interface Definition - -```cairo -/// Interface representing `HelloContract`. -/// This interface allows modification and retrieval of the contract's storage count. -#[starknet::interface] -pub trait ICounter { - /// Increase count. - fn increase_count(ref self: T, amount: u32); - /// Decrease count. - fn reduce_count(ref self: T, amount: u32); - /// Multiply count. - fn multiply_count(ref self: T, amount: u32); - /// Divide count. - fn divide_count(ref self: T, amount: u32); - /// Retrieve count. - fn get_count(self: @T) -> u32; -} -``` - -**What it does:** Defines the public interface for the Counter contract with five operations (increase, decrease, multiply, divide, and retrieve). - ---- - -### Contract Definition - -```cairo -/// Simple contract for managing count. -#[starknet::contract] -mod Counter { - use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; - use starknet::{ContractAddress, get_caller_address}; - use cairo_program::integer::{add_num, sub_num, mul_num, div_num}; - - #[storage] - struct Storage { - count: u32, - owner: ContractAddress, - } -``` - -**What it does:** -- Imports storage access tools and caller identification -- Imports the four arithmetic functions from `cairo_program` -- Defines storage with a `count` (the current value) and `owner` (who can modify it) - ---- - -### Constructor - -```cairo - #[constructor] - fn constructor(ref self: ContractState, owner: ContractAddress) { - self.owner.write(owner); - } -``` - -**What it does:** Runs once when the contract is deployed. Sets the owner address. - ---- - -### Security Helper - -```cairo - #[generate_trait] - impl InternalImpl of InternalTrait { - fn only_owner(self: @ContractState) { - assert(get_caller_address() == self.owner.read(), 'Caller is not the owner'); - } - } -``` - -**What it does:** Provides a reusable security check that ensures only the owner can call certain functions. - ---- - -### Public Functions - -```cairo - #[abi(embed_v0)] - impl CounterImpl of super::ICounter { - fn increase_count(ref self: ContractState, amount: u32) { - self.only_owner(); - assert(amount != 0, 'Amount cannot be 0'); - let current = self.count.read(); - let new_count = add_num(current, amount); - self.count.write(new_count); - } - - fn reduce_count(ref self: ContractState, amount: u32) { - self.only_owner(); - let current = self.count.read(); - let new_count = sub_num(current, amount); - self.count.write(new_count); - } - - fn multiply_count(ref self: ContractState, amount: u32) { - self.only_owner(); - assert(amount != 0, 'Amount cannot be 0'); - let current = self.count.read(); - let new_count = mul_num(current, amount); - self.count.write(new_count); - } - - fn divide_count(ref self: ContractState, amount: u32) { - self.only_owner(); - assert(amount != 0, 'Amount cannot be 0'); - let current = self.count.read(); - let new_count = div_num(current, amount); - self.count.write(new_count); - } - - fn get_count(self: @ContractState) -> u32 { - self.count.read() - } - } -} -``` - -**What it does:** -- `increase_count`: Adds amount to count (owner only, amount must be non-zero) -- `reduce_count`: Subtracts amount from count (owner only) -- `multiply_count`: Multiplies count by amount (owner only, amount must be non-zero) -- `divide_count`: Divides count by amount (owner only, amount must be non-zero) -- `get_count`: Returns current count (anyone can call) - -Each state-changing function: -1. Checks that the caller is the owner -2. Validates input (amount != 0 where applicable) -3. Reads the current count -4. Calls the appropriate arithmetic function -5. Writes the new count to storage - ---- - -## How It All Works Together - -1. **cairo_program** provides safe arithmetic functions with overflow/underflow checks -2. **starknet_contracts** uses these functions in a smart contract -3. The contract stores a count value on the blockchain -4. Only the owner can modify the count -5. Anyone can read the current count -6. All operations use the safe arithmetic functions to prevent errors From bba322986b44bfa287e0c01f25dadb88f9c4cd7d Mon Sep 17 00:00:00 2001 From: chukwudiikeh Date: Thu, 21 May 2026 17:03:23 +0000 Subject: [PATCH 7/7] Restructure: move contracts to CairoTest-1, add CairoTest-2 --- .vscode/settings.json | 3 -- .../starknet_contracts}/.gitignore | 0 .../starknet_contracts}/Scarb.lock | 0 .../starknet_contracts}/Scarb.toml | 0 .../starknet_contracts}/snfoundry.toml | 0 .../starknet_contracts}/src/lib.cairo | 0 .../src/restricted_token.cairo | 0 CairoTest-2/Readme.md | 1 + cairo_program/.gitignore | 1 - cairo_program/.vscode/settings.json | 3 -- cairo_program/README.md | 17 ------- cairo_program/Scarb.lock | 6 --- cairo_program/Scarb.toml | 19 -------- cairo_program/src/bool.cairo | 29 ------------ cairo_program/src/bytearray.cairo | 5 -- cairo_program/src/hello_world.cairo | 6 --- cairo_program/src/integer.cairo | 46 ------------------- cairo_program/src/lib.cairo | 5 -- cairo_program/src/short_string.cairo | 5 -- 19 files changed, 1 insertion(+), 145 deletions(-) delete mode 100644 .vscode/settings.json rename {starknet_contracts => CairoTest-1/starknet_contracts}/.gitignore (100%) rename {starknet_contracts => CairoTest-1/starknet_contracts}/Scarb.lock (100%) rename {starknet_contracts => CairoTest-1/starknet_contracts}/Scarb.toml (100%) rename {starknet_contracts => CairoTest-1/starknet_contracts}/snfoundry.toml (100%) rename {starknet_contracts => CairoTest-1/starknet_contracts}/src/lib.cairo (100%) rename {starknet_contracts => CairoTest-1/starknet_contracts}/src/restricted_token.cairo (100%) create mode 100644 CairoTest-2/Readme.md delete mode 100644 cairo_program/.gitignore delete mode 100644 cairo_program/.vscode/settings.json delete mode 100644 cairo_program/README.md delete mode 100644 cairo_program/Scarb.lock delete mode 100644 cairo_program/Scarb.toml delete mode 100644 cairo_program/src/bool.cairo delete mode 100644 cairo_program/src/bytearray.cairo delete mode 100644 cairo_program/src/hello_world.cairo delete mode 100644 cairo_program/src/integer.cairo delete mode 100644 cairo_program/src/lib.cairo delete mode 100644 cairo_program/src/short_string.cairo diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index edc55ce..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "snyk.advanced.autoSelectOrganization": true -} \ No newline at end of file diff --git a/starknet_contracts/.gitignore b/CairoTest-1/starknet_contracts/.gitignore similarity index 100% rename from starknet_contracts/.gitignore rename to CairoTest-1/starknet_contracts/.gitignore diff --git a/starknet_contracts/Scarb.lock b/CairoTest-1/starknet_contracts/Scarb.lock similarity index 100% rename from starknet_contracts/Scarb.lock rename to CairoTest-1/starknet_contracts/Scarb.lock diff --git a/starknet_contracts/Scarb.toml b/CairoTest-1/starknet_contracts/Scarb.toml similarity index 100% rename from starknet_contracts/Scarb.toml rename to CairoTest-1/starknet_contracts/Scarb.toml diff --git a/starknet_contracts/snfoundry.toml b/CairoTest-1/starknet_contracts/snfoundry.toml similarity index 100% rename from starknet_contracts/snfoundry.toml rename to CairoTest-1/starknet_contracts/snfoundry.toml diff --git a/starknet_contracts/src/lib.cairo b/CairoTest-1/starknet_contracts/src/lib.cairo similarity index 100% rename from starknet_contracts/src/lib.cairo rename to CairoTest-1/starknet_contracts/src/lib.cairo diff --git a/starknet_contracts/src/restricted_token.cairo b/CairoTest-1/starknet_contracts/src/restricted_token.cairo similarity index 100% rename from starknet_contracts/src/restricted_token.cairo rename to CairoTest-1/starknet_contracts/src/restricted_token.cairo diff --git a/CairoTest-2/Readme.md b/CairoTest-2/Readme.md new file mode 100644 index 0000000..a086d91 --- /dev/null +++ b/CairoTest-2/Readme.md @@ -0,0 +1 @@ +This is the link to my TEST-2 on the Starknetagentic: https://github.com/chukwudiikeh/Starknet- \ No newline at end of file diff --git a/cairo_program/.gitignore b/cairo_program/.gitignore deleted file mode 100644 index eb5a316..0000000 --- a/cairo_program/.gitignore +++ /dev/null @@ -1 +0,0 @@ -target diff --git a/cairo_program/.vscode/settings.json b/cairo_program/.vscode/settings.json deleted file mode 100644 index edc55ce..0000000 --- a/cairo_program/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "snyk.advanced.autoSelectOrganization": true -} \ No newline at end of file diff --git a/cairo_program/README.md b/cairo_program/README.md deleted file mode 100644 index 8cf3016..0000000 --- a/cairo_program/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# Cairo Program - -Welcome to **Cairo Bootcamp 6** — a hands-on, developer-focused program designed to help you learn Cairo from first principles and build real-world applications. This repository contains learning materials, examples, and exercises used throughout the bootcamp. - ---- - - - ---- - -## 📁 Repository Structure -. -├── src/ -├── starknet_contracts/ # Smart contracts (stateful logic) -├── exercises/ # Practice tasks and assignments -├── notes/ # Learning notes (felt, syntax, etc.) -└── README.md \ No newline at end of file diff --git a/cairo_program/Scarb.lock b/cairo_program/Scarb.lock deleted file mode 100644 index f297123..0000000 --- a/cairo_program/Scarb.lock +++ /dev/null @@ -1,6 +0,0 @@ -# Code generated by scarb DO NOT EDIT. -version = 1 - -[[package]] -name = "cairo_program" -version = "0.1.0" diff --git a/cairo_program/Scarb.toml b/cairo_program/Scarb.toml deleted file mode 100644 index ead2619..0000000 --- a/cairo_program/Scarb.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "cairo_program" -version = "0.1.0" -edition = "2025_12" - -# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html - -[lib] - -[executable] -name = "cairo_program" - -[cairo] -enable-gas = false - -[dependencies] -cairo_execute = "2.18.0" - - diff --git a/cairo_program/src/bool.cairo b/cairo_program/src/bool.cairo deleted file mode 100644 index cafad1e..0000000 --- a/cairo_program/src/bool.cairo +++ /dev/null @@ -1,29 +0,0 @@ -#[executable] -fn main() { - let result: bool = is_adult(18); - println!("bool result = {}", result); - - let is_even_result: bool = is_even(17); - println!("result = {}", is_even_result); -} - -// determine boolean for adults -fn is_adult(x: u8) -> bool { - // let mut outcome: bool = false; - if x <= 18 { - // outcome = false; - return false; - } - // outcome = true; - return true; -} - - -// determine even numbers -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 deleted file mode 100644 index 0c7f306..0000000 --- a/cairo_program/src/bytearray.cairo +++ /dev/null @@ -1,5 +0,0 @@ -#[executable] -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/hello_world.cairo b/cairo_program/src/hello_world.cairo deleted file mode 100644 index 1739c6d..0000000 --- a/cairo_program/src/hello_world.cairo +++ /dev/null @@ -1,6 +0,0 @@ -#[executable] -fn main() { - let x: felt252 = 32; - println!("x is: {}", x); - println!("Hello, World!"); -} diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo deleted file mode 100644 index 7d7ac7a..0000000 --- a/cairo_program/src/integer.cairo +++ /dev/null @@ -1,46 +0,0 @@ -use core::num::traits::{CheckedMul, CheckedAdd, CheckedSub}; - -#[executable] -fn main() { - let x: u32 = 10; - let y: u32 = 5; - - println!("Addition: {} + {} = {}", x, y, add_num(x, y)); - println!("Subtraction: {} - {} = {}", x, y, sub_num(x, y)); - println!("Multiplication: {} * {} = {}", x, y, mul_num(x, y)); - println!("Division: {} / {} = {}", x, y, div_num(x, y)); -} - -pub fn add_num(x: u32, y: u32) -> u32 { - match x.checked_add(y) { - Option::Some(val) => val, - Option::None => panic!("Addition overflow"), - } -} - -pub fn sub_num(x: u32, y: u32) -> u32 { - if y > x { - panic!("Subtraction underflow"); - } - match x.checked_sub(y) { - Option::Some(val) => val, - Option::None => panic!("Subtraction underflow"), - } -} - -pub fn mul_num(x: u32, y: u32) -> u32 { - match x.checked_mul(y) { - Option::Some(val) => val, - Option::None => panic!("Multiplication overflow"), - } -} - -pub fn div_num(x: u32, y: u32) -> u32 { - if y == 0 { - panic!("Division by zero"); - } - if y > x { - panic!("Result would be less than 1"); - } - x / y -} diff --git a/cairo_program/src/lib.cairo b/cairo_program/src/lib.cairo deleted file mode 100644 index 56cad6d..0000000 --- a/cairo_program/src/lib.cairo +++ /dev/null @@ -1,5 +0,0 @@ -// mod hello_world; -// mod short_string; -pub mod integer; -// mod bool; -// mod bytearray; \ No newline at end of file diff --git a/cairo_program/src/short_string.cairo b/cairo_program/src/short_string.cairo deleted file mode 100644 index 6674470..0000000 --- a/cairo_program/src/short_string.cairo +++ /dev/null @@ -1,5 +0,0 @@ -#[executable] -fn main() { - let bootcamp_name: felt252 = 'Bootcamp 6.0'; - println!("bootcamp name is: {}", bootcamp_name); -}