1 前言在之前的技術視點文章中,我們介紹了目前本體主網支持的智能合約體系以及相應的智能合約開發工具 SmartX。很多小伙伴都想上手練一練
1. 前言
在之前的技術視點文章中,我們介紹了目前本體主網支持的智能合約體系以及相應的智能合約開發工具 SmartX。很多小伙伴都想上手練一練。在本期的本體技術視點中,我們將正式開始講述智能合約語法部分。
本體的智能合約 API 分為7個模塊,分別是 Blockchain & Block API、Runtime API、Storage API、Native API、Upgrade API、Execution Engine API 以及 Static & Dynamic Call API。本期我們將介紹 Blockchain & Block API,這是本體智能合約體系中最基礎的部分。其中,Blockchain API 支持基本的區塊鏈查詢操作,如獲取當前塊高等;Block API 支持基本的區塊查詢操作,如查詢指定區塊交易數等。同時,文末將提供視頻講解。
在這之前,小伙伴們可以在本體智能合約開發工具 SmartX 中新建一個合約,跟著我們進行操作。
2. Blockchain API 使用方法
智能合約函數的引用與 Python 的引用如出一轍。開發者可以根據需要引入相應的函數。例如,下面語句引入了獲取當前最新塊高函數 GetHeight 和獲取區塊頭函數 GetHeader。
from ontology.interop.System.Blockchain import GetHeight, GetHeader
2.1 GetHeight
開發者可以使用 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) # 打印height
return height #在函數運行結束后返回height
2.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
2.3 GetTransactionByHash
開發者可以使用 GetTransactionByHash 函數通過交易哈希獲取交易。交易哈希以 bytearray 的格式,作為參數傳入 GetTransactionByHash。這個函數的關鍵在于如何轉換將十六進制格式的交易哈希轉變為 bytearray 格式的交易哈希。
我們以16進制格式的交易哈希為例,實現將十六進制格式的交易哈希轉變為 bytearray 格式的交易哈希。示例哈希如下:
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
首先,將該交易哈希反序得到:
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
開發者可以通過 SmartX 提供的轉換工具 Hex Number(little endian) <--> Number 實現這一步。
然后,將其轉成 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}
開發者可以通過 SmartX 提供的轉換工具 String <--> Byte Array 實現這一步。
最后,將得到的 bytearray 轉換成相應的字符串:
\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():
# 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
開發者可以使用 GetTransactionHeight 函數通過交易哈希獲取交易高度。我們還是以上個例子中的哈希為例:
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
開發者可以使用 GetContract 函數通過合約哈希獲取合約。其中,合約哈希的轉換過程與上面講到的交易哈希轉換過程一致。
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
開發者可以使用 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)
3. Block API 使用方法
Block API 中可供引用的函數有三個,它們分別是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我們依次介紹下這三個函數。
3.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
3.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
3.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) # index starts from 0.
return tx
04 后記
Blockchain & Block API 在智能合約中起到查詢區塊鏈數據和區塊數據的作用,是智能合約最不可缺少的一部分。(Sheldon)
關鍵詞: 智能合約API Blockchain API 查詢