Skip to content

Commit

Permalink
Instantiate Query with during dao instantiation implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek-1857 committed May 29, 2024
1 parent ee083f0 commit 0101a1d
Show file tree
Hide file tree
Showing 50 changed files with 887 additions and 196 deletions.
23 changes: 23 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 @@ -104,6 +104,7 @@ dao-voting-snip721-roles = { path = "./contracts/voting/dao-voting-snip721-roles
dao-voting-snip721-staked = { path = "./contracts/voting/dao-voting-snip721-staked", version = "2.4.0",default-features = false }
dao-voting-token-staked = { path = "./contracts/voting/dao-voting-token-staked", version = "2.4.0",default-features = false }
dao-testing ={ path = "./packages/dao-testing/"}
dao-utils ={ path = "./packages/dao-utils/"}
# v1 dependencies. used for state migrations.
cw-core-v1 = { package = "cw-core", version = "0.1.0" ,default-features = false}
cw-proposal-single-v1 = { package = "cw-proposal-single", version = "0.1.0",default-features = false }
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 @@ -34,6 +34,7 @@ schemars = { workspace = true }
serde = { workspace = true }
secret-cw-controllers = { workspace = true }
shade-protocol ={ workspace = true }
dao-utils = {workspace = true}


[dev-dependencies]
Expand Down
189 changes: 161 additions & 28 deletions contracts/dao-dao-core/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ use dao_interface::{
},
voting,
};
use dao_utils::voting_cw4_init::GroupContract;
use dao_utils::voting_snip721_roles_init::NftContract;
use secret_cw2::{get_contract_version, set_contract_version, ContractVersion};
use secret_toolkit::utils::InitCallback;
use secret_toolkit::{serialization::Json, storage::Keymap, utils::HandleCallback};
use secret_utils::Duration;
use shade_protocol::basic_staking::Auth;
use shade_protocol::utils::asset::RawContract;
use shade_protocol::Contract;
use snip20_reference_impl::msg::ExecuteAnswer;

