Deploy and Upgrade

In this section, we will learn how to deploy your contract to the Aptos blockchain and upgrade it.



Steps

1. Install the Aptos CLI to interact with a contract.

2. Initialize a profile.


aptos init --profile {profile_name}

3. Select Devnet as the default network.

  • This will create a private key for you and a new .aptos directory will be created.

4. In Move.toml, change the [addresses] contract_name = to the new account address.

  • You can find this in config.yaml, under [profile_name].account.

5. Publish your contract under a new object (using this command), this will automatically compile the contract.


PUBLISHER_PROFILE=your_profile_name
PUBLISHER_ADDR=0x$(aptos config show-profiles --profile=$PUBLISHER_PROFILE | grep 'account' | sed -n 's/.*"account": \"\(.*\)\".*/\1/p')
aptos move create-object-and-publish-package \
--address-name aptogotchi_addr \
--named-addresses aptogotchi_addr=$PUBLISHER_ADDR\
--profile $PUBLISHER_PROFILE \
--assume-yes


Making changes to a published smart contract

  • New changes to the smart contract need to be backward compatible.
  • You can rename published functions, however modifying function signatures or struct keys in an already deployed contract will throw an error.
  • You will have to re-publish the modified code under a new contract.

After every change to the contract, you should run:


APTOGOTCHI_CONTRACT_OBJECT_ADDR="find_the_object_address_from_output_of_publish_command"
aptos move upgrade-object-package \
--object-address $APTOGOTCHI_CONTRACT_OBJECT_ADDR \
--named-addresses aptogotchi_addr=$APTOGOTCHI_CONTRACT_OBJECT_ADDR\
--profile $PUBLISHER_PROFILE \
--assume-yes



More Resources

To learn more about how to test, compile, publish, or upgrade a contract, check out the Aptos developer docs.

Link