From e2c129646ee983802483acd8f809f5d3bbe7aba0 Mon Sep 17 00:00:00 2001 From: pscott <30843220+pscott@users.noreply.github.com> Date: Fri, 8 Sep 2023 13:35:54 +0200 Subject: [PATCH] move to oz ownable library (#541) --- starknet/src/external.cairo | 1 - starknet/src/external/ownable.cairo | 63 ----------------------------- starknet/src/lib.cairo | 2 - starknet/src/space/space.cairo | 19 ++++----- 4 files changed, 8 insertions(+), 77 deletions(-) delete mode 100644 starknet/src/external.cairo delete mode 100644 starknet/src/external/ownable.cairo diff --git a/starknet/src/external.cairo b/starknet/src/external.cairo deleted file mode 100644 index cadc81b5..00000000 --- a/starknet/src/external.cairo +++ /dev/null @@ -1 +0,0 @@ -mod ownable; diff --git a/starknet/src/external/ownable.cairo b/starknet/src/external/ownable.cairo deleted file mode 100644 index 7630b5e6..00000000 --- a/starknet/src/external/ownable.cairo +++ /dev/null @@ -1,63 +0,0 @@ -// OZ ownable module, adding here directly until OZ releases a Scarb package -// Migrated lib to v2 -// TODO: OZ now has released a scarb package, use that instead -#[starknet::contract] -mod Ownable { - use starknet::{ContractAddress, get_caller_address}; - - #[storage] - struct Storage { - _owner: ContractAddress - } - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - OwnershipTransferred: OwnershipTransferred - } - - #[derive(Drop, starknet::Event)] - struct OwnershipTransferred { - previous_owner: ContractAddress, - new_owner: ContractAddress - } - - #[internal] - fn initializer(ref self: ContractState) { - let caller: ContractAddress = get_caller_address(); - _transfer_ownership(ref self, caller); - } - - #[internal] - fn assert_only_owner(self: @ContractState) { - let owner: ContractAddress = self._owner.read(); - let caller: ContractAddress = get_caller_address(); - assert(!caller.is_zero(), 'Caller is the zero address'); - assert(caller == owner, 'Caller is not the owner'); - } - - #[internal] - fn owner(self: @ContractState) -> ContractAddress { - self._owner.read() - } - - #[internal] - fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { - assert(!new_owner.is_zero(), 'New owner is the zero address'); - assert_only_owner(@self); - _transfer_ownership(ref self, new_owner); - } - - #[internal] - fn renounce_ownership(ref self: ContractState) { - assert_only_owner(@self); - _transfer_ownership(ref self, Zeroable::zero()); - } - - #[internal] - fn _transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { - let previous_owner: ContractAddress = self._owner.read(); - self._owner.write(new_owner); - self.emit(Event::OwnershipTransferred(OwnershipTransferred { previous_owner, new_owner })); - } -} diff --git a/starknet/src/lib.cairo b/starknet/src/lib.cairo index 36ebbab5..0946928c 100644 --- a/starknet/src/lib.cairo +++ b/starknet/src/lib.cairo @@ -12,8 +12,6 @@ mod voting_strategies; mod utils; -mod external; - mod interfaces; mod tests; diff --git a/starknet/src/space/space.cairo b/starknet/src/space/space.cairo index f6736622..63a5a055 100644 --- a/starknet/src/space/space.cairo +++ b/starknet/src/space/space.cairo @@ -73,6 +73,7 @@ trait ISpace { #[starknet::contract] mod Space { use super::ISpace; + use openzeppelin::access::ownable::Ownable; use starknet::{ storage_access::{StorePacking, StoreUsingPacking}, ClassHash, ContractAddress, info, Store, syscalls, SyscallResult, @@ -95,7 +96,6 @@ mod Space { }, constants::INITIALIZE_SELECTOR }, - external::ownable::Ownable }; #[storage] @@ -295,8 +295,7 @@ mod Space { //TODO: temporary component syntax let mut state = Ownable::unsafe_new_contract_state(); - Ownable::initializer(ref state); - Ownable::transfer_ownership(ref state, owner); + Ownable::InternalImpl::initializer(ref state, owner); _set_dao_uri(ref self, dao_uri); _set_max_voting_duration( ref self, max_voting_duration @@ -469,7 +468,7 @@ mod Space { fn cancel(ref self: ContractState, proposal_id: u256) { //TODO: temporary component syntax let state = Ownable::unsafe_new_contract_state(); - Ownable::assert_only_owner(@state); + Ownable::InternalImpl::assert_only_owner(@state); let mut proposal = self._proposals.read(proposal_id); assert_proposal_exists(@proposal); @@ -525,7 +524,7 @@ mod Space { ref self: ContractState, class_hash: ClassHash, initialize_calldata: Array ) -> SyscallResult<()> { let state: Ownable::ContractState = Ownable::unsafe_new_contract_state(); - Ownable::assert_only_owner(@state); + Ownable::InternalImpl::assert_only_owner(@state); assert(class_hash.is_non_zero(), 'Class Hash cannot be zero'); starknet::replace_class_syscall(class_hash)?; @@ -554,7 +553,7 @@ mod Space { fn owner(self: @ContractState) -> ContractAddress { //TODO: temporary component syntax let state = Ownable::unsafe_new_contract_state(); - Ownable::owner(@state) + Ownable::OwnableImpl::owner(@state) } fn max_voting_duration(self: @ContractState) -> u32 { @@ -616,7 +615,7 @@ mod Space { fn update_settings(ref self: ContractState, input: UpdateSettingsCalldata) { //TODO: temporary component syntax let state = Ownable::unsafe_new_contract_state(); - Ownable::assert_only_owner(@state); + Ownable::InternalImpl::assert_only_owner(@state); // Needed because the compiler will go crazy if we try to use `input` directly let _min_voting_duration = input.min_voting_duration; @@ -785,15 +784,13 @@ mod Space { fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { //TODO: temporary component syntax let mut state = Ownable::unsafe_new_contract_state(); - Ownable::assert_only_owner(@state); - Ownable::transfer_ownership(ref state, new_owner); + Ownable::OwnableImpl::transfer_ownership(ref state, new_owner); } fn renounce_ownership(ref self: ContractState) { //TODO: temporary component syntax let mut state = Ownable::unsafe_new_contract_state(); - Ownable::assert_only_owner(@state); - Ownable::renounce_ownership(ref state); + Ownable::OwnableImpl::renounce_ownership(ref state); } }