Skip to content

Commit

Permalink
feat: update admin
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Jun 5, 2024
1 parent 2eadb70 commit 309e39d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/interface/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ trait INaming<TContractState> {
);

// admin
fn set_admin(ref self: TContractState, new_admin: ContractAddress);

fn set_expiry(ref self: TContractState, root_domain: felt252, expiry: u64);

Expand All @@ -124,4 +123,5 @@ trait INaming<TContractState> {

fn toggle_ar_discount_renew(ref self: TContractState);

fn update_admin(ref self: TContractState, new_admin: ContractAddress);
}
19 changes: 14 additions & 5 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ mod Naming {
}
};
use identity::interface::identity::{IIdentity, IIdentityDispatcher, IIdentityDispatcherTrait};
use openzeppelin::token::erc20::interface::{
IERC20Camel, IERC20CamelDispatcher, IERC20CamelDispatcherTrait
use openzeppelin::{
access::ownable::OwnableComponent,
token::erc20::interface::{IERC20Camel, IERC20CamelDispatcher, IERC20CamelDispatcherTrait}
};
use storage_read::{main::storage_read_component, interface::IStorageRead};

Expand All @@ -38,7 +39,9 @@ mod Naming {
DomainMigrated: DomainMigrated,
SubdomainsReset: SubdomainsReset,
SaleMetadata: SaleMetadata,
StorageReadEvent: storage_read_component::Event
StorageReadEvent: storage_read_component::Event,
#[flat]
OwnableEvent: OwnableComponent::Event,
}

#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -137,6 +140,8 @@ mod Naming {
_ar_discount_renew_enabled: bool,
#[substorage(v0)]
storage_read: storage_read_component::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
}

#[constructor]
Expand All @@ -154,9 +159,13 @@ mod Naming {
}

component!(path: storage_read_component, storage: storage_read, event: StorageReadEvent);
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

#[abi(embed_v0)]
impl StorageReadComponent = storage_read_component::StorageRead<ContractState>;
#[abi(embed_v0)]
impl OwnableTwoStepImpl = OwnableComponent::OwnableTwoStepImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

#[abi(embed_v0)]
impl NamingImpl of INaming<ContractState> {
Expand Down Expand Up @@ -699,9 +708,9 @@ mod Naming {

// ADMIN

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

fn set_expiry(
Expand Down
1 change: 1 addition & 0 deletions src/tests/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mod test_usecases;
mod test_features;
mod test_altcoin;
mod test_ar_discount;
mod test_admin_update;
2 changes: 1 addition & 1 deletion src/tests/naming/test_abuses.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn test_non_admin_cannot_set_admin() {

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

#[test]
Expand Down
38 changes: 38 additions & 0 deletions src/tests/naming/test_admin_update.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use starknet::testing;
use starknet::ContractAddress;
use starknet::contract_address::ContractAddressZeroable;
use starknet::contract_address_const;
use starknet::testing::set_contract_address;
use super::super::utils;
use super::common::deploy;
use naming::naming::main::Naming;
use naming::interface::naming::{INamingDispatcher, INamingDispatcherTrait};
use openzeppelin::{
access::ownable::interface::{IOwnableTwoStep, IOwnableTwoStepDispatcher, IOwnableTwoStepDispatcherTrait},
token::erc20::{
interface::{IERC20Camel, IERC20CamelDispatcher, IERC20CamelDispatcherTrait}
}};

#[test]
#[available_gas(2000000000)]
fn test_update_admin() {
// setup
let (_, _, _, naming) = deploy();
let admin = contract_address_const::<0x123>();
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');

// 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.accept_ownership();
assert(ownable2Step.owner() == admin, 'change of admin failed');
}

0 comments on commit 309e39d

Please sign in to comment.