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
2 changes: 2 additions & 0 deletions cairo_program/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ fn add_num(x: u8, y: u8) -> u8 {
fn sub_num(x: u8, y: u8) -> u8 {
return x - y;
}


3 changes: 3 additions & 0 deletions starknet_contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions starknet_contracts/snfoundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
29 changes: 29 additions & 0 deletions starknet_contracts/src/arithmetic.cairo
Original file line number Diff line number Diff line change
@@ -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()
}
45 changes: 43 additions & 2 deletions starknet_contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -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<T> {
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<ContractState> {

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()
}

}
}