Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks controller #195

Merged
merged 5 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 16 additions & 45 deletions contracts/cw4-group/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ use cosmwasm_std::{
to_binary, Binary, CanonicalAddr, Deps, DepsMut, Env, HandleResponse, HumanAddr, InitResponse,
MessageInfo, Order, StdResult,
};
use cw0::{
hooks::{add_hook, prepare_hooks, remove_hook, HOOKS},
maybe_canonical,
};
use cw0::maybe_canonical;
use cw2::set_contract_version;
use cw4::{
HooksResponse, Member, MemberChangedHookMsg, MemberDiff, MemberListResponse, MemberResponse,
Member, MemberChangedHookMsg, MemberDiff, MemberListResponse, MemberResponse,
TotalWeightResponse,
};
use cw_storage_plus::Bound;

use crate::error::ContractError;
use crate::msg::{HandleMsg, InitMsg, QueryMsg};
use crate::state::{ADMIN, MEMBERS, TOTAL};
use crate::state::{ADMIN, HOOKS, MEMBERS, TOTAL};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw4-group";
Expand Down Expand Up @@ -67,8 +64,8 @@ pub fn handle(
HandleMsg::UpdateMembers { add, remove } => {
handle_update_members(deps, env, info, add, remove)
}
HandleMsg::AddHook { addr } => handle_add_hook(deps, info, addr),
HandleMsg::RemoveHook { addr } => handle_remove_hook(deps, info, addr),
HandleMsg::AddHook { addr } => Ok(HOOKS.handle_add_hook(&ADMIN, deps, info, addr)?),
HandleMsg::RemoveHook { addr } => Ok(HOOKS.handle_remove_hook(&ADMIN, deps, info, addr)?),
}
}

Expand All @@ -82,7 +79,7 @@ pub fn handle_update_members(
// make the local update
let diff = update_members(deps.branch(), env.block.height, info.sender, add, remove)?;
// call all registered hooks
let messages = prepare_hooks(deps.storage, |h| diff.clone().into_cosmos_msg(h))?;
let messages = HOOKS.prepare_hooks(deps.storage, |h| diff.clone().into_cosmos_msg(h))?;
Ok(HandleResponse {
messages,
attributes: vec![],
Expand Down Expand Up @@ -129,26 +126,6 @@ pub fn update_members(
Ok(MemberChangedHookMsg { diffs })
}

pub fn handle_add_hook(
deps: DepsMut,
info: MessageInfo,
addr: HumanAddr,
) -> Result<HandleResponse, ContractError> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;
add_hook(deps.storage, addr)?;
Ok(HandleResponse::default())
}

pub fn handle_remove_hook(
deps: DepsMut,
info: MessageInfo,
addr: HumanAddr,
) -> Result<HandleResponse, ContractError> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;
remove_hook(deps.storage, addr)?;
Ok(HandleResponse::default())
}

pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Member {
Expand All @@ -158,17 +135,12 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::ListMembers { start_after, limit } => {
to_binary(&list_members(deps, start_after, limit)?)
}
QueryMsg::Admin {} => to_binary(&ADMIN.query_admin(deps)?),
QueryMsg::TotalWeight {} => to_binary(&query_total_weight(deps)?),
QueryMsg::Hooks {} => to_binary(&query_hooks(deps)?),
QueryMsg::Admin {} => to_binary(&ADMIN.query_admin(deps)?),
QueryMsg::Hooks {} => to_binary(&HOOKS.query_hooks(deps)?),
}
}

fn query_hooks(deps: Deps) -> StdResult<HooksResponse> {
let hooks = HOOKS.may_load(deps.storage)?.unwrap_or_default();
Ok(HooksResponse { hooks })
}

