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

Proposal Execute Callback Messages #879

Closed
Closed
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ cw-wormhole = { path = "./packages/cw-wormhole", version = "2.5.0" }
cw20-stake = { path = "./contracts/staking/cw20-stake", version = "2.5.0" }
cw721-controllers = { path = "./packages/cw721-controllers", version = "2.5.0" }
cw721-roles = { path = "./contracts/external/cw721-roles", version = "2.5.0" }
dao-callback-messages = { path = "./contracts/test/dao-callback-messages", version = "2.5.0" }
dao-cw721-extensions = { path = "./packages/dao-cw721-extensions", version = "2.5.0" }
dao-dao-core = { path = "./contracts/dao-dao-core", version = "2.5.0" }
dao-dao-macros = { path = "./packages/dao-dao-macros", version = "2.5.0" }
Expand Down
1 change: 1 addition & 0 deletions contracts/dao-dao-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ cw20-base = { workspace = true }
cw721-base = { workspace = true }
dao-proposal-sudo = { workspace = true }
dao-voting-cw20-balance = { workspace = true }
dao-callback-messages = { workspace = true }
36 changes: 26 additions & 10 deletions contracts/dao-dao-core/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use cosmwasm_std::{
use cw2::{get_contract_version, set_contract_version, ContractVersion};
use cw_paginate_storage::{paginate_map, paginate_map_keys, paginate_map_values};
use cw_storage_plus::Map;
use cw_utils::{parse_reply_instantiate_data, Duration};
use cw_utils::{parse_reply_execute_data, parse_reply_instantiate_data, Duration};
use dao_interface::{
msg::{ExecuteMsg, InitialItem, InstantiateMsg, MigrateMsg, QueryMsg},
query::{
AdminNominationResponse, Cw20BalanceResponse, DaoURIResponse, DumpStateResponse,
GetItemResponse, PauseInfoResponse, ProposalModuleCountResponse, SubDao,
},
state::{
Admin, Config, ModuleInstantiateCallback, ModuleInstantiateInfo, ProposalModule,
Admin, CallbackMessages, Config, ModuleInstantiateInfo, ProposalModule,
ProposalModuleStatus,
},
voting,
Expand All @@ -30,9 +30,10 @@ use crate::state::{
pub(crate) const CONTRACT_NAME: &str = "crates.io:dao-dao-core";
pub(crate) const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

const PROPOSAL_MODULE_REPLY_ID: u64 = 0;
const PROPOSAL_MODULE_INSTANTIATE_REPLY_ID: u64 = 0;
const VOTE_MODULE_INSTANTIATE_REPLY_ID: u64 = 1;
const VOTE_MODULE_UPDATE_REPLY_ID: u64 = 2;
const PROPOSAL_MODULE_EXECUTE_REPLY_ID: u64 = 3;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -71,7 +72,7 @@ pub fn instantiate(
.proposal_modules_instantiate_info
.into_iter()
.map(|info| info.into_wasm_msg(env.contract.address.clone()))
.map(|wasm| SubMsg::reply_on_success(wasm, PROPOSAL_MODULE_REPLY_ID))
.map(|wasm| SubMsg::reply_on_success(wasm, PROPOSAL_MODULE_INSTANTIATE_REPLY_ID))
.collect();
if proposal_module_msgs.is_empty() {
return Err(ContractError::NoActiveProposalModules {});
Expand Down Expand Up @@ -228,7 +229,10 @@ pub fn execute_proposal_hook(

Ok(Response::default()
.add_attribute("action", "execute_proposal_hook")
.add_messages(msgs))
.add_submessages(
msgs.into_iter()
.map(|msg| SubMsg::reply_on_success(msg, PROPOSAL_MODULE_EXECUTE_REPLY_ID)),
))
}

pub fn execute_nominate_admin(
Expand Down Expand Up @@ -392,7 +396,7 @@ pub fn execute_update_proposal_modules(
let to_add: Vec<SubMsg<Empty>> = to_add
.into_iter()
.map(|info| info.into_wasm_msg(env.contract.address.clone()))
.map(|wasm| SubMsg::reply_on_success(wasm, PROPOSAL_MODULE_REPLY_ID))
.map(|wasm| SubMsg::reply_on_success(wasm, PROPOSAL_MODULE_INSTANTIATE_REPLY_ID))
.collect();

Ok(Response::default()
Expand Down Expand Up @@ -952,7 +956,7 @@ pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> Result<Response, Con
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
PROPOSAL_MODULE_REPLY_ID => {
PROPOSAL_MODULE_INSTANTIATE_REPLY_ID => {
let res = parse_reply_instantiate_data(msg)?;
let prop_module_addr = deps.api.addr_validate(&res.contract_address)?;
let total_module_count = TOTAL_PROPOSAL_MODULE_COUNT.load(deps.storage)?;
Expand All @@ -973,7 +977,7 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractE

// Check for module instantiation callbacks
let callback_msgs = match res.data {
Some(data) => from_json::<ModuleInstantiateCallback>(&data)
Some(data) => from_json::<CallbackMessages>(&data)
.map(|m| m.msgs)
.unwrap_or_else(|_| vec![]),
None => vec![],
Expand All @@ -983,7 +987,6 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractE
.add_attribute("prop_module".to_string(), res.contract_address)
.add_messages(callback_msgs))
}

VOTE_MODULE_INSTANTIATE_REPLY_ID => {
let res = parse_reply_instantiate_data(msg)?;
let vote_module_addr = deps.api.addr_validate(&res.contract_address)?;
Expand All @@ -999,7 +1002,7 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractE

// Check for module instantiation callbacks
let callback_msgs = match res.data {
Some(data) => from_json::<ModuleInstantiateCallback>(&data)
Some(data) => from_json::<CallbackMessages>(&data)
.map(|m| m.msgs)
.unwrap_or_else(|_| vec![]),
None => vec![],
Expand All @@ -1017,6 +1020,19 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractE

Ok(Response::default().add_attribute("voting_module", vote_module_addr))
}
PROPOSAL_MODULE_EXECUTE_REPLY_ID => match parse_reply_execute_data(msg) {
Ok(res) => {
let callback_msgs = match res.data {
Some(data) => from_json::<CallbackMessages>(&data)
.map(|m| m.msgs)
.unwrap_or_else(|_| vec![]),
None => vec![],
};

Ok(Response::default().add_messages(callback_msgs))
}
Err(_) => Ok(Response::default()),
},
_ => Err(ContractError::UnknownReplyID {}),
}
}
Expand Down
103 changes: 103 additions & 0 deletions contracts/dao-dao-core/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ fn v1_cw_core_contract() -> Box<dyn Contract<Empty>> {
Box::new(contract)
}

fn dao_callback_messages_contract() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
dao_callback_messages::contract::execute,
dao_callback_messages::contract::instantiate,
dao_callback_messages::contract::query,
);
Box::new(contract)
}

fn instantiate_gov(app: &mut App, code_id: u64, msg: InstantiateMsg) -> Addr {
app.instantiate_contract(
code_id,
Expand Down Expand Up @@ -3197,3 +3206,97 @@ fn test_query_info() {
}
)
}

#[test]
pub fn test_callback_messages() {
let (core_addr, mut app) = do_standard_instantiate(true, None);

// Store and instantiate the dao-callback-messages contract
let callback_id = app.store_code(dao_callback_messages_contract());
let callback_addr = app
.instantiate_contract(
callback_id,
Addr::unchecked(CREATOR_ADDR),
&Empty {},
&[],
"dao-callback-messages",
None,
)
.unwrap();

// Get the proposal module
let proposal_modules: Vec<ProposalModule> = app
.wrap()
.query_wasm_smart(
core_addr.clone(),
&QueryMsg::ProposalModules {
start_after: None,
limit: None,
},
)
.unwrap();
let proposal_module = proposal_modules[0].address.clone();

// Test successful callback
let success_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: callback_addr.to_string(),
msg: to_json_binary(&dao_callback_messages::msg::ExecuteMsg::Execute {
msgs: vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: callback_addr.to_string(),
msg: to_json_binary(&dao_callback_messages::msg::ExecuteMsg::Execute {
msgs: vec![],
})
.unwrap(),
funds: vec![],
})],
})
.unwrap(),
funds: vec![],
});

let res = app.execute_contract(
proposal_module.clone(),
core_addr.clone(),
&ExecuteMsg::ExecuteProposalHook {
msgs: vec![success_msg],
},
&[],
);
assert!(res.is_ok());

// Check for error attributes not in the response
let attrs = res
.unwrap()
.events
.iter()
.flat_map(|e| e.attributes.clone())
.collect::<Vec<_>>();
let callback_failed_attr = attrs
.iter()
.find(|attr| attr.key == "callback_message_failed");
assert!(callback_failed_attr.is_none());

// Test error callback
let error_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: callback_addr.to_string(),
msg: to_json_binary(&dao_callback_messages::msg::ExecuteMsg::Execute {
msgs: vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: "non_existent_contract".to_string(),
msg: to_json_binary(&"{}").unwrap(),
funds: vec![],
})],
})
.unwrap(),
funds: vec![],
});

