Be a crypto hero in the market by logging in

Don't you have an account? Sign in

Token price

  • BTC

    9,776,142.1KRW

    -0.8%

  • ETH

    215,284.1KRW

    -1.3%

  • XRP

    341.8KRW

    -0.5%

  • USDT

    1,194.8KRW

    0.5%

  • BCH

    264,135.3KRW

    -1.7%

  • LTC

    65,272.2KRW

    -2.9%

  • BNB

    22,160.6KRW

    2.9%

  • EOS

    3,515.4KRW

    -5.9%

  • BSV

    104,704.1KRW

    2.1%

  • XLM

    76.4KRW

    1.9%

  • TRX

    18.9KRW

    -3.6%

  • ADA

    47.9KRW

    -2.4%

  • XMR

    62,861.3KRW

    0.3%

  • HT

    4,323.8KRW

    0.7%

  • DASH

    84,775.6KRW

    0.9%

  • XTZ

    1,084.9KRW

    -0.9%

  • NEO

    8,802.9KRW

    0.6%

  • ETC

    5,416.8KRW

    -4.7%

  • MKR

    590,287.2KRW

    -1.4%

  • USDC

    1,195.3KRW

    0.7%

  • XEM

    45.5KRW

    -1.3%

  • ONT

    758.1KRW

    0.8%

  • DOGE

    3.0KRW

    0.3%

  • BAT

    267.5KRW

    9.0%

  • ZEC

    44,145.1KRW

    0.7%

  • PAX

    1,196.3KRW

    0.8%

  • INB

    766.6KRW

    -5.6%

  • ZRX

    382.4KRW

    2.1%

  • VET

    4.1KRW

    -1.8%

  • TUSD

    1,197.5KRW

    1.0%

  • QTUM

    2,099.7KRW

    -1.5%

  • DCR

    18,721.0KRW

    -1.5%

  • RVN

    41.5KRW

    -1.9%

  • BTG

    9,786.6KRW

    5.1%

  • LINK

    485.1KRW

    2.0%

  • NANO

    1,012.7KRW

    8.8%

  • OMG

    955.1KRW

    -2.4%

  • DGB

    10.0KRW

    -2.9%

  • KCS

    1,328.8KRW

    -0.3%

  • LSK

    951.6KRW

    -0.9%

  • BCD

    606.3KRW

    4.8%

  • SC

    2.6KRW

    -7.4%

  • REP

    9,817.1KRW

    1.5%

  • WAVES

    1,034.6KRW

    1.6%

  • DAI

    1,188.3KRW

    -0.4%

  • ICX

    201.8KRW

    -1.8%

  • THETA

    109.3KRW

    -3.4%

  • XIN

    205,980.6KRW

    -4.9%

  • XIN

    205,980.6KRW

    -4.9%

  • BCN

    0.5KRW

    -0.8%

  • MONA

    1,331.4KRW

    -1.6%

  • BTS

    31.3KRW

    -2.5%

  • KMD

    703.2KRW

    -1.6%

  • QNT

    6,583.3KRW

    -4.0%

  • HC

    1,784.3KRW

    2.0%

  • MAID

    163.0KRW

    2.4%

  • XVG

    4.6KRW

    -0.5%

  • IOST

    5.7KRW

    -1.3%

  • BTM

    68.1KRW

    -0.1%

  • BTM

    68.1KRW

    -0.1%

  • AE

    237.4KRW

    5.3%

  • MCO

    4,176.2KRW

    -1.2%

  • ARDR

    64.2KRW

    -0.9%

  • NEXO

    110.5KRW

    2.8%

  • AOA

    9.2KRW

    -1.6%

  • GNT

    60.7KRW

    -1.9%

  • ZIL

    6.7KRW

    -1.4%

  • NPXS

    0.2KRW

    -0.8%

  • ENJ

    71.9KRW

    -3.4%

  • STEEM

    159.1KRW

    -2.3%

  • XZC

    6,186.4KRW

    -3.9%

  • SNT

    15.1KRW

    -1.6%

  • ELF

    94.8KRW

    -1.2%

  • ETN

    4.4KRW

    0.9%

  • STRAT

    437.2KRW

    -0.0%

  • FCT

    4,170.3KRW

    -0.8%

  • WTC

    906.7KRW

    1.8%

  • RDD

    1.3KRW

    2.9%

  • LRC

    39.7KRW

    6.4%

  • ELA

    2,227.3KRW

    -1.5%

  • ANT

    1,055.6KRW

    2.9%

  • PPT

    589.0KRW

    -2.9%

  • PAI

    20.4KRW

    -2.9%

  • PAI

    20.4KRW

    -2.9%

  • R

    60.0KRW

    0.4%

  • AION

    81.3KRW

    -1.2%

  • LOOM

    29.4KRW

    4.2%

  • ODE

    122.4KRW

    -0.2%

  • ARK

    242.0KRW

    -1.1%

  • FNB

    46.6KRW

    -5.7%

  • WAX

    30.6KRW

    -2.1%

  • BNT

    405.1KRW

    -3.3%

  • POWR

    57.3KRW

    1.6%

  • DENT

    0.3KRW

    2.2%

  • PIVX

    308.3KRW

    0.5%

  • MOAC

    270.0KRW

    3.9%

  • CNX

    297.7KRW

    0.3%

  • PAY

    118.6KRW

    -5.2%

  • TTC

    44.3KRW

    -3.7%

  • POLY

    32.1KRW

    1.4%

  • GUSD

    1,181.7KRW

    0.4%

  • BORA

    10.7KRW

    -2.5%

  • REPO

    80.6KRW

    7.9%

  • HUM

    16.7KRW

    -3.5%

  • MBL

    1.9KRW

    -0.8%

  • LKY

    140.3KRW

    -9.5%

  • CPT

    2.2KRW

    -1.9%

  • AERGO

    54.8KRW

    -1.3%

  • COSM

    8.2KRW

    -2.4%

  • APIS

    0.5KRW

    -1.3%

  • UPP

    11.8KRW

    -5.5%

  • BAAS

    0.7KRW

    -2.7%

  • TEMCO

    0.9KRW

    1.2%

  • MVL

    0.4KRW

    -6.4%

  • MEETONE

    1.4KRW

    -5.0%

  • DCC

    0.9KRW

    0.1%

  • ABL

    10.1KRW

    -2.7%

  • eDEL

    2.4KRW

    -0.7%

  • CLB

    3.9KRW

    58.1%

  • AMON

    4.6KRW

    8.2%

  • PTON

    0.1KRW

    -0.8%

  • HORUS

    1.5KRW

    -5.6%

  • PXL

    21.0KRW

    1.7%

  • AKRO

    2.4KRW

    -58.8%

  • XRA

    10.2KRW

    -0.0%

  • WET

    6.0KRW

    -0.7%

  • VRA

    0.4KRW

    -5.1%

  • AID

    4.3KRW

    -20.2%

  • NEWS

    2.8KRW

    -15.5%

  • KARMA

    0.1KRW

    -15.5%

  • RBTC

    9,745,610.9KRW

    -1.1%

  • RBG

    1.5KRW

    -5.5%

  • SEAL

    0.9KRW

    0.1%

  • PUB

    0.2KRW

    -2.7%

  • NPER

    3.5KRW

    0.1%

  • PUT

    2.4KRW

    1.4%

  • CCH

    0.0KRW

    0.0%

  • KNT

    0.0KRW

    -17.8%

  • IQ

    4.9KRW

    -13.3%

  • BLACK

    0.8KRW

    10.0%

  • CET

    28.2KRW

    -1.6%

  • RCD

    3.0KRW

    --%

  • MCC

    1.7KRW

    0.1%

  • INC

    1.8KRW

    -10.6%

  • BZKY

    0.1KRW

    -3.3%

  • CRE

    2.0KRW

    -2.7%

Community

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

Ontology Gas | 10.08| 18

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?