Optionally perform state migrations of Secret Network smart contracts
Contracts on Secret Network can be initialized as migratable, which allows the contract administrator to upload a new version of the contract and then send a migrate
message to move to the new code, which "migrates" storage from a previous contract to the new contract.
There are two key components to a migratable contract:
A designated admin
whose address will be allowed to perform migrations.
The availability of a MigrateMsg
transaction.
On instantiation
, the contract creator can specify an admin
address, which can be the contract creator's wallet address, an external account, or a governance contract, etc. Whatever the address is, this admin
gains the ability to migrate the contract to a new codeId
and codeHash
, and can also update or clear the admin
address. When the admin
invokes the MigrateMsg
message, the migrate()
function is called on the new contract, where the new contract can optionally perform state migrations from an old contract.
Once the migrate()
function is invoked, the contract address now points to the new code, and anybody contacting that address will reach the new code. The old code becomes unreachable.
Performing a contract migration is a three step process:
Write a newer version of the contract you wish to update
Upload the new smart contract, but don’t instantiate it
Use a dedicated MigrateMsg transaction to point the new contract to the code you wish to migrate
When migrating, the new contract must have a migrate
function as an entry_point.
The migrate
function provides the ability to make any desired changes to the contract's state, similar to a database migration.
If the migrate
function returns an error, the transaction will abort and no state changes will occur.
Secret contracts instantiated prior to the v1.11 network upgrade can be migrated via a SCRT governance signaling proposal. If approved for migration, the contracts' code hashes will be matched to a chosen admin key and added to a list which is hardcoded inside the enclave for reference.
In order for a Secret contract instantiated after the v1.11 network upgrade to be migrated, the contract must be instantiated with an admin. To query whether or not a Secret contract was instantiated with an admin
, use the contractInfo
method:
The query will return the admin address, if there is one:
To query if a Secret contract was migrated successfully, use the contractHistory
method:
The method is designed to retrieve the history of a contract, specifically its code changes over time. The method returns an object containing an array of ContractCodeHistoryEntry
items.