let res = app.execute_contract(
proposal_module,
core_addr,
&ExecuteMsg::ExecuteProposalHook {
msgs: vec![error_msg],
},
&[],
);
assert!(res.is_err());
}
4 changes: 2 additions & 2 deletions contracts/external/dao-migrator/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cosmwasm_std::{
use cw2::set_contract_version;
use dao_interface::{
query::SubDao,
state::{ModuleInstantiateCallback, ProposalModule},
state::{CallbackMessages, ProposalModule},
};

use crate::{
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn instantiate(
CORE_ADDR.save(deps.storage, &info.sender)?;

Ok(
Response::default().set_data(to_json_binary(&ModuleInstantiateCallback {
Response::default().set_data(to_json_binary(&CallbackMessages {
msgs: vec![WasmMsg::Execute {
contract_addr: env.contract.address.to_string(),
msg: to_json_binary(&MigrateV1ToV2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cosmwasm_std::{
};
use cw2::set_contract_version;

use dao_interface::state::ModuleInstantiateCallback;
use dao_interface::state::CallbackMessages;
use dao_pre_propose_approval_single::msg::{
ApproverProposeMessage, ExecuteExt as ApprovalExt, ExecuteMsg as PreProposeApprovalExecuteMsg,
};
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn instantiate(
let addr = deps.api.addr_validate(&msg.pre_propose_approval_contract)?;
PRE_PROPOSE_APPROVAL_CONTRACT.save(deps.storage, &addr)?;

Ok(resp.set_data(to_json_binary(&ModuleInstantiateCallback {
Ok(resp.set_data(to_json_binary(&CallbackMessages {
msgs: vec![
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: addr.to_string(),
Expand Down
23 changes: 23 additions & 0 deletions contracts/test/dao-callback-messages/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "dao-callback-messages"
authors = ["Gabe <[email protected]>"]
description = "A contract for sending callback messages in the data field."
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
version = { workspace = true }

[lib]
crate-type = ["cdylib", "rlib"]
doctest = false

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []

[dependencies]
cosmwasm-std = { workspace = true }
cosmwasm-schema = { workspace = true }
dao-interface = { workspace = true }
3 changes: 3 additions & 0 deletions contracts/test/dao-callback-messages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# dao-proposal-sudo

A contract for sending callback messages through the data field of the response.
43 changes: 43 additions & 0 deletions contracts/test/dao-callback-messages/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Binary, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo, Response, StdResult,
};
use dao_interface::state::CallbackMessages;

use crate::msg::ExecuteMsg;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: Empty,
) -> StdResult<Response> {
Ok(Response::new().add_attribute("method", "instantiate"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
match msg {
ExecuteMsg::Execute { msgs } => execute_execute(msgs),
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(_deps: Deps, _env: Env, _msg: Empty) -> StdResult<Binary> {
Ok(Binary::default())
}

pub fn execute_execute(msgs: Vec<CosmosMsg>) -> StdResult<Response> {
let data = to_json_binary(&CallbackMessages { msgs })?;

Ok(Response::default()
.add_attribute("action", "execute")
.set_data(data))
}
2 changes: 2 additions & 0 deletions contracts/test/dao-callback-messages/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod contract;
pub mod msg;
7 changes: 7 additions & 0 deletions contracts/test/dao-callback-messages/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::CosmosMsg;

#[cw_serde]
pub enum ExecuteMsg {
Execute { msgs: Vec<CosmosMsg> },
}
Loading
Loading