Skip to content

Commit

Permalink
removed the ability to update the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Mar 21, 2024
1 parent 89c0823 commit 46bdbf7
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 104 deletions.
24 changes: 0 additions & 24 deletions contracts/external/cw-admin-factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub fn execute(
code_id,
label,
} => instantiate_contract(deps, env, info, msg, code_id, label),
ExecuteMsg::UpdateAdmin { admin } => execute_update_admin(deps, info, admin),
}
}

Expand Down Expand Up @@ -80,29 +79,6 @@ pub fn instantiate_contract(
.add_submessage(msg))
}

pub fn execute_update_admin(
deps: DepsMut,
info: MessageInfo,
admin: Option<String>,
) -> Result<Response, ContractError> {
// Only allow the current admin to update the admin. If no admin, no admin
// can ever be set.
let current_admin = ADMIN.load(deps.storage)?;
if current_admin.map_or(false, |a| a != info.sender) {
return Err(ContractError::Unauthorized {});
}

let new_admin = admin.map(|s| deps.api.addr_validate(&s)).transpose()?;
ADMIN.save(deps.storage, &new_admin)?;

Ok(Response::default()
.add_attribute("action", "update_admin")
.add_attribute(
"admin",
new_admin.map_or("_none".to_string(), |a| a.to_string()),
))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
Expand Down
6 changes: 2 additions & 4 deletions contracts/external/cw-admin-factory/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use cosmwasm_std::{Addr, Binary};

#[cw_serde]
pub struct InstantiateMsg {
/// The account allowed to execute this contract.
/// The account allowed to execute this contract. If no admin, anyone can
/// execute it.
pub admin: Option<String>,
}

Expand All @@ -16,9 +17,6 @@ pub enum ExecuteMsg {
code_id: u64,
label: String,
},
/// Update the admin that is allowed to execute this contract. If there is
/// no admin, this cannot be called and there will never be an admin.
UpdateAdmin { admin: Option<String> },
}

#[cw_serde]
Expand Down
76 changes: 0 additions & 76 deletions contracts/external/cw-admin-factory/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,82 +246,6 @@ pub fn test_authorized_set_self_admin() {
assert_eq!(contract_info.admin, Some(core_addr))
}

#[test]
pub fn test_update_admin() {
let mut app = App::default();
let code_id = app.store_code(factory_contract());

let instantiate = InstantiateMsg {
admin: Some(ADMIN_ADDR.to_string()),
};
let factory_addr = app
.instantiate_contract(
code_id,
Addr::unchecked(ADMIN_ADDR),
&instantiate,
&[],
"cw-admin-factory",
None,
)
.unwrap();

// Query admin.
let current_admin: AdminResponse = app
.wrap()
.query_wasm_smart(factory_addr.clone(), &QueryMsg::Admin {})
.unwrap();
assert_eq!(current_admin.admin, Some(Addr::unchecked(ADMIN_ADDR)));

// Fails when not the admin.
let err: ContractError = app
.execute_contract(
Addr::unchecked("not_admin"),
factory_addr.clone(),
&ExecuteMsg::UpdateAdmin {
admin: Some(NEW_ADMIN_ADDR.to_string()),
},
&[],
)
.unwrap_err()
.downcast()
.unwrap();
assert_eq!(err, ContractError::Unauthorized {});

// Succeeds as the admin.
app.execute_contract(
Addr::unchecked(ADMIN_ADDR),
factory_addr.clone(),
&ExecuteMsg::UpdateAdmin {
admin: Some(NEW_ADMIN_ADDR.to_string()),
},
&[],
)
.unwrap();

// Query new admin.
let current_admin: AdminResponse = app
.wrap()
.query_wasm_smart(factory_addr.clone(), &QueryMsg::Admin {})
.unwrap();
assert_eq!(current_admin.admin, Some(Addr::unchecked(NEW_ADMIN_ADDR)));

// Clear the admin.
app.execute_contract(
Addr::unchecked(NEW_ADMIN_ADDR),
factory_addr.clone(),
&ExecuteMsg::UpdateAdmin { admin: None },
&[],
)
.unwrap();

// Query cleared admin.
let current_admin: AdminResponse = app
.wrap()
.query_wasm_smart(factory_addr, &QueryMsg::Admin {})
.unwrap();
assert_eq!(current_admin.admin, None);
}

#[test]
pub fn test_set_self_admin_mock() {
let mut deps = mock_dependencies();
Expand Down

0 comments on commit 46bdbf7

Please sign in to comment.