Skip to main content

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.

In this tutorial, you will utilize 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 initial setup guide.


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 To do so, navigate to the file and place the following code just above the InstantiateMsg struct.

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct MigrateMsg {}

2. Update

Now that MigrateMsg is defined, you will need to update the file.

  1. Update the import from crate::msg to include MigrateMsg.

    use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg, MigrateMsg};

  2. Add the following method above instantiate.

    #[cfg_attr(not(feature = "library"), entry_point)]
    pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {

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.

terrain 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.

terrain contract:migrate counter --signer test1

If you would like to specify the address of the desired administrator for your smart contract, you may utilize the --admin-address flag in the deploy command followed by the wallet address of the desired administrator.

terrain deploy counter --signer test1 --admin-address <insert-admin-wallet-address>