
这是先前在Ontology Medium博客上发布的官方教程
激动地将其发布给Habr读者。 随时提出任何相关问题,并为教程材料提出更好的格式
前言
在本文中,我们将开始介绍本体的智能合约API。 本体的智能合约API分为7个模块:
在本文中,我们将介绍
Blockchain&Block API ,它是Ontology智能合约系统的最基本组成部分。 Blockchain API支持基本的区块链查询操作,例如获取当前区块高度,而Block API支持基本的区块查询操作,例如查询给定区块的交易数量。
让我们开始吧!首先,在
SmartX中创建新合同,然后按照以下说明进行操作。
1.如何使用区块链API
智能合约功能的引用与Python的引用相同。 开发人员可以根据需要引入适当的功能。 例如,以下语句引入了
GetHeight (获取当前块高度的函数)和
GetHeader (获取
块头的函数)。
from ontology.interop.System.Blockchain import GetHeight, GetHeader
1.1。 格高
您可以使用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)
1.2 GetHeader
您可以使用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
1.3 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提供的转换工具
String <-> Byte Array进行此操作。 最后,将结果字节数组转换为相应的字符串:
\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():
1.4 GetTransactionHeight
开发人员可以使用
GetTransactionHeight函数通过事务哈希获取事务高度。 让我们在上面的示例中使用哈希:
from ontology.interop.System.Blockchain import GetTransactionHeight def demo():
1.5获取合同
您可以使用GetContract函数通过合同哈希获取合同。 合约哈希的转换过程与上述交易哈希的转换过程一致。
from ontology.interop.System.Blockchain import GetContract def demo():
1.6 GetBlock
您可以使用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 。 我们将一一介绍。
2.1 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
2.2 GetTransactions
您可以使用
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
2.3 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来探索它们与本体链的交互。
您是开发人员吗? 确保您已加入
Discord上的我们的技术社区。 另外,请访问我们网站上的
开发人员中心,在那里您可以找到开发人员工具,文档等。
在其他地方找到本体
本体网站GitHub /
不和谐电报(
英文 /
俄文 )
Twitter /
Reddit /