Momwe mungalembe mgwirizano wanzeru ku Python pa netiweki ya Ontology. Gawo 3: Runtime API

Momwe mungalembe mgwirizano wanzeru ku Python pa netiweki ya Ontology. Gawo 3: Runtime API

Ili ndi gawo lachitatu pamndandanda wazolemba zamaphunziro zopanga makontrakitala anzeru ku Python pa netiweki ya Ontology blockchain. M’nkhani zam’mbuyomo tinazoloΕ΅erana nazo

  1. Blockchain & Block API
  2. API yosungirako.

Tsopano popeza muli ndi lingaliro la momwe mungatchulire API yoyenera yosungirako mukamapanga mgwirizano wanzeru pogwiritsa ntchito Python pa netiweki ya Ontology, tiyeni tipitilize kuphunzira kugwiritsa ntchito. Runtime API (Contract Execution API). Runtime API ili ndi ma API 8 ogwirizana omwe amapereka njira zofananira zogwirira ntchito ndikuthandizira omanga kupeza, kusintha, ndi kutsimikizira deta.

Pansipa pali kufotokozera mwachidule za data ya 8 API:

Momwe mungalembe mgwirizano wanzeru ku Python pa netiweki ya Ontology. Gawo 3: Runtime API

Tiyeni tiwone mwatsatanetsatane momwe tingagwiritsire ntchito deta ya 8 API. Izi zisanachitike, mutha kupanga mgwirizano watsopano mu chida cha Ontology smart contract development SmartX ndipo tsatirani malangizo omwe ali pansipa.

Momwe mungagwiritsire ntchito Runtime API

Pali njira ziwiri zotumizira Runtime API: ontology.interop.System.Runtime ΠΈ ontology.interop.Ontology.Runtime. Njira ya Ontology ili ndi ma API omwe angowonjezeredwa kumene. Mizere ili m'munsiyi imalowetsa deta ya API.

from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHash

Notify API

Ntchito ya Notify imawulutsa chochitika pa netiweki yonse. Muchitsanzo chomwe chili pansipa, ntchito ya Notify ibweza chingwe cha hex "hello word" ndikuchifalitsa pamaneti onse.

from ontology.interop.System.Runtime import Notify
def demo():
    Notify("hello world")

Mutha kuwona izi m'mabuku:

Momwe mungalembe mgwirizano wanzeru ku Python pa netiweki ya Ontology. Gawo 3: Runtime API

GetTime API

Ntchito ya GetTime imabweretsanso sitampu yamakono, yomwe imabwezeretsa nthawi ya Unix yomwe ntchitoyi idatchedwa. Chigawo cha muyeso ndi chachiwiri.

from ontology.interop.System.Runtime import GetTime
def demo():
    time=GetTime()
    return time # return a uint num

GetCurrentBlockHash API

Ntchito ya GetCurrentBlockHash imabwezera hashi ya block yomwe ilipo.

from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
def demo():
    block_hash = GetCurrentBlockHash()
    return block_hash

Seriize ndi Deserialize

Izi ndi ntchito ziwiri za serialization ndi deserialization. Ntchito ya Serialize imatembenuza chinthu kukhala chinthu cha bytearray, ndipo ntchito ya Deserialize imasintha mawonekedwe a bytearray kukhala chinthu choyambirira. Chitsanzo cha code pansipa chimasintha magawo omwe akubwera ndikuwasunga muzosungirako zokhazikika za mgwirizano. Imapezanso deta kuchokera kusungirako kosalekeza kwa mgwirizano ndikusintha kukhala chinthu choyambirira.

from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
from ontology.interop.System.Storage import Put, Get, GetContext

def Main(operation, args):
    if operation == 'serialize_to_bytearray':
        data = args[0]
        return serialize_to_bytearray(data)
    if operation == 'deserialize_from_bytearray':
        key = args[0]
        return deserialize_from_bytearray(key)
    return False


def serialize_to_bytearray(data):
    sc = GetContext()
    key = "1"
    byte_data = Serialize(data)
    Put(sc, key, byte_data)


def deserialize_from_bytearray(key):
    sc = GetContext()
    byte_data = Get(sc, key)
    data = Deserialize(byte_data)
    return data

Base58ToAddress ndi AddressToBase58

Ma adilesi awiriwa amagwira ntchito. Ntchito ya Base58ToAddress imatembenuza adilesi yosungidwa ya base58 kukhala adilesi ya bytearray, ndipo AddressToBase58 imatembenuza adilesi ya bytearray kukhala adilesi yosungidwa ya base58.

from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
def demo():
    base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
    addr=Base58ToAddress(base58_addr)
    Log(addr)
    base58_addr=AddressToBase58(addr)
    Log(base58_addr)

Onani Mboni

Ntchito ya CheckWitness(kuchokera kuAcct) ili ndi magwiridwe antchito awiri:

  • Tsimikizirani ngati woyimba foniyo akuchokera kuAcct. Ngati inde (ndiko kuti, kutsimikizira siginecha kwadutsa), ntchitoyo imabwerera.
  • Onani ngati chinthu chomwe chikuyitanira ntchito yomwe ilipo tsopano ndi mgwirizano. Ngati ndi mgwirizano ndipo ntchitoyo ikuchitidwa kuchokera ku mgwirizano, ndiye kuti chitsimikiziro chadutsa. Ndiye kuti, onetsetsani ngati fromAcct ndiye mtengo wobwerera wa GetCallingScriptHash(). Ntchito ya GetCallingScriptHash() ikhoza kutenga mtengo wa hashi wa mgwirizano wamakono.

GetCallingScriptHash():

Zambiri Guthub

from ontology.interop.System.Runtime import CheckWitness
from ontology.interop.Ontology.Runtime import Base58ToAddress
def demo():
    addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
    res=CheckWitness(addr)
    return res

Zambiri zitha kupezeka pa Guthub. M’nkhani yotsatira tidzafotokoza zimenezi Native APIkuti muphunzire kusamutsa katundu mu Ontology smart contracts.

Nkhaniyi idamasuliridwa ndi akonzi Hashrate & Shares makamaka Ontology Russia.

Kodi ndinu wopanga mapulogalamu? Lowani nawo gulu lathu laukadaulo ku Kusamvana. Komanso, yang'anani Developer Center Ontology kuti mupeze zida zambiri, zolemba, ndi zina zambiri.

Tsegulani ntchito za opanga. Tsekani ntchitoyi - pezani mphotho.

Ikani pulogalamu ya talente ya Ontology kwa ophunzira

Ontology

Webusaiti ya Ontology - GitHub - Kusamvana - Telegraph Russian - Twitter - Reddit

Source: www.habr.com

Kuwonjezera ndemanga