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: 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;
}
}
8 changes: 8 additions & 0 deletions src/IOwnable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use starknet::ContractAddress;

#[starknet::interface]
pub trait IOwnable<TContractState> {
fn set_owner(ref self: TContractState, new_owner: ContractAddress);

fn get_owner(self: @TContractState) -> ContractAddress;
}
118 changes: 114 additions & 4 deletions src/aggregator.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,130 @@ pub trait IAggregator<TContractState> {

/// Simple contract for managing count.
#[starknet::contract]
mod Agggregator {
pub mod Agggregator {
use cohort_4::IOwnable::{IOwnableDispatcher, IOwnableDispatcherTrait};
use cohort_4::counter::{ICounterDispatcher, ICounterDispatcherTrait};
use cohort_4::killswitch::{IKillSwitchDispatcher, IKillSwitchDispatcherTrait};
use starknet::ContractAddress;
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use starknet::{ContractAddress, get_caller_address};


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

#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
increased_aggregator_count: increased_aggregator_count,
decreased_aggregator_count: decreased_aggregator_count,
}


#[derive(Drop, starknet::Event)]
pub struct increased_aggregator_count {
pub amount: u32,
pub caller_address: ContractAddress,
}


#[derive(Drop, starknet::Event)]
pub struct decreased_aggregator_count {
pub amount: u32,
pub caller_address: ContractAddress,
}


#[constructor]
fn constructor(ref self: ContractState, counter: ContractAddress, killswitch: ContractAddress) {
fn constructor(
ref self: ContractState,
counter: ContractAddress,
killswitch: ContractAddress,
ownable_address: ContractAddress,
) {
self.__validator(counter, killswitch, ownable_address);

self.counter.write(counter);
self.killswitch.write(killswitch);
self.ownable_address.write(ownable_address);
}


#[abi(embed_v0)]
impl AggregatorImpl of super::IAggregator<ContractState> {
fn increase_count(ref self: ContractState, amount: u32) {

let caller = get_caller_address();

// check if the caller has access
self.__has_access();

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);

self
.emit(
Event::increased_aggregator_count(
increased_aggregator_count {
amount: amount,
caller_address: caller,
},
),
);
}

fn increase_counter_count(ref self: ContractState, amount: u32) {
let killswitch: IKillSwitchDispatcher = IKillSwitchDispatcher {
contract_address: self.killswitch.read(),
};

self.__has_access();

let caller = get_caller_address();

assert(killswitch.get_status(), 'not active');
ICounterDispatcher { contract_address: self.counter.read() }.increase_count(amount)
ICounterDispatcher { contract_address: self.counter.read() }.increase_count(amount);

self
.emit(
Event::increased_aggregator_count(
increased_aggregator_count {
amount: amount,
caller_address: caller
},
),
);
}

fn decrease_count_by_one(ref self: ContractState) {
self.__has_access();

let caller = get_caller_address();

let current_count = self.get_count();
assert!(current_count != 0, "Amount cannot be 0");
self.count.write(current_count - 1);

self
.emit(
Event::decreased_aggregator_count(
decreased_aggregator_count {
amount: 1, caller_address: caller,
},
),
);
}

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

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

#[generate_trait]
impl internalImpl of InternalTrait {
fn __has_access(self: @ContractState) {
let caller = get_caller_address();

let owner = IOwnableDispatcher { contract_address: self.ownable_address.read() }
.get_owner();

assert(caller == owner, 'Only owner can increase count');
}

fn __validator(
self: @ContractState,
counter: ContractAddress,
killswitch: ContractAddress,
ownable_address: ContractAddress,
) {
// Check if addresses are not reused
assert(counter != killswitch && counter != ownable_address, 'counter address reused');

assert(
killswitch != ownable_address && killswitch != counter, 'killswitch address reused',
);

assert(
ownable_address != killswitch && ownable_address != counter,
'ownable address reused',
)
}
}
}
24 changes: 22 additions & 2 deletions src/counter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,50 @@ pub trait ICounter<TContractState> {

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


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

#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
CounterIncreased: CounterWasIncreased,
CounterDecreased: CounterWasDecreased,
}

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

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

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

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);
self.emit(Event::CounterDecreased(CounterWasDecreased { amount: 1 }));
}

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
}
}
}
}
16 changes: 14 additions & 2 deletions src/killswitch.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@ pub trait IKillSwitch<TContractState> {

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

#[storage]
struct Storage {
status: bool,
}

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

#[derive(Drop, starknet::Event)]
pub struct EventSwitched{
pub status: bool
}

#[abi(embed_v0)]
impl KillSwitchImpl of super::IKillSwitch<ContractState> {
fn switch(ref self: ContractState) {
// assert(amount != 0, 'Amount cannot be 0');
self.status.write(!self.status.read());
self.emit(Event::EventSwitched(EventSwitched{
status: self.status.read()
}))
}


Expand Down
11 changes: 6 additions & 5 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
pub mod hello;
pub mod IHello;
pub mod INumber;
pub mod IOwnable;
pub mod aggregator;
pub mod counter;
pub mod hello;
pub mod killswitch;
pub mod aggregator;

pub mod ownable;


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
47 changes: 47 additions & 0 deletions src/ownable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use starknet::{ContractAddress, get_caller_address};
use crate::IOwnable::IOwnable;

#[starknet::contract]
mod Ownable {
use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use super::*;


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

#[constructor]
fn constructor(ref self: ContractState, owner: ContractAddress) {
self.__zero_checker(owner);
self.owner.write(owner);
}

#[abi(embed_v0)]
impl OwnableImpl of IOwnable<ContractState> {
fn set_owner(ref self: ContractState, new_owner: ContractAddress) {
let caller = get_caller_address();
assert(self.owner.read() == caller, 'Only owner can set new owner');

self.__zero_checker(new_owner);

assert(
self.owner.read() != new_owner, 'New owner not valid address',
);
self.owner.write(new_owner);
}

fn get_owner(self: @ContractState) -> ContractAddress {
self.owner.read()
}
}

#[generate_trait]
impl internalimpl of internalInterface {
fn __zero_checker(ref self: ContractState, some_address: ContractAddress) {
let zero_address = 'zero_address'.try_into().unwrap();
assert(some_address != zero_address, 'Address cannot be zero');
}
}
}
Loading