Skip to content

Commit 54d1d3d

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

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ 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`; Change `MOCK_CONTRACT_ADDR`
34+
value to match the contract address from `mock_env`. ([#2211])
3235

3336
[#2118]: https://github.com/CosmWasm/cosmwasm/pull/2118
37+
[#2211]: https://github.com/CosmWasm/cosmwasm/issues/2211
3438

3539
## [2.1.3] - 2024-08-08
3640

packages/std/src/testing/mock.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ use crate::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
5252
use crate::{Decimal256, DelegationRewardsResponse, DelegatorValidatorsResponse};
5353
use crate::{RecoverPubkeyError, StdError, StdResult, SystemError, VerificationError};
5454

55-
pub const MOCK_CONTRACT_ADDR: &str = "cosmos2contract";
55+
pub const MOCK_CONTRACT_ADDR: &str =
56+
"cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs";
5657

5758
/// Creates all external requirements that can be injected for unit tests.
5859
///
@@ -337,12 +338,62 @@ fn validate_length(bytes: &[u8]) -> StdResult<()> {
337338
}
338339
}
339340

340-
/// Returns a default environment with height, time, chain_id, and contract address
341+
/// Returns a default environment with height, time, chain_id, and contract address.
341342
/// You can submit as is to most contracts, or modify height/time if you want to
342343
/// test for expiration.
343344
///
344345
/// This is intended for use in test code only.
346+
///
347+
/// The contract address uses the same bech32 prefix as [`MockApi`](crate::testing::MockApi). While
348+
/// this is good for the majority of users, you might need to create your `Env`s
349+
/// differently if you need a valid address using a different prefix.
350+
///
351+
/// ## Examples
352+
///
353+
/// Create an env:
354+
///
355+
/// ```
356+
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
357+
/// use cosmwasm_std::testing::mock_env;
358+
///
359+
/// let env = mock_env();
360+
/// assert_eq!(env, Env {
361+
/// block: BlockInfo {
362+
/// height: 12_345,
363+
/// time: Timestamp::from_nanos(1_571_797_419_879_305_533),
364+
/// chain_id: "cosmos-testnet-14002".to_string(),
365+
/// },
366+
/// transaction: Some(TransactionInfo { index: 3 }),
367+
/// contract: ContractInfo {
368+
/// address: Addr::unchecked("cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs"),
369+
/// },
370+
/// });
371+
/// ```
372+
///
373+
/// Mutate and reuse environment:
374+
///
375+
/// ```
376+
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
377+
/// use cosmwasm_std::testing::mock_env;
378+
///
379+
/// let env1 = mock_env();
380+
///
381+
/// // First test with `env1`
382+
///
383+
/// let mut env2 = env1.clone();
384+
/// env2.block.height += 1;
385+
/// env2.block.time = env1.block.time.plus_seconds(6);
386+
///
387+
/// // `env2` is one block and 6 seconds later
388+
///
389+
/// let mut env3 = env2.clone();
390+
/// env3.block.height += 1;
391+
/// env3.block.time = env2.block.time.plus_nanos(5_500_000_000);
392+
///
393+
/// // `env3` is one block and 5.5 seconds later
394+
/// ```
345395
pub fn mock_env() -> Env {
396+
let contract_addr = MockApi::default().addr_make("cosmos2contract");
346397
Env {
347398
block: BlockInfo {
348399
height: 12_345,
@@ -351,7 +402,7 @@ pub fn mock_env() -> Env {
351402
},
352403
transaction: Some(TransactionInfo { index: 3 }),
353404
contract: ContractInfo {
354-
address: Addr::unchecked(MOCK_CONTRACT_ADDR),
405+
address: contract_addr,
355406
},
356407
}
357408
}
@@ -1204,6 +1255,12 @@ mod tests {
12041255
const ETH_BLOCK_HEADER: &[u8] =
12051256
include_bytes!("../../../crypto/testdata/eth-headers/1699693797.394876721s.json");
12061257

1258+
#[test]
1259+
fn mock_env_matches_mock_contract_addr() {
1260+
let contract_address = mock_env().contract.address;
1261+
assert_eq!(contract_address, Addr::unchecked(MOCK_CONTRACT_ADDR));
1262+
}
1263+
12071264
#[test]
12081265
fn mock_info_works() {
12091266
#[allow(deprecated)]

0 commit comments

Comments
 (0)