Le yinxalenye yokuqala kuthotho lwee-tutorials ekudaleni izivumelwano zePython smart kwinethiwekhi ye-Ontology blockchain usebenzisa isixhobo sophuhliso lwekhontrakthi ehlakaniphile.
Kweli nqaku, siza kuqala ukuqhelana kwethu ne-Ontology smart contract API. I-Ontology smart contract API yahlulwe yangamodyuli ezisi-7:
- Blockchain & Block API,
- ixesha lokusebenza API,
- yokugcina API,
- i-API yemveli,
- Phucula i-API,
- Execution Engine API kunye
- Static & Dynamic Call API.
I-Blockchain & Block API iyona nxalenye ephambili ye-Ontology ye-smart contract system. I-Blockchain API isekela imisebenzi yombuzo we-blockchain esisiseko, njengokufumana ukuphakama kwebhloko yangoku, ngelixa i-API yeBlock isekela imisebenzi yombuzo webhloko esisiseko, njengokubuza inani leentengiselwano kwibhloko enikiweyo.
Masiqalise!
Okokuqala, yenza isivumelwano esitsha
1. Indlela yokusebenzisa iBlockchain API
Amakhonkco kwimisebenzi yekontrakthi ehlakaniphile ayafana namakhonkco ePython. Ungangenisa imisebenzi ehambelanayo njengoko kufuneka. Umzekelo, le ngxelo ilandelayo yazisa umsebenzi we-GetHeight ukufumana ubude bebhloko yangoku, kunye nomsebenzi we-GetHeader ukufumana i-header yebhloko.
from ontology.interop.System.Blockchain import GetHeight, GetHeader
GetHeight
I-GetHeight isetyenziselwa ukufumana inombolo yokugqibela yokulandelelana kwebhloko kwi-blockchain, njengoko kubonisiwe kumzekelo ongezantsi. Kumzekelo wokugqibela, siya kushiya umsebenzi oPhambili ukuze kube lula, kodwa ungayongeza ukuba kuyimfuneko.
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
I-GetHeader isetyenziselwa ukufumana i-header yebhloko, ipharamitha yinombolo yesiriyali yebhloko kwibhloko. Umzekelo:
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
Fumana iTransactionByHash
I-GetTransactionByHash isetyenziselwa ukufumana intengiselwano nge-hash yentengiselwano. I-hash yentengiselwano ithunyelwe ku Fumana iTransactionByHash njengeeparamitha kwifomati ye-bytearray. Isitshixo kulo msebenzi kukuguqula i-hash yentengiselwano kwifomati ye-hex kwi-hash yentengiselwano kwi-bytearray format. Eli linyathelo elibalulekileyo. Ngaphandle koko, uya kufumana impazamo ebonisa ukuba akukho bloko enaloo block hash. Masithathe i-hash yentengiselwano kwifomati ye-hex njengomzekelo ukuyiguqulela kwifomati ye-bytearray. Umzekelo ukhangeleka ngolu hlobo:
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1
Okokuqala, buyisela umva i-hash yentengiselwano:
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
Abaphuhlisi banokufezekisa eli nyathelo ngokusebenzisa inombolo yeHex(endian encinci)Isixhobo sokuguqula inani esibonelelwe nguSmartX.
Emva koko uguqule isiphumo kwifomati ye-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}
Oku kunokwenziwa kusetyenziswa isixhobo sokuguqula iString Byte Array esinikezwe yiSmartX. Okokugqibela, guqula i-bytearray enesiphumo kumtya ofanayo:
xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f
Oku kulandelayo ngumzekelo we-GetTransactionByHash umsebenzi, othatha intengiselwano usebenzisa i-hash yentengiselwano:
from ontology.interop.System.Blockchain import GetTransactionByHash
def demo():
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
tx=GetTransactionByHash(tx_hash)
return tx
GetTransactionHeight
I-GetTransactionHeight isetyenziselwa ukufumana ubude bentengiselwano nge-hash yentengiselwano. Masithathe ihashi kulo mzekelo ungasentla:
from ontology.interop.System.Blockchain import GetTransactionHeight
def demo():
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
height=GetTransactionHeight(tx_hash)
return height
GetContract
Abaphuhlisi banokusebenzisa umsebenzi we-GetContract ukuze bafumane isivumelwano nge-hash yekhontrakthi. Inkqubo yokuguqulwa kwe-hash yekhontrakthi iyafana nenkqubo yokuguqula i-hash ekhankanywe ngasentla.
from ontology.interop.System.Blockchain import GetContract
def demo():
# contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"
contract_hash=bytearray(b"xfax98x96x21x32x32xbbxf0x9fx23x91xfaxefx95x9bxffxa5x75x1axd8")
contract=GetContract(contract_hash)
return contract
GetBlock
I-GetBlock isetyenziselwa ukufumana ibhloko. Kukho iindlela ezimbini zokufumana ibhloko ethile.
1. Fumana ibhloko ngobude bebhloko:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block=GetBlock(1408)
return block
2. Fumana ibhloko ngehashi yebhloko:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block_hash=bytearray(b'x16xe0xc5x40x82x79x77x30x44xeax66xc8xc4x5dx17xf7x17x73x92x33x6dx54xe3x48x46x0bxc3x2fxe2x15x03xe4')
block=GetBlock(block_hash)
2. Indlela yokusebenzisa iBlock API
Kukho imisebenzi emithathu ekhoyo kwiBlock API: Fumana iiTransactions, GetTransactionCountkwaye Fumana iTransactionByIndex. Siza kuziqhekeza nganye nganye.
GetTransactionCount
I-GetTransactionCount isetyenziselwa ukufumana inani lentengiselwano kwibhloko enikiweyo.
from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionCount
def demo():
block=GetBlock(1408)
count=GetTransactionCount(block)
return count
Fumana iiTransactions
Abaphuhlisi banokusebenzisa umsebenzi we-GetTransactions ukufumana zonke iintengiselwano kwibhloko enikiweyo.
from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactions
def demo():
block=GetBlock(1408)
txs=GetTransactions(block)
return txs
Fumana iTransactionByIndex
I-GetTransactionByIndex isetyenziselwa ukufumana intengiselwano ethile kwibhloko enikiweyo.
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
Isikhokelo esipheleleyo sinokufunyanwa kwethu
Emva kwegama
I-Blockchain kunye ne-Block API yinxalenye eyimfuneko yeekontraki ezihlakaniphile njengoko unokuzisebenzisa ukucela idatha ye-blockchain kunye ne-block data kwiikontrakthi ezihlakaniphile. Kumanqaku alandelayo, siza kuxubusha indlela yokusebenzisa ezinye ii-APIs kwaye sifumanise ukuba zidibana njani ne-Ontology blockchain.
Eli nqaku liguqulelwe ngabahleli beHashrate&Shares ingakumbi iOntologyRussia.
khala
Ngaba ungumphuhlisi? Joyina uluntu lwethu lwetekhnoloji ku
Intology
Iwebhusayithi yeOntology GitHub /Ingxoxo - yocingo
IsiNgesi /IsiRashiya Twitter /Reddit
umthombo: www.habr.com