Be a crypto hero in the market by logging in

Don't you have an account? Sign in

Token price

  • BTC

    9,736,198.9KRW

    -1.3%

  • ETH

    214,513.9KRW

    -1.9%

  • XRP

    340.8KRW

    -0.9%

  • USDT

    1,188.7KRW

    0.0%

  • BCH

    263,394.9KRW

    -2.1%

  • LTC

    65,057.6KRW

    -3.2%

  • BNB

    22,009.8KRW

    2.2%

  • EOS

    3,513.7KRW

    -6.0%

  • BSV

    103,768.4KRW

    1.1%

  • XLM

    75.9KRW

    1.0%

  • TRX

    18.8KRW

    -4.4%

  • ADA

    47.6KRW

    -3.1%

  • XMR

    62,535.2KRW

    -0.3%

  • HT

    4,293.0KRW

    -0.1%

  • DASH

    84,472.6KRW

    0.5%

  • XTZ

    1,081.6KRW

    -1.3%

  • NEO

    8,770.9KRW

    0.1%

  • ETC

    5,407.0KRW

    -4.9%

  • MKR

    584,499.5KRW

    -2.6%

  • USDC

    1,188.8KRW

    0.2%

  • XEM

    45.2KRW

    -1.7%

  • ONT

    752.3KRW

    0.0%

  • DOGE

    3.0KRW

    -0.2%

  • BAT

    261.1KRW

    6.0%

  • ZEC

    43,966.0KRW

    0.2%

  • PAX

    1,189.1KRW

    0.2%

  • INB

    761.0KRW

    -6.5%

  • ZRX

    382.3KRW

    2.1%

  • VET

    4.1KRW

    -2.7%

  • TUSD

    1,189.8KRW

    0.4%

  • QTUM

    2,091.1KRW

    -2.0%

  • DCR

    18,679.9KRW

    -1.9%

  • RVN

    41.2KRW

    -2.6%

  • BTG

    9,702.8KRW

    4.1%

  • LINK

    485.1KRW

    2.0%

  • NANO

    1,010.2KRW

    8.4%

  • OMG

    950.7KRW

    -2.9%

  • DGB

    9.9KRW

    -4.2%

  • KCS

    1,326.9KRW

    -0.5%

  • LSK

    949.1KRW

    -1.2%

  • BCD

    577.7KRW

    -0.2%

  • REP

    9,775.6KRW

    1.2%

  • SC

    2.5KRW

    -10.0%

  • WAVES

    1,028.2KRW

    1.0%

  • ICX

    202.2KRW

    -1.7%

  • DAI

    1,183.9KRW

    -0.8%

  • XIN

    204,816.2KRW

    -5.4%

  • XIN

    204,816.2KRW

    -5.4%

  • THETA

    107.3KRW

    -5.2%

  • BCN

    0.5KRW

    0.1%

  • MONA

    1,338.5KRW

    -1.1%

  • BTS

    30.8KRW

    -4.2%

  • KMD

    701.0KRW

    -1.8%

  • QNT

    6,601.3KRW

    -3.8%

  • HC

    1,764.1KRW

    0.9%

  • MAID

    162.4KRW

    2.0%

  • XVG

    4.5KRW

    -1.6%

  • IOST

    5.7KRW

    -2.7%

  • BTM

    67.3KRW

    -1.2%

  • BTM

    67.3KRW

    -1.2%

  • AE

    230.6KRW

    2.2%

  • MCO

    4,161.8KRW

    -1.7%

  • ARDR

    63.8KRW

    -1.4%

  • NEXO

    110.6KRW

    2.8%

  • AOA

    9.4KRW

    0.4%

  • GNT

    60.5KRW

    -2.0%

  • ZIL

    6.7KRW

    -2.1%

  • NPXS

    0.2KRW

    -1.3%

  • ENJ

    71.6KRW

    -4.0%

  • STEEM

    159.1KRW

    -2.3%

  • XZC

    6,189.4KRW

    -3.8%

  • SNT

    15.0KRW

    -2.5%

  • ELF

    94.2KRW

    -2.0%

  • ETN

    4.4KRW

    0.8%

  • STRAT

    434.7KRW

    -0.6%

  • FCT

    4,181.3KRW

    -0.6%

  • WTC

    901.2KRW

    1.3%

  • RDD

    1.3KRW

    3.0%

  • LRC

    39.3KRW

    5.4%

  • ELA

    2,203.3KRW

    -2.8%

  • ANT

    1,052.7KRW

    2.2%

  • PPT

    584.5KRW

    -3.7%

  • R

    60.4KRW

    0.9%

  • PAI

    20.2KRW

    -4.2%

  • PAI

    20.2KRW

    -4.2%

  • LOOM

    29.1KRW

    3.2%

  • AION

    79.7KRW

    -3.2%

  • ODE

    122.3KRW

    -0.2%

  • ARK

    239.7KRW

    -2.1%

  • FNB

    46.8KRW

    -5.4%

  • WAX

    30.6KRW

    -1.9%

  • BNT

    403.3KRW

    -4.3%

  • POWR

    56.8KRW

    0.7%

  • DENT

    0.3KRW

    1.3%

  • PIVX

    304.7KRW

    -0.8%

  • MOAC

    270.9KRW

    4.5%

  • CNX

    299.1KRW

    0.7%

  • TTC

    44.5KRW

    -3.2%

  • PAY

    117.7KRW

    -5.8%

  • POLY

    31.7KRW

    0.1%

  • GUSD

    1,177.7KRW

    0.0%

  • BORA

    10.7KRW

    -2.9%

  • REPO

    80.3KRW

    9.9%

  • HUM

    16.7KRW

    -4.2%

  • MBL

    1.9KRW

    -1.4%

  • LKY

    138.3KRW

    -10.7%

  • CPT

    2.2KRW

    -2.0%

  • AERGO

    54.8KRW

    -1.3%

  • COSM

    8.1KRW

    -3.2%

  • APIS

    0.5KRW

    -1.2%

  • UPP

    11.8KRW

    -5.3%

  • BAAS

    0.7KRW

    -2.9%

  • TEMCO

    0.9KRW

    0.4%

  • MVL

    0.4KRW

    -7.4%

  • MEETONE

    1.4KRW

    -7.2%

  • DCC

    0.9KRW

    0.1%

  • ABL

    10.0KRW

    -3.4%

  • eDEL

    2.4KRW

    -0.7%

  • CLB

    3.7KRW

    49.3%

  • AMON

    4.5KRW

    3.5%

  • PTON

    0.1KRW

    -1.0%

  • HORUS

    1.5KRW

    -6.6%

  • PXL

    20.9KRW

    1.0%

  • AKRO

    2.4KRW

    -58.5%

  • XRA

    10.2KRW

    -0.1%

  • WET

    6.1KRW

    0.9%

  • VRA

    0.4KRW

    -5.3%

  • AID

    4.2KRW

    -20.5%

  • KARMA

    0.1KRW

    -15.3%

  • NEWS

    2.7KRW

    -15.7%

  • RBTC

    9,728,310.7KRW

    -1.4%

  • RBG

    1.4KRW

    -4.4%

  • SEAL

    0.9KRW

    0.1%

  • PUB

    0.2KRW

    -3.0%

  • NPER

    3.5KRW

    0.1%

  • PUT

    2.4KRW

    1.5%

  • CCH

    0.1KRW

    0.0%

  • KNT

    0.0KRW

    -17.7%

  • IQ

    5.0KRW

    -10.1%

  • BLACK

    0.8KRW

    9.7%

  • CET

    28.0KRW

    -2.1%

  • RCD

    3.0KRW

    --%

  • MCC

    1.7KRW

    0.1%

  • INC

    1.7KRW

    -16.4%

  • BZKY

    0.1KRW

    -2.7%

  • CRE

    2.0KRW

    -3.2%

Community

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

Ontology | 10.08| 30

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?