Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push Admin Actions #4

Merged
merged 4 commits into from
Aug 5, 2024
Merged
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
80 changes: 75 additions & 5 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
use starknet::ContractAddress;

#[starknet::interface]
pub trait IPushComm<TContractState> {}
pub trait IPushComm<TContractState> {
// Push Admin
fn complete_migration(ref self: TContractState);
fn get_migration_status(self: @TContractState) -> bool;
fn set_push_core_address(ref self: TContractState, core_address: felt252);
fn get_push_core_address(self: @TContractState) -> felt252;
fn verify_channel_alias(ref self: TContractState, channel_address: felt252);
fn get_push_governance_address(self: @TContractState) -> felt252;
fn set_push_governance_address(ref self: TContractState, governance_address: felt252);
fn get_push_token_address(self: @TContractState) -> felt252;
fn set_push_token_address(ref self: TContractState, push_token_address: felt252);
}

#[starknet::contract]
pub mod PushComm {
use openzeppelin::access::ownable::interface::OwnableABI;
use core::starknet::storage::StoragePointerWriteAccess;
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::ContractAddress;
use openzeppelin::access::ownable::OwnableComponent;
Expand All @@ -28,9 +43,10 @@ pub mod PushComm {
// Channels
delegatedNotificationSenders: Map<ContractAddress, bool>,
// Contract State
governance: ContractAddress,
governance: felt252,
is_migration_complete: bool,
push_core_address: ContractAddress
push_core_address: felt252,
push_token_address: felt252
}

#[starknet::storage_node]
Expand All @@ -48,7 +64,15 @@ pub mod PushComm {
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
OwnableEvent: OwnableComponent::Event
OwnableEvent: OwnableComponent::Event,
ChannelAlias: ChannelAlias
}

#[derive(Drop, starknet::Event)]
struct ChannelAlias {
#[key]
channel_owner_address: ContractAddress,
ethereum_channel_address: felt252,
}

#[constructor]
Expand All @@ -59,5 +83,51 @@ pub mod PushComm {


#[abi(embed_v0)]
impl PushComm of super::IPushComm<ContractState> {}
impl PushComm of super::IPushComm<ContractState> {
fn complete_migration(ref self: ContractState) {
self.ownable.assert_only_owner();
self.is_migration_complete.write(true);
}

fn get_migration_status(self: @ContractState) -> bool {
self.is_migration_complete.read()
}

fn set_push_core_address(ref self: ContractState, core_address: felt252) {
self.ownable.assert_only_owner();
self.push_core_address.write(core_address);
}

fn get_push_core_address(self: @ContractState) -> felt252 {
self.push_core_address.read()
}

fn verify_channel_alias(ref self: ContractState, channel_address: felt252) {
self
.emit(
ChannelAlias {
channel_owner_address: self.owner(),
ethereum_channel_address: channel_address
}
);
}

fn set_push_governance_address(ref self: ContractState, governance_address: felt252) {
self.ownable.assert_only_owner();
self.governance.write(governance_address);
}

fn get_push_governance_address(self: @ContractState) -> felt252 {
self.governance.read()
}

fn set_push_token_address(ref self: ContractState, push_token_address: felt252) {
self.ownable.assert_only_owner();
self.push_token_address.write(push_token_address);
}

fn get_push_token_address(self: @ContractState) -> felt252 {
self.push_token_address.read()
}
}
}
14 changes: 14 additions & 0 deletions tests/common.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use starknet::ContractAddress;
use snforge_std::{declare, ContractClassTrait, cheat_caller_address, CheatSpan};

// Constants
pub fn PUSH_ADMIN() -> ContractAddress {
'push_admin'.try_into().unwrap()
}

pub fn deploy_contract() -> ContractAddress {
let contract = declare("PushComm").unwrap();
let calldata = array![PUSH_ADMIN().into()];
let (contract_address, _) = contract.deploy(@calldata).unwrap();
contract_address
}
14 changes: 0 additions & 14 deletions tests/test_contract.cairo

This file was deleted.

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

use snforge_std::{declare, ContractClassTrait, cheat_caller_address, CheatSpan, spy_events};
use push_comm::{IPushCommDispatcher, IPushCommDispatcherTrait};
use super::common::{PUSH_ADMIN, deploy_contract};

#[test]
fn test_migration_status() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };

let migration_status = push_comm.get_migration_status();
assert(migration_status == false, 'Initial migration set to false');

// admin sets the migration status
cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1));
push_comm.complete_migration();

let migration_status = push_comm.get_migration_status();
assert(migration_status == true, 'Migration status not updated');
}


#[test]
fn test_core_contract_address() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };

let CORE_ADDRESS: felt252 = 'some addrs';

// admin sets the core channel address
cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1));
push_comm.set_push_core_address(CORE_ADDRESS);

let UPDATED_ADDRESS = push_comm.get_push_core_address();
assert(CORE_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed');
}


#[test]
fn test_gov_address() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };

let GOV_ADDRESS: felt252 = 'some addrs';

// admin sets the migration status
cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1));
push_comm.set_push_governance_address(GOV_ADDRESS);

let UPDATED_ADDRESS = push_comm.get_push_governance_address();
assert(GOV_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed');
}

#[test]
fn test_push_token_address() {
let contract_address = deploy_contract();
let push_comm = IPushCommDispatcher { contract_address };

let TOKEN_ADDRESS: felt252 = 'some addrs';

// admin sets the migration status
cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1));
push_comm.set_push_governance_address(TOKEN_ADDRESS);

let UPDATED_ADDRESS = push_comm.get_push_governance_address();
assert(TOKEN_ADDRESS == UPDATED_ADDRESS, 'Core Contract Update Failed');
}

Loading