Be a crypto hero in the market by logging in

Don't you have an account? Sign in

Token price

  • BTC

    11,978,444.5KRW

    2.6%

  • ETH

    329,007.7KRW

    3.9%

  • XRP

    340.8KRW

    2.2%

  • BCH

    479,503.0KRW

    6.1%

  • BSV

    353,919.9KRW

    4.5%

  • USDT

    1,208.0KRW

    -0.1%

  • EOS

    5,240.3KRW

    5.6%

  • BNB

    27,591.1KRW

    3.8%

  • LTC

    54,759.1KRW

    -0.5%

  • XTZ

    4,185.5KRW

    3.4%

  • ADA

    73.9KRW

    4.8%

  • XLM

    88.6KRW

    5.8%

  • XMR

    102,186.9KRW

    6.7%

  • TRX

    25.5KRW

    5.0%

  • ETC

    11,803.2KRW

    2.6%

  • HT

    5,729.4KRW

    4.6%

  • NEO

    17,205.0KRW

    3.3%

  • DASH

    129,828.1KRW

    2.7%

  • ATOM

    5,803.4KRW

    -3.5%

  • HEDG

    3,432.4KRW

    -0.1%

  • MKR

    790,203.2KRW

    1.7%

  • ZEC

    76,101.4KRW

    3.3%

  • ONT

    1,071.8KRW

    4.6%

  • XEM

    73.6KRW

    0.6%

  • USDC

    1,206.0KRW

    -0.1%

  • BAT

    329.9KRW

    0.7%

  • VET

    8.3KRW

    3.1%

  • DOGE

    3.2KRW

    2.6%

  • ALGO

    556.9KRW

    3.1%

  • QTUM

    3,009.7KRW

    3.9%

  • DCR

    26,329.5KRW

    4.6%

  • PAX

    1,205.3KRW

    -0.2%

  • ICX

    482.0KRW

    -1.8%

  • LSK

    1,915.7KRW

    11.8%

  • INB

    644.1KRW

    1.9%

  • BTG

    12,678.8KRW

    3.2%

  • ZRX

    345.5KRW

    1.9%

  • RVN

    38.2KRW

    3.8%

  • OMG

    1,371.4KRW

    2.4%

  • REP

    16,541.2KRW

    -0.8%

  • WAVES

    1,744.3KRW

    2.2%

  • BCD

    928.7KRW

    4.6%

  • TUSD

    1,205.7KRW

    -0.2%

  • LINK

    485.1KRW

    2.0%

  • HBAR

    51.3KRW

    -6.6%

  • MONA

    2,481.7KRW

    2.9%

  • NANO

    1,177.0KRW

    1.8%

  • XIN

    327,292.8KRW

    4.7%

  • XIN

    327,292.8KRW

    4.7%

  • SC

    3.6KRW

    1.9%

  • THETA

    169.4KRW

    8.7%

  • CNX

    2,508.4KRW

    2.3%

  • NEXO

    238.3KRW

    3.4%

  • ENJ

    158.3KRW

    5.7%

  • KCS

    1,520.1KRW

    0.6%

  • BCN

    0.7KRW

    12.6%

  • BTM

    115.6KRW

    3.5%

  • BTM

    115.6KRW

    3.5%

  • VSYS

    58.2KRW

    -1.2%

  • KMD

    941.9KRW

    6.4%

  • DGB

    8.5KRW

    1.1%

  • BTS

    39.3KRW

    4.0%

  • MCO

    6,770.0KRW

    2.8%

  • HC

    2,325.4KRW

    1.7%

  • STEEM

    272.8KRW

    6.7%

  • IOST

    7.7KRW

    2.8%

  • ZIL

    9.4KRW

    4.5%

  • XVG

    5.6KRW

    2.4%

  • SXP

    1,387.3KRW

    -3.9%

  • ABBC

    147.9KRW

    3.8%

  • SNT

    23.6KRW

    11.5%

  • AION

    198.5KRW

    13.8%

  • AE

    249.3KRW

    4.5%

  • GNT

    76.5KRW

    3.8%

  • ARDR

    74.5KRW

    3.1%

  • XZC

    7,789.5KRW

    -1.8%

  • ELF

    135.5KRW

    4.2%

  • MANA

    69.8KRW

    12.0%

  • MATIC

    27.0KRW

    7.0%

  • WAX

    63.9KRW

    9.6%

  • QNT

    5,112.6KRW

    0.5%

  • RIF

    106.7KRW

    1.9%

  • LRC

    58.9KRW

    8.3%

  • NPXS

    0.2KRW

    2.2%

  • MAID

    125.0KRW

    7.0%

  • STRAT

    555.4KRW

    0.7%

  • POWR

    128.8KRW

    23.3%

  • GRIN

    1,413.5KRW

    1.5%

  • PAI

    33.8KRW

    -3.8%

  • PAI

    33.8KRW

    -3.8%

  • TOMO

    700.7KRW

    1.6%

  • BEAM

    868.6KRW

    2.8%

  • ETN

    4.8KRW

    1.8%

  • ANT

    1,487.4KRW

    1.1%

  • ELA

    2,606.3KRW

    3.4%

  • TRUE

    530.2KRW

    -0.3%

  • WTC

    683.6KRW

    7.0%

  • WAN

    363.1KRW

    10.5%

  • ARK

    315.5KRW

    0.9%

  • ORBS

    17.5KRW

    1.6%

  • FET

    52.0KRW

    2.3%

  • NAS

    657.5KRW

    4.2%

  • FCT

    3,752.5KRW

    6.1%

  • RDD

    1.1KRW

    -1.3%

  • PPT

    562.0KRW

    -4.0%

  • OCEAN

    84.6KRW

    3.7%

  • LOOM

    30.0KRW

    3.2%

  • BNT

    417.9KRW

    3.4%

  • CTXC

    111.8KRW

    2.9%

  • PIVX

    472.0KRW

    4.4%

  • DAI

    1,221.2KRW

    1.3%

  • LAMB

    39.4KRW

    3.6%

  • CVC

    37.2KRW

    2.8%

  • BORA

    35.7KRW

    -2.2%

  • R

    43.1KRW

    1.6%

  • FTM

    11.5KRW

    1.4%

  • DENT

    0.3KRW

    -0.7%

  • TTC

    46.2KRW

    6.8%

  • ABT

    177.3KRW

    -0.5%

  • POLY

    35.2KRW

    2.4%

  • AOA

    2.5KRW

    5.6%

  • CET

    18.0KRW

    4.1%

  • MOAC

    246.9KRW

    1.6%

  • MBL

    3.5KRW

    3.6%

  • ODE

    62.8KRW

    5.9%

  • AERGO

    47.5KRW

    3.1%

  • MOC

    34.4KRW

    9.2%

  • COSM

    15.3KRW

    6.5%

  • GTO

    15.2KRW

    1.6%

  • PAY

    67.9KRW

    1.4%

  • BCV

    9.7KRW

    1.2%

  • REPO

    78.3KRW

    3.9%

  • SOC

    5.7KRW

    6.3%

  • CPT

    2.6KRW

    3.0%

  • PRO

    156.1KRW

    21.3%

  • SYM

    115.4KRW

    1.8%

  • FNB

    3.3KRW

    0.3%

  • FLETA

    8.9KRW

    0.7%

  • VRA

    1.8KRW

    11.7%

  • UPP

    17.6KRW

    16.0%

  • RFR

    1.3KRW

    11.7%

  • AMO

    0.4KRW

    -14.7%

  • ELAC

    8.1KRW

    1.8%

  • LKY

    102.5KRW

    -0.0%

  • GUSD

    1,211.9KRW

    0.9%

  • EOSC

    4.9KRW

    9.0%

  • BAAS

    0.8KRW

    -8.7%

  • ADT

    5.1KRW

    -1.3%

  • HUM

    8.5KRW

    -14.0%

  • RBG

    1.2KRW

    3.5%

  • LYM

    4.4KRW

    -1.5%

  • MVL

    0.4KRW

    1.8%

  • BWX

    65.4KRW

    3.8%

  • AKRO

    2.8KRW

    29.2%

  • DCC

    0.9KRW

    -0.0%

  • eDEL

    2.4KRW

    -0.7%

  • RBTC

    12,053,844.3KRW

    7.1%

  • CLR

    14.0KRW

    -6.8%

  • AIDUS

    4.3KRW

    5.6%

  • YEED

    0.3KRW

    4.0%

  • UBEX

    0.5KRW

    1.5%

  • ABL

    6.6KRW

    3.5%

  • TEMCO

    0.4KRW

    10.4%

  • MEETONE

    0.7KRW

    4.5%

  • FREE

    0.0KRW

    6.3%

  • QDAO

    17,082.5KRW

    0.2%

  • ZPR

    0.8KRW

    3.9%

  • XRA

    10.6KRW

    0.1%

  • ISR

    4.7KRW

    2.8%

  • PTON

    0.1KRW

    -0.4%

  • HORUS

    1.1KRW

    80.6%

  • WET

    6.1KRW

    -1.4%

  • CLB

    1.6KRW

    9.2%

  • PXL

    21.5KRW

    0.6%

  • AMON

    1.2KRW

    2.5%

  • INS

    1,419.5KRW

    4.8%

  • BBC

    1.2KRW

    4.3%

  • WIKEN

    1.8KRW

    -0.1%

  • NEWS

    1.7KRW

    -6.4%

  • SEAL

    0.8KRW

    -23.8%

  • PUB

    0.2KRW

    3.3%

  • NPER

    3.5KRW

    -0.0%

  • PUT

    3.5KRW

    9.2%

  • STAR

    0.8KRW

    3.0%

  • KARMA

    0.0KRW

    1.7%

  • CCH

    0.0KRW

    --%

  • IQ

    11.9KRW

    67.2%

  • KNT

    0.0KRW

    3.4%

  • APIX

    0.6KRW

    -4.0%

  • BLACK

    12.7KRW

    -0.0%

  • RCD

    3.0KRW

    --%

  • MCC

    0.3KRW

    12.0%

  • INC

    0.3KRW

    9.4%

  • BZKY

    0.1KRW

    -0.0%

  • CRE

    1.5KRW

    5.4%

