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 .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scarb 2.11.4
starknet-foundry 0.40.0
2 changes: 1 addition & 1 deletion src/IHello.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub trait IHelloStarknet<TContractState> {
fn get_balance(self: @TContractState) -> felt252;

fn add_and_subtract(ref self: TContractState, amount: felt252);
}
}
2 changes: 1 addition & 1 deletion src/INumber.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub trait INumber<TContractState> {
fn set_number(ref self: TContractState, amount: u8);
/// Returns the current number
fn get_number(self: @TContractState) -> u8;
}
}
59 changes: 36 additions & 23 deletions src/aggregator.cairo
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
#[starknet::interface]
pub trait IAggregator<TContractState> {
/// Increase contract count.
fn increase_count(ref self: TContractState, amount: u32);
/// Increase contract count.
///
fn increase_counter_count(ref self: TContractState, amount: u32);

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

fn activate_switch(ref self: TContractState);
}

/// Simple contract for managing count.
#[starknet::contract]
mod Agggregator {
use cohort_4::counter::{ICounterDispatcher, ICounterDispatcherTrait};
use cohort_4::killswitch::{IKillSwitchDispatcher, IKillSwitchDispatcherTrait};
use starknet::ContractAddress;
use cohort_4::interfaces::Iaggregator::IAggregator;
use cohort_4::interfaces::Icounter::{ICounterDispatcher, ICounterDispatcherTrait};
use cohort_4::interfaces::Ikillswitch::{IKillSwitchDispatcher, IKillSwitchDispatcherTrait};
use cohort_4::interfaces::Iowner::{IOwnerDispatcher, IOwnerDispatcherTrait};
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::{ContractAddress, get_caller_address};


#[storage]
struct Storage {
count: u32,
counter: ContractAddress,
killswitch: ContractAddress,
ownable: ContractAddress,
}


#[constructor]
fn constructor(ref self: ContractState, counter: ContractAddress, killswitch: ContractAddress) {
fn constructor(
ref self: ContractState,
counter: ContractAddress,
killswitch: ContractAddress,
ownable: ContractAddress,
) {
assert(counter != killswitch && counter != ownable, 'use counter address');
assert(killswitch != counter && killswitch != ownable, 'use killswitch address');
assert(ownable != killswitch && ownable != counter, 'use counter address');
self.counter.write(counter);
self.killswitch.write(killswitch);
self.ownable.write(ownable);
}


#[abi(embed_v0)]
impl AggregatorImpl of super::IAggregator<ContractState> {
impl AggregatorImpl of IAggregator<ContractState> {
fn increase_count(ref self: ContractState, amount: u32) {
self.check_for_owner();
assert(amount > 0, 'Amount cannot be 0');
let counter = ICounterDispatcher { contract_address: self.counter.read() };
let counter_count = counter.get_count();
self.count.write(counter_count + amount);
}

fn increase_counter_count(ref self: ContractState, amount: u32) {
self.check_for_owner();
let killswitch: IKillSwitchDispatcher = IKillSwitchDispatcher {
contract_address: self.killswitch.read(),
};
assert(killswitch.get_status(), 'not active');
assert(killswitch.get_status(), 'should be active');
ICounterDispatcher { contract_address: self.counter.read() }.increase_count(amount)
}

fn decrease_count_by_one(ref self: ContractState) {
self.check_for_owner();
let current_count = self.get_count();
assert!(current_count != 0, "Amount cannot be 0");
self.count.write(current_count - 1);
}

fn activate_switch(ref self: ContractState) {
self.check_for_owner();

let killswitch: IKillSwitchDispatcher = IKillSwitchDispatcher {
contract_address: self.killswitch.read(),
};
Expand All @@ -74,4 +75,16 @@ mod Agggregator {
self.count.read()
}
}


#[generate_trait]
impl privateAggregator of privateAggregatorTrait {
fn check_for_owner(self: @ContractState) {
let caller = get_caller_address();

let ownable = IOwnerDispatcher { contract_address: self.ownable.read() };
let owner = ownable.get_owner();
assert(caller == owner, 'you are not the owner');
}
}
}
46 changes: 28 additions & 18 deletions src/counter.cairo
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
/// Interface representing `Counter`.
/// 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: u32);

/// Decrease contract count by one
fn decrease_count_by_one(ref self: TContractState);

/// Retrieve contract count.
fn get_count(self: @TContractState) -> u32;
}

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

#[storage]
struct Storage {
count: u32,
}


#[event]
#[derive(Drop, starknet::Event)]
enum Event {
IncreaseCount: IncreaseCount,
DecreaseCount: DecreaseCount,
}

#[derive(Drop, starknet::Event)]
pub struct IncreaseCount {
pub count: u32,
}

#[derive(Drop, starknet::Event)]
pub struct DecreaseCount {
pub count: u32,
}

#[abi(embed_v0)]
impl CounterImpl of super::ICounter<ContractState> {
impl CounterImpl of ICounter<ContractState> {
fn increase_count(ref self: ContractState, amount: u32) {
assert(amount > 0, 'Amount cannot be 0');
let counter_count = self.get_count();

let new_count = counter_count + amount;
self.count.write(counter_count + amount);
self.emit(Event::IncreaseCount(IncreaseCount { count: new_count }));
}

fn decrease_count_by_one(ref self: ContractState) {
let current_count = self.get_count();
assert(current_count > 0, 'Amount cannot be 0');
self.count.write(current_count - 1);
let new_count = current_count - 1;
self.count.write(new_count);
self.emit(Event::DecreaseCount(DecreaseCount { count: new_count }));
}

fn get_count(self: @ContractState) -> u32 {
self.count.read()
self.count.read()
}
}
}
6 changes: 3 additions & 3 deletions src/hello.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ mod HelloStarknet {
}

fn add_and_subtract(ref self: ContractState, amount: felt252) {
self._add(amount);
self._subtract(amount);
self._add(amount);
self._subtract(amount);
}
}

Expand All @@ -52,4 +52,4 @@ mod HelloStarknet {
number
}
}
}
}
15 changes: 15 additions & 0 deletions src/interfaces/Iaggregator.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[starknet::interface]
pub trait IAggregator<TContractState> {
/// Increase contract count.
fn increase_count(ref self: TContractState, amount: u32);
/// Increase contract count.
///
fn increase_counter_count(ref self: TContractState, amount: u32);

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

fn activate_switch(ref self: TContractState);
}
11 changes: 11 additions & 0 deletions src/interfaces/Icounter.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[starknet::interface]
pub trait ICounter<TContractState> {
/// Increase contract count.
fn increase_count(ref self: TContractState, amount: u32);

/// Decrease contract count by one
fn decrease_count_by_one(ref self: TContractState);

/// Retrieve contract count.
fn get_count(self: @TContractState) -> u32;
}
10 changes: 10 additions & 0 deletions src/interfaces/Ikillswitch.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// Interface representing `HelloContract`.
/// This interface allows modification and retrieval of the contract count.
#[starknet::interface]
pub trait IKillSwitch<TContractState> {
/// Increase contract count.
fn switch(ref self: TContractState);

/// Retrieve contract count.
fn get_status(self: @TContractState) -> bool;
}
7 changes: 7 additions & 0 deletions src/interfaces/Iowner.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[starknet::interface]
pub trait IOwner<TContractState> {
fn get_owner(self: @TContractState) -> starknet::ContractAddress;


fn transfer_owner(ref self: TContractState, new_owner: starknet::ContractAddress);
}
14 changes: 2 additions & 12 deletions src/killswitch.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/// Interface representing `HelloContract`.
/// This interface allows modification and retrieval of the contract count.
#[starknet::interface]
pub trait IKillSwitch<TContractState> {
/// Increase contract count.
fn switch(ref self: TContractState);

/// Retrieve contract count.
fn get_status(self: @TContractState) -> bool;
}