use crate::query_auth_init::QueryAuthInstantiateMsg;
use crate::state::{
ACTIVE_PROPOSAL_MODULE_COUNT, ADMIN, CONFIG, ITEMS, NOMINATED_ADMIN, PAUSED, PROPOSAL_MODULES,
REPLY_IDS, SNIP20_LIST, SNIP721_LIST, SUBDAO_LIST, TOKEN_VIEWING_KEY,
Expand Down Expand Up @@ -60,27 +66,33 @@ pub fn instantiate(
.unwrap_or_else(|| env.contract.address.clone());
ADMIN.save(deps.storage, &admin)?;

let vote_module_msg = msg
.clone()
.voting_module_instantiate_info
.to_cosmos_msg(env.contract.address.clone());
let reply_id = REPLY_IDS.add_event(deps.storage, ReplyEvent::VotingModuleInstantiate {})?;
let vote_module_msg: SubMsg<Empty> = SubMsg::reply_on_success(vote_module_msg, reply_id);

let proposal_module_msgs: Vec<SubMsg<Empty>> = msg
.proposal_modules_instantiate_info
.into_iter()
.map(|info| {
let wasm = info.clone().to_cosmos_msg(env.contract.address.clone());
let reply_id = REPLY_IDS
.add_event(deps.storage, ReplyEvent::ProposalModuleInstantiate {})
.unwrap();
SubMsg::reply_on_success(wasm, reply_id)
})
.collect();
if proposal_module_msgs.is_empty() {
return Err(ContractError::NoActiveProposalModules {});
}
let query_auth_msg = QueryAuthInstantiateMsg {
admin_auth: Contract {
address: env.contract.address.clone(),
code_hash: env.contract.code_hash,
},
prng_seed: to_binary(&"seed".to_string())?,
};
let reply_id = REPLY_IDS.add_event(
deps.storage,
ReplyEvent::InstantiateQueryAuth {
voting_module_instantiate_info: msg.voting_module_instantiate_info.clone(),
proposal_modules_instantiate_info: msg.proposal_modules_instantiate_info,
},
)?;
let query_auth_submsg: SubMsg<Empty> = SubMsg::reply_on_success(
query_auth_msg.to_cosmos_msg(
None,
env.contract.address.to_string(),
msg.query_auth_code_id,
msg.query_auth_code_hash,
None,
)?,
reply_id,
);

let _: dao_utils::voting_cw4_init::InstantiateMsg =
from_binary(&msg.voting_module_instantiate_info.msg)?;

if let Some(initial_items) = msg.initial_items {
// O(N*N) deduplication.
Expand All @@ -100,12 +112,7 @@ pub fn instantiate(
Ok(Response::new()
.add_attribute("action", "instantiate")
.add_attribute("sender", info.sender)
.set_data(to_binary(&AnyContractInfo {
addr: env.contract.address,
code_hash: env.contract.code_hash,
})?)
.add_submessage(vote_module_msg)
.add_submessages(proposal_module_msgs))
.add_submessage(query_auth_submsg))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down Expand Up @@ -1012,7 +1019,7 @@ pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, C
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
let reply_event = REPLY_IDS.get_event(deps.storage, msg.id)?;
match reply_event {
ReplyEvent::ProposalModuleInstantiate {} => match msg.result {
Expand Down Expand Up @@ -1090,10 +1097,136 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractE
}
SubMsgResult::Err(_) => Err(ContractError::TokenExecuteError {}),
},
ReplyEvent::InstantiateQueryAuth {
voting_module_instantiate_info,
proposal_modules_instantiate_info,
} => match msg.result {
SubMsgResult::Ok(res) => {
let query_auth_info: AnyContractInfo =
from_binary(&res.data.clone().unwrap_or_default())?;
let msg = update_query_auth(
voting_module_instantiate_info.clone(),
RawContract {
address: query_auth_info.addr.clone().to_string(),
code_hash: query_auth_info.code_hash.clone(),
},
env.contract.address.clone().to_string(),
)?;
let reply_id =
REPLY_IDS.add_event(deps.storage, ReplyEvent::VotingModuleInstantiate {})?;
let vote_module_msg: SubMsg<Empty> = SubMsg::reply_on_success(msg, reply_id);

let proposal_module_msgs: Vec<SubMsg<Empty>> = proposal_modules_instantiate_info
.into_iter()
.map(|info| {
let msg = update_query_auth(
info.clone(),
RawContract {
address: query_auth_info.addr.to_string(),
code_hash: query_auth_info.code_hash.clone(),
},
env.contract.address.clone().to_string(),
)
.unwrap();

let reply_id = REPLY_IDS
.add_event(deps.storage, ReplyEvent::ProposalModuleInstantiate {})
.unwrap();
SubMsg::reply_on_success(msg, reply_id)
})
.collect();
// if proposal_module_msgs.is_empty() {
// return Err(ContractError::NoActiveProposalModules {});
// }
Ok(Response::new()
.add_attribute("action", "instantiate query_auth with dao as admin")
.add_submessage(vote_module_msg)
.add_submessages(proposal_module_msgs))
}
SubMsgResult::Err(err) => Err(ContractError::Std(StdError::GenericErr { msg: err })),
},
_ => Err(ContractError::UnknownReplyID {}),
}
}

// Function to update query_auth field in binary msg
pub(crate) fn update_query_auth(
info: ModuleInstantiateInfo,
new_query_auth: RawContract,
admin: String,
) -> StdResult<CosmosMsg> {
// Voting CW4
if let Ok(mut msg) = from_binary::<dao_utils::voting_cw4_init::InstantiateMsg>(&info.msg) {
if let GroupContract::New {
ref mut query_auth, ..
} = msg.group_contract
{
*query_auth = Some(new_query_auth);
}
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

// Voting Snip20 Staked
if let Ok(mut msg) =
from_binary::<dao_utils::voting_snip20_staked_init::InstantiateMsg>(&info.msg)
{
msg.query_auth = Some(new_query_auth);
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

// Voting Token Staked
if let Ok(mut msg) =
from_binary::<dao_utils::voting_token_staked_init::InstantiateMsg>(&info.msg)
{
msg.query_auth = Some(new_query_auth);
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

// Voting Snip721 Staked
if let Ok(mut msg) =
from_binary::<dao_utils::voting_snip721_staked_init::InstantiateMsg>(&info.msg)
{
msg.query_auth = Some(new_query_auth);
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

// Voting Snip721 Roles
if let Ok(mut msg) =
from_binary::<dao_utils::voting_snip721_roles_init::InstantiateMsg>(&info.msg)
{
if let NftContract::New {
ref mut query_auth, ..
} = msg.nft_contract
{
*query_auth = Some(new_query_auth);
}
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

// Proposal Single
if let Ok(mut msg) = from_binary::<dao_utils::proposal_single_init::InstantiateMsg>(&info.msg) {
msg.query_auth = Some(new_query_auth);
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

// Proposal Multiple
if let Ok(mut msg) = from_binary::<dao_utils::proposal_multiple_init::InstantiateMsg>(&info.msg)
{
msg.query_auth = Some(new_query_auth);
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

// Proposal Condorcet
if let Ok(msg) = from_binary::<dao_utils::proposal_condorcet_init::InstantiateMsg>(&info.msg) {
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

// If none of the types matched, return an error
Err(StdError::generic_err(
"Failed to deserialize data into any known struct",
))
}

pub(crate) fn derive_proposal_module_prefix(mut dividend: usize) -> StdResult<String> {
dividend += 1;
// Pre-allocate string
Expand Down
2 changes: 1 addition & 1 deletion contracts/dao-dao-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

pub mod contract;
mod error;
pub mod query_auth_init;
pub mod snip20_msg;
pub mod state;

#[cfg(test)]
mod tests;

Expand Down
14 changes: 14 additions & 0 deletions contracts/dao-dao-core/src/query_auth_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Binary;
use secret_toolkit::utils::InitCallback;
use shade_protocol::Contract;

#[cw_serde]
pub struct QueryAuthInstantiateMsg {
pub admin_auth: Contract,
pub prng_seed: Binary,
}

impl InitCallback for QueryAuthInstantiateMsg {
const BLOCK_SIZE: usize = 256;
}
2 changes: 1 addition & 1 deletion contracts/external/cw-fund-distributor/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn instantiate(
deps.storage,
&Config {
owner: info.sender,
query_auth: msg.query_auth.into_valid(deps.api)?,
query_auth: msg.query_auth.into_valid(deps.api).unwrap_or_default(),
},
)?;

Expand Down
5 changes: 4 additions & 1 deletion contracts/external/cw4-group/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ pub fn instantiate(
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
QUERY_AUTH.save(deps.storage, &msg.query_auth.into_valid(deps.api)?)?;
QUERY_AUTH.save(
deps.storage,
&msg.query_auth.into_valid(deps.api).unwrap_or_default(),
)?;
OWNER.save(deps.storage, &info.sender)?;
create(deps, msg.admin, msg.members, env.block.height)?;
Ok(Response::default().set_data(to_binary(&AnyContractInfo {
Expand Down
6 changes: 4 additions & 2 deletions contracts/external/snip721-roles-impl/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ where
info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
self.query_auth
.save(deps.storage, &msg.query_auth.into_valid(deps.api)?)?;
self.query_auth.save(
deps.storage,
&msg.query_auth.into_valid(deps.api).unwrap_or_default(),
)?;
let creator_raw = deps.api.addr_canonicalize(info.sender.as_str())?;
save(deps.storage, CREATOR_KEY, &creator_raw)?;
let admin_raw = msg
Expand Down
6 changes: 3 additions & 3 deletions contracts/external/snip721-roles-impl/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::large_enum_variant)]

use cosmwasm_schema::QueryResponses;
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Binary, Coin};
use schemars::JsonSchema;
use secret_toolkit::permit::Permit;
Expand Down Expand Up @@ -45,7 +45,7 @@ pub struct InstantiateResponse {
/// This type represents optional configuration values.
/// All values are optional and have defaults which are more private by default,
/// but can be overridden if necessary
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
#[cw_serde]
pub struct InstantiateConfig {
/// indicates whether the token IDs and the number of tokens controlled by the contract are
/// public. If the token supply is private, only minters can view the token IDs and
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Default for InstantiateConfig {
}

/// info needed to perform a callback message after instantiation
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
#[cw_serde]
pub struct PostInstantiateCallback {
/// the callback message to execute
pub msg: Binary,
Expand Down
6 changes: 5 additions & 1 deletion contracts/proposal/dao-proposal-multiple/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ pub fn instantiate(
allow_revoting: msg.allow_revoting,
close_proposal_on_execution_failure: msg.close_proposal_on_execution_failure,
veto: msg.veto,
query_auth: msg.query_auth.into_valid(deps.api)?,
query_auth: msg
.query_auth
.unwrap_or_default()
.into_valid(deps.api)
.unwrap_or_default(),
};

// Initialize proposal count to zero so that queries return zero
Expand Down
Loading

0 comments on commit 0101a1d

Please sign in to comment.