技术指南 | Python智能合约开发?看这一篇就够了
01 前言
在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的技术视点中,我们将正式开始讲述智能合约语法部分。
02 Blockchain API 使用方法
from ontology.interop.System.Blockchain import GetHeight, GetHeader
from ontology.interop.System.Runtime import Notify from ontology.interop.System.Blockchain import GetHeight def Main(operation): if operation == 'demo': return demo() return Falsedef demo(): height=GetHeight() Notify(height) # 打印height return height #在函数运行结束后返回height
2.2 GetHeader
2.3 GetTransactionByHash
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
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
首先,将该交易哈希反序得到:
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
{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}
\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f
GetTransactionByHash 函数通过交易哈希获取交易的例子如下:
from ontology.interop.System.Blockchain import GetTransactionByHash
def demo():
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
tx_hash=bytearray(b"\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")
tx=GetTransactionByHash(tx_hash)
return tx
2.5 GetContract
from ontology.interop.System.Blockchain import GetTransactionHeight
def demo():
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
tx_hash=bytearray(b"\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")
height=GetTransactionHeight(tx_hash)
return height
from ontology.interop.System.Blockchain import GetContract
def demo():
# contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"
contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")
contract=GetContract(contract_hash)
return contract
2.6 GetBlock
3.1
GetTransactionCount
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)
03 Block API 使用方法
Block API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。
3
.3 GetTransactionByIndex
from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionCount
def demo():
block=GetBlock(1408)
count=GetTransactionCount(block)
return count
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
04 后记
Blockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看和学习。