From 319d6f95d2afeca065fd02d5a49a6d67e53fdd19 Mon Sep 17 00:00:00 2001 From: Henry Peters Date: Wed, 29 Apr 2026 16:34:43 +0100 Subject: [PATCH 1/3] feat: working on starknet contract --- cairo_program/src/integer.cairo | 2 ++ starknet_contracts/src/arithmetic.cairo | 28 +++++++++++++++++++++++ starknet_contracts/src/lib.cairo | 30 ++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 starknet_contracts/src/arithmetic.cairo diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo index 9501eca..da92c81 100644 --- a/cairo_program/src/integer.cairo +++ b/cairo_program/src/integer.cairo @@ -18,3 +18,5 @@ fn add_num(x: u8, y: u8) -> u8 { fn sub_num(x: u8, y: u8) -> u8 { return x - y; } + + diff --git a/starknet_contracts/src/arithmetic.cairo b/starknet_contracts/src/arithmetic.cairo new file mode 100644 index 0000000..4b77363 --- /dev/null +++ b/starknet_contracts/src/arithmetic.cairo @@ -0,0 +1,28 @@ + +pub fn add(x: u32, y: u32) -> u32 { + x + y +} + +pub fn subtract(x: u32, y: u32) -> u32{ + assert(x > y, 'y cant br greater than x'); + x - y +} + +pub fn multiply(x: u32, y: u32) -> u32 { + let x_u256: u256 = x.into(); + let y_u256: u256 = y.into(); + + let result = x_u256 * y_u256; + assert(result <= 255, 'Overflow'); + result.try_into().unwrap() +} + +pub fn divisio(x: u32, y:u32) -> u32 { + assert(y != 0, 'Undefined'); + let x_u256: u256 = x.into(); + let y_u256: u256 = y.into(); + + let result = x_u256 / y_u256; + assert(result <= 255, 'Overflow'); + result.try_into().unwrap() +} \ No newline at end of file diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index f1b82bb..1b82497 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -1,3 +1,8 @@ +mod arithmetic; + +use arithmetic::{add, subtract}; +use starknet::{ContractAddress, get_caller_address}; + /// Interface representing `HelloContract`. /// This interface allows modification and retrieval of the contract's storage count. #[starknet::interface] @@ -6,23 +11,46 @@ pub trait ICounter { fn increase_count(ref self: T, amount: u32); /// Retrieve count. fn get_count(self: @T) -> u32; + fn only_owner(self: @T); + fn reduce_count(ref self: T, amount: u32); } /// Simple contract for managing count. #[starknet::contract] mod Counter { use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; + use super::{add, subtract}; + use super::{ContractAddress, get_caller_address}; #[storage] struct Storage { count: u32, + owner: ContractAddress } + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.owner.write(owner); + } + #[abi(embed_v0)] impl CounterImpl of super::ICounter { + + fn only_owner(self: @ContractState) { + let caller = get_caller_address(); + assert(caller == self.owner.read(), 'Not owner'); + } + fn increase_count(ref self: ContractState, amount: u32) { + self.only_owner(); + assert(amount != 0, 'Amount cannot be 0'); + self.count.write(add(self.count.read(), amount)); + } + + fn reduce_count(ref self: ContractState, amount: u32) { + self.only_owner(); assert(amount != 0, 'Amount cannot be 0'); - self.count.write(self.count.read() + amount); + self.count.write(subtract(self.count.read(), amount)); } fn get_count(self: @ContractState) -> u32 { From e872dd1528ab234a29b2cdc40dcc19b66610610a Mon Sep 17 00:00:00 2001 From: Henry Peters Date: Wed, 6 May 2026 10:57:25 +0100 Subject: [PATCH 2/3] feature/assignment-1 --- starknet_contracts/Scarb.toml | 3 +++ starknet_contracts/snfoundry.toml | 3 +++ starknet_contracts/src/arithmetic.cairo | 3 ++- starknet_contracts/src/lib.cairo | 7 ++++++- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/starknet_contracts/Scarb.toml b/starknet_contracts/Scarb.toml index 29cf20f..c668332 100644 --- a/starknet_contracts/Scarb.toml +++ b/starknet_contracts/Scarb.toml @@ -12,6 +12,9 @@ starknet = "2.18.0" snforge_std = "0.56.0" assert_macros = "2.18.0" +[sncast.sepolia] +rpc_url = "https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_10/G4jDk3UFm0zD_87h21VJH" + [[target.starknet-contract]] sierra = true diff --git a/starknet_contracts/snfoundry.toml b/starknet_contracts/snfoundry.toml index c06aab7..caba84c 100644 --- a/starknet_contracts/snfoundry.toml +++ b/starknet_contracts/snfoundry.toml @@ -9,3 +9,6 @@ # wait-params = { timeout = 300, retry-interval = 10 } # Wait for submitted transaction parameters # block-explorer = "StarkScan" # Block explorer service used to display links to transaction details # show-explorer-links = true # Print links pointing to pages with transaction details in the chosen block explorer + +[sncast.sepolia] +account = "_hen-account1" \ No newline at end of file diff --git a/starknet_contracts/src/arithmetic.cairo b/starknet_contracts/src/arithmetic.cairo index 4b77363..5a71dda 100644 --- a/starknet_contracts/src/arithmetic.cairo +++ b/starknet_contracts/src/arithmetic.cairo @@ -4,7 +4,8 @@ pub fn add(x: u32, y: u32) -> u32 { } pub fn subtract(x: u32, y: u32) -> u32{ - assert(x > y, 'y cant br greater than x'); + assert(x > 0, 'x must be greater than 0'); + assert(x > y, 'y cant be greater than x'); x - y } diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index 1b82497..623ee2d 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -11,8 +11,9 @@ pub trait ICounter { fn increase_count(ref self: T, amount: u32); /// Retrieve count. fn get_count(self: @T) -> u32; - fn only_owner(self: @T); fn reduce_count(ref self: T, amount: u32); + fn only_owner(self: @T); + fn get_owner(self: @T) -> ContractAddress; } /// Simple contract for managing count. @@ -56,5 +57,9 @@ mod Counter { fn get_count(self: @ContractState) -> u32 { self.count.read() } + + fn get_owner(self: @ContractState) -> ContractAddress { + self.owner.read() + } } } From 462799280691d010baff40fa551e7efe082cc474 Mon Sep 17 00:00:00 2001 From: Henry Peters Date: Wed, 6 May 2026 11:26:16 +0100 Subject: [PATCH 3/3] feat: change_owner function --- starknet_contracts/src/lib.cairo | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index 623ee2d..635e290 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -7,6 +7,7 @@ use starknet::{ContractAddress, get_caller_address}; /// This interface allows modification and retrieval of the contract's storage count. #[starknet::interface] pub trait ICounter { + fn change_owner(ref self: T, new_owner: ContractAddress); /// Increase count. fn increase_count(ref self: T, amount: u32); /// Retrieve count. @@ -19,7 +20,8 @@ pub trait ICounter { /// Simple contract for managing count. #[starknet::contract] mod Counter { - use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; + use crate::ICounter; +use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; use super::{add, subtract}; use super::{ContractAddress, get_caller_address}; @@ -41,6 +43,11 @@ mod Counter { let caller = get_caller_address(); assert(caller == self.owner.read(), 'Not owner'); } + + fn change_owner(ref self: ContractState, new_owner: ContractAddress) { + self.only_owner(); + self.owner.write(new_owner); + } fn increase_count(ref self: ContractState, amount: u32) { self.only_owner(); @@ -61,5 +68,6 @@ mod Counter { fn get_owner(self: @ContractState) -> ContractAddress { self.owner.read() } + } }