Skip to content

Commit

Permalink
feat: introduce contract dispatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
sprtd committed Oct 16, 2024
1 parent 238c30e commit ef71102
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
.snfoundry.toml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ snforge test
> Uses `scarb` to build contracts first, then proceed to run tests using `snforge`
- Filter tests
```bash
snforge test test_increase_
snforge test test_increase
```
- Run specific tests
```bash
Expand Down
16 changes: 16 additions & 0 deletions assignments/assignment_5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--ASSIGNMENT CREATED ON 11/10/2024-->

# ASSIGNMENT 4

## Introduction to Dispatchers
Dispatcher provides an easy way to call other contracts. We have 2 contracts:
1. `Ownable`
2. `Counter`

Both contracts have their respective `IContractDispatcher` and `IContractDispatcherTrait` traits that can be used within the caller `Aggregator` contract to target the methods we intend to call

- Create a `KillSwitch` contract
- Add logic to `Aggregator` contract to ensure that the `is_on` variable of the `KillSwitch` contract is set to true before proceeding to adjust the `set_counter_count` method of `Counter` contract
- Write tests to validate that all the methods of your `Aggregator` contract is functional
- Create a PR with your modifications to the main branch of [cairo-bootcamp-3 repo]( https://github.com/BlockheaderWeb3-Community/cairo-bootcamp-3)

2 changes: 1 addition & 1 deletion snfoundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ url = "https://free-rpc.nethermind.io/sepolia-juno/"
[sncast.deploy_dev]
account = "deploy_dev"
accounts-file = "~/.starknet_accounts/starknet_open_zeppelin_accounts.json"
url = "https://free-rpc.nethermind.io/sepolia-juno/"
url = "https://free-rpc.nethermind.io/sepolia-juno/"
79 changes: 79 additions & 0 deletions src/aggregator.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use starknet::ContractAddress;

#[starknet::interface]
pub trait IAggregator<T> {
// references Ownable methods
fn get_ownable_owner(self: @T) -> ContractAddress;
fn set_ownable_owner(ref self: T, new_owner: ContractAddress);

// references Counter methods
fn get_counter_count(self: @T) -> u32;
fn set_counter_count(ref self: T, amount: u32);


// references Counter functions
fn get_aggr_owner(self: @T) -> ContractAddress;
}

#[starknet::contract]
mod Aggregator {
use cairo_bootcamp_3::{
counter::{ICounterDispatcher, ICounterDispatcherTrait},
ownable::{IOwnableDispatcher, IOwnableDispatcherTrait}
};
use super::{IAggregator};
use starknet::{ContractAddress};


#[storage]
struct Storage {
aggr_count: u32,
aggr_owner: ContractAddress,
ownable_addr: ContractAddress,
counter_addr: ContractAddress,
}

#[constructor]
fn constructor(ref self: ContractState, owner_addr: ContractAddress, ownable_addr: ContractAddress, counter_addr: ContractAddress,) {
self.aggr_owner.write(owner_addr);
self.ownable_addr.write(ownable_addr);
self.counter_addr.write(counter_addr);
}

#[abi(embed_v0)]
impl AggrImpl of IAggregator<ContractState> {
// references Ownable contract methods
// returns the address of the ownable contract
fn get_ownable_owner(
self: @ContractState,
) -> ContractAddress {
IOwnableDispatcher { contract_address: self.ownable_addr.read()}.get_owner()
}

fn set_ownable_owner(
ref self: ContractState, new_owner: ContractAddress
) {
IOwnableDispatcher { contract_address: self.ownable_addr.read()}.set_owner(new_owner);
}

// references Counter contract methods
fn get_counter_count(self: @ContractState) -> u32 {
ICounterDispatcher { contract_address: self.counter_addr.read()}.get_count()
}

fn set_counter_count(
ref self: ContractState, amount: u32
) {
ICounterDispatcher { contract_address: self.counter_addr.read()}.set_count(amount);
}

fn get_aggr_owner(self: @ContractState) -> ContractAddress {
self.aggr_owner.read()
}
}
}

// 0x1dd6e81d875e3451d14d418af0f42464bbaec19127465e700fb07cb403eb4cc - ca



34 changes: 34 additions & 0 deletions src/counter.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[starknet::interface]
pub trait ICounter<T> {
fn get_count(self: @T) -> u32;
fn set_count(ref self: T, amount: u32);
}

#[starknet::contract]
mod Counter {
use cairo_bootcamp_3::{errors, addition};
#[storage]
struct Storage {
count: u32,
}

#[abi(embed_v0)]
impl ICounterImpl of super::ICounter<ContractState> {
// increase count by the value passed in as argument
fn set_count(ref self: ContractState, amount: u32) {
assert(amount != 0, errors::Errors::ZERO_AMOUNT);
let current_count: u32 = self.count.read();
let result = addition::add(current_count, amount);
self.count.write(result);
}

// get current count
fn get_count(self: @ContractState) -> u32 {
self.count.read()
}
}
}
// 0x0726aae552474f128529c982392e8490c496395c1a2d7879c029203ad12e97bb

//__________________________________TODAY_______________________
// 0x45f8e8b3d6ecf220d78fdc13a523ae8ecaa90581ee68baa958d8ba3181841e9 - counter ca
1 change: 1 addition & 0 deletions src/errors.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod Errors {
pub const ZERO_AMOUNT: felt252 = 'amount cannot be zero';
pub const NOT_OWNER: felt252 = 'CALLER NOT OWNER';
pub const NOT_ADMIN: felt252 = 'CALLER NOT ADMIN';
pub const ZERO_ADDRESS: felt252 = 'ZERO ADDRESS!';
Expand Down
6 changes: 5 additions & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
pub mod counter;
pub mod counter_v1;
pub mod counter_v2;
pub mod student_registry;
pub mod student_struct;
pub mod errors;
pub mod accounts;

pub mod ownable_counter;

pub mod ownable;
pub mod addition;
pub mod aggregator;

55 changes: 55 additions & 0 deletions src/ownable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use starknet::ContractAddress;

#[starknet::interface]
pub trait IOwnable<T> {
fn set_owner(ref self: T, new_owner: ContractAddress) -> bool;
fn get_owner(self: @T) -> ContractAddress;
}


#[starknet::contract]
mod Ownable {
use core::num::traits::zero::Zero;
use starknet::{get_caller_address, ContractAddress};
use super::IOwnable;
use cairo_bootcamp_3::errors;

#[storage]
struct Storage {
owner: ContractAddress
}

#[constructor]
fn constructor(ref self: ContractState, init_owner: ContractAddress) {
assert(!init_owner.is_zero(), errors::Errors::ZERO_ADDRESS);
self.owner.write(init_owner);
}

#[abi(embed_v0)]
impl OwnerImpl of IOwnable<ContractState> {
fn set_owner(ref self: ContractState, new_owner: ContractAddress) -> bool {
assert(!new_owner.is_zero(), errors::Errors::ZERO_ADDRESS);
let caller = get_caller_address();
assert(caller == self.owner.read(), errors::Errors::NOT_OWNER);
self.owner.write(new_owner);
true
}

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

// 0x65f0904d094297f08575291f2da8600b60c12e764b63fdfef8c1044a3eaa34b -- owner cohort_dev
// 0x2dd6cf710592d99869d8868deab6c7d99369f629deb5c9ab71648170031640a -- ownable CA

// 0x6331d7d1cb9bc762785d083570d0d594fcf57cf3e5384209b59435c3f7e6d8b -- justice



//__________________________________TODAY_______________________
// 0xdedef0be763547e8e505d12fac321d0de4e9bd51635ac5fa00ae61d12e463e
// 0x6f2f6eb269f9741d5bb9cb633bfb632a0d71e0622b195ef4c4e66e8f1fee9fe - deploy_dev account
// 0x2a601649affa4fb870f919058baeed96729b1d7be7282b978e5ba50852d7c77 - ownable ca

0 comments on commit ef71102

Please sign in to comment.