fn query_total_weight(deps: Deps) -> StdResult<TotalWeightResponse> {
let weight = TOTAL.load(deps.storage)?;
Ok(TotalWeightResponse { weight })
Expand Down Expand Up @@ -217,9 +189,8 @@ mod tests {
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{from_slice, Api, OwnedDeps, Querier, Storage};
use cw0::hooks::HookError;
use cw4::{member_key, TOTAL_KEY};
use cw_controllers::AdminError;
use cw_controllers::{AdminError, HookError};

const INIT_ADMIN: &str = "juan";
const USER1: &str = "somebody";
Expand Down Expand Up @@ -399,7 +370,7 @@ mod tests {
let mut deps = mock_dependencies(&[]);
do_init(deps.as_mut());

let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert!(hooks.hooks.is_empty());

let contract1 = HumanAddr::from("hook1");
Expand All @@ -418,7 +389,7 @@ mod tests {
add_msg.clone(),
)
.unwrap_err();
assert_eq!(err, AdminError::NotAdmin {}.into());
assert_eq!(err, HookError::Admin(AdminError::NotAdmin {}).into());

// admin can add it, and it appears in the query
let admin_info = mock_info(INIT_ADMIN, &[]);
Expand All @@ -429,7 +400,7 @@ mod tests {
add_msg.clone(),
)
.unwrap();
let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert_eq!(hooks.hooks, vec![contract1.clone()]);

// cannot remove a non-registered contract
Expand All @@ -450,7 +421,7 @@ mod tests {
addr: contract2.clone(),
};
let _ = handle(deps.as_mut(), mock_env(), admin_info.clone(), add_msg2).unwrap();
let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert_eq!(hooks.hooks, vec![contract1.clone(), contract2.clone()]);

// cannot re-add an existing contract
Expand All @@ -474,7 +445,7 @@ mod tests {
remove_msg.clone(),
)
.unwrap_err();
assert_eq!(err, AdminError::NotAdmin {}.into());
assert_eq!(err, HookError::Admin(AdminError::NotAdmin {}).into());

// remove the original
let _ = handle(
Expand All @@ -484,7 +455,7 @@ mod tests {
remove_msg.clone(),
)
.unwrap();
let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert_eq!(hooks.hooks, vec![contract2.clone()]);
}

Expand All @@ -493,7 +464,7 @@ mod tests {
let mut deps = mock_dependencies(&[]);
do_init(deps.as_mut());

let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert!(hooks.hooks.is_empty());

let contract1 = HumanAddr::from("hook1");
Expand Down
3 changes: 1 addition & 2 deletions contracts/cw4-group/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use cosmwasm_std::StdError;
use thiserror::Error;

use cw0::hooks::HookError;
use cw_controllers::AdminError;
use cw_controllers::{AdminError, HookError};

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
Expand Down
4 changes: 3 additions & 1 deletion contracts/cw4-group/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use cw4::TOTAL_KEY;
use cw_controllers::Admin;
use cw_controllers::{Admin, Hooks};
use cw_storage_plus::{Item, SnapshotMap, Strategy};

pub const ADMIN: Admin = Admin::new("admin");
pub const HOOKS: Hooks = Hooks::new("cw4-hooks");

pub const TOTAL: Item<u64> = Item::new(TOTAL_KEY);

pub const MEMBERS: SnapshotMap<&[u8], u64> = SnapshotMap::new(
Expand Down
62 changes: 16 additions & 46 deletions contracts/cw4-stake/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ use cosmwasm_std::{
coin, coins, to_binary, BankMsg, Binary, CanonicalAddr, CosmosMsg, Deps, DepsMut, Env,
HandleResponse, HumanAddr, InitResponse, MessageInfo, Order, StdResult, Storage, Uint128,
};
use cw0::{
hooks::{add_hook, remove_hook, HOOKS},
maybe_canonical,
};
use cw0::maybe_canonical;
use cw2::set_contract_version;
use cw4::{
HooksResponse, Member, MemberChangedHookMsg, MemberDiff, MemberListResponse, MemberResponse,
Member, MemberChangedHookMsg, MemberDiff, MemberListResponse, MemberResponse,
TotalWeightResponse,
};
use cw_storage_plus::Bound;

use crate::error::ContractError;
use crate::msg::{ClaimsResponse, HandleMsg, InitMsg, QueryMsg, StakedResponse};
use crate::state::{Config, ADMIN, CONFIG, MEMBERS, STAKE, TOTAL};
use crate::state::{Config, ADMIN, CONFIG, HOOKS, MEMBERS, STAKE, TOTAL};
use cw0::claim::{claim_tokens, create_claim, CLAIMS};
use cw0::hooks::prepare_hooks;

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw4-stake";
Expand Down Expand Up @@ -61,8 +57,8 @@ pub fn handle(
) -> Result<HandleResponse, ContractError> {
match msg {
HandleMsg::UpdateAdmin { admin } => Ok(ADMIN.handle_update_admin(deps, info, admin)?),
HandleMsg::AddHook { addr } => handle_add_hook(deps, info, addr),
HandleMsg::RemoveHook { addr } => handle_remove_hook(deps, info, addr),
HandleMsg::AddHook { addr } => Ok(HOOKS.handle_add_hook(&ADMIN, deps, info, addr)?),
HandleMsg::RemoveHook { addr } => Ok(HOOKS.handle_remove_hook(&ADMIN, deps, info, addr)?),
HandleMsg::Bond {} => handle_bond(deps, env, info),
HandleMsg::Unbond { tokens: amount } => handle_unbond(deps, env, info, amount),
HandleMsg::Claim {} => handle_claim(deps, env, info),
Expand Down Expand Up @@ -182,7 +178,7 @@ fn update_membership(

// alert the hooks
let diff = MemberDiff::new(sender, old, new);
prepare_hooks(storage, |h| {
HOOKS.prepare_hooks(storage, |h| {
MemberChangedHookMsg::one(diff.clone()).into_cosmos_msg(h)
})
}
Expand Down Expand Up @@ -224,26 +220,6 @@ pub fn handle_claim(
})
}

pub fn handle_add_hook(
deps: DepsMut,
info: MessageInfo,
addr: HumanAddr,
) -> Result<HandleResponse, ContractError> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;
add_hook(deps.storage, addr)?;
Ok(HandleResponse::default())
}

pub fn handle_remove_hook(
deps: DepsMut,
info: MessageInfo,
addr: HumanAddr,
) -> Result<HandleResponse, ContractError> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;
remove_hook(deps.storage, addr)?;
Ok(HandleResponse::default())
}

pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Member {
Expand All @@ -253,19 +229,14 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::ListMembers { start_after, limit } => {
to_binary(&list_members(deps, start_after, limit)?)
}
QueryMsg::Admin {} => to_binary(&ADMIN.query_admin(deps)?),
QueryMsg::TotalWeight {} => to_binary(&query_total_weight(deps)?),
QueryMsg::Hooks {} => to_binary(&query_hooks(deps)?),
QueryMsg::Claims { address } => to_binary(&query_claims(deps, address)?),
QueryMsg::Staked { address } => to_binary(&query_staked(deps, address)?),
QueryMsg::Admin {} => to_binary(&ADMIN.query_admin(deps)?),
QueryMsg::Hooks {} => to_binary(&HOOKS.query_hooks(deps)?),
}
}

fn query_hooks(deps: Deps) -> StdResult<HooksResponse> {
let hooks = HOOKS.may_load(deps.storage)?.unwrap_or_default();
Ok(HooksResponse { hooks })
}

fn query_total_weight(deps: Deps) -> StdResult<TotalWeightResponse> {
let weight = TOTAL.load(deps.storage)?;
Ok(TotalWeightResponse { weight })
Expand Down Expand Up @@ -334,10 +305,9 @@ mod tests {
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{from_slice, Api, StdError, Storage};
use cw0::claim::Claim;
use cw0::hooks::HookError;
use cw0::Duration;
use cw4::{member_key, TOTAL_KEY};
use cw_controllers::AdminError;
use cw_controllers::{AdminError, HookError};

const INIT_ADMIN: &str = "juan";
const USER1: &str = "somebody";
Expand Down Expand Up @@ -724,7 +694,7 @@ mod tests {
let mut deps = mock_dependencies(&[]);
default_init(deps.as_mut());

let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert!(hooks.hooks.is_empty());

let contract1 = HumanAddr::from("hook1");
Expand All @@ -743,7 +713,7 @@ mod tests {
add_msg.clone(),
)
.unwrap_err();
assert_eq!(err, AdminError::NotAdmin {}.into());
assert_eq!(err, HookError::Admin(AdminError::NotAdmin {}).into());

// admin can add it, and it appears in the query
let admin_info = mock_info(INIT_ADMIN, &[]);
Expand All @@ -754,7 +724,7 @@ mod tests {
add_msg.clone(),
)
.unwrap();
let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert_eq!(hooks.hooks, vec![contract1.clone()]);

// cannot remove a non-registered contract
Expand All @@ -775,7 +745,7 @@ mod tests {
addr: contract2.clone(),
};
let _ = handle(deps.as_mut(), mock_env(), admin_info.clone(), add_msg2).unwrap();
let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert_eq!(hooks.hooks, vec![contract1.clone(), contract2.clone()]);

// cannot re-add an existing contract
Expand All @@ -799,7 +769,7 @@ mod tests {
remove_msg.clone(),
)
.unwrap_err();
assert_eq!(err, AdminError::NotAdmin {}.into());
assert_eq!(err, HookError::Admin(AdminError::NotAdmin {}).into());

// remove the original
let _ = handle(
Expand All @@ -809,7 +779,7 @@ mod tests {
remove_msg.clone(),
)
.unwrap();
let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert_eq!(hooks.hooks, vec![contract2.clone()]);
}

Expand All @@ -818,7 +788,7 @@ mod tests {
let mut deps = mock_dependencies(&[]);
default_init(deps.as_mut());

let hooks = query_hooks(deps.as_ref()).unwrap();
let hooks = HOOKS.query_hooks(deps.as_ref()).unwrap();
assert!(hooks.hooks.is_empty());

let contract1 = HumanAddr::from("hook1");
Expand Down
3 changes: 1 addition & 2 deletions contracts/cw4-stake/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use cosmwasm_std::StdError;
use thiserror::Error;

use cw0::hooks::HookError;
use cw_controllers::AdminError;
use cw_controllers::{AdminError, HookError};

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
Expand Down
3 changes: 2 additions & 1 deletion contracts/cw4-stake/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use cosmwasm_std::Uint128;
use cw0::Duration;
use cw4::TOTAL_KEY;
use cw_controllers::Admin;
use cw_controllers::{Admin, Hooks};
use cw_storage_plus::{Item, Map, SnapshotMap, Strategy};

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
Expand All @@ -17,6 +17,7 @@ pub struct Config {
}

pub const ADMIN: Admin = Admin::new("admin");
pub const HOOKS: Hooks = Hooks::new("cw4-hooks");
pub const CONFIG: Item<Config> = Item::new("config");
pub const TOTAL: Item<u64> = Item::new(TOTAL_KEY);

Expand Down
6 changes: 3 additions & 3 deletions packages/controllers/src/admin.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use cosmwasm_std::{
CanonicalAddr, Deps, DepsMut, HandleResponse, HumanAddr, MessageInfo, StdError, StdResult,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use thiserror::Error;

use cosmwasm_std::{
CanonicalAddr, Deps, DepsMut, HandleResponse, HumanAddr, MessageInfo, StdError, StdResult,
};
use cw0::maybe_canonical;
use cw_storage_plus::Item;

Expand Down
Loading