Cumu scrive un cuntrattu intelligente in Python nantu à a reta di Ontology. Parte 1: Blockchain & Block API

Cumu scrive un cuntrattu intelligente in Python nantu à a reta di Ontology. Parte 1: Blockchain & Block API

Questa hè a prima parte in una seria di tutoriali nantu à a creazione di cuntratti intelligenti Python nantu à a reta di blockchain di Ontology utilizendu l'uttellu di sviluppu di cuntratti intelligenti. SmartX.

In questu articulu, avemu da principià a nostra cunniscenza cù l'API di cuntrattu intelligenti Ontology. L'API di Ontology smart contract hè divisa in 7 moduli:

  1. Blockchain è Block API,
  2. runtime API,
  3. API di almacenamiento,
  4. API nativa,
  5. Upgrade API,
  6. Execution Engine API è
  7. API di chiamata statica è dinamica.

Blockchain & Block API hè a parte principale di u sistema di cuntrattu intelligenti Ontology. L'API Blockchain supporta l'operazioni di basa di quistione di blockchain, cum'è ottene l'altezza di u bloccu attuale, mentre chì l'API Block supporta l'operazioni basi di quistione di bloccu, cum'è l'interrogazione di u nùmeru di transazzione per un bloccu determinatu.

Cuminciamu !

Prima, crea un novu cuntrattu SmartXe poi seguitate i instructions sottu.

1. Cumu utilizà l'API Blockchain

I ligami per e funzioni intelligenti di u cuntrattu sò identici à i ligami Python. Pudete entre e funzioni currispundenti cum'è necessariu. Per esempiu, a seguente dichjarazione introduce una funzione GetHeight per uttene l'altezza di u bloccu attuale, è una funzione GetHeader per uttene l'intestazione di u bloccu.

from ontology.interop.System.Blockchain import GetHeight, GetHeader

Get Height

GetHeight hè utilizatu per uttene l'ultimu numaru di sequenza di bloccu in u blockchain, cum'è mostra in l'esempiu sottu. In l'ultimu esempiu, omettemu a funzione Main per comodità, ma pudete aghjunghje se ne necessariu.

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

GetHeader hè utilizatu per uttene l'intestazione di u bloccu, u paràmetru hè u numeru di serie di u bloccu in u bloccu. Esempiu:

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

GetTransactionByHash hè utilizatu per ottene una transazzione via un hash di transazzione. L'hash di transazzione hè mandatu à GetTransactionByHash cum'è paràmetri in formatu bytearray. A chjave per sta funzione hè di cunvertisce l'hash di transazzione in formatu hex à l'hash di transazzione in formatu bytearray. Questu hè un passu impurtante. Altrimenti, averete un errore chì indica chì ùn ci hè micca un bloccu cù quellu hash di bloccu. Pigliemu l'hash di transazzione in u formatu hex cum'è un esempiu per cunvertisce in u formatu bytearray. Un esempiu pare cusì:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

Prima, inverse l'hash di transazzione:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

I sviluppatori ponu rializà stu passu utilizendu l'uttellu di cunversione Numeru Hex (little endian) furnitu da SmartX.

Allora cunvertisce u risultatu in u formatu 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}

Questu pò esse fattu cù u strumentu di cunversione String Byte Array furnitu da SmartX. Infine, cunvertisce u bytearray risultatu in una stringa simili:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

U seguitu hè un esempiu di una funzione GetTransactionByHash chì piglia una transazzione cù l'hash di a transazzione:

from ontology.interop.System.Blockchain import GetTransactionByHash
def demo():
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    
    tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
    tx=GetTransactionByHash(tx_hash)
    return tx

GetTransactionHeight

GetTransactionHeight hè utilizatu per uttene l'altitudine di transazzione via l'hash di transazzione. Pigliemu l'hash da l'esempiu sopra:

from ontology.interop.System.Blockchain import  GetTransactionHeight
def demo():
    #   tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    
    tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
    height=GetTransactionHeight(tx_hash)
    return height

GetContract

I sviluppatori ponu utilizà a funzione GetContract per ottene un cuntrattu via l'hash di u cuntrattu. U prucessu di cunversione di l'hash di u cuntrattu hè u listessu cum'è u prucessu di cunversione di l'hash di transazzione mintuatu sopra.

from ontology.interop.System.Blockchain import GetContract
def demo():
    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"    
    contract_hash=bytearray(b"xfax98x96x21x32x32xbbxf0x9fx23x91xfaxefx95x9bxffxa5x75x1axd8")
    contract=GetContract(contract_hash)
    return contract

GetBlock

GetBlock hè adupratu per uttene un bloccu. Ci hè duie manere di ottene un bloccu specificu.

1. Pigliate l'altezza di bloccu per bloccu:

from ontology.interop.System.Blockchain import GetBlock
def demo():
    block=GetBlock(1408)
    return block

2. Ottene un bloccu per bloccu hash:

from ontology.interop.System.Blockchain import GetBlock
def demo():    
    block_hash=bytearray(b'x16xe0xc5x40x82x79x77x30x44xeax66xc8xc4x5dx17xf7x17x73x92x33x6dx54xe3x48x46x0bxc3x2fxe2x15x03xe4')
    block=GetBlock(block_hash)

2. Cumu aduprà Block API

Ci sò trè funzioni dispunibili in l'API Block: GetTransactions, GetTransactionCounte GetTransactionByIndex. Li sparteremu unu à unu.

GetTransactionCount

GetTransactionCount hè utilizatu per uttene u numeru di transazzione per un bloccu datu.

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

I sviluppatori ponu utilizà a funzione GetTransactions per uttene tutte e transazzione in un bloccu datu.

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

GetTransactionByIndex hè utilizatu per uttene una transazzione specifica in un bloccu datu.

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

Una guida cumpleta pò esse truvata nantu à u nostru GitHub.

Afterword

L'API Blockchain & Block hè una parte indispensabile di i cuntratti intelligenti cum'è pudete aduprà per dumandà dati di blockchain è bluccà dati in cuntratti intelligenti. In l'articuli seguenti, discuteremu cumu utilizà u restu di l'API è scopre cumu interagisce cù l'Ontology blockchain.

L'articulu hè statu traduttu da l'editori di Hashrate&Shares in particulare per OntologyRussia. pienghje

Sò un sviluppatore? Unisci à a nostra cumunità tecnologica à Discord. Inoltre, fate un ochju Centru di sviluppatori nant'à u nostru situ web, induve pudete truvà strumenti di sviluppatore, documentazione è più.

Ontologia

Source: www.habr.com

Add a comment