1 前言在之前的技術(shù)視點(diǎn)文章中,我們介紹了目前本體主網(wǎng)支持的智能合約體系以及相應(yīng)的智能合約開(kāi)發(fā)工具 SmartX。很多小伙伴都想上手練一練
1. 前言
在之前的技術(shù)視點(diǎn)文章中,我們介紹了目前本體主網(wǎng)支持的智能合約體系以及相應(yīng)的智能合約開(kāi)發(fā)工具 SmartX。很多小伙伴都想上手練一練。在本期的本體技術(shù)視點(diǎn)中,我們將正式開(kāi)始講述智能合約語(yǔ)法部分。
本體的智能合約 API 分為7個(gè)模塊,分別是 Blockchain & Block API、Runtime API、Storage API、Native API、Upgrade API、Execution Engine API 以及 Static & Dynamic Call API。本期我們將介紹 Blockchain & Block API,這是本體智能合約體系中最基礎(chǔ)的部分。其中,Blockchain API 支持基本的區(qū)塊鏈查詢操作,如獲取當(dāng)前塊高等;Block API 支持基本的區(qū)塊查詢操作,如查詢指定區(qū)塊交易數(shù)等。同時(shí),文末將提供視頻講解。
在這之前,小伙伴們可以在本體智能合約開(kāi)發(fā)工具 SmartX 中新建一個(gè)合約,跟著我們進(jìn)行操作。
2. Blockchain API 使用方法
智能合約函數(shù)的引用與 Python 的引用如出一轍。開(kāi)發(fā)者可以根據(jù)需要引入相應(yīng)的函數(shù)。例如,下面語(yǔ)句引入了獲取當(dāng)前最新塊高函數(shù) GetHeight 和獲取區(qū)塊頭函數(shù) GetHeader。
from ontology.interop.System.Blockchain import GetHeight, GetHeader
2.1 GetHeight
開(kāi)發(fā)者可以使用 GetHeight 來(lái)獲取當(dāng)前最新塊高,具體例子如下。在后面的例子中,為了節(jié)省空間,我們將省略 Main 函數(shù),小伙伴在練習(xí)的時(shí)候可以根據(jù)需要加入。
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) # 打印height
return height #在函數(shù)運(yùn)行結(jié)束后返回height
2.2 GetHeader
開(kāi)發(fā)者可以使用 GetHeader 來(lái)獲取區(qū)塊頭,參數(shù)是某個(gè)塊的塊高。具體例子如下:
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
2.3 GetTransactionByHash
開(kāi)發(fā)者可以使用 GetTransactionByHash 函數(shù)通過(guò)交易哈希獲取交易。交易哈希以 bytearray 的格式,作為參數(shù)傳入 GetTransactionByHash。這個(gè)函數(shù)的關(guān)鍵在于如何轉(zhuǎn)換將十六進(jìn)制格式的交易哈希轉(zhuǎn)變?yōu)?bytearray 格式的交易哈希。
我們以16進(jìn)制格式的交易哈希為例,實(shí)現(xiàn)將十六進(jìn)制格式的交易哈希轉(zhuǎn)變?yōu)?bytearray 格式的交易哈希。示例哈希如下:
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
首先,將該交易哈希反序得到:
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
開(kāi)發(fā)者可以通過(guò) SmartX 提供的轉(zhuǎn)換工具 Hex Number(little endian) <--> Number 實(shí)現(xiàn)這一步。
然后,將其轉(zhuǎn)成 bytearray 格式:
{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}
開(kāi)發(fā)者可以通過(guò) SmartX 提供的轉(zhuǎn)換工具 String <--> Byte Array 實(shí)現(xiàn)這一步。
最后,將得到的 bytearray 轉(zhuǎn)換成相應(yīng)的字符串:
\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 函數(shù)通過(guò)交易哈希獲取交易的例子如下:
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.4 GetTransactionHeight
開(kāi)發(fā)者可以使用 GetTransactionHeight 函數(shù)通過(guò)交易哈希獲取交易高度。我們還是以上個(gè)例子中的哈希為例:
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
2.5 GetContract
開(kāi)發(fā)者可以使用 GetContract 函數(shù)通過(guò)合約哈希獲取合約。其中,合約哈希的轉(zhuǎn)換過(guò)程與上面講到的交易哈希轉(zhuǎn)換過(guò)程一致。
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
開(kāi)發(fā)者可以使用 GetBlock 函數(shù)獲取區(qū)塊。有兩種方法可以獲取指定區(qū)塊:
1. 通過(guò)塊高獲取區(qū)塊:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block=GetBlock(1408)
return block
2. 通過(guò)區(qū)塊哈希獲取區(qū)塊:
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)
3. Block API 使用方法
Block API 中可供引用的函數(shù)有三個(gè),它們分別是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我們依次介紹下這三個(gè)函數(shù)。
3.1 GetTransactionCount
開(kāi)發(fā)者可以使用 GetTransactionCount 函數(shù)獲取指定區(qū)塊的交易數(shù)量。
from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionCount
def demo():
block=GetBlock(1408)
count=GetTransactionCount(block)
return count
3.2 GetTransactions
開(kāi)發(fā)者可以使用 GetTransactions 函數(shù)獲取獲取指定區(qū)塊的所有交易。
from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactions
def demo():
block=GetBlock(1408)
txs=GetTransactions(block)
return txs
3.3 GetTransactionByIndex
開(kāi)發(fā)者可以使用 GetTransactionByIndex 函數(shù)獲取指定區(qū)塊的指定交易。
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 在智能合約中起到查詢區(qū)塊鏈數(shù)據(jù)和區(qū)塊數(shù)據(jù)的作用,是智能合約最不可缺少的一部分。(Sheldon)
關(guān)鍵詞: 智能合約API Blockchain API 查詢