Be a crypto hero in the market by logging in

Don't you have an account? Sign in

Token price

  • BTC

    11,923,099.8KRW

    2.1%

  • ETH

    327,976.6KRW

    3.6%

  • XRP

    340.4KRW

    2.1%

  • BCH

    476,800.8KRW

    5.5%

  • BSV

    350,986.1KRW

    3.7%

  • USDT

    1,208.2KRW

    -0.1%

  • EOS

    5,203.1KRW

    4.8%

  • BNB

    27,491.4KRW

    3.4%

  • LTC

    54,759.1KRW

    -0.5%

  • XTZ

    4,136.4KRW

    2.0%

  • ADA

    73.7KRW

    4.4%

  • XLM

    88.2KRW

    5.2%

  • XMR

    101,581.3KRW

    6.0%

  • TRX

    25.3KRW

    4.5%

  • ETC

    11,727.5KRW

    1.9%

  • HT

    5,674.1KRW

    3.5%

  • DASH

    129,593.4KRW

    2.5%

  • NEO

    17,106.1KRW

    2.7%

  • ATOM

    5,800.4KRW

    -3.5%

  • HEDG

    3,474.2KRW

    1.1%

  • MKR

    785,495.0KRW

    1.1%

  • ZEC

    75,724.0KRW

    2.8%

  • ONT

    1,066.3KRW

    4.1%

  • XEM

    73.1KRW

    -0.1%

  • USDC

    1,207.5KRW

    0.1%

  • BAT

    329.2KRW

    0.5%

  • VET

    8.2KRW

    2.8%

  • DOGE

    3.2KRW

    1.9%

  • ALGO

    557.4KRW

    3.1%

  • QTUM

    2,990.1KRW

    3.2%

  • DCR

    26,221.2KRW

    4.2%

  • PAX

    1,206.9KRW

    -0.1%

  • ICX

    480.9KRW

    -2.1%

  • LSK

    1,915.1KRW

    11.8%

  • INB

    643.3KRW

    1.8%

  • BTG

    12,582.5KRW

    2.4%

  • ZRX

    344.2KRW

    1.5%

  • RVN

    38.1KRW

    3.4%

  • OMG

    1,368.1KRW

    2.2%

  • REP

    16,421.0KRW

    -1.5%

  • WAVES

    1,740.4KRW

    2.0%

  • BCD

    927.1KRW

    4.3%

  • TUSD

    1,206.1KRW

    -0.1%

  • LINK

    485.1KRW

    2.0%

  • HBAR

    51.1KRW

    -7.1%

  • MONA

    2,477.7KRW

    3.0%

  • NANO

    1,170.5KRW

    1.2%

  • XIN

    325,408.8KRW

    4.2%

  • XIN

    325,408.8KRW

    4.2%

  • SC

    3.6KRW

    1.3%

  • THETA

    168.9KRW

    8.3%

  • CNX

    2,495.6KRW

    1.8%

  • NEXO

    242.0KRW

    4.9%

  • ENJ

    157.0KRW

    4.8%

  • KCS

    1,520.4KRW

    0.6%

  • BCN

    0.7KRW

    11.9%

  • BTM

    115.2KRW

    3.2%

  • BTM

    115.2KRW

    3.2%

  • VSYS

    58.0KRW

    -1.2%

  • KMD

    937.2KRW

    5.9%

  • DGB

    8.5KRW

    0.2%

  • BTS

    39.3KRW

    4.2%

  • MCO

    6,741.0KRW

    2.4%

  • HC

    2,318.5KRW

    1.4%

  • STEEM

    272.5KRW

    6.6%

  • IOST

    7.7KRW

    2.4%

  • ZIL

    9.3KRW

    4.0%

  • XVG

    5.5KRW

    2.2%

  • SXP

    1,387.5KRW

    -3.7%

  • ABBC

    147.4KRW

    3.4%

  • SNT

    23.4KRW

    10.6%

  • AION

    195.3KRW

    11.8%

  • GNT

    77.2KRW

    4.7%

  • AE

    248.9KRW

    4.3%

  • ARDR

    74.1KRW

    2.6%

  • XZC

    7,750.5KRW

    -2.0%

  • ELF

    134.8KRW

    3.6%

  • MANA

    69.4KRW

    11.4%

  • MATIC

    26.7KRW

    5.8%

  • WAX

    63.9KRW

    8.9%

  • QNT

    5,092.7KRW

    0.0%

  • RIF

    107.0KRW

    2.0%

  • LRC

    59.8KRW

    9.9%

  • NPXS

    0.2KRW

    2.0%

  • MAID

    124.5KRW

    6.7%

  • POWR

    130.7KRW

    24.8%

  • STRAT

    552.3KRW

    0.0%

  • PAI

    33.9KRW

    -3.2%

  • PAI

    33.9KRW

    -3.2%

  • GRIN

    1,407.8KRW

    1.1%

  • TOMO

    695.4KRW

    0.8%

  • BEAM

    868.0KRW

    2.7%

  • ANT

    1,490.7KRW

    1.6%

  • ETN

    4.8KRW

    1.3%

  • ELA

    2,571.9KRW

    1.9%

  • TRUE

    529.9KRW

    -0.3%

  • WTC

    681.4KRW

    6.6%

  • WAN

    361.5KRW

    10.0%

  • ARK

    313.8KRW

    0.4%

  • ORBS

    17.6KRW

    2.2%

  • FET

    51.9KRW

    2.1%

  • RDD

    1.2KRW

    8.1%

  • NAS

    654.4KRW

    3.7%

  • FCT

    3,744.6KRW

    5.8%

  • PPT

    556.9KRW

    -5.0%

  • OCEAN

    83.1KRW

    2.0%

  • LOOM

    29.7KRW

    2.3%

  • BNT

    415.6KRW

    2.8%

  • CTXC

    111.3KRW

    2.5%

  • PIVX

    466.1KRW

    2.9%

  • DAI

    1,217.5KRW

    1.0%

  • LAMB

    39.3KRW

    3.4%

  • CVC

    36.9KRW

    1.9%

  • BORA

    35.8KRW

    -2.0%

  • R

    43.1KRW

    1.6%

  • FTM

    11.4KRW

    1.1%

  • DENT

    0.3KRW

    -1.3%

  • TTC

    46.1KRW

    6.9%

  • ABT

    176.1KRW

    -1.3%

  • POLY

    35.2KRW

    2.4%

  • CET

    18.0KRW

    3.6%

  • AOA

    2.4KRW

    2.3%

  • MOAC

    245.3KRW

    1.0%

  • MBL

    3.4KRW

    2.8%

  • ODE

    62.9KRW

    6.2%

  • AERGO

    47.2KRW

    2.4%

  • MOC

    34.2KRW

    8.5%

  • COSM

    15.1KRW

    5.4%

  • GTO

    15.2KRW

    1.7%

  • PAY

    67.5KRW

    0.7%

  • BCV

    9.7KRW

    2.0%

  • REPO

    78.3KRW

    3.8%

  • SOC

    5.7KRW

    5.9%

  • CPT

    2.6KRW

    2.9%

  • SYM

    114.2KRW

    1.8%

  • PRO

    151.3KRW

    17.8%

  • FNB

    3.3KRW

    -0.2%

  • FLETA

    8.9KRW

    0.3%

  • VRA

    1.7KRW

    9.6%

  • UPP

    18.3KRW

    20.4%

  • AMO

    0.4KRW

    -6.3%

  • RFR

    1.3KRW

    10.9%

  • ELAC

    8.0KRW

    0.5%

  • LKY

    102.5KRW

    -0.0%

  • GUSD

    1,210.3KRW

    0.8%

  • EOSC

    4.9KRW

    9.0%

  • BAAS

    0.8KRW

    -8.7%

  • ADT

    5.2KRW

    -0.9%

  • HUM

    8.5KRW

    -14.5%

  • RBG

    1.2KRW

    3.5%

  • LYM

    4.4KRW

    -2.9%

  • MVL

    0.4KRW

    1.3%

  • BWX

    65.0KRW

    3.2%

  • DCC

    0.9KRW

    -0.0%

  • AKRO

    2.7KRW

    23.7%

  • eDEL

    2.4KRW

    -0.7%

  • RBTC

    11,879,718.6KRW

    5.5%

  • CLR

    14.0KRW

    -6.8%

  • AIDUS

    4.3KRW

    5.4%

  • YEED

    0.3KRW

    3.8%

  • UBEX

    0.5KRW

    1.3%

  • ABL

    6.6KRW

    3.0%

  • TEMCO

    0.4KRW

    10.4%

  • MEETONE

    0.7KRW

    5.0%

  • FREE

    0.0KRW

    5.9%

  • QDAO

    17,053.8KRW

    0.0%

  • ZPR

    0.8KRW

    2.7%

  • XRA

    10.6KRW

    0.1%

  • ISR

    4.8KRW

    4.7%

  • PTON

    0.1KRW

    -0.5%

  • HORUS

    1.1KRW

    79.5%

  • WET

    6.0KRW

    -2.8%

  • PXL

    22.1KRW

    3.3%

  • CLB

    1.6KRW

    10.3%

  • AMON

    1.2KRW

    1.7%

  • INS

    1,425.2KRW

    5.2%

  • BBC

    1.2KRW

    4.9%

  • WIKEN

    1.8KRW

    -1.3%

  • NEWS

    1.7KRW

    -8.9%

  • SEAL

    0.8KRW

    -24.2%

  • PUB

    0.2KRW

    2.7%

  • NPER

    3.5KRW

    -0.0%

  • PUT

    3.6KRW

    11.6%

  • STAR

    0.8KRW

    3.0%

  • KARMA

    0.0KRW

    1.4%

  • CCH

    0.0KRW

    --%

  • IQ

    12.0KRW

    68.0%

  • KNT

    0.0KRW

    2.8%

  • APIX

    0.6KRW

    -1.1%

  • BLACK

    12.7KRW

    -0.0%

  • RCD

    3.0KRW

    --%

  • MCC

    0.3KRW

    13.5%

  • INC

    0.3KRW

    18.5%

  • BZKY

    0.1KRW

    -0.0%

  • CRE

    1.5KRW

    4.8%

Community

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

Ontology Gas | 10.08| 79

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?