Skip to content

Commit

Permalink
Let mock_env return a contract address that is compatible with MockApi
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Aug 19, 2024
1 parent 9a1c80c commit b4faaf5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ and this project adheres to
[`is_human_readable`](https://docs.rs/serde/latest/serde/trait.Serializer.html#method.is_human_readable).
This is to make these types more efficient when used together with the new
[MessagePack](https://msgpack.org) encoding. ([#2118])
- cosmwasm-std: Let `mock_env` return a contract address that is valid bech32
and uses the same bech32 prefix as `MockApi`. ([#2211])

[#2118]: https://github.com/CosmWasm/cosmwasm/pull/2118
[#2211]: https://github.com/CosmWasm/cosmwasm/issues/2211

## [2.1.3] - 2024-08-08

Expand Down
54 changes: 52 additions & 2 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,62 @@ fn validate_length(bytes: &[u8]) -> StdResult<()> {
}
}

/// Returns a default environment with height, time, chain_id, and contract address
/// Returns a default environment with height, time, chain_id, and contract address.
/// You can submit as is to most contracts, or modify height/time if you want to
/// test for expiration.
///
/// This is intended for use in test code only.
///
/// The contract address uses the same bech32 prefix as [`MockApi`](crate::testing::MockApi). While
/// this is good for the majority of users, you might need to create your `Env`s
/// differently if you need a valid address using a different prefix.
///
/// ## Examples
///
/// Create an env:
///
/// ```
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
/// use cosmwasm_std::testing::mock_env;
///
/// let env = mock_env();
/// assert_eq!(env, Env {
/// block: BlockInfo {
/// height: 12_345,
/// time: Timestamp::from_nanos(1_571_797_419_879_305_533),
/// chain_id: "cosmos-testnet-14002".to_string(),
/// },
/// transaction: Some(TransactionInfo { index: 3 }),
/// contract: ContractInfo {
/// address: Addr::unchecked("cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs"),
/// },
/// });
/// ```
///
/// Mutate and reuse environment:
///
/// ```
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
/// use cosmwasm_std::testing::mock_env;
///
/// let env1 = mock_env();
///
/// // First test with `env1`
///
/// let mut env2 = env1.clone();
/// env2.block.height += 1;
/// env2.block.time = env1.block.time.plus_seconds(6);
///
/// // `env2` is one block and 6 seconds later
///
/// let mut env3 = env2.clone();
/// env3.block.height += 1;
/// env3.block.time = env2.block.time.plus_nanos(5_500_000_000);
///
/// // `env3` is one block and 5.5 seconds later
/// ```
pub fn mock_env() -> Env {
let contract_addr = MockApi::default().addr_make(MOCK_CONTRACT_ADDR);
Env {
block: BlockInfo {
height: 12_345,
Expand All @@ -351,7 +401,7 @@ pub fn mock_env() -> Env {
},
transaction: Some(TransactionInfo { index: 3 }),
contract: ContractInfo {
address: Addr::unchecked(MOCK_CONTRACT_ADDR),
address: contract_addr,
},
}
}
Expand Down

0 comments on commit b4faaf5

Please sign in to comment.