9 December 2019

How to Write a Smart Contract with Python on Ontology? Part 5: Native API


In the previous Python tutorial posts, I have introduced the Ontology Smart Contract in
Part 1: Blockchain & Block API and
Part 2: Storage API
Part 3: Runtime API
Part 4: Native API and described how to use smart contracts for ONT / ONG transfer.

Today we will talk about how to use Upgrade API to upgrade smart contract. There are 2 APIs: Destroy and Migrate.

They are used as follows:


Now let’s go into more detail about how to use these 2 APIs. Before that, you can create a new contract in the Ontology smart contract development tool SmartX and follow the instructions below. As usual, at the end of the article, I will attach the link to the source code.

How to Use Upgrade API

As usual, you need to import the 2 functions as follows before using them.

from ontology.interop.Ontology.Contract import Migrate
from ontology.interop.System.Contract import Destroy

Destroy API

Destroy API is used for destroying and deleting a smart contract on the chain. Below is a code sample of the 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():
    Notify(["The contract has been destoryed"])
    return True

Migrate API

The Migrate API is used for migrating smart contracts. The existing contract will be replaced by the newly migrated contract. The data saved by the old contract will also be migrated to the new contract. The old contract will be deleted after migration.
Please note that the assets in the smart contract will not be migrated automatically so you need to transfer them out in advance. Otherwise, you will not be able to retrieve your assets.

The parameter list of the Migrate function is as follows:


See below the code sample of 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)
    if res:
        Notify(["Migrate successfully"])
        return True
        return False

You can follow the steps below to see the execution result of the code sample on SmartX :

1. Copy and paste the above code to SmartX and compile before putting in the parameters. Pay special attention to two issues when inputting parameters:

  • Make sure avm_code does not exist on the chain, otherwise an error will be returned;
  • Migrate requires a high gas limit, so you need to adjust the gas limit when running the function.


2. After deployment, you can get the new contract hash from the dashboard. In this example, the new contract hash is be4606c4663081b70f745ed9fc64d4c3b0d9c183.



In this article, we introduced the Upgrade API of the Ontology blockchain. Developers can use this API to upgrade smart contracts. Destroy API is used for destroying smart contracts on the chain and Migrate API is for migrating smart contracts. In the next article, we will introduce the Static & Dynamic API to explore how to implement the static and dynamic call of Ontology smart contracts with Python.

Find the tutorial on GitHub here.

Are you a developer? Make sure you have joined our tech community on Discord. Also, take a look at the Developer Center on our website, there you can find developer tools, documentation, and more.

Find Ontology elsewhere

Ontology website
GitHub / Discord
Telegram English / Russian
Twitter / Reddit
Tags:pythonsmart contractblockchainontologynetworkontologysmartx
Hubs: Python DevOps
225 2
Leave a comment