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

image



This is the third part of a series of tutorials on creating smart contracts in Python on the Ontology blockchain network. In previous articles, we met



  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 using Python on the Ontology network, let's get to know how to use the Runtime API (Contract Execution API). The Runtime API has 8 related APIs that provide common interfaces for executing the contract and help developers receive, transform, and validate data.



Below is a brief description of the 8 API data:



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 SmartX smart contract development tool and follow the instructions below.



How to use Runtime API



There are two ways to import the Runtime API: ontology.interop.System.Runtime and ontology.interop.Ontology.Runtime . The ontology path contains the recently 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 pass it across the network.



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





You can see this in the 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 and Deserialize



These are a couple of serialization and deserialization functions. The Serialize function converts the object to a bytearray object, and the Deserialize function converts the bytearray to the original object. The following code sample converts incoming parameters and stores them in the permanent storage of the contract. It also retrieves data from the permanent storage of the contract and converts it 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



This pair of address translation functions. The Base58ToAddress function converts the encoded address of base58 to an address in the form of bytearray, and AddressToBase58 converts the address in the form of bytearray to the encoded address of base58.



 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 functions:





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 on Guthub . In the next article, we will introduce the Native API to learn how to transfer assets in Ontology smart contracts.




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


Are you a developer? Join our tech community on Discord . Also, check out the Ontology Developer Center for more tools, documentation, and more.




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



Apply for Ontology Student Talent Program



Ontology



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




All Articles