From f9b41f8fbe5a0125dec1b07a53852b439edef801 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 21 Dec 2020 20:23:30 +0100 Subject: [PATCH 1/3] Add event attributes to cw4-group and cw4-stake --- contracts/cw4-group/src/contract.rs | 13 ++++++--- contracts/cw4-stake/src/contract.rs | 41 +++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/contracts/cw4-group/src/contract.rs b/contracts/cw4-group/src/contract.rs index a1a2abf49..70f583c6f 100644 --- a/contracts/cw4-group/src/contract.rs +++ b/contracts/cw4-group/src/contract.rs @@ -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; @@ -76,13 +76,20 @@ pub fn handle_update_members( add: Vec, remove: Vec, ) -> Result { + 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, }) } diff --git a/contracts/cw4-stake/src/contract.rs b/contracts/cw4-stake/src/contract.rs index c95ba369f..d1e12dd39 100644 --- a/contracts/cw4-stake/src/contract.rs +++ b/contracts/cw4-stake/src/contract.rs @@ -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; @@ -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, }) } @@ -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, }) } @@ -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 { match msg { QueryMsg::Member { From d6968f6d9dc56d5e387e4d497671b8697aacf6ee Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 21 Dec 2020 20:29:02 +0100 Subject: [PATCH 2/3] Admin/Hook controllers return event attributes --- packages/controllers/src/admin.rs | 23 +++++++++++++++++---- packages/controllers/src/hooks.rs | 33 ++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/packages/controllers/src/admin.rs b/packages/controllers/src/admin.rs index 8cbd67d70..6d9cbea75 100644 --- a/packages/controllers/src/admin.rs +++ b/packages/controllers/src/admin.rs @@ -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; @@ -73,9 +73,24 @@ impl<'a> Admin<'a> { new_admin: Option, ) -> Result { self.assert_admin(deps.as_ref(), &info.sender)?; - self.set(deps, new_admin)?; - // TODO: add some common log attributes here - Ok(HandleResponse::default()) + + 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.clone())?; + + Ok(HandleResponse { + messages: vec![], + attributes, + data: None, + }) } pub fn query_admin(&self, deps: Deps) -> StdResult { diff --git a/packages/controllers/src/hooks.rs b/packages/controllers/src/hooks.rs index bb1cceb75..612c070b9 100644 --- a/packages/controllers/src/hooks.rs +++ b/packages/controllers/src/hooks.rs @@ -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; @@ -80,9 +81,18 @@ impl<'a> Hooks<'a> { addr: HumanAddr, ) -> Result { 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( @@ -93,9 +103,18 @@ impl<'a> Hooks<'a> { addr: HumanAddr, ) -> Result { 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 { From 014c57ef6dafe1c45a2b6b5a2ff6b8dce9679b0e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 21 Dec 2020 20:41:44 +0100 Subject: [PATCH 3/3] Fix clippy warning --- packages/controllers/src/admin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/controllers/src/admin.rs b/packages/controllers/src/admin.rs index 6d9cbea75..a0aa0ff5f 100644 --- a/packages/controllers/src/admin.rs +++ b/packages/controllers/src/admin.rs @@ -84,7 +84,7 @@ impl<'a> Admin<'a> { attr("sender", info.sender), ]; - self.set(deps, new_admin.clone())?; + self.set(deps, new_admin)?; Ok(HandleResponse { messages: vec![],