Community

How to Write a Smart Contract on Ontology with Python — (Part 3)

Ontology | 10.08| 172

How to Write a Smart Contract on Ontology with Python — (Part 3)

Introduction

In the previous tech point article, we introduced the Ontology Smart Contract Storage API. Now that you have an idea about how to call the relevant API for persistent storage when developing Python smart contract on Ontology, let’s go on to introduce how to use the Runtime API (Contract Execution API). The Runtime API has 8 related APIs that provide common interfaces for contract execution and help developers get, convert, and validate data. Here’s a brief description of these 8 APIs:

Let’s take a closer look at how to use these 8 APIs. Before that, you can create a new contract in the Ontology smart contract development tool SmartX and follow the instructions below. As usual, at the end of the article, we will provide the link of the source code.

How to Use Runtime API

There are 2 paths to import the Runtime API, ontology.interop.System.Runtime and ontology.interop.Ontology.Runtime. The Ontology path contains newly-added APIs. The following lines import these APIs.

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

Notify API

The Notify function broadcasts events to the entire network. In the following example, the Notify function will return a “hello world” hex string and broadcast it to the entire network.

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

You can view it in Logs:

GetTime API

The GetTime function returns the current timestamp, which returns the Unix time at which the function was called. The unit is second.

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

GetCurrentBlockHash API