/// Simple contract for managing count.
#[starknet::contract]
mod KillSwitch {
use cohort_4::interfaces::Ikillswitch::IKillSwitch;
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};

#[storage]
Expand All @@ -21,7 +11,7 @@ mod KillSwitch {


#[abi(embed_v0)]
impl KillSwitchImpl of super::IKillSwitch<ContractState> {
impl KillSwitchImpl of IKillSwitch<ContractState> {
fn switch(ref self: ContractState) {
// assert(amount != 0, 'Amount cannot be 0');
self.status.write(!self.status.read());
Expand Down
16 changes: 12 additions & 4 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
pub mod hello;
pub mod IHello;
pub mod INumber;

pub mod aggregator;
pub mod counter;
pub mod hello;
pub mod killswitch;
pub mod aggregator;
pub mod ownable;

pub mod interfaces {
pub mod Iaggregator;
pub mod Icounter;
pub mod Ikillswitch;
pub mod Iowner;
}


fn main() {
// Function calls (Uncomment to execute them)
// say_name("Sylvia Nnoruka!");
// intro_to_felt();

let num_1 = 5;
let num_2 = 10;
let sum = sum_num(num_1, num_2);
println!("The sum of {} and {} is = {}", num_1, num_2, sum);

// check_u16(6553); // Uncomment if needed
is_greater_than_50(3);
}
Expand Down
63 changes: 63 additions & 0 deletions src/ownable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#[starknet::contract]
pub mod Ownable {
use starknet::event::EventEmitter;
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::{ContractAddress, contract_address_const, get_caller_address};
use crate::interfaces::Iowner::IOwner;


#[storage]
struct Storage {
pub owner: ContractAddress,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
OwnershipTransferred: OwnershipTransferred,
}

#[derive(Drop, starknet::Event)]
struct OwnershipTransferred {
previous_owner: ContractAddress,
new_owner: ContractAddress,
}

#[constructor]
fn constructor(ref self: ContractState, initial_owner: ContractAddress) {
let address_zero = contract_address_const::<0>();
assert(initial_owner != address_zero, 'Owner cannot be zero');
self.owner.write(initial_owner);
}


#[abi(embed_v0)]
impl Owner of IOwner<ContractState> {
fn get_owner(self: @ContractState) -> ContractAddress {
self.owner.read()
}


fn transfer_owner(ref self: ContractState, new_owner: ContractAddress) {
self.check_for_owner();
let address_zero = contract_address_const::<0>();
assert(new_owner != address_zero, 'new owner cannot be zero');

let previous_owner = self.owner.read();
self.owner.write(new_owner)
}
}


//private function

#[generate_trait]
impl Internalownable of InternalownableTrait {
fn check_for_owner(self: @ContractState) {
let caller = get_caller_address();

let owner = self.owner.read();
assert(caller == owner, 'you are not the owner');
}
}
}
Loading