var img = document.createElement('img'); img.src = "https://terradocs.matomo.cloud//piwik.php?idsite=1&rec=1&url=https://docs.terra.money" + location.pathname; img.style = "border:0"; img.alt = "tracker"; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(img,s);
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.

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)]
_2
pub struct MigrateMsg {}

2. Update contract.rs

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

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


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

  2. Add the following method above instantiate.


    _4
    #[cfg_attr(not(feature = "library"), entry_point)]
    _4
    pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
    _4
    Ok(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.


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


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


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