diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fdcb8761c..7d4daa12f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,12 @@ and this project adheres to [#2158]: https://github.com/CosmWasm/cosmwasm/pull/2158 +### Changed + +- cosmwasm-std: Add message_info and deprecate mock_info ([#2160]) + +[#2160]: https://github.com/CosmWasm/cosmwasm/pull/2160 + ## [2.0.3] - 2024-05-10 ### Changed diff --git a/MIGRATING.md b/MIGRATING.md index 2b20ac4b57..262299f0be 100644 --- a/MIGRATING.md +++ b/MIGRATING.md @@ -4,7 +4,7 @@ This guide explains what is needed to upgrade contracts when migrating over major releases of `cosmwasm`. Note that you can also view the [complete CHANGELOG](./CHANGELOG.md) to understand the differences. -## 1.5.x -> 2.0.0 +## 1.5.x -> 2.0.x - Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use): @@ -262,6 +262,10 @@ major releases of `cosmwasm`. Note that you can also view the in 2.0. To keep the CosmWasm 1.x behaviour, just set payload to `Binary::default()`. +- In test code, replace calls to `mock_info` with `message_info`. This takes a + `&Addr` as the first argument which you get by using owned `Addr` in the test + bodies. + ## 1.4.x -> 1.5.0 - Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use): diff --git a/contracts/burner/src/contract.rs b/contracts/burner/src/contract.rs index 06332db0d7..aa33c2dd52 100644 --- a/contracts/burner/src/contract.rs +++ b/contracts/burner/src/contract.rs @@ -83,7 +83,7 @@ fn cleanup(storage: &mut dyn Storage, mut limit: usize) -> usize { mod tests { use super::*; use cosmwasm_std::testing::{ - mock_dependencies, mock_dependencies_with_balance, mock_env, mock_info, + message_info, mock_dependencies, mock_dependencies_with_balance, mock_env, }; use cosmwasm_std::{coins, Attribute, StdError, Storage, SubMsg}; @@ -102,8 +102,10 @@ mod tests { fn instantiate_fails() { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make("creator"); + let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(1000, "earth")); + let info = message_info(&creator, &coins(1000, "earth")); // we can just call .unwrap() to assert this was a success let res = instantiate(deps.as_mut(), mock_env(), info, msg); match res.unwrap_err() { @@ -164,6 +166,8 @@ mod tests { fn execute_cleans_up_data() { let mut deps = mock_dependencies_with_balance(&coins(123456, "gold")); + let anon = deps.api.addr_make("anon"); + // store some sample data deps.storage.set(b"foo", b"bar"); deps.storage.set(b"key2", b"data2"); @@ -179,7 +183,7 @@ mod tests { let res = execute( deps.as_mut(), mock_env(), - mock_info("anon", &[]), + message_info(&anon, &[]), ExecuteMsg::Cleanup { limit: Some(2) }, ) .unwrap(); @@ -192,7 +196,7 @@ mod tests { let res = execute( deps.as_mut(), mock_env(), - mock_info("anon", &[]), + message_info(&anon, &[]), ExecuteMsg::Cleanup { limit: Some(2) }, ) .unwrap(); diff --git a/contracts/crypto-verify/src/contract.rs b/contracts/crypto-verify/src/contract.rs index f52716df73..88820edaa2 100644 --- a/contracts/crypto-verify/src/contract.rs +++ b/contracts/crypto-verify/src/contract.rs @@ -215,7 +215,7 @@ pub fn query_list_verifications(deps: Deps) -> StdResult OwnedDeps { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make(CREATOR); let msg = InstantiateMsg {}; - let info = mock_info(CREATOR, &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); deps diff --git a/contracts/cyberpunk/src/contract.rs b/contracts/cyberpunk/src/contract.rs index 80254fe64d..6f4ce5b5f8 100644 --- a/contracts/cyberpunk/src/contract.rs +++ b/contracts/cyberpunk/src/contract.rs @@ -229,14 +229,15 @@ fn query_denom(deps: Deps, denom: String) -> StdResult { mod tests { use super::*; use cosmwasm_std::testing::{ - mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage, + message_info, mock_dependencies, mock_env, MockApi, MockQuerier, MockStorage, }; use cosmwasm_std::{from_json, DenomMetadata, DenomUnit, OwnedDeps}; fn setup() -> OwnedDeps { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make("creator"); let msg = Empty {}; - let info = mock_info("creator", &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); deps @@ -250,9 +251,10 @@ mod tests { #[test] fn debug_works() { let mut deps = setup(); + let caller = deps.api.addr_make("caller"); let msg = ExecuteMsg::Debug {}; - execute(deps.as_mut(), mock_env(), mock_info("caller", &[]), msg).unwrap(); + execute(deps.as_mut(), mock_env(), message_info(&caller, &[]), msg).unwrap(); } #[test] diff --git a/contracts/hackatom/src/contract.rs b/contracts/hackatom/src/contract.rs index d1afbc6f5a..52c02ce84d 100644 --- a/contracts/hackatom/src/contract.rs +++ b/contracts/hackatom/src/contract.rs @@ -294,7 +294,8 @@ fn query_int() -> IntResponse { mod tests { use super::*; use cosmwasm_std::testing::{ - mock_dependencies, mock_dependencies_with_balances, mock_env, mock_info, MOCK_CONTRACT_ADDR, + message_info, mock_dependencies, mock_dependencies_with_balances, mock_env, + MOCK_CONTRACT_ADDR, }; // import trait Storage to get access to read use cosmwasm_std::{coins, Binary, Storage, SubMsg}; @@ -303,20 +304,21 @@ mod tests { fn proper_initialization() { let mut deps = mock_dependencies(); - let verifier: String = deps.api.addr_make("verifies").into(); - let beneficiary: String = deps.api.addr_make("benefits").into(); - let creator: String = deps.api.addr_make("creator").into(); + let verifier = deps.api.addr_make("verifies"); + let beneficiary = deps.api.addr_make("benefits"); + let creator = deps.api.addr_make("creator"); + let expected_state = State { - verifier: deps.api.addr_validate(&verifier).unwrap(), - beneficiary: deps.api.addr_validate(&beneficiary).unwrap(), - funder: deps.api.addr_validate(&creator).unwrap(), + verifier: verifier.clone(), + beneficiary: beneficiary.clone(), + funder: creator.clone(), }; let msg = InstantiateMsg { - verifier, - beneficiary, + verifier: verifier.to_string(), + beneficiary: beneficiary.to_string(), }; - let info = mock_info(creator.as_str(), &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(res.messages.len(), 0); assert_eq!(res.attributes, [("Let the", "hacking begin")]); @@ -331,34 +333,36 @@ mod tests { fn instantiate_and_query() { let mut deps = mock_dependencies(); - let verifier: String = deps.api.addr_make("verifies").into(); - let beneficiary: String = deps.api.addr_make("benefits").into(); - let creator: String = deps.api.addr_make("creator").into(); + let verifier = deps.api.addr_make("verifies"); + let beneficiary = deps.api.addr_make("benefits"); + let creator = deps.api.addr_make("creator"); + let msg = InstantiateMsg { - verifier: verifier.clone(), - beneficiary, + verifier: verifier.to_string(), + beneficiary: beneficiary.to_string(), }; - let info = mock_info(&creator, &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); // now let's query let query_response = query_verifier(deps.as_ref()).unwrap(); - assert_eq!(query_response.verifier, verifier); + assert_eq!(query_response.verifier, verifier.as_str()); } #[test] fn migrate_verifier() { let mut deps = mock_dependencies(); - let verifier: String = deps.api.addr_make("verifies").into(); - let beneficiary: String = deps.api.addr_make("benefits").into(); - let creator: String = deps.api.addr_make("creator").into(); + let verifier = deps.api.addr_make("verifies"); + let beneficiary = deps.api.addr_make("benefits"); + let creator = deps.api.addr_make("creator"); + let msg = InstantiateMsg { - verifier: verifier.clone(), - beneficiary, + verifier: verifier.to_string(), + beneficiary: beneficiary.to_string(), }; - let info = mock_info(&creator, &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); @@ -386,28 +390,35 @@ mod tests { fn sudo_can_steal_tokens() { let mut deps = mock_dependencies(); - let verifier: String = deps.api.addr_make("verifies").into(); - let beneficiary: String = deps.api.addr_make("benefits").into(); - let creator: String = deps.api.addr_make("creator").into(); + let verifier = deps.api.addr_make("verifies"); + let beneficiary = deps.api.addr_make("benefits"); + let creator = deps.api.addr_make("creator"); + let msg = InstantiateMsg { - verifier, - beneficiary, + verifier: verifier.to_string(), + beneficiary: beneficiary.to_string(), }; - let info = mock_info(&creator, &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); // sudo takes any tax it wants - let to_address = String::from("community-pool"); + let to_address = deps.api.addr_make("community-pool"); let amount = coins(700, "gold"); let sys_msg = SudoMsg::StealFunds { - recipient: to_address.clone(), + recipient: to_address.to_string(), amount: amount.clone(), }; let res = sudo(deps.as_mut(), mock_env(), sys_msg).unwrap(); assert_eq!(1, res.messages.len()); let msg = res.messages.first().expect("no message"); - assert_eq!(msg, &SubMsg::new(BankMsg::Send { to_address, amount })); + assert_eq!( + msg, + &SubMsg::new(BankMsg::Send { + to_address: to_address.to_string(), + amount + }) + ); } #[test] @@ -430,17 +441,17 @@ mod tests { let mut deps = mock_dependencies(); // initialize the store - let creator: String = deps.api.addr_make("creator").into(); - let verifier: String = deps.api.addr_make("verifies").into(); - let beneficiary: String = deps.api.addr_make("benefits").into(); + let creator = deps.api.addr_make("creator"); + let verifier = deps.api.addr_make("verifies"); + let beneficiary = deps.api.addr_make("benefits"); let instantiate_msg = InstantiateMsg { - verifier: verifier.clone(), - beneficiary: beneficiary.clone(), + verifier: verifier.to_string(), + beneficiary: beneficiary.to_string(), }; let init_amount = coins(1000, "earth"); - let init_info = mock_info(&creator, &init_amount); - let init_res = instantiate(deps.as_mut(), mock_env(), init_info, instantiate_msg).unwrap(); + let info = message_info(&creator, &init_amount); + let init_res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap(); assert_eq!(init_res.messages.len(), 0); // balance changed in init @@ -449,7 +460,7 @@ mod tests { .update_balance(MOCK_CONTRACT_ADDR, init_amount); // beneficiary can release it - let execute_info = mock_info(verifier.as_str(), &[]); + let execute_info = message_info(&verifier, &[]); let execute_res = execute( deps.as_mut(), mock_env(), @@ -462,13 +473,13 @@ mod tests { assert_eq!( msg, &SubMsg::new(BankMsg::Send { - to_address: beneficiary.clone(), + to_address: beneficiary.to_string(), amount: coins(1000, "earth"), }), ); assert_eq!( execute_res.attributes, - vec![("action", "release"), ("destination", &beneficiary)], + vec![("action", "release"), ("destination", beneficiary.as_str())], ); assert_eq!(execute_res.data, Some(vec![0xF0, 0x0B, 0xAA].into())); } @@ -478,17 +489,17 @@ mod tests { let mut deps = mock_dependencies(); // initialize the store - let creator: String = deps.api.addr_make("creator").into(); - let verifier: String = deps.api.addr_make("verifies").into(); - let beneficiary: String = deps.api.addr_make("benefits").into(); + let creator = deps.api.addr_make("creator"); + let verifier = deps.api.addr_make("verifies"); + let beneficiary = deps.api.addr_make("benefits"); let instantiate_msg = InstantiateMsg { - verifier: verifier.clone(), - beneficiary: beneficiary.clone(), + verifier: verifier.to_string(), + beneficiary: beneficiary.to_string(), }; let init_amount = coins(1000, "earth"); - let init_info = mock_info(&creator, &init_amount); - let init_res = instantiate(deps.as_mut(), mock_env(), init_info, instantiate_msg).unwrap(); + let info = message_info(&creator, &init_amount); + let init_res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap(); assert_eq!(init_res.messages.len(), 0); // balance changed in init @@ -497,7 +508,7 @@ mod tests { .update_balance(MOCK_CONTRACT_ADDR, init_amount); // beneficiary cannot release it - let execute_info = mock_info(beneficiary.as_str(), &[]); + let execute_info = message_info(&beneficiary, &[]); let execute_res = execute( deps.as_mut(), mock_env(), @@ -512,9 +523,9 @@ mod tests { assert_eq!( state, State { - verifier: Addr::unchecked(verifier), - beneficiary: Addr::unchecked(beneficiary), - funder: Addr::unchecked(creator), + verifier: verifier.clone(), + beneficiary: beneficiary.clone(), + funder: creator.clone(), } ); } @@ -525,19 +536,19 @@ mod tests { let mut deps = mock_dependencies(); // initialize the store - let verifier: String = deps.api.addr_make("verifies").into(); - let beneficiary: String = deps.api.addr_make("benefits").into(); - let creator: String = deps.api.addr_make("creator").into(); + let verifier = deps.api.addr_make("verifies"); + let beneficiary = deps.api.addr_make("benefits"); + let creator = deps.api.addr_make("creator"); let instantiate_msg = InstantiateMsg { - verifier, - beneficiary: beneficiary.clone(), + verifier: verifier.to_string(), + beneficiary: beneficiary.to_string(), }; - let init_info = mock_info(&creator, &coins(1000, "earth")); - let init_res = instantiate(deps.as_mut(), mock_env(), init_info, instantiate_msg).unwrap(); + let info = message_info(&creator, &coins(1000, "earth")); + let init_res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap(); assert_eq!(0, init_res.messages.len()); - let execute_info = mock_info(&beneficiary, &[]); + let execute_info = message_info(&beneficiary, &[]); // this should panic let _ = execute( deps.as_mut(), @@ -551,15 +562,20 @@ mod tests { fn execute_user_errors_in_api_calls() { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make("creator"); + let anyone = deps.api.addr_make("anyone"); + let verifier = deps.api.addr_make("verifies"); + let beneficiary = deps.api.addr_make("benefits"); + let instantiate_msg = InstantiateMsg { - verifier: deps.api.addr_make("verifies").into(), - beneficiary: deps.api.addr_make("benefits").into(), + verifier: verifier.to_string(), + beneficiary: beneficiary.to_string(), }; - let init_info = mock_info("creator", &coins(1000, "earth")); - let init_res = instantiate(deps.as_mut(), mock_env(), init_info, instantiate_msg).unwrap(); - assert_eq!(0, init_res.messages.len()); + let info = message_info(&creator, &coins(1000, "earth")); + let response = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap(); + assert_eq!(0, response.messages.len()); - let execute_info = mock_info("anyone", &[]); + let execute_info = message_info(&anyone, &[]); execute( deps.as_mut(), mock_env(), diff --git a/contracts/ibc-reflect-send/src/contract.rs b/contracts/ibc-reflect-send/src/contract.rs index d3a2bfe441..f550c2104c 100644 --- a/contracts/ibc-reflect-send/src/contract.rs +++ b/contracts/ibc-reflect-send/src/contract.rs @@ -202,19 +202,20 @@ fn query_admin(deps: Deps) -> StdResult { #[cfg(test)] mod tests { use super::*; - use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; + use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env}; const CREATOR: &str = "creator"; #[test] fn instantiate_works() { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make(CREATOR); let msg = InstantiateMsg {}; - let info = mock_info(CREATOR, &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); let admin = query_admin(deps.as_ref()).unwrap(); - assert_eq!(CREATOR, admin.admin.as_str()); + assert_eq!(admin.admin.as_str(), creator.as_str()); } } diff --git a/contracts/ibc-reflect-send/src/ibc.rs b/contracts/ibc-reflect-send/src/ibc.rs index aabacf913a..3954e1785c 100644 --- a/contracts/ibc-reflect-send/src/ibc.rs +++ b/contracts/ibc-reflect-send/src/ibc.rs @@ -230,9 +230,9 @@ mod tests { use crate::msg::{AccountResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; use cosmwasm_std::testing::{ - mock_dependencies, mock_env, mock_ibc_channel_connect_ack, mock_ibc_channel_open_init, - mock_ibc_channel_open_try, mock_ibc_packet_ack, mock_info, MockApi, MockQuerier, - MockStorage, + message_info, mock_dependencies, mock_env, mock_ibc_channel_connect_ack, + mock_ibc_channel_open_init, mock_ibc_channel_open_try, mock_ibc_packet_ack, MockApi, + MockQuerier, MockStorage, }; use cosmwasm_std::{coin, coins, BankMsg, CosmosMsg, IbcAcknowledgement, OwnedDeps}; @@ -240,8 +240,9 @@ mod tests { fn setup() -> OwnedDeps { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make(CREATOR); let msg = InstantiateMsg {}; - let info = mock_info(CREATOR, &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); deps @@ -337,6 +338,7 @@ mod tests { // init contract let mut deps = setup(); + let creator = deps.api.addr_make(CREATOR); // channel handshake connect(deps.as_mut(), channel_id); // get feedback from WhoAmI packet @@ -352,7 +354,7 @@ mod tests { channel_id: channel_id.into(), msgs: msgs_to_dispatch, }; - let info = mock_info(CREATOR, &[]); + let info = message_info(&creator, &[]); let mut res = execute(deps.as_mut(), mock_env(), info, handle_msg).unwrap(); assert_eq!(1, res.messages.len()); let msg = match res.messages.swap_remove(0).msg { @@ -380,6 +382,7 @@ mod tests { // init contract let mut deps = setup(); + let creator = deps.api.addr_make(CREATOR); // channel handshake connect(deps.as_mut(), reflect_channel_id); // get feedback from WhoAmI packet @@ -390,7 +393,7 @@ mod tests { reflect_channel_id: "random-channel".into(), transfer_channel_id: transfer_channel_id.into(), }; - let info = mock_info(CREATOR, &coins(12344, "utrgd")); + let info = message_info(&creator, &coins(12344, "utrgd")); execute(deps.as_mut(), mock_env(), info, msg).unwrap_err(); // let's try with no sent funds in the message @@ -398,7 +401,7 @@ mod tests { reflect_channel_id: reflect_channel_id.into(), transfer_channel_id: transfer_channel_id.into(), }; - let info = mock_info(CREATOR, &[]); + let info = message_info(&creator, &[]); execute(deps.as_mut(), mock_env(), info, msg).unwrap_err(); // 3rd times the charm @@ -406,7 +409,7 @@ mod tests { reflect_channel_id: reflect_channel_id.into(), transfer_channel_id: transfer_channel_id.into(), }; - let info = mock_info(CREATOR, &coins(12344, "utrgd")); + let info = message_info(&creator, &coins(12344, "utrgd")); let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(1, res.messages.len()); match &res.messages[0].msg { diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs index d66dd49af0..9e7e7ddfe8 100644 --- a/contracts/ibc-reflect/src/contract.rs +++ b/contracts/ibc-reflect/src/contract.rs @@ -347,9 +347,10 @@ pub fn ibc_packet_timeout( mod tests { use super::*; use cosmwasm_std::testing::{ - mock_dependencies, mock_env, mock_ibc_channel_close_init, mock_ibc_channel_connect_ack, - mock_ibc_channel_open_init, mock_ibc_channel_open_try, mock_ibc_packet_recv, mock_info, - mock_wasmd_attr, MockApi, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR, + message_info, mock_dependencies, mock_env, mock_ibc_channel_close_init, + mock_ibc_channel_connect_ack, mock_ibc_channel_open_init, mock_ibc_channel_open_try, + mock_ibc_packet_recv, mock_wasmd_attr, MockApi, MockQuerier, MockStorage, + MOCK_CONTRACT_ADDR, }; use cosmwasm_std::{attr, coin, coins, from_json, BankMsg, OwnedDeps, WasmMsg}; @@ -361,12 +362,13 @@ mod tests { fn setup() -> OwnedDeps { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make(CREATOR); let msg = InstantiateMsg { reflect_code_id: REFLECT_ID, }; - let info = mock_info(CREATOR, &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); - assert_eq!(0, res.messages.len()); + assert_eq!(res.messages.len(), 0); deps } @@ -420,11 +422,12 @@ mod tests { #[test] fn instantiate_works() { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make(CREATOR); let msg = InstantiateMsg { reflect_code_id: 17, }; - let info = mock_info("creator", &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()) } diff --git a/contracts/queue/src/contract.rs b/contracts/queue/src/contract.rs index 02b39e2198..7f87d5f413 100644 --- a/contracts/queue/src/contract.rs +++ b/contracts/queue/src/contract.rs @@ -179,14 +179,15 @@ fn query_open_iterators(deps: Deps, count: u32) -> Empty { mod tests { use super::*; use cosmwasm_std::testing::{ - mock_dependencies_with_balance, mock_env, mock_info, MockApi, MockQuerier, MockStorage, + message_info, mock_dependencies_with_balance, mock_env, MockApi, MockQuerier, MockStorage, }; use cosmwasm_std::{coins, from_json, OwnedDeps}; /// Instantiates a contract with no elements fn create_contract() -> (OwnedDeps, MessageInfo) { let mut deps = mock_dependencies_with_balance(&coins(1000, "earth")); - let info = mock_info("creator", &coins(1000, "earth")); + let creator = deps.api.addr_make("creator"); + let info = message_info(&creator, &coins(1000, "earth")); let res = instantiate(deps.as_mut(), mock_env(), info.clone(), InstantiateMsg {}).unwrap(); assert_eq!(0, res.messages.len()); (deps, info) diff --git a/contracts/reflect/src/contract.rs b/contracts/reflect/src/contract.rs index 3b4149fcd7..1da4d73bcf 100644 --- a/contracts/reflect/src/contract.rs +++ b/contracts/reflect/src/contract.rs @@ -174,7 +174,7 @@ fn query_raw(deps: Deps, contract: String, key: Binary) -> StdResu mod tests { use super::*; use crate::testing::mock_dependencies_with_custom_querier; - use cosmwasm_std::testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR}; + use cosmwasm_std::testing::{message_info, mock_env, MOCK_CONTRACT_ADDR}; use cosmwasm_std::{ coin, coins, from_json, AllBalanceResponse, BankMsg, BankQuery, Binary, Event, StakingMsg, StdError, SubMsgResponse, SubMsgResult, @@ -183,9 +183,10 @@ mod tests { #[test] fn proper_instantialization() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(1000, "earth")); + let info = message_info(&creator, &coins(1000, "earth")); // we can just call .unwrap() to assert this was a success let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); @@ -193,15 +194,16 @@ mod tests { // it worked, let's query the state let value = query_owner(deps.as_ref()).unwrap(); - assert_eq!("creator", value.owner.as_str()); + assert_eq!(value.owner, creator.to_string()); } #[test] fn reflect() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); let payload = vec![BankMsg::Send { @@ -213,7 +215,7 @@ mod tests { let msg = ExecuteMsg::ReflectMsg { msgs: payload.clone(), }; - let info = mock_info("creator", &[]); + let info = message_info(&creator, &[]); let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap(); let payload: Vec<_> = payload.into_iter().map(SubMsg::new).collect(); assert_eq!(payload, res.messages); @@ -222,9 +224,11 @@ mod tests { #[test] fn reflect_requires_owner() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); + let random = deps.api.addr_make("random"); let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); // signer is not owner @@ -235,7 +239,7 @@ mod tests { .into()]; let msg = ExecuteMsg::ReflectMsg { msgs: payload }; - let info = mock_info("random", &[]); + let info = message_info(&random, &[]); let res = execute(deps.as_mut(), mock_env(), info, msg); match res.unwrap_err() { ReflectError::NotCurrentOwner { .. } => {} @@ -246,12 +250,13 @@ mod tests { #[test] fn reflect_reject_empty_msgs() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); - let info = mock_info("creator", &[]); + let info = message_info(&creator, &[]); let payload = vec![]; let msg = ExecuteMsg::ReflectMsg { msgs: payload }; @@ -262,9 +267,10 @@ mod tests { #[test] fn reflect_multiple_messages() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); let payload = vec![ @@ -286,7 +292,7 @@ mod tests { let msg = ExecuteMsg::ReflectMsg { msgs: payload.clone(), }; - let info = mock_info("creator", &[]); + let info = message_info(&creator, &[]); let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap(); let payload: Vec<_> = payload.into_iter().map(SubMsg::new).collect(); assert_eq!(payload, res.messages); @@ -295,12 +301,13 @@ mod tests { #[test] fn change_owner_works() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); - let info = mock_info("creator", &[]); + let info = message_info(&creator, &[]); let new_owner = deps.api.addr_make("friend"); let msg = ExecuteMsg::ChangeOwner { owner: new_owner.to_string(), @@ -316,23 +323,25 @@ mod tests { #[test] fn change_owner_requires_current_owner_as_sender() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); + let random = deps.api.addr_make("random"); + let friend = deps.api.addr_make("friend"); let msg = InstantiateMsg {}; - let creator = String::from("creator"); - let info = mock_info(&creator, &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); - let random = String::from("random"); - let info = mock_info(&random, &[]); - let new_owner = String::from("friend"); - let msg = ExecuteMsg::ChangeOwner { owner: new_owner }; + let info = message_info(&random, &[]); + let msg = ExecuteMsg::ChangeOwner { + owner: friend.to_string(), + }; let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err(); assert_eq!( err, ReflectError::NotCurrentOwner { - expected: creator, - actual: random + expected: creator.to_string(), + actual: random.to_string(), } ); } @@ -340,13 +349,13 @@ mod tests { #[test] fn change_owner_errors_for_invalid_new_address() { let mut deps = mock_dependencies_with_custom_querier(&[]); - let creator = String::from("creator"); + let creator = deps.api.addr_make("creator"); let msg = InstantiateMsg {}; - let info = mock_info(&creator, &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); - let info = mock_info(&creator, &[]); + let info = message_info(&creator, &[]); let msg = ExecuteMsg::ChangeOwner { owner: String::from("x"), }; @@ -400,9 +409,10 @@ mod tests { #[test] fn reflect_subcall() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); let id = 123u64; @@ -417,7 +427,7 @@ mod tests { let msg = ExecuteMsg::ReflectSubMsg { msgs: vec![payload.clone()], }; - let info = mock_info("creator", &[]); + let info = message_info(&creator, &[]); let mut res = execute(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(1, res.messages.len()); let msg = res.messages.pop().expect("must have a message"); @@ -428,9 +438,10 @@ mod tests { #[test] fn reply_and_query() { let mut deps = mock_dependencies_with_custom_querier(&[]); + let creator = deps.api.addr_make("creator"); let msg = InstantiateMsg {}; - let info = mock_info("creator", &coins(2, "token")); + let info = message_info(&creator, &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); let id = 123u64; diff --git a/contracts/staking/src/contract.rs b/contracts/staking/src/contract.rs index 9015006ea2..43350dc91b 100644 --- a/contracts/staking/src/contract.rs +++ b/contracts/staking/src/contract.rs @@ -322,13 +322,14 @@ pub fn _bond_all_tokens( // we deduct pending claims from our account balance before reinvesting. // if there is not enough funds, we just return a no-op - match update_item(deps.storage, KEY_TOTAL_SUPPLY, |mut supply: Supply| { + let updated = update_item(deps.storage, KEY_TOTAL_SUPPLY, |mut supply: Supply| { balance.amount = balance.amount.checked_sub(supply.claims)?; // this just triggers the "no op" case if we don't have min_withdrawal left to reinvest balance.amount.checked_sub(invest.min_withdrawal)?; supply.bonded += balance.amount; Ok(supply) - }) { + }); + match updated { Ok(_) => {} // if it is below the minimum, we do a no-op (do not revert other state from withdrawal) Err(StdError::Overflow { .. }) => return Ok(Response::default()), @@ -406,7 +407,7 @@ pub fn query_investment(deps: Deps) -> StdResult { mod tests { use super::*; use cosmwasm_std::testing::{ - mock_dependencies, mock_env, mock_info, MockQuerier, StakingQuerier, MOCK_CONTRACT_ADDR, + message_info, mock_dependencies, mock_env, MockQuerier, StakingQuerier, MOCK_CONTRACT_ADDR, }; use cosmwasm_std::{coins, Addr, Coin, CosmosMsg, Decimal, FullDelegation, Validator}; use std::str::FromStr; @@ -456,22 +457,23 @@ mod tests { } } - fn get_balance(deps: Deps, addr: &str) -> Uint128 { - query_balance(deps, addr).unwrap().balance + fn get_balance(deps: Deps, addr: &Addr) -> Uint128 { + query_balance(deps, addr.as_str()).unwrap().balance } - fn get_claims(deps: Deps, addr: &str) -> Uint128 { - query_claims(deps, addr).unwrap().claims + fn get_claims(deps: Deps, addr: &Addr) -> Uint128 { + query_claims(deps, addr.as_str()).unwrap().claims } #[test] fn initialization_with_missing_validator() { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make("creator"); + deps.querier .staking .update("ustake", &[sample_validator("john")], &[]); - let creator = deps.api.addr_make("creator").to_string(); let msg = InstantiateMsg { name: "Cool Derivative".to_string(), symbol: "DRV".to_string(), @@ -480,7 +482,7 @@ mod tests { exit_tax: Decimal::percent(2), min_withdrawal: Uint128::new(50), }; - let info = mock_info(&creator, &[]); + let info = message_info(&creator, &[]); // make sure we can instantiate with this let res = instantiate(deps.as_mut(), mock_env(), info, msg); @@ -495,6 +497,8 @@ mod tests { #[test] fn proper_initialization() { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make("creator"); + deps.querier.staking.update( "ustake", &[ @@ -505,7 +509,6 @@ mod tests { &[], ); - let creator = deps.api.addr_make("creator").to_string(); let msg = InstantiateMsg { name: "Cool Derivative".to_string(), symbol: "DRV".to_string(), @@ -514,7 +517,7 @@ mod tests { exit_tax: Decimal::percent(2), min_withdrawal: Uint128::new(50), }; - let info = mock_info(&creator, &[]); + let info = message_info(&creator, &[]); // make sure we can instantiate with this let res = instantiate(deps.as_mut(), mock_env(), info, msg.clone()).unwrap(); @@ -533,7 +536,7 @@ mod tests { // investment info correct let invest = query_investment(deps.as_ref()).unwrap(); - assert_eq!(&invest.owner, &creator); + assert_eq!(&invest.owner, creator.as_str()); assert_eq!(&invest.validator, &msg.validator); assert_eq!(invest.exit_tax, msg.exit_tax); assert_eq!(invest.min_withdrawal, msg.min_withdrawal); @@ -548,18 +551,19 @@ mod tests { let mut deps = mock_dependencies(); set_validator(&mut deps.querier); - let creator = deps.api.addr_make("creator").to_string(); + let creator = deps.api.addr_make("creator"); + let bob = deps.api.addr_make("bob"); + let instantiate_msg = default_init(2, 50); - let info = mock_info(&creator, &[]); + let info = message_info(&creator, &[]); // make sure we can instantiate with this let res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap(); assert_eq!(0, res.messages.len()); // let's bond some tokens now - let bob = deps.api.addr_make("bob").to_string(); let bond_msg = ExecuteMsg::Bond {}; - let info = mock_info(&bob, &[coin(10, "random"), coin(1000, "ustake")]); + let info = message_info(&bob, &[coin(10, "random"), coin(1000, "ustake")]); // try to bond and make sure we trigger delegation let res = execute(deps.as_mut(), mock_env(), info, bond_msg).unwrap(); @@ -588,18 +592,21 @@ mod tests { let mut deps = mock_dependencies(); set_validator(&mut deps.querier); - let creator = deps.api.addr_make("creator").to_string(); + let creator = deps.api.addr_make("creator"); + let bob = deps.api.addr_make("bob"); + let alice = deps.api.addr_make("alice"); + let contract = deps.api.addr_make(MOCK_CONTRACT_ADDR); + let instantiate_msg = default_init(2, 50); - let info = mock_info(creator.as_str(), &[]); + let info = message_info(&creator, &[]); // make sure we can instantiate with this let res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap(); assert_eq!(0, res.messages.len()); // let's bond some tokens now - let bob = deps.api.addr_make("bob").to_string(); let bond_msg = ExecuteMsg::Bond {}; - let info = mock_info(bob.as_str(), &[coin(10, "random"), coin(1000, "ustake")]); + let info = message_info(&bob, &[coin(10, "random"), coin(1000, "ustake")]); let res = execute(deps.as_mut(), mock_env(), info, bond_msg).unwrap(); assert_eq!(1, res.messages.len()); @@ -608,11 +615,13 @@ mod tests { // fake a reinvestment (this must be sent by the contract itself) let rebond_msg = ExecuteMsg::_BondAllTokens {}; - let info = mock_info(MOCK_CONTRACT_ADDR, &[]); + let info = message_info(&contract, &[]); deps.querier .bank - .update_balance(MOCK_CONTRACT_ADDR, coins(500, "ustake")); - let _ = execute(deps.as_mut(), mock_env(), info, rebond_msg).unwrap(); + .update_balance(&contract, coins(500, "ustake")); + let mut env = mock_env(); + env.contract.address = contract.clone(); + let _ = execute(deps.as_mut(), env, info, rebond_msg).unwrap(); // update the querier with new bond set_delegation(&mut deps.querier, 1500, "ustake"); @@ -625,9 +634,8 @@ mod tests { assert_eq!(invest.nominal_value, ratio); // we bond some other tokens and get a different issuance price (maintaining the ratio) - let alice = deps.api.addr_make("alice").to_string(); let bond_msg = ExecuteMsg::Bond {}; - let info = mock_info(alice.as_str(), &[coin(3000, "ustake")]); + let info = message_info(&alice, &[coin(3000, "ustake")]); let res = execute(deps.as_mut(), mock_env(), info, bond_msg).unwrap(); assert_eq!(1, res.messages.len()); @@ -635,10 +643,7 @@ mod tests { set_delegation(&mut deps.querier, 3000, "ustake"); // alice should have gotten 2000 DRV for the 3000 stake, keeping the ratio at 1.5 - assert_eq!( - get_balance(deps.as_ref(), alice.as_str()), - Uint128::new(2000) - ); + assert_eq!(get_balance(deps.as_ref(), &alice), Uint128::new(2000)); let invest = query_investment(deps.as_ref()).unwrap(); assert_eq!(invest.token_supply, Uint128::new(3000)); @@ -651,18 +656,19 @@ mod tests { let mut deps = mock_dependencies(); set_validator(&mut deps.querier); - let creator = deps.api.addr_make("creator").to_string(); + let creator = deps.api.addr_make("creator"); + let bob = deps.api.addr_make("bob"); + let instantiate_msg = default_init(2, 50); - let info = mock_info(creator.as_str(), &[]); + let info = message_info(&creator, &[]); // make sure we can instantiate with this let res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap(); assert_eq!(0, res.messages.len()); // let's bond some tokens now - let bob = deps.api.addr_make("bob").to_string(); let bond_msg = ExecuteMsg::Bond {}; - let info = mock_info(bob.as_str(), &[coin(500, "photon")]); + let info = message_info(&bob, &[coin(500, "photon")]); // try to bond and make sure we trigger delegation let res = execute(deps.as_mut(), mock_env(), info, bond_msg); @@ -679,18 +685,20 @@ mod tests { let mut deps = mock_dependencies(); set_validator(&mut deps.querier); - let creator = deps.api.addr_make("creator").to_string(); + let creator = deps.api.addr_make("creator"); + let bob = deps.api.addr_make("bob"); + let contract = deps.api.addr_make(MOCK_CONTRACT_ADDR); + let instantiate_msg = default_init(10, 50); - let info = mock_info(creator.as_str(), &[]); + let info = message_info(&creator, &[]); // make sure we can instantiate with this let res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap(); assert_eq!(0, res.messages.len()); // let's bond some tokens now - let bob = deps.api.addr_make("bob").to_string(); let bond_msg = ExecuteMsg::Bond {}; - let info = mock_info(bob.as_str(), &[coin(10, "random"), coin(1000, "ustake")]); + let info = message_info(&bob, &[coin(10, "random"), coin(1000, "ustake")]); let res = execute(deps.as_mut(), mock_env(), info, bond_msg).unwrap(); assert_eq!(1, res.messages.len()); @@ -700,21 +708,23 @@ mod tests { // fake a reinvestment (this must be sent by the contract itself) // after this, we see 1000 issues and 1500 bonded (and a price of 1.5) let rebond_msg = ExecuteMsg::_BondAllTokens {}; - let info = mock_info(MOCK_CONTRACT_ADDR, &[]); + let info = message_info(&contract, &[]); deps.querier .bank - .update_balance(MOCK_CONTRACT_ADDR, coins(500, "ustake")); - let _ = execute(deps.as_mut(), mock_env(), info, rebond_msg).unwrap(); + .update_balance(&contract, coins(500, "ustake")); + let mut env = mock_env(); + env.contract.address = contract.clone(); + let _ = execute(deps.as_mut(), env, info, rebond_msg).unwrap(); // update the querier with new bond, lower balance set_delegation(&mut deps.querier, 1500, "ustake"); - deps.querier.bank.update_balance(MOCK_CONTRACT_ADDR, vec![]); + deps.querier.bank.update_balance(&contract, vec![]); // creator now tries to unbond these tokens - this must fail let unbond_msg = ExecuteMsg::Unbond { amount: Uint128::new(600), }; - let info = mock_info(creator.as_str(), &[]); + let info = message_info(&creator, &[]); let res = execute(deps.as_mut(), mock_env(), info, unbond_msg); match res.unwrap_err() { StakingError::Std { @@ -732,7 +742,7 @@ mod tests { let owner_cut = Uint128::new(60); let bobs_claim = Uint128::new(810); let bobs_balance = Uint128::new(400); - let info = mock_info(bob.as_str(), &[]); + let info = message_info(&bob, &[]); let res = execute(deps.as_mut(), mock_env(), info, unbond_msg).unwrap(); assert_eq!(1, res.messages.len()); let delegate = &res.messages[0].msg; @@ -748,10 +758,10 @@ mod tests { set_delegation(&mut deps.querier, 690, "ustake"); // check balances - assert_eq!(get_balance(deps.as_ref(), bob.as_str()), bobs_balance); - assert_eq!(get_balance(deps.as_ref(), creator.as_str()), owner_cut); + assert_eq!(get_balance(deps.as_ref(), &bob), bobs_balance); + assert_eq!(get_balance(deps.as_ref(), &creator), owner_cut); // proper claims - assert_eq!(get_claims(deps.as_ref(), bob.as_str()), bobs_claim); + assert_eq!(get_claims(deps.as_ref(), &bob), bobs_claim); // supplies updated, ratio the same (1.5) let ratio = Decimal::from_str("1.5").unwrap(); diff --git a/contracts/virus/src/contract.rs b/contracts/virus/src/contract.rs index 38b06f3463..9fd358f5f0 100644 --- a/contracts/virus/src/contract.rs +++ b/contracts/virus/src/contract.rs @@ -96,16 +96,17 @@ pub fn execute_spread( #[cfg(test)] mod tests { use super::*; - use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; + use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env}; const CREATOR: &str = "creator"; #[test] fn instantiate_works() { let mut deps = mock_dependencies(); + let creator = deps.api.addr_make(CREATOR); let msg = InstantiateMsg {}; - let info = mock_info(CREATOR, &[]); + let info = message_info(&creator, &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); - assert_eq!(0, res.messages.len()); + assert_eq!(res.messages.len(), 0); } } diff --git a/packages/std/src/coin.rs b/packages/std/src/coin.rs index daf1df1738..2d8a9b1f37 100644 --- a/packages/std/src/coin.rs +++ b/packages/std/src/coin.rs @@ -62,14 +62,14 @@ impl fmt::Display for Coin { /// /// ``` /// # use cosmwasm_std::{coins, BankMsg, CosmosMsg, Response, SubMsg}; -/// # use cosmwasm_std::testing::{mock_env, mock_info}; +/// # use cosmwasm_std::testing::mock_env; /// # let env = mock_env(); -/// # let info = mock_info("sender", &[]); +/// # let recipient = "blub".to_string(); /// let tip = coins(123, "ucosm"); /// /// let mut response: Response = Default::default(); /// response.messages = vec![SubMsg::new(BankMsg::Send { -/// to_address: info.sender.into(), +/// to_address: recipient, /// amount: tip, /// })]; /// ``` @@ -85,7 +85,7 @@ pub fn coins(amount: u128, denom: impl Into) -> Vec { /// # use cosmwasm_std::{coin, BankMsg, CosmosMsg, Response, SubMsg}; /// # use cosmwasm_std::testing::{mock_env, mock_info}; /// # let env = mock_env(); -/// # let info = mock_info("sender", &[]); +/// # let recipient = "blub".to_string(); /// let tip = vec![ /// coin(123, "ucosm"), /// coin(24, "ustake"), @@ -93,7 +93,7 @@ pub fn coins(amount: u128, denom: impl Into) -> Vec { /// /// let mut response: Response = Default::default(); /// response.messages = vec![SubMsg::new(BankMsg::Send { -/// to_address: info.sender.into(), +/// to_address: recipient, /// amount: tip, /// })]; /// ``` diff --git a/packages/std/src/testing/assertions.rs b/packages/std/src/testing/assertions.rs index f091acedba..dcf98459a8 100644 --- a/packages/std/src/testing/assertions.rs +++ b/packages/std/src/testing/assertions.rs @@ -170,6 +170,7 @@ mod tests { )] fn assert_approx_with_custom_panic_msg() { let adjective = "extra"; + #[allow(dead_code)] #[derive(Debug)] struct Foo(u32); assert_approx_eq!( diff --git a/packages/std/src/testing/message_info.rs b/packages/std/src/testing/message_info.rs new file mode 100644 index 0000000000..c6d903a35a --- /dev/null +++ b/packages/std/src/testing/message_info.rs @@ -0,0 +1,80 @@ +use crate::{Addr, Coin, MessageInfo}; + +/// A constructor function for [`MessageInfo`]. +/// +/// This is designed for writing contract tests. +/// It lives in `cosmwasm_std::testing` because constructing MessageInfo +/// objects is not something that you usually need in contract code. +/// +/// ## Examples +/// +/// ``` +/// # use cosmwasm_std::{DepsMut, Env, Response, MessageInfo, StdResult}; +/// # struct InstantiateMsg { +/// # pub verifier: String, +/// # pub beneficiary: String, +/// # } +/// # pub fn instantiate( +/// # _deps: DepsMut, +/// # _env: Env, +/// # _info: MessageInfo, +/// # _msg: InstantiateMsg, +/// # ) -> StdResult { +/// # Ok(Response::new().add_attribute("action", "instantiate")) +/// # } +/// use cosmwasm_std::coins; +/// use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env}; +/// +/// let mut deps = mock_dependencies(); +/// +/// // Create some Addr instances for testing +/// let creator = deps.api.addr_make("creator"); +/// let verifier = deps.api.addr_make("verifies"); +/// let beneficiary = deps.api.addr_make("benefits"); +/// +/// let msg = InstantiateMsg { +/// verifier: verifier.to_string(), +/// beneficiary: beneficiary.to_string(), +/// }; +/// let info = message_info(&creator, &coins(1000, "earth")); +/// let response = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); +/// assert_eq!(response.messages.len(), 0); +/// ``` +pub fn message_info(sender: &Addr, funds: &[Coin]) -> MessageInfo { + MessageInfo { + sender: sender.clone(), + funds: funds.to_vec(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{coins, Uint128}; + + #[test] + fn message_info_works() { + let addr = Addr::unchecked("cosmwasm1..."); + + let info = message_info(&addr, &[]); + assert_eq!( + info, + MessageInfo { + sender: addr.clone(), + funds: vec![], + } + ); + + let info = message_info(&addr, &coins(123, "foo")); + assert_eq!( + info, + MessageInfo { + sender: addr.clone(), + funds: vec![Coin { + amount: Uint128::new(123), + denom: "foo".to_string(), + }], + } + ); + } +} diff --git a/packages/std/src/testing/mock.rs b/packages/std/src/testing/mock.rs index 9944a286d4..3730ae6d59 100644 --- a/packages/std/src/testing/mock.rs +++ b/packages/std/src/testing/mock.rs @@ -286,6 +286,7 @@ pub fn mock_env() -> Env { /// Just set sender and funds for the message. /// This is intended for use in test code only. +#[deprecated(note = "This is inconvenient and unsafe. Use message_info instead.")] pub fn mock_info(sender: &str, funds: &[Coin]) -> MessageInfo { MessageInfo { sender: Addr::unchecked(sender), @@ -1113,6 +1114,7 @@ mod tests { #[test] fn mock_info_works() { + #[allow(deprecated)] let info = mock_info("my name", &coins(100, "atom")); assert_eq!( info, diff --git a/packages/std/src/testing/mod.rs b/packages/std/src/testing/mod.rs index 820580c40c..59f080e374 100644 --- a/packages/std/src/testing/mod.rs +++ b/packages/std/src/testing/mod.rs @@ -4,19 +4,23 @@ // Both unit tests and integration tests are compiled to native code, so everything in here does not need to compile to Wasm. mod assertions; +mod message_info; mod mock; pub use assertions::assert_approx_eq_impl; #[cfg(test)] pub use assertions::assert_hash_works_impl; +pub use message_info::message_info; +#[allow(deprecated)] +pub use mock::mock_info; #[cfg(feature = "cosmwasm_1_3")] pub use mock::DistributionQuerier; #[cfg(feature = "staking")] pub use mock::StakingQuerier; pub use mock::{ mock_dependencies, mock_dependencies_with_balance, mock_dependencies_with_balances, mock_env, - mock_info, mock_wasmd_attr, BankQuerier, MockApi, MockQuerier, MockQuerierCustomHandlerResult, + mock_wasmd_attr, BankQuerier, MockApi, MockQuerier, MockQuerierCustomHandlerResult, MockStorage, MOCK_CONTRACT_ADDR, }; #[cfg(feature = "stargate")] diff --git a/packages/vm/src/cache.rs b/packages/vm/src/cache.rs index 40ba51c8da..8518ea6aae 100644 --- a/packages/vm/src/cache.rs +++ b/packages/vm/src/cache.rs @@ -488,6 +488,7 @@ fn save_wasm_to_disk(dir: impl Into, wasm: &[u8]) -> VmResult let mut file = OpenOptions::new() .write(true) .create(true) + .truncate(true) .open(filepath) .map_err(|e| VmError::cache_err(format!("Error opening Wasm file for writing: {e}")))?; file.write_all(wasm)