Skip to content

Commit

Permalink
feat: refactor felt to address
Browse files Browse the repository at this point in the history
  • Loading branch information
eddnewgate committed Aug 18, 2024
1 parent 3c414b8 commit 3f974d3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
8 changes: 3 additions & 5 deletions src/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use starknet::ContractAddress;
use starknet::{ContractAddress, EthAddress};

#[starknet::interface]
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 set_push_core_address(ref self: TContractState, core_address: EthAddress);
fn get_push_core_address(self: @TContractState) -> EthAddress;
fn get_push_governance_address(self: @TContractState) -> ContractAddress;
fn set_push_governance_address(ref self: TContractState, governance_address: ContractAddress);
fn get_push_token_address(self: @TContractState) -> ContractAddress;
Expand Down
30 changes: 11 additions & 19 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod interface;

use starknet::ContractAddress;
use starknet::{ContractAddress};
pub use interface::IPushComm;


Expand All @@ -17,7 +17,7 @@ pub mod PushComm {
use openzeppelin::access::ownable::interface::OwnableABI;
use core::starknet::storage::StoragePointerWriteAccess;
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::{ContractAddress, get_caller_address};
use starknet::{ContractAddress, get_caller_address, EthAddress};
use starknet::{get_execution_info};
use openzeppelin::access::ownable::OwnableComponent;

Expand All @@ -44,7 +44,7 @@ pub mod PushComm {
// Contract State
governance: ContractAddress,
is_migration_complete: bool,
push_core_address: felt252,
push_core_address: EthAddress,
push_token_address: ContractAddress,
// Chain Info
chain_name: felt252,
Expand All @@ -54,8 +54,8 @@ pub mod PushComm {
#[starknet::storage_node]
pub struct User {
is_activated: bool,
is_public_key_registered: bool,
start_block: u256,
// TODO: optimized packing
start_block: u64,
subscribed_count: u256,
is_subscribed: Map<ContractAddress, bool>,
subscribed: Map<ContractAddress, u256>,
Expand Down Expand Up @@ -193,16 +193,17 @@ pub mod PushComm {
let user_info = self.users.entry(user).storage_node_mut();

if !user_info.is_activated.read() {
let block_number = get_execution_info().unbox().block_info.unbox().block_number;
user_info.is_activated.write(true);
user_info.start_block.write(1);
user_info.start_block.write(block_number);

let user_count = self.users_count.read();
self.map_address_users.write(user_count, user);
self.users_count.write(user_count + 1);
}
}

fn _check_notifi_req(
fn _check_notif_req(
self: @ContractState, channel: ContractAddress, recipient: ContractAddress
) -> bool {
let caller_address = get_caller_address();
Expand All @@ -223,7 +224,7 @@ pub mod PushComm {
recipient: ContractAddress,
indentity: ByteArray
) -> bool {
let success = self._check_notifi_req(channel, recipient);
let success = self._check_notif_req(channel, recipient);
if success {
self
.emit(
Expand Down Expand Up @@ -295,21 +296,12 @@ pub mod PushComm {


// Admin
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) {
fn set_push_core_address(ref self: ContractState, core_address: EthAddress) {
self.ownable.assert_only_owner();
self.push_core_address.write(core_address);
}

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

Expand Down
1 change: 1 addition & 0 deletions tests/common.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn CHAIN_NAME() -> felt252 {
'Starknet'.try_into().unwrap()
}


pub fn deploy_contract() -> ContractAddress {
let contract = declare("PushComm").unwrap();

Expand Down
21 changes: 2 additions & 19 deletions tests/test_push_admin.cairo
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
use starknet::ContractAddress;
use starknet::{ContractAddress, EthAddress};

use snforge_std::{declare, ContractClassTrait, cheat_caller_address, CheatSpan, spy_events};
use push_comm::interface::{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';
let CORE_ADDRESS: EthAddress = 'some addrs'.try_into().unwrap();

// admin sets the core channel address
cheat_caller_address(contract_address, PUSH_ADMIN(), CheatSpan::TargetCalls(1));
Expand Down

0 comments on commit 3f974d3

Please sign in to comment.