Messages
The interaction with a CosmWasm smart contract is facilitated through messages. Within most contracts, you will find a msg.rs file that outlines these messages.
The instantiate message, for example, is used to create a new instance of a CosmWasm smart contract on the blockchain. This message is typically defined in msg.rs as InstantiateMsg and is then passed to an instantiate function in the main contract.rs for processing.
The examples used here are very simple. However, if you are confused about the arguments that can be passed, you can look in the contract's schema folder. There, you will find at least two relevant files:
- instantiate_msg.json - The expected shape and types for the instantiate message
- execute_msg.json - The expected shape and types for each of the messages that the contract can use to execute an action
Some contracts with large APIs may have many more schema files. Explore them to find the message or command you're looking for.
In the nameservice example contract, there are only two valid messages once the contract has been instantiated:
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Register { name: String },
Transfer { name: String, to: String },
}
The context of this code can be found here.
The ExecuteMsg message can then be processed within contract.rs. Each of the enum values can then be handled in the execute function as follows:
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Register { name } => execute_register(deps, env, info, name),
ExecuteMsg::Transfer { name, to } => execute_transfer(deps, env, info, name, to),
}
}
Here you can find the source code for the execute function.