ProHoster > Blog > Administration > How to write a smart contract in Python on the Ontology network. Part 1: Blockchain & Block API
How to write a smart contract in Python on the Ontology network. Part 1: Blockchain & Block API
This is the first part in a series of tutorials on creating Python smart contracts on the Ontology blockchain network using the smart contract development tool. SmartX.
In this article, we will begin our acquaintance with the Ontology smart contract API. The Ontology smart contract API is divided into 7 modules:
Blockchain & Block API,
runtime API,
storage API,
native API,
Upgrade API,
Execution Engine API and
Static & Dynamic Call API.
Blockchain & Block API is the main part of the Ontology smart contract system. The Blockchain API supports basic blockchain query operations, such as getting the current block height, while 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 SmartXand then follow the instructions below.
1. How to use Blockchain API
Links to smart contract functions are identical to Python links. You can enter the corresponding functions as needed. For example, the following statement introduces a GetHeight function to get the current block height, and a GetHeader function to get the block's header.
from ontology.interop.System.Blockchain import GetHeight, GetHeader
GetHeight
GetHeight is used to get the last block sequence number in the blockchain, as shown in the example below. In the last example, we will omit the Main function for convenience, but you can add it if necessary.
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():
height=GetHeight()
Notify(height) # print height
return height #return height after running the function
GetHeader
GetHeader is used to get the block header, the parameter is the serial number of the block in the blockchain. Example:
from ontology.interop.System.Runtime import Notify
from ontology.interop.System.Blockchain import GetHeader
def demo():
block_height=10
header=GetHeader(block_height)
Notify(header)
return header
GetTransactionByHash
GetTransactionByHash is used to get a transaction via a transaction hash. The transaction hash is sent to GetTransactionByHash as parameters in bytearray format. The key to this function is to convert the transaction hash in hex format to the transaction hash in bytearray format. This is an important step. Otherwise, you would get an error that indicates that there is no block with that block hash. Let's take the transaction hash in hex format as an example to convert it to bytearray format. An example looks like this:
Developers can use the GetContract function to get a contract via the hash of the contract. The contract hash conversion process is the same as the transaction hash conversion process mentioned above.
GetBlock is used to get a block. There are two ways to get a specific block.
1. Get block by block height:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block=GetBlock(1408)
return block
2. Get a block by block hash:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block_hash=bytearray(b'x16xe0xc5x40x82x79x77x30x44xeax66xc8xc4x5dx17xf7x17x73x92x33x6dx54xe3x48x46x0bxc3x2fxe2x15x03xe4')
block=GetBlock(block_hash)
2. How to use Block API
There are three available functions in the Block API: GetTransactions, GetTransactionCountand GetTransactionByIndex. We will break them down one by one.
GetTransactionCount
GetTransactionCount is used 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():
block=GetBlock(1408)
count=GetTransactionCount(block)
return count
GetTransactions
Developers can use the GetTransactions function to get all transactions in a given block.
from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactions
def demo():
block=GetBlock(1408)
txs=GetTransactions(block)
return txs
GetTransactionByIndex
GetTransactionByIndex is used to get a specific transaction in a given block.
from ontology.interop.System.Blockchain import GetBlock
from ontology.interop.System.Block import GetTransactionByIndex
def demo():
block=GetBlock(1408)
tx=GetTransactionByIndex(block,0) # index starts from 0.
return tx
The Blockchain & Block API is an indispensable part of smart contracts as you can use them to request blockchain data and block data in smart contracts. In the following articles, we will discuss how to use the rest of the APIs and find out how they interact with the Ontology blockchain.
The article was translated by the editors of Hashrate&Shares especially for OntologyRussia. ΠΊΠ»ΠΈΠΊ
Are you a developer? Join our tech community at Discord. Also, take a look at Developer Center on our website, where you can find developer tools, documentation, and more.