
这是关于使用智能合约开发工具在本体区块链网络上用Python创建智能合约的系列教育文章的第一部分 .
在本文中,我们将开始熟悉本体智能合约API。 本体智能合约API分为7个模块:
- 区块链和区块 API,
- 运行时API
- 存储API
- 原生API
- 升级API,
- 执行引擎 API 和
- 静态和动态调用 API。
区块链和区块API是本体智能合约系统的核心部分。 Blockchain API支持基本的区块链查询操作,例如获取区块的当前高度,而Block API支持基本的区块查询操作,例如查询给定区块的交易数量。
让我们开始吧!
首先,创建一个新合约 ,然后按照以下说明进行操作。
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) # print height
return height #return height after running the function获取标题
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 用于使用交易哈希来检索交易。 交易哈希被发送到 通过哈希获取交易 作为字节数组格式的参数。 该函数的关键是将十六进制格式的交易哈希转换为字节数组格式的交易哈希。 这是重要的一步。 否则,您将收到一条错误消息,指示不存在具有该块哈希的块。 我们以十六进制格式的交易哈希为例,将其转换为字节数组格式。 该示例如下所示:
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1首先反转交易哈希:
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279开发者可以使用SmartX提供的Hex Number(little endian)数字转换工具来完成此步骤。
然后将结果转换为字节数组格式:
{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 提供的字符串字节数组转换工具来完成。 最后,将生成的字节数组转换为字符串,如下所示:
xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f下面是 GetTransactionByHash 函数的示例,它通过交易哈希获取交易:
from ontology.interop.System.Blockchain import GetTransactionByHash
def demo():
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
tx=GetTransactionByHash(tx_hash)
return tx获取交易高度
GetTransactionHeight 用于使用交易哈希来获取交易高度。 让我们从上面的例子中获取哈希值:
from ontology.interop.System.Blockchain import GetTransactionHeight
def demo():
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
height=GetTransactionHeight(tx_hash)
return height获取合约
开发人员可以使用 GetContract 函数通过合约哈希来检索合约。 合约哈希转换过程与上面提到的交易哈希转换过程相同。
from ontology.interop.System.Blockchain import GetContract
def demo():
# contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"
contract_hash=bytearray(b"xfax98x96x21x32x32xbbxf0x9fx23x91xfaxefx95x9bxffxa5x75x1axd8")
contract=GetContract(contract_hash)
return contract获取块
GetBlock 用于获取块。 有两种方法可以获取特定块。
1. 按块高度获取块:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block=GetBlock(1408)
return block2. 逐块获取哈希值:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block_hash=bytearray(b'x16xe0xc5x40x82x79x77x30x44xeax66xc8xc4x5dx17xf7x17x73x92x33x6dx54xe3x48x46x0bxc3x2fxe2x15x03xe4')
block=GetBlock(block_hash)2. 如何使用区块API
Block API 中有 XNUMX 个可用函数: 获取交易, 获取交易计数和 按索引获取交易。 我们将一一整理。
获取交易计数
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 用于检索给定块中的特定交易。
from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionByIndex
def demo():
block=GetBlock(1408)
tx=GetTransactionByIndex(block,0) # index starts from 0.
return tx完整的指南可以在我们的网站上找到 .
后记
区块链和区块API是智能合约中不可或缺的一部分,因为您可以使用它来查询智能合约中的区块链数据和区块数据。 在以后的文章中,我们将讨论如何使用其余的 API 并了解它们如何与本体区块链交互。
该文章由 Hashrate&Shares 的编辑特别为 OntologyRussia 翻译。
你是开发者吗? 加入我们的技术社区 . 另外,看看 在我们的网站上,您可以在其中找到开发人员工具、文档等。
本体论
- /
- Telegram /
- /
来源: habr.com
