From 3f974d3a639bcf7fad185b94eff7ea247e9e0d39 Mon Sep 17 00:00:00 2001 From: eddnewgate Date: Mon, 19 Aug 2024 00:03:43 +0545 Subject: [PATCH] feat: refactor felt to address --- src/interface.cairo | 8 +++----- src/lib.cairo | 30 +++++++++++------------------- tests/common.cairo | 1 + tests/test_push_admin.cairo | 21 ++------------------- 4 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/interface.cairo b/src/interface.cairo index febf571..c70e022 100644 --- a/src/interface.cairo +++ b/src/interface.cairo @@ -1,12 +1,10 @@ -use starknet::ContractAddress; +use starknet::{ContractAddress, EthAddress}; #[starknet::interface] pub trait IPushComm { // 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; diff --git a/src/lib.cairo b/src/lib.cairo index a89f542..7a0a02c 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,6 +1,6 @@ pub mod interface; -use starknet::ContractAddress; +use starknet::{ContractAddress}; pub use interface::IPushComm; @@ -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; @@ -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, @@ -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, subscribed: Map, @@ -193,8 +193,9 @@ 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); @@ -202,7 +203,7 @@ pub mod PushComm { } } - fn _check_notifi_req( + fn _check_notif_req( self: @ContractState, channel: ContractAddress, recipient: ContractAddress ) -> bool { let caller_address = get_caller_address(); @@ -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( @@ -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() } diff --git a/tests/common.cairo b/tests/common.cairo index 892b3ec..9c4e61e 100644 --- a/tests/common.cairo +++ b/tests/common.cairo @@ -19,6 +19,7 @@ pub fn CHAIN_NAME() -> felt252 { 'Starknet'.try_into().unwrap() } + pub fn deploy_contract() -> ContractAddress { let contract = declare("PushComm").unwrap(); diff --git a/tests/test_push_admin.cairo b/tests/test_push_admin.cairo index bf23c02..a59b9c4 100644 --- a/tests/test_push_admin.cairo +++ b/tests/test_push_admin.cairo @@ -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));