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/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 new file mode 100644 index 0000000..5a71dda --- /dev/null +++ b/starknet_contracts/src/arithmetic.cairo @@ -0,0 +1,29 @@ + +pub fn add(x: u32, y: u32) -> u32 { + x + y +} + +pub fn subtract(x: u32, y: u32) -> u32{ + assert(x > 0, 'x must be greater than 0'); + assert(x > y, 'y cant be 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..635e290 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -1,32 +1,73 @@ +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] pub trait ICounter { + fn change_owner(ref self: T, new_owner: ContractAddress); /// Increase count. fn increase_count(ref self: T, amount: u32); /// Retrieve count. fn get_count(self: @T) -> u32; + fn reduce_count(ref self: T, amount: u32); + fn only_owner(self: @T); + fn get_owner(self: @T) -> ContractAddress; } /// 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}; #[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 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(); assert(amount != 0, 'Amount cannot be 0'); - self.count.write(self.count.read() + amount); + 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(subtract(self.count.read(), amount)); } fn get_count(self: @ContractState) -> u32 { self.count.read() } + + fn get_owner(self: @ContractState) -> ContractAddress { + self.owner.read() + } + } }