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 part 3 of a series of tutorials on creating smart contracts in Python on the Ontology blockchain network. In previous articles, we have seen

  1. Blockchain & Block API
  2. Storage API.

Now that you have an idea of ​​how to call the appropriate persistent storage API when developing a smart contract with 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 contract execution and help developers get, transform, and validate data.

Below is a brief description of data 8 API:

How to write a smart contract in Python on the Ontology network. Part 3: Runtime API

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.

How to use Runtime API

There are two ways to import Runtime API: ontology.interop.System.Runtime и ontology.interop.Ontology.Runtime. Ontology path contains newly added APIs. The lines below import the API data.

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 the event throughout the network. In the example below, the Notify function will return the "hello word" hex string and broadcast 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 returns the Unix time at which the function was called. The unit of measurement is the 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 and Deserialize

This is a pair of serialization and deserialization functions. The Serialize function converts an object to a bytearray object, and the Deserialize function converts a bytearray to its original object. The following sample code converts the incoming parameters and stores them in the contract's persistent storage. It also retrieves the data from the contract's persistent storage and converts it to 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

This pair of address translation functions. The Base58ToAddress function converts a base58 encoded address to a bytearray address, and AddressToBase58 converts a bytearray address 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)

Check Witness

The CheckWitness(fromAcct) function has two functionality:

  • Verify if the object calling the current function is fromAcct. If yes (that is, signature verification passed), the function returns.
  • Check if the object calling the current function is a contract. If it is a contract and the function is executed from the contract, then verification is passed. That is, verify if fromAcct is the return value of GetCallingScriptHash(). The GetCallingScriptHash() function can take the contract hash value of the current smart contract.

GetCallingScriptHash():

More on 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 APIto learn how to transfer assets in Ontology smart contracts.

The article was translated by the editors Hashrate&Shares especially for OntologyRussia.

Are you a developer? Join our tech community at Discord. Also, take a look at Developer Center Ontology for more tools, documentation, and more.

Open tasks for developers. Close the task - get a reward.

Apply now for the Ontology talent program for students

Ontology

Ontology websiteGitHubDiscordTelegram EnglishTwitter Reddit

Source: habr.com

Add a comment