How to Write a Smart Contract in Python on the Ontology Network. Part 3: Runtime API

How to Write a Smart Contract in Python on the Ontology Network. Part 3: Runtime API

This is the 3rd part of a series of tutorial articles on creating smart contracts in Python on the Ontology blockchain network. In the previous articles, we got acquainted with

  1. Blockchain & Block API
  2. Storage API.

Now that you have an understanding of how to invoke the appropriate API for persistent storage when developing a smart contract using Python on the Ontology network, let's move on to learning how to use Runtime API (Contract Execution API). The Runtime API has 8 related APIs that provide common interfaces for executing contracts and assist developers in retrieving, transforming, and verifying data.

Below is a brief description of these 8 APIs:

How to Write a Smart Contract in Python on the Ontology Network. Part 3: Runtime API

Let's delve deeper into 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.

How to Use the Runtime API

There are two ways to import Runtime API: ontology.interop.System.Runtime and ontology.interop.Ontology.Runtime. The Ontology path contains recently added APIs. The lines below 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 an event across the network. In the example below, the Notify function will return the hex string 'hello world' and transmit it throughout the network.

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

You can see this in the logs:

How to Write a Smart Contract in Python on the Ontology Network. Part 3: Runtime API

GetTime API

The GetTime function returns the current timestamp, which provides the Unix time at which the function was called. The unit of measure is seconds.

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 and Deserialize

These are a pair of serialization and deserialization functions. The Serialize function transforms an object into a bytearray, while the Deserialize function converts a bytearray back into the original object. The sample code below performs the transformation of incoming parameters and saves them in the contract's persistent storage. It also retrieves data from the contract's persistent storage and transforms it back into the original object.

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

These are a pair of address conversion functions. The Base58ToAddress function converts a base58 encoded address to a bytearray form address, while AddressToBase58 converts a bytearray form address back to 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 whether the calling object fromAcct is valid. If yes (i.e., the signature check passes), the function returns.
  • Check if the calling object fromAcct is a contract. If it is a contract and the function is executed from within the contract, then verification passes. That is, verify if fromAcct matches the return value of GetCallingScriptHash(). The function GetCallingScriptHash() can take the hash value of the current smart contract.

GetCallingScriptHash():

Learn more at Guthub

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

More information can be found at Guthub. In the next article, we will present Native API, to master how to transfer assets in Ontology smart contracts.

The article was translated by the editorial team Hashrate&Shares specifically for OntologyRussia.

Are you a developer? Join our technical community on Discord. Also, check out the Developer Center Ontology, where you can find more tools, documentation, and much more.

Open tasks for developers. Close the task — earn a reward.

Apply to the Ontology talent program for students

Ontology

Ontology website — GitHub — Discord — Telegram Russian — Twitter — Reddit

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster