diff --git a/src/lib.cairo b/src/lib.cairo index 5ea1e19..2e848ab 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,8 +1,23 @@ +use starknet::ContractAddress; + #[starknet::interface] -pub trait IPushComm {} +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 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; @@ -28,9 +43,10 @@ pub mod PushComm { // Channels delegatedNotificationSenders: Map, // 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] @@ -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] @@ -59,5 +83,51 @@ pub mod PushComm { #[abi(embed_v0)] - impl PushComm of super::IPushComm {} + impl PushComm of super::IPushComm { + 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() + } + } } diff --git a/tests/common.cairo b/tests/common.cairo new file mode 100644 index 0000000..6028cea --- /dev/null +++ b/tests/common.cairo @@ -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 +} diff --git a/tests/test_contract.cairo b/tests/test_contract.cairo deleted file mode 100644 index 5c2ee09..0000000 --- a/tests/test_contract.cairo +++ /dev/null @@ -1,14 +0,0 @@ -use starknet::ContractAddress; - -use snforge_std::{declare, ContractClassTrait}; - -fn deploy_contract(name: ByteArray) -> ContractAddress { - let contract = declare(name).unwrap(); - let (contract_address, _) = contract.deploy(@array![0]).unwrap(); - contract_address -} - -#[test] -fn test_test() { - let _ = deploy_contract("PushComm"); -} diff --git a/tests/test_push_admin.cairo b/tests/test_push_admin.cairo new file mode 100644 index 0000000..5a8cecb --- /dev/null +++ b/tests/test_push_admin.cairo @@ -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'); +} +