@@ -337,12 +337,62 @@ fn validate_length(bytes: &[u8]) -> StdResult<()> {
337
337
}
338
338
}
339
339
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.
341
341
/// You can submit as is to most contracts, or modify height/time if you want to
342
342
/// test for expiration.
343
343
///
344
344
/// 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
+ /// ```
345
394
pub fn mock_env ( ) -> Env {
395
+ let contract_addr = MockApi :: default ( ) . addr_make ( MOCK_CONTRACT_ADDR ) ;
346
396
Env {
347
397
block : BlockInfo {
348
398
height : 12_345 ,
@@ -351,7 +401,7 @@ pub fn mock_env() -> Env {
351
401
} ,
352
402
transaction : Some ( TransactionInfo { index : 3 } ) ,
353
403
contract : ContractInfo {
354
- address : Addr :: unchecked ( MOCK_CONTRACT_ADDR ) ,
404
+ address : contract_addr ,
355
405
} ,
356
406
}
357
407
}
0 commit comments