@@ -52,7 +52,8 @@ use crate::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
52
52
use crate :: { Decimal256 , DelegationRewardsResponse , DelegatorValidatorsResponse } ;
53
53
use crate :: { RecoverPubkeyError , StdError , StdResult , SystemError , VerificationError } ;
54
54
55
- pub const MOCK_CONTRACT_ADDR : & str = "cosmos2contract" ;
55
+ pub const MOCK_CONTRACT_ADDR : & str =
56
+ "cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs" ;
56
57
57
58
/// Creates all external requirements that can be injected for unit tests.
58
59
///
@@ -337,12 +338,62 @@ fn validate_length(bytes: &[u8]) -> StdResult<()> {
337
338
}
338
339
}
339
340
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.
341
342
/// You can submit as is to most contracts, or modify height/time if you want to
342
343
/// test for expiration.
343
344
///
344
345
/// 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
+ /// ```
345
395
pub fn mock_env ( ) -> Env {
396
+ let contract_addr = MockApi :: default ( ) . addr_make ( "cosmos2contract" ) ;
346
397
Env {
347
398
block : BlockInfo {
348
399
height : 12_345 ,
@@ -351,7 +402,7 @@ pub fn mock_env() -> Env {
351
402
} ,
352
403
transaction : Some ( TransactionInfo { index : 3 } ) ,
353
404
contract : ContractInfo {
354
- address : Addr :: unchecked ( MOCK_CONTRACT_ADDR ) ,
405
+ address : contract_addr ,
355
406
} ,
356
407
}
357
408
}
@@ -1204,6 +1255,12 @@ mod tests {
1204
1255
const ETH_BLOCK_HEADER : & [ u8 ] =
1205
1256
include_bytes ! ( "../../../crypto/testdata/eth-headers/1699693797.394876721s.json" ) ;
1206
1257
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
+
1207
1264
#[ test]
1208
1265
fn mock_info_works ( ) {
1209
1266
#[ allow( deprecated) ]
0 commit comments