Skip to content

Commit b4faaf5

Browse files
committed
Let mock_env return a contract address that is compatible with MockApi
1 parent 9a1c80c commit b4faaf5

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ and this project adheres to
2929
[`is_human_readable`](https://docs.rs/serde/latest/serde/trait.Serializer.html#method.is_human_readable).
3030
This is to make these types more efficient when used together with the new
3131
[MessagePack](https://msgpack.org) encoding. ([#2118])
32+
- cosmwasm-std: Let `mock_env` return a contract address that is valid bech32
33+
and uses the same bech32 prefix as `MockApi`. ([#2211])
3234

3335
[#2118]: https://github.com/CosmWasm/cosmwasm/pull/2118
36+
[#2211]: https://github.com/CosmWasm/cosmwasm/issues/2211
3437

3538
## [2.1.3] - 2024-08-08
3639

packages/std/src/testing/mock.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,62 @@ fn validate_length(bytes: &[u8]) -> StdResult<()> {
337337
}
338338
}
339339

340-
/// Returns a default environment with height, time, chain_id, and contract address
340+
/// Returns a default environment with height, time, chain_id, and contract address.
341341
/// You can submit as is to most contracts, or modify height/time if you want to
342342
/// test for expiration.
343343
///
344344
/// This is intended for use in test code only.
345+
///
346+
/// The contract address uses the same bech32 prefix as [`MockApi`](crate::testing::MockApi). While
347+
/// this is good for the majority of users, you might need to create your `Env`s
348+
/// differently if you need a valid address using a different prefix.
349+
///
350+
/// ## Examples
351+
///
352+
/// Create an env:
353+
///
354+
/// ```
355+
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
356+
/// use cosmwasm_std::testing::mock_env;
357+
///
358+
/// let env = mock_env();
359+
/// assert_eq!(env, Env {
360+
/// block: BlockInfo {
361+
/// height: 12_345,
362+
/// time: Timestamp::from_nanos(1_571_797_419_879_305_533),
363+
/// chain_id: "cosmos-testnet-14002".to_string(),
364+
/// },
365+
/// transaction: Some(TransactionInfo { index: 3 }),
366+
/// contract: ContractInfo {
367+
/// address: Addr::unchecked("cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs"),
368+
/// },
369+
/// });
370+
/// ```
371+
///
372+
/// Mutate and reuse environment:
373+
///
374+
/// ```
375+
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
376+
/// use cosmwasm_std::testing::mock_env;
377+
///
378+
/// let env1 = mock_env();
379+
///
380+
/// // First test with `env1`
381+
///
382+
/// let mut env2 = env1.clone();
383+
/// env2.block.height += 1;
384+
/// env2.block.time = env1.block.time.plus_seconds(6);
385+
///
386+
/// // `env2` is one block and 6 seconds later
387+
///
388+
/// let mut env3 = env2.clone();
389+
/// env3.block.height += 1;
390+
/// env3.block.time = env2.block.time.plus_nanos(5_500_000_000);
391+
///
392+
/// // `env3` is one block and 5.5 seconds later
393+
/// ```
345394
pub fn mock_env() -> Env {
395+
let contract_addr = MockApi::default().addr_make(MOCK_CONTRACT_ADDR);
346396
Env {
347397
block: BlockInfo {
348398
height: 12_345,
@@ -351,7 +401,7 @@ pub fn mock_env() -> Env {
351401
},
352402
transaction: Some(TransactionInfo { index: 3 }),
353403
contract: ContractInfo {
354-
address: Addr::unchecked(MOCK_CONTRACT_ADDR),
404+
address: contract_addr,
355405
},
356406
}
357407
}

0 commit comments

Comments
 (0)