Skip to content

Commit

Permalink
add optional post_init_data to set in result in InstantiateMsg
Browse files Browse the repository at this point in the history
a hack to workaround scrtlabs/SecretNetwork#1323

Useful when you want to instantiate this contract and return data back to the calling contract that is available in the Reply
  • Loading branch information
luca992 committed Mar 16, 2023
1 parent 22ca6ca commit 5ae22b4
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::collections::HashSet;

use base64::{engine::general_purpose, Engine as _};
use cosmwasm_std::{
Addr, Api, attr, Binary, BlockInfo, CanonicalAddr, CosmosMsg, Deps, DepsMut, Env,
MessageInfo, Response, StdError, StdResult, Storage, to_binary, WasmMsg,
attr, to_binary, Addr, Api, Binary, BlockInfo, CanonicalAddr, CosmosMsg, Deps, DepsMut, Env,
MessageInfo, Response, StdError, StdResult, Storage, WasmMsg,
};
use cosmwasm_storage::{PrefixedStorage, ReadonlyPrefixedStorage};
use primitive_types::U256;
Expand Down Expand Up @@ -121,7 +121,12 @@ pub fn instantiate(
} else {
Vec::new()
};
Ok(Response::new().add_messages(messages))
let res = Response::new().add_messages(messages);
if let Some(some_post_init_data) = msg.post_init_data {
Ok(res.set_data(some_post_init_data))
} else {
Ok(res)
}
}

///////////////////////////////////// Handle //////////////////////////////////////
Expand All @@ -133,12 +138,7 @@ pub fn instantiate(
/// * `env` - Env of contract's environment
/// * `info` - contract execution info for authorization - identity of the call, and payment.
/// * `msg` - HandleMsg passed in with the execute message
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> StdResult<Response> {
let mut config: Config = load(deps.storage, CONFIG_KEY)?;
let mut deps = deps;
let mut _deps = &mut deps;
Expand Down
3 changes: 3 additions & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub struct InstantiateMsg {
/// contract that instantiated it, but it could be used to execute any
/// contract
pub post_init_callback: Option<PostInstantiateCallback>,
/// optional data to set in result
/// a hack to workaround https://github.com/scrtlabs/SecretNetwork/issues/1323
pub post_init_data: Option<Binary>,
}

/// This type represents optional configuration values.
Expand Down
3 changes: 3 additions & 0 deletions src/unittest_handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod tests {
royalty_info: None,
config: None,
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down Expand Up @@ -96,6 +97,7 @@ mod tests {
royalty_info: None,
config: Some(init_config),
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down Expand Up @@ -183,6 +185,7 @@ mod tests {
royalty_info: None,
config: None,
post_init_callback,
post_init_data: None,
};

let init_response = instantiate(&mut deps.as_mut(), &env, info, init_msg).unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/unittest_mint_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod tests {
royalty_info: None,
config: None,
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down
4 changes: 4 additions & 0 deletions src/unittest_non_transferable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod tests {
royalty_info: None,
config: None,
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down Expand Up @@ -89,6 +90,7 @@ mod tests {
royalty_info: None,
config: Some(init_config),
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down Expand Up @@ -140,6 +142,7 @@ mod tests {
royalty_info,
config: Some(init_config),
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down Expand Up @@ -220,6 +223,7 @@ mod tests {
royalty_info: None,
config: None,
post_init_callback,
post_init_data: None,
};

let init_response = instantiate(&mut deps.as_mut(), &env, info, init_msg).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/unittest_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod tests {
royalty_info: None,
config: None,
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down Expand Up @@ -83,6 +84,7 @@ mod tests {
royalty_info: None,
config: Some(init_config),
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down
3 changes: 3 additions & 0 deletions src/unittest_royalties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod tests {
royalty_info,
config: None,
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down Expand Up @@ -86,6 +87,7 @@ mod tests {
royalty_info,
config: Some(init_config),
post_init_callback: None,
post_init_data: None,
};

(instantiate(&mut deps.as_mut(), &env, info, init_msg), deps)
Expand Down Expand Up @@ -286,6 +288,7 @@ mod tests {
royalty_info: None,
config: None,
post_init_callback,
post_init_data: None,
};

let init_response = instantiate(&mut deps.as_mut(), &env, info, init_msg).unwrap();
Expand Down

0 comments on commit 5ae22b4

Please sign in to comment.