Cryptography
Python
Programming
DevOps
Cryptocurrencies
9 October

How to Write a Smart Contract with Python on Ontology? Part 3: Runtime API

Tutorial
image

Introduction


Earlier, I have introduced the Ontology Smart Contract in
Part 1: Blockchain & Block API and
Part 2: Storage API
Now when 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 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:

image

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, I will provide a link to 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:

image

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():


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

Find the full tutorial here.

Summary


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



This is an official tutorial published earlier on Ontology Medium blog

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
GitHub / Discord
Telegram English / Russian
Twitter / Reddit

0
251 2
Leave a comment