Technical Perspectives | How much does the Python Smart Contract Execution API know?

01 lead

In the previous issue, we introduced the Ontology Smart Contract Storage API . I believe that many small partners understand how to call the relevant API for persistent storage when developing Python smart contracts on the ontology. In this issue we discuss how to use the Runtime API (Contract Execution API). The Runtime API has eight related APIs that provide a common interface for contract execution, helping developers get data, transform data, and validate data. A brief description of these 8 APIs is as follows:

Figure | Network

Let's take a closer look at how to use these 8 APIs. Prior to this, the small partners can create a new contract in the Ontology Smart Contract Development Tool SmartX and follow us. Again, at the end of the article we will give you all the source code and video explanations for this tutorial.

02 Runtime API Usage

The reference to the Runtime API is divided into two paths: ontology.interop.System.Runtime and ontology.interop.Ontology.Runtime. Among them, the Ontology path contains a new API. The following statements reference these APIs.

 From ontology.interop.System.Runtime import GetTime, CheckWitness, Notify, Serialize, Deserialize from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHas 
2.1 Notify API
The Notify function pushes events to the entire network. In the following example, the function Notify will return a "hello world" hex string and push it to the entire network.
  From ontology.interop.System.Runtime import Notify
 Def demo():
     Notify("hello world") 

Little friends can view in Logs:

2.2 GetTime API
The GetTime function returns the current timestamp, which returns the Unix time at which the function was called, in seconds.
  From ontology.interop.System.Runtime import GetTime
 Def demo():
     Return time # returns the timestamp in seconds 
2.3 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 
2.4 Serialize and 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 snippet implements serialization of incoming parameters and stores them in the persistent store of the contract. It also implements the fetching of data from the contract's persistent store and deserializing it.

  From ontology.interop.System.Runtime import 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) # Serialize the passed arguments Put(sc, key, byte_data) # Store the serialized data in the blockchain 

Def deserialize_from_bytearray(key): sc = GetContext() byte_data = Get(sc, key) # Extract data from the blockchain by key data = Deserialize(byte_data) # Deserialize the bytearray data into the original type data return data

2.5 Base58ToAddress & AdressToBase58

  From ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
 Def demo():
     Addr=Base58ToAddress(base58_addr) # Convert base58 address to bytearray form address Notify(addr)
     Base58_addr=AddressToBase58(addr) #convert the bytearray address to base58 address Notify(base58_addr) 
This is a pair of address translation functions. Among them, 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.
2.6 CheckWitness
The CheckWitness(fromAcct) function has two functions:

  • Verify that the current function caller is not fromAcct. If yes (ie 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, the verification is passed. That is, verify that fromAcct is the return value of GetCallingScriptHash(). Among them, the GetCallingScriptHash() function can get the contract hash value of the current smart contract.


  From ontology.interop.System.Runtime import CheckWitness
 From ontology.interop.Ontology.Runtime import Base58ToAddress
 Def demo():
     Res=CheckWitness(addr) # Verify that the caller address is AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z
     Return res 

03 Summary

In this technical point of view, we introduced the Runtime API of the ontology blockchain, which is very useful in ontology Python smart contracts. In the next technical point of view, we will introduce the Native API to explore how to transfer funds in ontology smart contracts. In all the grammar parts of this issue, we provide Chinese videos, and the friends can watch the lessons.