如何在本體網絡上用Python編寫智能合約。 第 1 部分:區塊鍊和區塊 API

如何在本體網絡上用Python編寫智能合約。 第 1 部分:區塊鍊和區塊 API

這是使用智能合約開發工具在本體區塊鍊網絡上創建Python智能合約系列教程的第一部分。 智能X.

在本文中,我們將開始熟悉本體智能合約API。 本體智能合約API分為7個模塊:

  1. 區塊鍊和區塊 API,
  2. 運行時API,
  3. 存儲API,
  4. 原生API,
  5. 升級API,
  6. 執行引擎 API 和
  7. 靜態和動態調用 API。

區塊鍊和區塊API是本體智能合約系統的主要部分。 Blockchain API支持基本的區塊鏈查詢操作,例如獲取當前區塊高度,而Block API支持基本的區塊查詢操作,例如查詢給定區塊的交易數量。

讓我們開始吧!

首先,創建一個新合約 智能X然後按照以下說明進行操作。

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 block

2. 逐塊獲取哈希值:

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

完整的指南可以在我們的網站上找到 GitHub上.

後記

區塊鍊和區塊API是智能合約不可或缺的一部分,您可以使用它們來請求智能合約中的區塊鏈數據和區塊數據。 在以後的文章中,我們將討論如何使用其餘的 API,並了解它們如何與本體區塊鏈交互。

該文章由 Hashrate&Shares 的編輯特別為 OntologyRussia 翻譯。

你是開發者嗎? 加入我們的技術社區 不和. 另外,看看 開發者中心 在我們的網站上,您可以在其中找到開發人員工具、文檔等。

本體論

來源: www.habr.com

添加評論