Technical Perspectives | Contract Upgrade for Python Smart Contract Tutorial

01 lead

 
In the previous technical point of view, we introduced the contract native API and described how to use smart contracts for ONT / ONG transfers. In this issue we will discuss how to upgrade a contract through the Upgrade API . There are 2 APIs for contract upgrades, Destroy and Migrate . Its usage is as follows:
Let's take a closer look at how to use these two APIs. Prior to this, the small partners can create a new contract in the Ontology Smart Contract Development Tool SmartX and follow us. As with the previous API tutorial, we will give a video example of this tutorial at the end of the article.

02 Upgrade API How to use

Need to introduce before using these two functions. The following two statements introduce the Migrate and Destroy functions respectively.
  From ontology.interop.Ontology.Contract import Migrate 

From ontology.interop.System.Contract import Destroy

2.1 Destroy API

The Destroy API is used to destroy the contract and the old contract will be deleted on the chain. Below is sample code to use this API.
  From ontology.interop.System.Contract import Destroy 

From ontology.interop.System.Runtime import Notify

Def Main(operation, args):

If operation == "destroy_contract":

Return destroy_contract()

Return False

Def destroy_contract():

Destroy() # call destroy to destroy this contract

Notify(["The contract has been destoryed"])

Return True You can see the results of this sample code on SmartX:

  1. Paste the above code into SmartX to compile and deploy;
  2. Clicking on the deployment again will pop up "Contract deployment failed, the contract has been deployed" because the same contract already exists on the chain;

3. Run the destroy_contract function to destroy the contract;

4. Click on the deployment contract again and you will find that the contract can be deployed again. It will not pop up again. "Contract deployment failed, the contract has been deployed." This can prove that the original contract has been destroyed in the chain.

2.2 Migrate API

The Migrate API is used to migrate contracts, old contracts will be replaced by new contracts, and data from old contracts will be automatically migrated to new contracts. The larger the amount of migrated data, the higher the migration cost. After the migration is successful, the old contract will be deleted.
Special Note: Assets in the contract will not be automatically migrated and need to be transferred in advance. Otherwise the assets in the contract will not be retrieved, which is equivalent to being transferred to the black hole address.
The list of incoming parameters for the Migrate function is as follows:

Again, we give the contract sample code using the Migrate function:

  •   From ontology.interop.Ontology.Contract import Migrate
     From ontology.interop.System.Runtime import Notify
     From ontology.libont import AddressFromVmCode 

Def Main(operation, args):
If operation == "migrate_contract":
If len(args) != 7:
Return False
Avm_code = args[0]
Need_storage = args[1]
Name = args[2]
Version = args[3]
Author = args[4]
Email = args[5]
Description = args[6]
Return migrate_contract(avm_code, need_storage, name, version, author, email, description)

Return False

Def migrate_contract(avm_code, need_storage, name, version, author, email, description):
Res = Migrate(avm_code, need_storage, name, version, author, email, description) # Call Migrate to migrate this contract if res:
Notify(["Migrate successfully"])
New_contract_hash=AddressFromVmCode(avm_code) # Calculate the new contract address Notify(new_contract_hash) # Print out the new contract address return True
Else:
Return False

You can see the results of this sample code on SmartX:
  1. Paste the above code into SmartX compilation and fill in the parameters. Pay special attention to the following two points when filling in parameters: a. To confirm that avm_code does not exist on the chain, it will report an error; b. Migrate requires a higher gas limit, so adjust the gas limit when running the function.

03 Conclusion

In this technical point of view, we introduced the Uplink API of the ontology blockchain, which developers can use to upgrade contracts. There are 2 APIs for contract upgrades, where the Destroy API is used to destroy contracts and the Migrate API is used to migrate contracts. I hope the tutorial will help everyone. In the next issue, we'll introduce the Static & Dynamic Call API for the Ontology Python Smart Contract Syntax, which describes how to make static and dynamic calls in Python smart contracts. The following is a Chinese video of this tutorial. Welcome friends to watch the study.
———————–

If you want to try to develop on the ontology public chain, please add the ontology research assistant: ontresearch, join the ontology technology community to discuss!