Migrating CosmWasm contracts on Terra
Contracts on Terra can be initialized as migratable, which allows the administrator to upload a new version of the contract and then send a migrate message to move to the new code.
This tutorial uses Terrain, a Terra development suite designed to simplify the scaffolding, deployment, and migration of smart contracts.
If this is your first time using Terrain, visit the Terrain setup guide.
Overview
There are two key components to a migratable contract.
- The availability of a
MigrateMsg
transaction. - A designated administrator whose address will be allowed to perform migrations.
1. Add MigrateMsg
to contract
To implement support for MigrateMsg
, you will need to add the message to msg.rs
. To do so, navigate to the msg.rs
file and place the following code just above the InstantiateMsg
struct.
_2#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]_2pub struct MigrateMsg {}
2. Update contract.rs
Now that MigrateMsg
is defined, you will need to update the contract.rs
file.
-
Update the import from
crate::msg
to includeMigrateMsg
._1use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg, MigrateMsg}; -
Add the following method above
instantiate
._4#[cfg_attr(not(feature = "library"), entry_point)]_4pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {_4Ok(Response::default())_4}
3. Call migrate
Adding the MigrateMsg to the smart contract allows the contract's administrator to migrate the contract in the future. When we deploy our contract, the wallet address of the signer will be automatically designated as the contract administrator. In the following command, the contract is deployed with the preconfigured LocalTerra test1
wallet as the signer and administrator of our counter contract.
_1terrain deploy counter --signer test1
If you decide to make changes to the deployed contract, you can migrate to the updated code by executing the following command.
_1terrain contract:migrate counter --signer test1
If you would like to specify the address of the desired administrator for your smart contract, use the --admin-address
flag in the deploy command followed by the wallet address of the desired administrator.
_1terrain deploy counter --signer test1 --admin-address <insert-admin-wallet-address>