Pull to refresh

How to Write a Smart Contract with Python on Ontology? Part 1: the Blockchain & Block API

PythonDistributed systemsCryptocurrencies

This is an official tutorial published earlier on Ontology Medium blog
Excited to publish it for Habr readers. Feel free to ask any related questions and suggest a better format for tutorial materials


In this article, we will begin to introduce the smart contract API of Ontology. The Ontology’s smart contract API is divided into 7 modules:

In this article, we will introduce the Blockchain & Block API, which is the most basic part of the Ontology smart contract system. The Blockchain API supports basic blockchain query operations, such as obtaining the current block height, whereas the Block API supports basic block query operations, such as querying the number of transactions for a given block.

Let’s get started!

First, create a new contract in SmartX and then follow the instructions below.

1. How to Use Blockchain API

References to smart contract functions are identical to Python’s references. Developers can introduce the appropriate functions as needed. For example, the following statement introduces GetHeight, the function to get the current block height, and GetHeader, the function to get the block header.

from ontology.interop.System.Blockchain import GetHeight, GetHeader

1.1. GetHeight

You can use GetHeight to get the latest block height, as shown in the following example. In the latter example, for convenience, we will omit the Main function, but you can add it if it’s needed.

from ontology.interop.System.Runtime import Notify
from ontology.interop.System.Blockchain import GetHeight
def Main(operation):
    if operation == 'demo':
        return demo()
    return False

def demo():
    Notify(height) # print height
    return height #return height after running the function 

1.2 GetHeader

You can use GetHeader to get the block header, and the parameter is the block height of a block. Here’s an example:

from ontology.interop.System.Runtime import Notify
from ontology.interop.System.Blockchain import GetHeader
def demo():
return header

1.3 GetTransactionByHash

You can use the GetTransactionByHash function to get transactions through transaction hash. The transaction hash is sent to GetTransactionByHash as parameters in bytearray format. The key to this function is how to convert a transaction hash in hex format to a transaction hash in bytearray format. This is an important step. Otherwise, you would get an error which indicates that there is no block of this block hash.

Let’s take the transaction hash in hex format as an example to convert it to bytearray format. The example is as follows:


First, reverse the transaction hash:


Developers can implement this step with the conversion tool Hex Number(little endian)<--> Number provided by SmartX.

Then, convert it to bytearray format:


Developers can do this with the conversion tool String <-->Byte Array provided by SmartX. Finally, convert the resulting bytearray to the corresponding string:


Below is an example of the GetTransactionByHash function that gets a transaction through a transaction hash:

from ontology.interop.System.Blockchain import GetTransactionByHash
def demo():
    # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    
    return tx

1.4 GetTransactionHeight

Developers can use the GetTransactionHeight function to get transaction height through transaction hash. Let’s take the hash in the above example:

from ontology.interop.System.Blockchain import  GetTransactionHeight
def demo():
    #   tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    
    return height

1.5 GetContract

You can use the GetContract function to get a contract through contract hash. The conversion process of the contract hash is consistent with the transaction hash conversion process mentioned above.

from ontology.interop.System.Blockchain import GetContract
def demo():
    # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"    
    return contract

1.6 GetBlock

You can use the GetBlock function to get the block. There are two ways to get a specific block:
1. Get the block by block height:

from ontology.interop.System.Blockchain import GetBlock
def demo():
    return block

2. Get the block by block hash:

from ontology.interop.System.Blockchain import GetBlock
def demo():    

2 How to Use Block API

There are three functions available in the Block API, which are GetTransactions, GetTransactionCount, and GetTransactionByIndex. We will introduce them one by one.

2.1 GetTransactionCount

You can use the GetTransactionCount function to get the number of transactions for a given block.

from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionCount
def demo():
    return count

2.2 GetTransactions

You can use the GetTransactions function to get all the transactions in a given block.

from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactions 
def demo():
    return txs

2.3 GetTransactionByIndex

You can use the GetTransactionByIndex function to get specific transactions in a given block.

from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionByIndex
def demo():
    tx=GetTransactionByIndex(block,0) # index starts from 0.
    return tx

Find the complete tutorial on our GitHub here.


The Blockchain & Block API is the most indispensable part of smart contracts since you can use them to query blockchain data and block data in smart contracts. In the next few articles, we will discuss how to use other APIs to explore their interaction with the Ontology blockchain.

Are you a developer? Make sure you have joined our tech community on Discord. Also, take a look at the Developer Center on our website, there you can find developer tools, documentation, and more.

Find Ontology elsewhere

Ontology website
GitHub / Discord
Telegram (English/ Russian )
Twitter / Reddit /
Tags:smart contractsmart contractsblockchainblockchain technologiesontology networkontologysmartxpythondltcryptocurrencycryptoblock apiblockchain apiapi
Hubs: Python Distributed systems Cryptocurrencies
Rating +16
Views 1.5k Add to bookmarks 2
Leave a comment

Popular right now

Python для веб-разработки
March 5, 202159,400 ₽SkillFactory
Программист Python
March 6, 202176,800 ₽GeekBrains
Факультет Python-разработки
March 12, 2021180,000 ₽GeekBrains
Python QA Engineer
March 16, 202160,000 ₽OTUS
Python-разработчик с нуля
March 22, 202169,900 ₽Нетология

Top of the last 24 hours