Skip to content

Commit 70f8809

Browse files
committed
Add message_info function
1 parent af4b239 commit 70f8809

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use crate::{Addr, Coin, MessageInfo};
2+
3+
/// A constructor function for [`MessageInfo`].
4+
///
5+
/// This is optimized for writing testing contract. It
6+
/// lives in `cosmwasm_std::testing` because constructing MessageInfo
7+
/// objects is not something that you usually need in contract code.
8+
///
9+
/// ## Examples
10+
///
11+
/// ```
12+
/// # use cosmwasm_std::{DepsMut, Env, Response, MessageInfo, StdResult};
13+
/// # struct InstantiateMsg {
14+
/// # pub verifier: String,
15+
/// # pub beneficiary: String,
16+
/// # }
17+
/// # pub fn instantiate(
18+
/// # _deps: DepsMut,
19+
/// # _env: Env,
20+
/// # _info: MessageInfo,
21+
/// # _msg: InstantiateMsg,
22+
/// # ) -> StdResult<Response> {
23+
/// # Ok(Response::new().add_attribute("action", "instantiate"))
24+
/// # }
25+
/// use cosmwasm_std::coins;
26+
/// use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env};
27+
///
28+
/// let mut deps = mock_dependencies();
29+
///
30+
/// // Create some Addr instances for testing
31+
/// let creator = deps.api.addr_make("creator");
32+
/// let verifier = deps.api.addr_make("verifies");
33+
/// let beneficiary = deps.api.addr_make("benefits");
34+
///
35+
/// let msg = InstantiateMsg {
36+
/// verifier: verifier.to_string(),
37+
/// beneficiary: beneficiary.to_string(),
38+
/// };
39+
/// let info = message_info(&creator, &coins(1000, "earth"));
40+
/// let response = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
41+
/// assert_eq!(response.messages.len(), 0);
42+
/// ```
43+
pub fn message_info(sender: &Addr, funds: &[Coin]) -> MessageInfo {
44+
MessageInfo {
45+
sender: sender.clone(),
46+
funds: funds.to_vec(),
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use cosmwasm_core::Uint128;
53+
54+
use crate::coins;
55+
56+
use super::*;
57+
58+
#[test]
59+
fn message_info_works() {
60+
let addr = Addr::unchecked("cosmwasm1...");
61+
62+
let info = message_info(&addr, &[]);
63+
assert_eq!(
64+
info,
65+
MessageInfo {
66+
sender: addr.clone(),
67+
funds: vec![],
68+
}
69+
);
70+
71+
let info = message_info(&addr, &coins(123, "foo"));
72+
assert_eq!(
73+
info,
74+
MessageInfo {
75+
sender: addr.clone(),
76+
funds: vec![Coin {
77+
amount: Uint128::new(123),
78+
denom: "foo".to_string(),
79+
}],
80+
}
81+
);
82+
}
83+
}

packages/std/src/testing/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// Exposed for testing only
44
// Both unit tests and integration tests are compiled to native code, so everything in here does not need to compile to Wasm.
55

6+
mod message_info;
67
mod mock;
78

89
pub use cosmwasm_core::testing::*;
910

11+
pub use message_info::message_info;
1012
#[cfg(feature = "cosmwasm_1_3")]
1113
pub use mock::DistributionQuerier;
1214
#[cfg(feature = "staking")]

0 commit comments

Comments
 (0)