diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..9f71edc --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +scarb 2.11.4 +starknet-foundry 0.40.0 diff --git a/src/IHello.cairo b/src/IHello.cairo index 0d1c511..df6f372 100644 --- a/src/IHello.cairo +++ b/src/IHello.cairo @@ -8,4 +8,4 @@ pub trait IHelloStarknet { fn get_balance(self: @TContractState) -> felt252; fn add_and_subtract(ref self: TContractState, amount: felt252); -} \ No newline at end of file +} diff --git a/src/INumber.cairo b/src/INumber.cairo index eb4014b..5a4f700 100644 --- a/src/INumber.cairo +++ b/src/INumber.cairo @@ -5,4 +5,4 @@ pub trait INumber { fn set_number(ref self: TContractState, amount: u8); /// Returns the current number fn get_number(self: @TContractState) -> u8; -} \ No newline at end of file +} diff --git a/src/aggregator.cairo b/src/aggregator.cairo index 3e4cbe8..4d9a851 100644 --- a/src/aggregator.cairo +++ b/src/aggregator.cairo @@ -1,26 +1,11 @@ -#[starknet::interface] -pub trait IAggregator { - /// 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] @@ -28,18 +13,30 @@ mod Agggregator { 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 { + impl AggregatorImpl of IAggregator { 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(); @@ -47,20 +44,24 @@ mod Agggregator { } 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(), }; @@ -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'); + } + } } diff --git a/src/counter.cairo b/src/counter.cairo index 190c073..5f6a87f 100644 --- a/src/counter.cairo +++ b/src/counter.cairo @@ -1,20 +1,7 @@ -/// Interface representing `Counter`. -/// This interface allows modification and retrieval of the contract count. -#[starknet::interface] -pub trait ICounter { - /// 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] @@ -22,22 +9,45 @@ mod Counter { 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 { + impl CounterImpl of ICounter { 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() } } } diff --git a/src/hello.cairo b/src/hello.cairo index fd7d3e2..13c95fd 100644 --- a/src/hello.cairo +++ b/src/hello.cairo @@ -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); } } @@ -52,4 +52,4 @@ mod HelloStarknet { number } } -} \ No newline at end of file +} diff --git a/src/interfaces/Iaggregator.cairo b/src/interfaces/Iaggregator.cairo new file mode 100644 index 0000000..58ff900 --- /dev/null +++ b/src/interfaces/Iaggregator.cairo @@ -0,0 +1,15 @@ +#[starknet::interface] +pub trait IAggregator { + /// 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); +} diff --git a/src/interfaces/Icounter.cairo b/src/interfaces/Icounter.cairo new file mode 100644 index 0000000..1ffa408 --- /dev/null +++ b/src/interfaces/Icounter.cairo @@ -0,0 +1,11 @@ +#[starknet::interface] +pub trait ICounter { + /// 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; +} diff --git a/src/interfaces/Ikillswitch.cairo b/src/interfaces/Ikillswitch.cairo new file mode 100644 index 0000000..f76c958 --- /dev/null +++ b/src/interfaces/Ikillswitch.cairo @@ -0,0 +1,10 @@ +/// Interface representing `HelloContract`. +/// This interface allows modification and retrieval of the contract count. +#[starknet::interface] +pub trait IKillSwitch { + /// Increase contract count. + fn switch(ref self: TContractState); + + /// Retrieve contract count. + fn get_status(self: @TContractState) -> bool; +} diff --git a/src/interfaces/Iowner.cairo b/src/interfaces/Iowner.cairo new file mode 100644 index 0000000..27127b9 --- /dev/null +++ b/src/interfaces/Iowner.cairo @@ -0,0 +1,7 @@ +#[starknet::interface] +pub trait IOwner { + fn get_owner(self: @TContractState) -> starknet::ContractAddress; + + + fn transfer_owner(ref self: TContractState, new_owner: starknet::ContractAddress); +} diff --git a/src/killswitch.cairo b/src/killswitch.cairo index 6437088..2b8ba78 100644 --- a/src/killswitch.cairo +++ b/src/killswitch.cairo @@ -1,17 +1,7 @@ -/// Interface representing `HelloContract`. -/// This interface allows modification and retrieval of the contract count. -#[starknet::interface] -pub trait IKillSwitch { - /// 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] @@ -21,7 +11,7 @@ mod KillSwitch { #[abi(embed_v0)] - impl KillSwitchImpl of super::IKillSwitch { + impl KillSwitchImpl of IKillSwitch { fn switch(ref self: ContractState) { // assert(amount != 0, 'Amount cannot be 0'); self.status.write(!self.status.read()); diff --git a/src/lib.cairo b/src/lib.cairo index 8b3993c..20fa20d 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -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); } diff --git a/src/ownable.cairo b/src/ownable.cairo new file mode 100644 index 0000000..7d74033 --- /dev/null +++ b/src/ownable.cairo @@ -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 { + 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'); + } + } +} diff --git a/tests/test_contract.cairo b/tests/test_contract.cairo index 9d6708a..d703609 100644 --- a/tests/test_contract.cairo +++ b/tests/test_contract.cairo @@ -1,49 +1,235 @@ -use starknet::ContractAddress; +use cohort_4::interfaces::Iaggregator::{IAggregatorDispatcher, IAggregatorDispatcherTrait}; +use cohort_4::interfaces::Icounter::{ + ICounterDispatcher, ICounterDispatcherTrait, ICounterSafeDispatcher, + ICounterSafeDispatcherTrait, +}; +use cohort_4::interfaces::Ikillswitch::{IKillSwitchDispatcher, IKillSwitchDispatcherTrait}; +use cohort_4::interfaces::Iowner::{IOwnerDispatcher, IOwnerDispatcherTrait}; +use snforge_std::{ + ContractClassTrait, DeclareResultTrait, declare, start_cheat_caller_address, + stop_cheat_caller_address, +}; +use starknet::{ContractAddress, contract_address_const}; -use snforge_std::{declare, ContractClassTrait, DeclareResultTrait}; -use cohort_4::counter::{ICounterDispatcher, ICounterDispatcherTrait, ICounterSafeDispatcher, ICounterSafeDispatcherTrait}; +fn deploy_contract() -> ( + ICounterDispatcher, + IKillSwitchDispatcher, + IOwnerDispatcher, + IAggregatorDispatcher, + ContractAddress, +) { + // owner address + let owner_address: ContractAddress = contract_address_const::<'1'>(); -fn deploy_contract() -> ContractAddress { - let countract_name: ByteArray = "Counter"; - let contract = declare(countract_name).unwrap().contract_class(); - let (contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap(); - contract_address + //counter deployment + let counter_countract_name: ByteArray = "Counter"; + let contract = declare(counter_countract_name).unwrap().contract_class(); + let (counter_contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap(); + let counter_dispatcher = ICounterDispatcher { contract_address: counter_contract_address }; + + //killswitch deployment + let killswitch_contract_name: ByteArray = "KillSwitch"; + let killswitch_contract = declare(killswitch_contract_name).unwrap().contract_class(); + let (killswitch_contract_address, _) = killswitch_contract.deploy(@ArrayTrait::new()).unwrap(); + let killswitch_dispatcher = IKillSwitchDispatcher { + contract_address: killswitch_contract_address, + }; + + //ownable deployment + let ownable_contract_name: ByteArray = "Ownable"; + let ownable_contract = declare(ownable_contract_name).unwrap().contract_class(); + // Deploy with owner_address as the initial owner + let (ownable_contract_address, _) = ownable_contract + .deploy(@array![owner_address.into()]) + .unwrap(); + let ownable_dispatcher = IOwnerDispatcher { contract_address: ownable_contract_address }; + + //aggregator deployment + let aggregator = declare("Agggregator").unwrap().contract_class(); + let (aggregator_contract_address, _) = aggregator + .deploy( + @array![ + counter_contract_address.into(), + killswitch_contract_address.into(), + ownable_contract_address.into(), + ], + ) + .unwrap(); + + let aggregator_dispatcher = IAggregatorDispatcher { + contract_address: aggregator_contract_address, + }; + + ( + counter_dispatcher, + killswitch_dispatcher, + ownable_dispatcher, + aggregator_dispatcher, + owner_address, + ) } #[test] fn test_increase_count() { - let contract_address = deploy_contract(); + let (counter_dispatcher, _, _, _, _) = deploy_contract(); + + let balance_before = counter_dispatcher.get_count(); + assert(balance_before == 0, 'Invalid balance'); - let dispatcher = ICounterDispatcher { contract_address }; + counter_dispatcher.increase_count(42); - let balance_before = dispatcher.get_count(); + let balance_after = counter_dispatcher.get_count(); + assert(balance_after == 42, 'Invalid balance'); +} + +#[test] +fn test_increase_count_aggregator_as_owner() { + let (_, _, _, aggregator_dispatcher, owner_address) = deploy_contract(); + + start_cheat_caller_address(aggregator_dispatcher.contract_address, owner_address); + + let balance_before = aggregator_dispatcher.get_count(); assert(balance_before == 0, 'Invalid balance'); - dispatcher.increase_count(42); + aggregator_dispatcher.increase_count(42); - let balance_after = dispatcher.get_count(); + let balance_after = aggregator_dispatcher.get_count(); assert(balance_after == 42, 'Invalid balance'); + + stop_cheat_caller_address(aggregator_dispatcher.contract_address); +} + +#[test] +#[should_panic(expected: ('you are not the owner',))] +fn test_increase_count_aggregator_as_non_owner() { + let (_, _, _, aggregator_dispatcher, _) = deploy_contract(); + + let non_owner_address: ContractAddress = contract_address_const::<'2'>(); + + start_cheat_caller_address(aggregator_dispatcher.contract_address, non_owner_address); + + aggregator_dispatcher.increase_count(42); + + let balance_after = aggregator_dispatcher.get_count(); + assert(balance_after == 0, 'you are not the owner'); + + stop_cheat_caller_address(aggregator_dispatcher.contract_address); } #[test] #[feature("safe_dispatcher")] fn test_cannot_increase_balance_with_zero_value() { - let contract_address = deploy_contract(); - - let dispatcher = ICounterDispatcher { contract_address }; + let (counter_dispatcher, _, _, _, _) = deploy_contract(); - let balance_before = dispatcher.get_count(); - assert(balance_before == 0 , 'Invalid balance'); + let balance_before = counter_dispatcher.get_count(); + assert(balance_before == 0, 'Invalid balance'); - let safe_dispatcher = ICounterSafeDispatcher { contract_address }; + let safe_dispatcher = ICounterSafeDispatcher { + contract_address: counter_dispatcher.contract_address, + }; match safe_dispatcher.increase_count(0) { Result::Ok(_) => core::panic_with_felt252('Should have panicked'), Result::Err(panic_data) => { assert(*panic_data.at(0) == 'Amount cannot be 0', *panic_data.at(0)); - } + }, }; - - +} + + +#[test] +fn test_increase_counter_count() { + let (counter_dispatcher, _, _, aggregator_dispatcher, owner_address) = deploy_contract(); + + start_cheat_caller_address(aggregator_dispatcher.contract_address, owner_address); + + let initial_count = counter_dispatcher.get_count(); + assert(initial_count == 0, 'invalid initial count'); + + aggregator_dispatcher.activate_switch(); + + aggregator_dispatcher.increase_counter_count(42); + + let final_count = counter_dispatcher.get_count(); + assert(final_count == 42, 'counter not increased correctly'); +} + +#[test] +fn test_decrease_count_by_one_aggregator() { + let (counter_dispatcher, _, _, aggregator_dispatcher, owner_address) = deploy_contract(); + + start_cheat_caller_address(aggregator_dispatcher.contract_address, owner_address); + let count_before = aggregator_dispatcher.get_count(); + assert(count_before == 0, 'invalid count'); + + aggregator_dispatcher.increase_count(20); + aggregator_dispatcher.decrease_count_by_one(); + + let count_after = aggregator_dispatcher.get_count(); + assert(count_after == 19, 'incorrect count'); +} + +#[test] +fn test_increase_activate_switch() { + let (_, killswitch_dispatcher, _, aggregator_dispatcher, owner_address) = deploy_contract(); + + let non_owner_address: ContractAddress = contract_address_const::<'2'>(); + + start_cheat_caller_address(aggregator_dispatcher.contract_address, owner_address); + let status = killswitch_dispatcher.get_status(); + assert(!status, 'failed'); + + aggregator_dispatcher.activate_switch(); + + stop_cheat_caller_address(aggregator_dispatcher.contract_address); + + let status_after = killswitch_dispatcher.get_status(); + assert(status_after, 'invalid status'); +} + + +#[test] +#[should_panic(expect: 'you are not the owner')] +fn test_increase_activate_switch_non_owner() { + let (_, killswitch_dispatcher, _, aggregator_dispatcher, _) = deploy_contract(); + + let non_owner_address: ContractAddress = contract_address_const::<'2'>(); + + start_cheat_caller_address(aggregator_dispatcher.contract_address, non_owner_address); + let status = killswitch_dispatcher.get_status(); + assert(!status, 'failed'); + + aggregator_dispatcher.activate_switch(); + + stop_cheat_caller_address(aggregator_dispatcher.contract_address); + + let status_after = killswitch_dispatcher.get_status(); + assert(status_after, 'invalid status'); +} + +#[test] +#[should_panic(expect: 'Amount cannot be 0')] +fn test_increase_count_by_zero() { + let (_, killswitch_dispatcher, _, aggregator_dispatcher, _) = deploy_contract(); + + let count_after = aggregator_dispatcher.get_count(); + assert(count_after == 0, 'incorrect count'); + + aggregator_dispatcher.increase_count(0); +} + +#[test] +#[should_panic(expect: 'should be active')] +fn test_increase_counter_count_error() { + let (_, killswitch_dispatcher, _, aggregator_dispatcher, _) = deploy_contract(); + + let non_owner_address: ContractAddress = contract_address_const::<'2'>(); + + start_cheat_caller_address(aggregator_dispatcher.contract_address, non_owner_address); + + let status_before = killswitch_dispatcher.get_status(); + assert(status_before, 'invalid status'); + + aggregator_dispatcher.increase_counter_count(42); } diff --git a/tests/test_counter.cairo b/tests/test_counter.cairo new file mode 100644 index 0000000..cf3006f --- /dev/null +++ b/tests/test_counter.cairo @@ -0,0 +1,69 @@ +use cohort_4::interfaces::Icounter::{ + ICounterDispatcher, ICounterDispatcherTrait, ICounterSafeDispatcher, + ICounterSafeDispatcherTrait, +}; +use snforge_std::{ContractClassTrait, DeclareResultTrait, declare}; +use starknet::ContractAddress; + + +fn deploy() -> (ICounterDispatcher, ICounterSafeDispatcher, ContractAddress) { + let counter_countract_name: ByteArray = "Counter"; + let contract = declare(counter_countract_name).unwrap().contract_class(); + let (contract_address, _) = contract.deploy(@ArrayTrait::new()).unwrap(); + let counter = ICounterDispatcher { contract_address }; + let safe_counter = ICounterSafeDispatcher { contract_address }; + return (counter, safe_counter, contract_address); +} + + +#[test] +fn test_initialized_count() { + let (counter, _, _) = deploy(); + let count_1 = counter.get_count(); + assert(count_1 == 0, 'invalid count value'); +} + +#[test] +fn test_increase_count() { + let (counter, _, _) = deploy(); + let count_1 = counter.get_count(); + assert(count_1 == 0, 'invalid count value'); + + let amount = 30; + counter.increase_count(amount); + let count_2 = counter.get_count(); + assert(count_2 == amount, 'Invalid amount'); + assert(count_2 != 10, 'invalid 10') +} + +#[test] +#[feature("safe_dispatcher")] +fn test_increase_count_by_zero_amount() { + let (counter, safe_counter, _) = deploy(); + + let count_1 = counter.get_count(); + assert(count_1 == 0, 'invalid count value'); + + match safe_counter.increase_count(0) { + Result::Ok(_) => panic!("amount passed not zero"), + Result::Err(panic_data) => { + assert(*panic_data.at(0) == 'Amount cannot be 0', *panic_data.at(0)) + }, + } +} + + +#[test] +fn test_for_decrease_count_by_one() { + let (counter, _, _) = deploy(); + + let count_1 = counter.get_count(); + assert(count_1 == 0, 'Invalid count value not zero'); + + counter.increase_count(2); + + counter.decrease_count_by_one(); + let count_2 = counter.get_count(); + assert(count_1 == count_2 - 1, 'Invalid count value'); +} + diff --git a/tests/test_killswitch.cairo b/tests/test_killswitch.cairo new file mode 100644 index 0000000..02eeea5 --- /dev/null +++ b/tests/test_killswitch.cairo @@ -0,0 +1,31 @@ +use cohort_4::interfaces::Ikillswitch::{IKillSwitchDispatcher, IKillSwitchDispatcherTrait}; +use snforge_std::{ContractClassTrait, DeclareResultTrait, declare}; +use starknet::ContractAddress; + +fn deploy() -> (IKillSwitchDispatcher, ContractAddress) { + let contract = declare("KillSwitch").unwrap().contract_class(); + let (contract_address, _) = contract.deploy(@array![]).unwrap(); + let killswitch = IKillSwitchDispatcher { contract_address }; + + (killswitch, contract_address) +} + +#[test] +fn test_get_status() { + let (killswitch, _) = deploy(); + + let current_status = killswitch.get_status(); + + assert(!current_status, 'status is not valid'); +} + +#[test] +fn test_switch() { + let (killswitch, _) = deploy(); + + let prev_status = killswitch.get_status(); + killswitch.switch(); + + assert(!prev_status, 'Status did not change'); +} +