Skip to content

Commit

Permalink
feat: remove update_admin and finalize upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Jun 5, 2024
1 parent 309e39d commit fcccbb2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 41 deletions.
2 changes: 0 additions & 2 deletions src/interface/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,4 @@ trait INaming<TContractState> {
fn blacklist_renewal_contract(ref self: TContractState, contract: ContractAddress);

fn toggle_ar_discount_renew(ref self: TContractState);

fn update_admin(ref self: TContractState, new_admin: ContractAddress);
}
26 changes: 11 additions & 15 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ mod Naming {
self._pricing_contract.write(pricing);
self._referral_contract.write(referral);
self._admin_address.write(admin);
self.ownable.initializer(admin);
}

component!(path: storage_read_component, storage: storage_read, event: StorageReadEvent);
Expand Down Expand Up @@ -708,15 +709,10 @@ mod Naming {

// ADMIN

fn update_admin(ref self: ContractState, new_admin: ContractAddress) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.initializer(new_admin);
}

fn set_expiry(
ref self: ContractState, root_domain: felt252, expiry: u64
) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
let hashed_domain = self.hash_domain(array![root_domain].span());
let domain_data = self._domain_data.read(hashed_domain);
let data = DomainData {
Expand All @@ -735,7 +731,7 @@ mod Naming {
}

fn claim_balance(ref self: ContractState, erc20: ContractAddress) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
let balance = IERC20CamelDispatcher { contract_address: erc20 }
.balanceOf(get_contract_address());
let has_claimed = IERC20CamelDispatcher { contract_address: erc20 }
Expand All @@ -744,45 +740,45 @@ mod Naming {
}

fn set_discount(ref self: ContractState, discount_id: felt252, discount: Discount) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
self.discounts.write(discount_id, discount);
}

fn set_pricing_contract(ref self: ContractState, pricing_contract: ContractAddress) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
self._pricing_contract.write(pricing_contract);
}

fn set_referral_contract(ref self: ContractState, referral_contract: ContractAddress) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
self._referral_contract.write(referral_contract);
}

fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
// todo: use components
assert(!new_class_hash.is_zero(), 'Class hash cannot be zero');
starknet::replace_class_syscall(new_class_hash).unwrap();
}

fn set_server_pub_key(ref self: ContractState, new_key: felt252) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
self._server_pub_key.write(new_key);
}

fn whitelist_renewal_contract(ref self: ContractState, contract: ContractAddress) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
self._whitelisted_renewal_contracts.write(contract, true);
}

fn blacklist_renewal_contract(ref self: ContractState, contract: ContractAddress) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
self._whitelisted_renewal_contracts.write(contract, false);
}


fn toggle_ar_discount_renew(ref self: ContractState) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self.ownable.assert_only_owner();
self._ar_discount_renew_enabled.write(!self._ar_discount_renew_enabled.read());
}
}
Expand Down
16 changes: 1 addition & 15 deletions src/tests/naming/test_abuses.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,7 @@ fn test_renewal_period_too_long() {

#[test]
#[available_gas(2000000000)]
#[should_panic(expected: ('you are not admin', 'ENTRYPOINT_FAILED'))]
fn test_non_admin_cannot_set_admin() {
// setup
let (_, _, _, naming) = deploy();
let non_admin_address = contract_address_const::<0x456>();
set_contract_address(non_admin_address);

// A non-admin tries to set a new admin
let new_admin = contract_address_const::<0x789>();
naming.update_admin(new_admin);
}

#[test]
#[available_gas(2000000000)]
#[should_panic(expected: ('you are not admin', 'ENTRYPOINT_FAILED'))]
#[should_panic(expected: ('Caller is not the owner', 'ENTRYPOINT_FAILED'))]
fn test_non_admin_cannot_claim_balance() {
// setup
let (eth, _, _, naming) = deploy();
Expand Down
29 changes: 20 additions & 9 deletions src/tests/naming/test_admin_update.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,28 @@ fn test_update_admin() {
let new_admin = contract_address_const::<0x456>();

let ownable2Step = IOwnableTwoStepDispatcher { contract_address: naming.contract_address };
assert(ownable2Step.owner() == contract_address_const::<0>(), 'admin should be 0');

// we call the update_admin function with the new admin
set_contract_address(admin);
naming.update_admin(new_admin);
assert(ownable2Step.owner() == new_admin, 'change of admin failed');
assert(ownable2Step.owner() == admin, 'admin not initialized');

// Now we go back to the first admin, this time using the ownable2Step
set_contract_address(new_admin);
ownable2Step.transfer_ownership(admin);
set_contract_address(admin);
ownable2Step.transfer_ownership(new_admin);
set_contract_address(new_admin);
ownable2Step.accept_ownership();
assert(ownable2Step.owner() == admin, 'change of admin failed');
assert(ownable2Step.owner() == new_admin, 'change of admin failed');
}


#[test]
#[available_gas(2000000000)]
#[should_panic(expected: ('Caller is not the owner', 'ENTRYPOINT_FAILED'))]
fn test_non_admin_cannot_set_admin() {
// setup
let (_, _, _, naming) = deploy();
let ownable2Step = IOwnableTwoStepDispatcher { contract_address: naming.contract_address };
let non_admin_address = contract_address_const::<0x456>();
set_contract_address(non_admin_address);

// A non-admin tries to set a new admin
let new_admin = contract_address_const::<0x789>();
ownable2Step.transfer_ownership(new_admin);
}

0 comments on commit fcccbb2

Please sign in to comment.