Search
 Coin Explorers
Search
 Coin Explorers

Portfolio

Markets

Project Reviews

Founder Stories

Features

Guides

News

Videos

Let’s stay in touch:

News

How to Write a Smart Contract on #Ontology with #Python — (Part 3)

In this tech point article, we will introduce how to use the Runtime API (Contract Execution API).

Oct 8, 2019 · 3 min read
  • Share on X
  • Share on Facebook
  • Share on Linkedin
How to Write a Smart Contract on #Ontology with #Python — (Part 3)

How to Write a Smart Contract on Ontology with Python — (Part 3) Introduction In the previous tech point article, we introduced the Ontology Smart Contract Storage API. Now that 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 introduce how to use the 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: 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, we will provide the link of 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, Deserializefrom 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 Notifydef demo(): Notify("hello world") You can view it in Logs: 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 GetTimedef 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 GetCurrentBlockHashdef 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, Deserializefrom ontology.interop.System.Storage import Put, Get, GetContextdef 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 Falsedef 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, AddressToBase58def 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(): from ontology.interop.System.Runtime import CheckWitnessfrom ontology.interop.Ontology.Runtime import Base58ToAddressdef demo(): addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z") res=CheckWitness(addr) return res Want more details? Find the full tutorial here. Summary In this article, we introduced the Runtime API of the Ontology blockchain, which is very important in the Ontology Python smart contract. In the next article, we will introduce the Native API to explore how to transfer assets in Ontology smart contracts.


  • Share on X
  • Share on Facebook
  • Share on Linkedin

Related News

Bitcoin has officially entered the Guinness World Records for a number of entries, the first of which is being recognized as the First Decentralized Cryptocurrency
News

Bitcoin has officially entered the Guinness World Records for a number of entries, the first of which is being recognized as the First Decentralized Cryptocurrency

Bitcoin now has multiple entries in the Guinness Book of World Records, including most valuable and the first decentralized cryptocurrency.

Oct 19, 2022

740 Million in Bitcoin exits exchanges, the biggest outflow since June's BTC price crash
News

740 Million in Bitcoin exits exchanges, the biggest outflow since June's BTC price crash

The technical outlook, however, remains bearish for Bitcoin, with the price eyeing a run-down toward $14,000 in Q4/2022.

Oct 18, 2022

Bitcoin Wins the Guinness World Record for First Decentralized Cryptocurrency
News

Bitcoin Wins the Guinness World Record for First Decentralized Cryptocurrency

Bitcoin has been honored as the oldest and most valuable crypto, while El Salvador is recognized as the first country to adopt it as legal tender. 

Oct 18, 2022

 Coin Explorers

PortfolioMarketsProject ReviewsFounder StoriesFeaturesGuidesNewsVideosTerms & ConditionsPrivacy Policy

Powered by

 Coin Explorers

Copyright © 2025 - All Rights Reserved