ontology nlp_如何使用Python on Ontology编写智能合约? 第5部分:本机API

ontology nlp

image

In the previous Python tutorial posts, I have introduced the Ontology Smart Contract in

在之前的Python教程帖子中,我已经介绍了本体智能合约

Part 1: Blockchain & Block API and 第1部分:区块链和区块APIPart 2: Storage API 第2部分:存储API Part 3: Runtime API 第3部分:运行时API Part 4: Native API and described how to use smart contracts for ONT / ONG transfer. 第4部分:本机API,并介绍了如何使用智能合约进行ONT / ONG传输。

Today we will talk about how to use

今天我们将讨论如何使用

升级API (Upgrade API)

to upgrade smart contract. There are 2 APIs:

升级智能合约。 有2个API:

破坏 (Destroy )

and

迁移 (Migrate)

.

They are used as follows:

它们的用法如下:

image

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.

现在,让我们更详细地介绍如何使用这两个API。 在此之前,您可以在本体智能合约开发工具SmartX中创建新合约,并按照以下说明进行操作。 和往常一样,在文章结尾,我将链接附加到源代码。

如何使用升级API (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

销毁API (Destroy API)

Destroy API is used for destroying and deleting a smart contract on the chain. Below is a code sample of the API.

销毁API用于销毁和删除链上的智能合约。 下面是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()
    Notify(["The contract has been destoryed"])
    return True

迁移API (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.

Migrate API用于迁移智能合约。 现有合同将被新迁移的合同替换。 旧合同保存的数据也将迁移到新合同。 迁移后,旧合同将被删除。

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:

迁移功能的参数列表如下:

image

See below the code sample of the Migrate function:

请参见下面的Migrate函数的代码示例:

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"])
        new_contract_hash=AddressFromVmCode(avm_code)
        Notify(new_contract_hash)
        return True
    else:
        return False
You can follow the steps below to see the execution result of the code sample on SmartX : 您可以按照以下步骤在SmartX上查看代码示例的执行结果:

1。 (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:

在输入参数之前,将以上代码复制并粘贴到SmartX并进行编译。 输入参数时,请特别注意两个问题:

  • Make sure avm_code does not exist on the chain, otherwise an error will be returned;

    确保链上不存在avm_code,否则将返回错误;
  • Migrate requires a high gas limit, so you need to adjust the gas limit when running the function.

    迁移需要很高的气体限制,因此运行该功能时需要调整气体限制。
image

2。 (2. )

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

部署后,您可以从仪表板获取新的合同哈希。 在此示例中,新合同哈希为be4606c4663081b70f745ed9fc64d4c3b0d9c183。

image

摘要 (Summary)

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.

在本文中,我们介绍了本体区块链的升级API。 开发人员可以使用此API升级智能合约。 Destroy API用于销毁链上的智能合约,而Migrate API用于迁移智能合约。 在下一篇文章中,我们将介绍静态和动态API,以探索如何使用Python实现对本体智能合约的静态和动态调用。

Find the tutorial on GitHub here.

此处找到GitHub上的教程。

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.

您是开发人员吗? 确保您已加入Discord上的我们的技术社区。 另外,请访问我们网站上的开发人员中心,在那里您可以找到开发人员工具,文档等。



在其他地方找到本体 (Find Ontology elsewhere)
Ontology website 本体网站 GitHub / GitHub / Discord Discord

Telegram English / Russian

电报英语 / 俄语

Twitter / Twitter / Reddit Reddit

翻译自: https://habr.com/en/post/474970/

ontology nlp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值