
这是使用
SmartX智能合约
开发工具在Ontology区块链网络上使用Python创建智能合约的系列教程的第一部分。
在本文中,我们将开始介绍本体智能合约API。 本体智能合约API分为7个模块:
- 区块链和区块API
- 存储API
- 运行时API
- 本机API
- 升级API
- 执行引擎API和
- 静态和动态调用API。
区块链和区块API是本体智能合约系统的重要组成部分。 Blockchain API支持基本的块请求操作,例如获取当前块的高度,而Block API支持基本的块请求操作,例如请求给定块的交易数。
让我们开始吧!首先,在
SmartX中创建一个新合同,然后按照以下说明进行操作。
1.如何使用区块链API
指向智能合约功能的链接与Python链接相同。 您可以根据需要输入适当的功能。 例如,以下语句引入了GetHeight —一个用于获取当前块高度的函数,以及GetHeader —一个用于获取块头的函数。
from ontology.interop.System.Blockchain import GetHeight, GetHeader
格高
GetHeight用于获取区块链中的最后一个块序列号,如下例所示。 在上一个示例中,为方便起见,我们将跳过Main函数,但是如果需要,您可以添加它。
from ontology.interop.System.Runtime import Notify from ontology.interop.System.Blockchain import GetHeight def Main(operation): if operation == 'demo': return demo() return False def demo(): height=GetHeight() Notify(height)
格特海德
GetHeader,用于获取区块的标题,参数是区块链中区块的序列号。 一个例子:
from ontology.interop.System.Runtime import Notify from ontology.interop.System.Blockchain import GetHeader def demo(): block_height=10 header=GetHeader(block_height) Notify(header) return header
GetTransactionByHash
GetTransactionByHash用于通过事务哈希获取事务。 事务哈希作为字节数组格式的参数发送到
GetTransactionByHash 。 此功能的关键是将十六进制格式的事务哈希转换为字节数组格式的事务哈希。 这是重要的一步。 否则,您将得到一个错误,指示没有带有这种块哈希的块。 让我们以事务的十六进制哈希为例,将其转换为字节数组格式。 示例如下:
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
首先反转事务哈希:
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
开发人员可以使用SmartX提供的十六进制数字(小端)数字转换工具执行此步骤。
然后将结果转换为字节数组格式:
{0xc1,0x89,0x0c,0x4d,0x73,0x06,0x26,0xdf,0xaa,0x94,0x49,0x41,0x9d,0x66,0x25,0x05,0xea,0xb3,0xbd,0xa2,0xe1,0xf0,0x1f,0x89,0x46,0x3c,0xc1,0xa4,0xa3,0x0a,0x27,0x9f}
可以使用SmartX提供的字符串字节数组转换工具来完成此操作。 最后,将结果字节数组转换为类似的字符串:
\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f
以下是使用事务哈希处理事务的GetTransactionByHash函数的示例:
from ontology.interop.System.Blockchain import GetTransactionByHash def demo():
GetTransactionHeight
GetTransactionHeight用于通过事务散列获取事务的高度。 让我们从上面的示例中获取哈希值:
from ontology.interop.System.Blockchain import GetTransactionHeight def demo():
获取合同
开发人员可以使用GetContract函数通过合同哈希获取合同。 合同哈希转换过程是上述相应的交易哈希转换过程。
from ontology.interop.System.Blockchain import GetContract def demo():
获取块
GetBlock用于获取块。 有两种获取特定块的方法。
1.按块高度获取块: from ontology.interop.System.Blockchain import GetBlock def demo(): block=GetBlock(1408) return block
2.逐块获取哈希: from ontology.interop.System.Blockchain import GetBlock def demo(): block_hash=bytearray(b'\x16\xe0\xc5\x40\x82\x79\x77\x30\x44\xea\x66\xc8\xc4\x5d\x17\xf7\x17\x73\x92\x33\x6d\x54\xe3\x48\x46\x0b\xc3\x2f\xe2\x15\x03\xe4') block=GetBlock(block_hash)
2.如何使用Block API
Block API中有三个可用函数:
GetTransactions ,
GetTransactionCount和
GetTransactionByIndex 。 我们将它们一一分类。
Gettransactioncount
GetTransactionCount用于获取给定块的事务数。
from ontology.interop.System.Blockchain import GetBlock from ontology.interop.System.Block import GetTransactionCount def demo(): block=GetBlock(1408) count=GetTransactionCount(block) return count
获取交易
开发人员可以使用GetTransactions函数获取此块中的所有事务。
from ontology.interop.System.Blockchain import GetBlock from ontology.interop.System.Block import GetTransactions def demo(): block=GetBlock(1408) txs=GetTransactions(block) return txs
GetTransactionByIndex
GetTransactionByIndex用于获取此块中的特定事务。
from ontology.interop.System.Blockchain import GetBlock from ontology.interop.System.Block import GetTransactionByIndex def demo(): block=GetBlock(1408) tx=GetTransactionByIndex(block,0)
完整指南可在我们的
GitHub上找到 。
后记
Blockchain&Block API是智能合约不可缺少的一部分,因为您可以使用它们来请求智能合约中的区块链数据和区块数据。 在以下文章中,我们将讨论如何使用其余的API,并找出它们与Ontology区块链的交互。
这篇文章是由Hashrate&Shares专为OntologyRussia翻译的。 点击
您是开发人员吗? 加入我们的
Discord技术社区。 此外,请访问我们网站上
的开发人员中心 ,您可以在其中找到开发人员工具,文档等。
本体论