Skip to content

Commit

Permalink
Merge pull request #206 from CosmWasm/better-events
Browse files Browse the repository at this point in the history
Set events for cw4
  • Loading branch information
ethanfrey authored Dec 21, 2020
2 parents 9a38ad4 + 014c57e commit e8fb1b9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
13 changes: 10 additions & 3 deletions contracts/cw4-group/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
to_binary, Binary, CanonicalAddr, Deps, DepsMut, Env, HandleResponse, HumanAddr, InitResponse,
MessageInfo, Order, StdResult,
attr, to_binary, Binary, CanonicalAddr, Deps, DepsMut, Env, HandleResponse, HumanAddr,
InitResponse, MessageInfo, Order, StdResult,
};
use cw0::maybe_canonical;
use cw2::set_contract_version;
Expand Down Expand Up @@ -76,13 +76,20 @@ pub fn handle_update_members(
add: Vec<Member>,
remove: Vec<HumanAddr>,
) -> Result<HandleResponse, ContractError> {
let attributes = vec![
attr("action", "update_members"),
attr("added", add.len()),
attr("removed", remove.len()),
attr("sender", &info.sender),
];

// make the local update
let diff = update_members(deps.branch(), env.block.height, info.sender, add, remove)?;
// call all registered hooks
let messages = HOOKS.prepare_hooks(deps.storage, |h| diff.clone().into_cosmos_msg(h))?;
Ok(HandleResponse {
messages,
attributes: vec![],
attributes,
data: None,
})
}
Expand Down
41 changes: 33 additions & 8 deletions contracts/cw4-stake/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
coin, coins, to_binary, BankMsg, Binary, CanonicalAddr, CosmosMsg, Deps, DepsMut, Env,
HandleResponse, HumanAddr, InitResponse, MessageInfo, Order, StdResult, Storage, Uint128,
attr, coin, coins, to_binary, BankMsg, Binary, CanonicalAddr, Coin, CosmosMsg, Deps, DepsMut,
Env, HandleResponse, HumanAddr, InitResponse, MessageInfo, Order, StdResult, Storage, Uint128,
};
use cw0::maybe_canonical;
use cw2::set_contract_version;
Expand Down Expand Up @@ -97,16 +97,21 @@ pub fn handle_bond(

let messages = update_membership(
deps.storage,
info.sender,
info.sender.clone(),
&sender_raw,
new_stake,
&cfg,
env.block.height,
)?;

let attributes = vec![
attr("action", "bond"),
attr("amount", sent),
attr("sender", info.sender),
];
Ok(HandleResponse {
messages,
attributes: vec![],
attributes,
data: None,
})
}
Expand Down Expand Up @@ -134,16 +139,21 @@ pub fn handle_unbond(

let messages = update_membership(
deps.storage,
info.sender,
info.sender.clone(),
&sender_raw,
new_stake,
&cfg,
env.block.height,
)?;

let attributes = vec![
attr("action", "unbond"),
attr("amount", amount),
attr("sender", info.sender),
];
Ok(HandleResponse {
messages,
attributes: vec![],
attributes,
data: None,
})
}
Expand Down Expand Up @@ -204,21 +214,36 @@ pub fn handle_claim(

let config = CONFIG.load(deps.storage)?;
let amount = coins(release.u128(), config.denom);
let amount_str = coins_to_string(&amount);

let messages = vec![BankMsg::Send {
from_address: env.contract.address,
to_address: info.sender,
to_address: info.sender.clone(),
amount,
}
.into()];

let attributes = vec![
attr("action", "claim"),
attr("tokens", amount_str),
attr("sender", info.sender),
];
Ok(HandleResponse {
messages,
attributes: vec![],
attributes,
data: None,
})
}

// TODO: put in cosmwasm-std
fn coins_to_string(coins: &[Coin]) -> String {
let strings: Vec<_> = coins
.iter()
.map(|c| format!("{}{}", c.amount, c.denom))
.collect();
strings.join(",")
}

pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Member {
Expand Down
21 changes: 18 additions & 3 deletions packages/controllers/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;

use cosmwasm_std::{
CanonicalAddr, Deps, DepsMut, HandleResponse, HumanAddr, MessageInfo, StdError, StdResult,
attr, CanonicalAddr, Deps, DepsMut, HandleResponse, HumanAddr, MessageInfo, StdError, StdResult,
};
use cw0::maybe_canonical;
use cw_storage_plus::Item;
Expand Down Expand Up @@ -73,9 +73,24 @@ impl<'a> Admin<'a> {
new_admin: Option<HumanAddr>,
) -> Result<HandleResponse, AdminError> {
self.assert_admin(deps.as_ref(), &info.sender)?;

let admin_str = match new_admin.as_ref() {
Some(admin) => admin.to_string(),
None => "None".to_string(),
};
let attributes = vec![
attr("action", "update_admin"),
attr("admin", admin_str),
attr("sender", info.sender),
];

self.set(deps, new_admin)?;
// TODO: add some common log attributes here
Ok(HandleResponse::default())

Ok(HandleResponse {
messages: vec![],
attributes,
data: None,
})
}

pub fn query_admin(&self, deps: Deps) -> StdResult<AdminResponse> {
Expand Down
33 changes: 26 additions & 7 deletions packages/controllers/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;

use cosmwasm_std::{
CosmosMsg, Deps, DepsMut, HandleResponse, HumanAddr, MessageInfo, StdError, StdResult, Storage,
attr, CosmosMsg, Deps, DepsMut, HandleResponse, HumanAddr, MessageInfo, StdError, StdResult,
Storage,
};
use cw_storage_plus::Item;

Expand Down Expand Up @@ -80,9 +81,18 @@ impl<'a> Hooks<'a> {
addr: HumanAddr,
) -> Result<HandleResponse, HookError> {
admin.assert_admin(deps.as_ref(), &info.sender)?;
self.add_hook(deps.storage, addr)?;
// TODO: add attributes here
Ok(HandleResponse::default())
self.add_hook(deps.storage, addr.clone())?;

let attributes = vec![
attr("action", "add_hook"),
attr("hook", addr),
attr("sender", info.sender),
];
Ok(HandleResponse {
messages: vec![],
attributes,
data: None,
})
}

pub fn handle_remove_hook(
Expand All @@ -93,9 +103,18 @@ impl<'a> Hooks<'a> {
addr: HumanAddr,
) -> Result<HandleResponse, HookError> {
admin.assert_admin(deps.as_ref(), &info.sender)?;
self.remove_hook(deps.storage, addr)?;
// TODO: add attributes here
Ok(HandleResponse::default())
self.remove_hook(deps.storage, addr.clone())?;

let attributes = vec![
attr("action", "remove_hook"),
attr("hook", addr),
attr("sender", info.sender),
];
Ok(HandleResponse {
messages: vec![],
attributes,
data: None,
})
}

pub fn query_hooks(&self, deps: Deps) -> StdResult<HooksResponse> {
Expand Down

0 comments on commit e8fb1b9

Please sign in to comment.