This guide walks through upgrading a contract on the SUI blockchain. The process involves deploying the contract, calling a transaction, and then upgrading the contract with a new string value. The contract files involved are located in the hello_world/sources/ directory: hello_world.move and version.move.
Ensure ts-node and npm are installed. Set up a .env file with the following fields:
NETWORK=testnet
OWNER_MNEMONIC_PHRASE=your_owner_mnemonic_phrase
OWNER_ADDRESS=your_owner_address
PACKAGE_ID=your_package_id
UPGRADE_CAP=your_upgrade_capability_id
VERSION=your_version_id
V_ADMIN_CAP=your_version_admin_capability_id
NEW_PACKAGE_ID=your_new_package_id-
Run npm install to install all necessary dependencies.
-
Run the setup script to deploy the contract and print required values:
ts-node script/setup.tsThis will output the following: PACKAGE_ID=0x....... UPGRADE_CAP=0x....... VERSION=0x....... V_ADMIN_CAP=0x.......
-
Copy these values into your .env file.
-
To mint an object with string Hello World run,
ts-node script/hello_world_before_upgrade.ts
To upgrade the contract and modify the string, follow these steps:
-
Run
ts-node script/migrateVersion.tsfor migrating version from 1 to 2.
-
In
hello_world.move, change the string to:text: string::utf8(b"Hello World v2")
-
Update the contract version by modifying the
VERSIONfield in the contract to2:VERSION = 2;
-
In
Move.toml, set the[package]published-atvalue to the old package ID:[package] published-at = "OLD_PACKAGE_ID"
-
Use the upgrade capability stored in the
.envfile. -
In the SUI CLI, connect to the address that deployed the original contract, then run:
sui client upgrade --upgrade-capability UPGRADE_CAP_ID
-
You will get a new package ID from the 6th steps. Update the
Move.tomlfile to reference the new package ID:[Packages] published-at = "NEW_PACKAGE_ID"
-
Finally, mint a new object with the upgraded string:
ts-node script/hello_world_after_upgrade.ts
This will create an object with the updated string Hello World v2.
NOTE: You are not allowed to change the function signature .