Skip to main content

Sudo execution

Sudo execution is a smart contract entry point that can only be called by trusted native Cosmos modules. It can not be called by users or other smart contracts. As an example, Sudo can be used to enforce governance proposals, and contract must be instantiated before governance can execute it.

Here's the sudo msg type:

/// SudoMsg is only exposed for internal Cosmos SDK modules to call.
/// This is showing how we can expose "admin" functionality than can not be called by
/// external users or contracts, but only trusted (native/Go) code in the blockchain
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum SudoMsg {
MoveFunds {
recipient: String,
amount: Vec<Coin>,
},
}

And the sudo entry point:

#[entry_point]
pub fn sudo(_deps: DepsMut, _env: Env, msg: SudoMsg) -> Result<Response, HackError> {
match msg {
SudoMsg::MoveFunds { recipient, amount } => {
let msg = BankMsg::Send {
to_address: recipient,
amount,
};
Ok(Response::new().add_message(msg))
}
}
}

This can be tested as normal.

When using multi-test you will need to add an additional call to the contract wrapper:

pub fn contract_template() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
crate::contract::execute,
crate::contract::instantiate,
crate::contract::query,
);
let contract_with_sudo = contract.with_sudo(crate::contract::sudo);
Box::new(contract_with_sudo)
}