The GetCurrentBlockHash function returns the hash of the current block.

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

Serialize & Deserialize

This is a pair of serialization and deserialization functions. The Serialize function serializes an object into a byte array object, and the Deserialize function deserializes the byte array into the original object. The following code sample implements serialization of incoming parameters and stores them in the persistent storage of the contract. It also extracts data from the contract’s persistent storage and deserializes it.

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 & AddressToBase58

This is a pair of address conversion functions. The Base58ToAddress function converts the base58 encoded address into a byte array form address, and AddressToBase58 converts the byte array form address into a base58 encoded address.

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)

CheckWitness

The CheckWitness(fromAcct) function has two functionalities:

  • Verify if the current function caller is fromAcct. If yes (i.e. signature verification passed), the function returns.
  • Check if the current function caller is a contract. If it is a contract, and the function is executed from the contract, then the verification is passed. That is, verify if fromAcct is the return value of GetCallingScriptHash(). The GetCallingScriptHash() function can get the contract hash value of the current smart contract.

GetCallingScriptHash():

ontio/ontology-python-compiler

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

Want more details? Find the full tutorial here.

Summary

In this article, we introduced the Runtime API of the Ontology blockchain, which is very important in the Ontology Python smart contract. In the next article, we will introduce the Native API to explore how to transfer assets in Ontology smart contracts.

Are you a developer? Make sure you have joined our tech community on Discord. Also, take a look at the Developer Center on our website, there you can find developer tools, documentation, and more.

Find Ontology elsewhere

Ontology website / Ontology GitHub / ONTO website / OWallet (GitHub)

Telegram (English)Discord

Twitter / Reddit / FacebookLinkedIn


How to Write a Smart Contract on Ontology with Python — (Part 3) was originally published in OntologyNetwork on Medium, where people are continuing the conversation by highlighting and responding to this story.

Comment 0

delete

Are you sure you want to delete this post?