Migrating CosmWasm contracts on Fluid
Contracts on Fluid 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 Fluid development suite designed to simplify the scaffolding, deployment, and migration of your contracts.
If this is your first time using Terrain, visit the Initial setup guide.
Overview
There are two key components to a migratable contract:
- A
MigrateMsgtransaction. - An admin set, which is the address that's allowed to perform migrations.
1. Add MigrateMsg to contract
To implement support for MigrateMsg, add the following to msg.rs:
_2#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]_2pub struct MigrateMsg {}
You can place this message anywhere, however; it is usually placed above the InstantiateMsg struct.
2. Update contract.rs
Now that MigrateMsg is defined, you will need to update contract.rs:
-
Update the import from
crate::msgto 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
In previous Terrain tutorials, you learned how to deploy the contract. Now you will be able to initialize it as migratable.
After adding MigrateMsg to the smart contract, redeploy and add the --set-signer-as-admin flag.
This flag tells Fluid that the transaction signer is allowed to migrate the contract in the future:
_1terrain deploy counter --signer test1 --set-signer-as-admin
With the new contract deployed you can make some changes then migrate to the new code with the following command:
_1terrain contract:migrate counter --signer test1