Bii o ṣe le kọ iwe adehun ọlọgbọn ni Python lori nẹtiwọọki Ontology. Apá 1: Blockchain & Àkọsílẹ API

Bii o ṣe le kọ iwe adehun ọlọgbọn ni Python lori nẹtiwọọki Ontology. Apá 1: Blockchain & Àkọsílẹ API

Eyi ni apakan akọkọ ninu lẹsẹsẹ awọn ikẹkọ lori ṣiṣẹda awọn iwe adehun smati Python lori nẹtiwọọki blockchain Ontology nipa lilo ohun elo idagbasoke adehun ọlọgbọn. SmartX.

Ninu nkan yii, a yoo bẹrẹ ifaramọ wa pẹlu Ontology smart guide API. Ontology smart guide API ti pin si awọn modulu 7:

  1. Blockchain & Idina API,
  2. API asiko isise,
  3. API ipamọ,
  4. API abinibi,
  5. API igbesoke,
  6. Ipaniyan Engine API ati
  7. Aimi & Yiyi ipe API.

Blockchain & Àkọsílẹ API jẹ apakan akọkọ ti eto adehun ijafafa Ontology. Blockchain API ṣe atilẹyin awọn iṣẹ ibeere blockchain ipilẹ, gẹgẹbi gbigba giga bulọọki lọwọlọwọ, lakoko ti Block API ṣe atilẹyin awọn iṣẹ ibeere bulọọki ipilẹ, gẹgẹbi ibeere nọmba awọn iṣowo fun bulọki kan.

Jẹ ki a bẹrẹ!

Ni akọkọ, ṣẹda adehun tuntun ni SmartXati ki o si tẹle awọn ilana ni isalẹ.

1. Bii o ṣe le lo Blockchain API

Awọn ọna asopọ si awọn iṣẹ adehun ijafafa jẹ aami si awọn ọna asopọ Python. O le tẹ awọn iṣẹ ti o baamu sii bi o ṣe nilo. Fun apẹẹrẹ, alaye atẹle n ṣafihan iṣẹ GetHeight kan lati gba giga bulọọki lọwọlọwọ, ati iṣẹ GetHeader kan lati gba akọsori bulọọki naa.

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

GetHeight

GetHeight ti wa ni lilo lati gba awọn ti o kẹhin Àkọsílẹ nọmba ọkọọkan ninu blockchain, bi han ninu awọn apẹẹrẹ ni isalẹ. Ninu apẹẹrẹ ti o kẹhin, a yoo fi iṣẹ akọkọ silẹ fun irọrun, ṣugbọn o le ṣafikun rẹ ti o ba jẹ dandan.

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

GbaHeader

GetHeader ni a lo lati gba akọsori Àkọsílẹ, paramita naa jẹ nọmba ni tẹlentẹle ti Àkọsílẹ ninu blockchain. Apeere:

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

GbaTransactionByHash

GetTransactionByHash ni a lo lati gba idunadura kan nipasẹ hash idunadura kan. Hash idunadura naa ni a firanṣẹ si GbaTransactionByHash bi sile ni bytearray kika. Bọtini si iṣẹ yii ni lati ṣe iyipada hash idunadura ni ọna kika hex si hash idunadura ni ọna kika bytearray. Eyi jẹ igbesẹ pataki kan. Bibẹẹkọ, iwọ yoo gba aṣiṣe ti o tọka si pe ko si bulọki pẹlu hash Àkọsílẹ yẹn. Jẹ ki a mu hash idunadura ni ọna kika hex gẹgẹbi apẹẹrẹ lati yi pada si ọna kika bytearray. Apẹẹrẹ kan dabi eyi:

9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1

Ni akọkọ, yiyipada hash idunadura naa:

c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279

Awọn olupilẹṣẹ le ṣaṣeyọri igbesẹ yii nipa lilo Nọmba Hex(endian kekere) Ohun elo iyipada nọmba ti a pese nipasẹ SmartX.

Lẹhinna yi abajade pada si ọna kika 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}

Eyi le ṣee ṣe nipa lilo ohun elo iyipada Okun Byte Array ti a pese nipasẹ SmartX. Nikẹhin, yi iyipada bytearray ti o yọrisi pada si okun ti o jọra:

xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f

Atẹle jẹ apẹẹrẹ ti iṣẹ GetTransactionByHash ti o gba idunadura kan nipa lilo hash ti idunadura naa:

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 ni a lo lati gba giga idunadura nipasẹ hash idunadura naa. Jẹ ki a mu hash lati apẹẹrẹ loke:

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

Gba Contract

Awọn olupilẹṣẹ le lo iṣẹ GetContract lati gba adehun nipasẹ hash ti adehun naa. Ilana iyipada hash adehun jẹ kanna bi ilana iyipada hash idunadura ti a mẹnuba loke.

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

GbaBlock

A lo GetBlock lati gba idina kan. Awọn ọna meji lo wa lati gba bulọọki kan pato.

1. Gba idina nipasẹ giga bulọọki:

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

2. Gba bulọọki nipasẹ hash Àkọsílẹ:

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

2. Bawo ni lati lo Àkọsílẹ API

Awọn iṣẹ mẹta ti o wa ninu API Block: Gba Awọn iṣowo, GetTransactionCountati GetTransactionByIndex. A yoo fọ wọn lulẹ ni ọkọọkan.

GetTransactionCount

GetTransactionCount ni a lo lati gba nọmba awọn iṣowo fun bulọọki ti a fun.

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

Gba Awọn iṣowo

Awọn olupilẹṣẹ le lo iṣẹ GetTransactions lati gba gbogbo awọn iṣowo ni bulọọki ti a fun.

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 ni a lo lati gba idunadura kan pato ni bulọọki ti a fun.

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

Itọsọna pipe ni a le rii lori wa GitHub.

Lẹhin Ọrọ

Blockchain & Block API jẹ apakan ti ko ṣe pataki ti awọn adehun ijafafa bi o ṣe le lo wọn lati beere data blockchain ati dina data ni awọn adehun ijafafa. Ninu awọn nkan atẹle, a yoo jiroro bi a ṣe le lo iyoku awọn API ati rii bii wọn ṣe nlo pẹlu blockchain Ontology.

Nkan naa jẹ itumọ nipasẹ awọn olootu ti Hashrate&Shares paapaa fun OntologyRussia. kigbe

Ṣe o jẹ olupilẹṣẹ bi? Darapọ mọ agbegbe imọ-ẹrọ wa ni Iwa. Bakannaa, wo Olùgbéejáde Center lori oju opo wẹẹbu wa, nibiti o ti le rii awọn irinṣẹ idagbasoke, iwe, ati diẹ sii.

Imọlẹ

orisun: www.habr.com

Fi ọrọìwòye kun