Addresses in CosmWasm MultiTest
We recently released version 0.19 of CosmWasm MultiTest, which includes extensions enabling the use of Bech32 and Bech32m addresses for testing CosmWasm smart contracts.
Since the inception of CosmWasm MultiTest, contract addresses have followed a format such as contract0, contract1, contract2, and so forth. While test writers should avoid presuming the specific structure of these contract addresses, replicating real-life blockchain address formats can be an attractive feature in certain test cases.
The following code snippets demonstrate how to configure the App
to enable Bech32 or Bech32m address formats accordingly.
Using Bech32 address format in CosmWasm MultiTest since version 0.19
use cw_multi_test::addons::{MockAddressGenerator, MockApiBech32};
use cw_multi_test::no_init;
// initialize application with Api that supports Bech32 address format
let mut app = AppBuilder::default()
.with_api(MockApiBech32::new("juno"))
.with_wasm(WasmKeeper::default()
.with_address_generator(MockAddressGenerator))
.build(no_init);
// test your contract
Using Bech32m address format in CosmWasm MultiTest since version 0.19
use cw_multi_test::addons::{MockAddressGenerator, MockApiBech32m};
use cw_multi_test::no_init;
// initialize application with Api that supports Bech32m address format
let mut app = AppBuilder::default()
.with_api(MockApiBech32m::new("osmo"))
.with_wasm(WasmKeeper::default()
.with_address_generator(MockAddressGenerator))
.build(no_init);
// test your contract
To utilize Bech32 address formats within CosmWasm MultiTest, the configuration of both the MockApiBech32
and the MockAddressGenerator
is required. Similarly, for Bech32m address formats, configuring both MockApiBech32m
, and the MockAddressGenerator
is necessary. MockApiBech32
, MockApiBech32m
and MockAddressGenerator
are opt-in extensions to CosmWasm MultiTest functionality that can be found in cw_multi_test::addons
module.
That’s it!
Since now, all contract addresses generated by CosmWasm MultiTest during contract instantiation will have Bech32 or Bech32m format, with the prefix specified in MockApiBech32::new
or MockApiBech32m::new
constructor accordingly.
And what about user addresses?
These can be easily generated using addr_make
function defined in MockApiBech32
and MockApiBech32m
, just like in the following example:
// initialize application with Api that supports
// Bech32 address format with "juno" prefix
let mut app = AppBuilder::default()
.with_api(MockApiBech32::new("juno"))
.with_wasm(WasmKeeper::default()
.with_address_generator(MockAddressGenerator))
.build(no_init);
// create user address in Bech32 format with "juno" prefix
let creator = app.api().addr_make("creator");
assert_eq!(
creator.as_str(),
"juno1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqsksmtyp"
);