Pulogalamu ya ProHoster > Blog > Ulamuliro > Momwe mungalembe mgwirizano wanzeru ku Python pa netiweki ya Ontology. Gawo 1: Blockchain & Block API
Momwe mungalembe mgwirizano wanzeru ku Python pa netiweki ya Ontology. Gawo 1: Blockchain & Block API
Ili ndi gawo loyamba la mndandanda wankhani zophunzitsa zopanga makontrakitala anzeru ku Python pa netiweki ya Ontology blockchain pogwiritsa ntchito chida chokulitsa mgwirizano wanzeru. SmartX.
M'nkhaniyi, tiyamba kudziwana ndi Ontology smart contract API. The Ontology smart contract API yagawidwa m'ma module 7:
Blockchain & Block API,
Runtime API
API yosungirako
Native API
Upgrade API,
Execution Engine API ndi
Static & Dynamic Call API.
Blockchain & Block API ndiye gawo lalikulu la Ontology smart contract system. Blockchain API imathandizira ntchito zoyambira zamafunso za blockchain, monga kupeza kutalika kwa chipika, pomwe Block API imathandizira ntchito zamafunso a blockchain, monga kufunsira kuchuluka kwa zomwe zachitika pa block yomwe wapatsidwa.
Tiyeni tiyambe!
Kuti muyambe, pangani mgwirizano watsopano mu SmartX, ndiyeno tsatirani malangizo ali mβmunsiwa.
1. Momwe mungagwiritsire ntchito Blockchain API
Zolemba za Smart contract ntchito ndizofanana ndi zolemba za Python. Mutha kuyika magwiridwe antchito ngati pakufunika. Mwachitsanzo, mawu otsatirawa amayambitsa GetHeight, ntchito yopezera kutalika kwa chipika, ndi GetHeader, ntchito yopezera mutu wa block.
from ontology.interop.System.Blockchain import GetHeight, GetHeader
GetHeight
GetHeight imagwiritsidwa ntchito kuti mupeze nambala yomaliza ya blockchain mu blockchain, monga tawonera mu chitsanzo pansipa. Mu chitsanzo chomaliza, kuti zitheke, tidzalumpha Ntchito Yaikulu, koma mukhoza kuwonjezera ngati kuli kofunikira.
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 imagwiritsidwa ntchito kupeza mutu wa block; parameter ndiye nambala ya blockchain mu blockchain. Chitsanzo:
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 imagwiritsidwa ntchito kubweza zomwe zachitika pogwiritsa ntchito hashi. Hashi yobwereketsa imatumizidwa ku GetTransactionByHash monga magawo mu mawonekedwe a bytearray. Chinsinsi cha ntchitoyi ndikutembenuza ma hashi mumtundu wa hex kukhala mawonekedwe a bytearray. Iyi ndi sitepe yofunika. Apo ayi mudzapeza cholakwika chosonyeza kuti palibe chipika chokhala ndi block hash. Tiyeni titenge ma hashi mumtundu wa hex ngati chitsanzo kuti tisinthe kukhala mtundu wa bytearray. Chitsanzo chikuwoneka motere:
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 imagwiritsidwa ntchito kubweza zomwe zachitika mu block yomwe wapatsidwa.
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