Contract Update and Destroy
Smart contracts support to be updated or destroyed after release. Before you can do that you need to implement the Update and Destroy interfaces in the old contracts.
#
Contract UpdateIn some cases you want to upgrade the smart contracts deployed on the blockchain or migrate the storage of a contract into another new contract, you need to update smart contracts. Neither the contract hash nor the storage is changed when the contract is upgraded.
#
Implementing the Update interfaceTo enable the update function, you need to implement the Update
method in the contract. In the contract template this method has been implemented by default, as follows:
public static bool Verify() => IsOwner();
public static void Update(ByteString nefFile, string manifest){ if (!IsOwner()) throw new Exception("No authorization."); ContractManagement.Update(nefFile, manifest, null);}
If you want to update the contract later, you must implement the update
method in the contract prior to deployment. For more information, refer to Deploying and Invoking Smart Contracts.
#
Updating the contractGet the compiled NEF file and Manifest file of the new contract ready
Base64-encode the NEF file with the tool like File to Base64 online
Compress and escape the Manifest file with the tool like JSON compressed and escaped online
Use the invoke command to call the update method of the contract
For information about invoking the contract, refer to Invoking contracts.
After the update
method is executed, the contract is upgraded and neither the contract hash nor the storage area is changed.
#
Contract DestructionTo destroy a contract, you need to reserve the destruction method in the contract.
To use the contract destroy feature, you need to implement the destroy method in the original contract. The destroy
method has been implemented by default in the contract template as follows.
public static bool Verify() => IsOwner();
public static void Destroy(){ if (!IsOwner()) throw new Exception("No authorization."); ContractManagement.Destroy();}
The Destroy method takes no arguments. When it is invoked, the contract and its storage, if any, are deleted. Then the contract is no longer available.