Technical Perspectives | Contract Upgrade for Python Smart Contract Tutorial
01 lead
02 Upgrade API How to use
From ontology.interop.Ontology.Contract import Migrate
From ontology.interop.System.Contract import Destroy
2.1 Destroy API
From ontology.interop.System.Contract import Destroy
From ontology.interop.System.Runtime import Notify
- How To | Build a Zero-Dependent Application on IPFS (Vol. 2)
- Technical Perspectives | Python Smart Contract Tutorial Native Contract Call
- The most heavy chain rule defect: your main chain I am the master
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:
- Paste the above code into SmartX to compile and deploy;
- 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
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
- 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
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!
We will continue to update Blocking; if you have any questions or suggestions, please contact us!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- Getting started with blockchain | Hash lock for cross-chain technology solutions
- Technical Perspectives | How much does the Python Smart Contract Execution API know?
- Comparison of IPFS and EdgeFS for Secure Edge/IoT Computing Use Cases
- Five major problems read through the PoA consensus algorithm
- Analysis | Pre-selection and pre-submission mechanisms for Byzantine fault-tolerant consensus
- Unlock Bitcoin for more apps, Miniscript yesterday, today and tomorrow!
- Technical Guide | Teach you to discuss Wasm contract development: (C++)