Function cosmwasm_std::instantiate2_address

source ·
pub fn instantiate2_address(
    checksum: &[u8],
    creator: &CanonicalAddr,
    salt: &[u8]
) -> Result<CanonicalAddr, Instantiate2AddressError>
Expand description

Creates a contract address using the predictable address format introduced with wasmd 0.29. When using instantiate2, this is a way to precompute the address. When using instantiate, the contract address will use a different algorithm and cannot be pre-computed as it contains inputs from the chain’s state at the time of message execution.

The predicable address format of instantiate2 is stable. But bear in mind this is a powerful tool that requires multiple software components to work together smoothly. It should be used carefully and tested thoroughly to avoid the loss of funds.

This method operates on CanonicalAddr to be implemented without chain interaction. The typical usage looks like this:

use cosmwasm_std::instantiate2_address;

#[entry_point]
pub fn execute(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: ExecuteMsg,
) -> Result<Response, StdError> {
    let canonical_creator = deps.api.addr_canonicalize(env.contract.address.as_str())?;
    let checksum = HexBinary::from_hex("9af782a3a1bcbcd22dbb6a45c751551d9af782a3a1bcbcd22dbb6a45c751551d")?;
    let salt = b"instance 1231";
    let canonical_addr = instantiate2_address(&checksum, &canonical_creator, salt)
        .map_err(|_| StdError::generic_err("Could not calculate addr"))?;
    let addr = deps.api.addr_humanize(&canonical_addr)?;

}