Technical Guide | Python Smart Contract Development? It’s enough to read this one.

01 Foreword

In the previous technical point of view article, we introduced the intelligent contract system supported by the current main network and the corresponding smart contract development tool SmartX. Many small partners want to get started. In this issue's technical point of view, we will officially begin to talk about the smart contract grammar part.

The ontology's smart contract API is divided into seven modules, namely Blockchain & Block API, Runtime API, Storage API, Native API, Upgrade API, Execution Engine API and Static & Dynamic Call API. In this issue we will introduce the Blockchain & Block API , which is the most basic part of the ontology intelligence contract system. Among them, the Blockchain API supports basic blockchain query operations, such as obtaining the current block height; the Block API supports basic block query operations, such as querying the number of block transactions.
Prior to this, the small partners can create a new contract in the Ontology Smart Contract Development Tool SmartX and follow us.

02 Blockchain API How to use


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 getting the current block height function GetHeight and getting the block header function GetHeader.
  From ontology.interop.System.Blockchain import GetHeight, GetHeader 
2.1 GetHeight

Developers can use GetHeight to get the current latest block height, as shown in the following example. In the latter example, in order to save space, we will omit the Main function, and the partner can join as needed during the exercise.
  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 #returns height after the function finishes running


2.2 GetHeader

Developers can use GetHeader to get the block header , and the parameter is the block height of a block. Specific examples are as follows:

  From ontology.interop.System.Runtime import Notify
 From ontology.interop.System.Blockchain import GetHeader
 Def demo():
 Return header 

2.3 GetTransactionByHash

Developers can use the GetTransactionByHash function to get transactions through transaction hashes . The transaction hash is passed to GetTransactionByHash as a parameter in the form of bytearray. The key to this function is how to convert a transaction hash that converts a hexadecimal format into a bytearray format.
Let's take the transaction hash in hexadecimal format as an example to convert the transaction hash in hexadecimal format to a transaction hash in bytearray format. The example hash is as follows:

First, the transaction hash is reversed:

Developers can implement this step with the conversion tool Hex Number(little endian) <–> Number provided by SmartX.
Then, convert it to the bytearray format:
  {0xc1,0x89,0x0c,0x4d,0x73,0x06,0x26,0xdf,0xaa,0x94,0x49,0x41,0x9d,0x66,0x25,0x05,0xea,0xb3,0xbd,0xa2,0xe1,0xf0,0x1f,0x89,0x46 , 0x3c, 0xc1, 0xa4, 0xa3, 0x0a, 0x27, 0x9f} 
Developers can implement this step with the conversion tool String <–> Byte Array provided by SmartX.
Finally, convert the resulting bytearray into the corresponding string:
  \xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xa2\xe1\xf0\x1f \x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f 

An example of the GetTransactionByHash function that gets a transaction through a transaction hash is as follows:

  From ontology.interop.System.Blockchain import GetTransactionByHash
 Def demo():
     # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    
     Tx_hash=bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0 \x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f")
     Return tx 
2.4 GetTransaction Height

Developers can use the GetTransactionHeight function to get the transaction height through a transaction hash . Let's take the hash in the above example as an example:
  From ontology.interop.System.Blockchain import GetTransactionHeight
 Def demo():
     # tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"    
     Tx_hash=bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0 \x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f")
     Return height 

2.5 GetContract

Developers can use the GetContract function to get contracts through contract hashes . Among them, 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 

2.6 GetBlock

Developers can use the GetBlock function to get the block . There are two ways to get the specified 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():    
     Block_hash=bytearray(b'\x16\xe0\xc5\x40\x82\x79\x77\x30\x44\xea\x66\xc8\xc4\x5d\x17\xf7\x17\x73\x92\x33\x6d\x54 \xe3\x48\x46\x0b\xc3\x2f\xe2\x15\x03\xe4')


03 Block API How to use

There are three functions available for reference in the Block API, which are GetTransactions, GetTransactionCount, and GetTransactionByIndex. We will introduce these three functions in turn.

3.1 GetTransactionCount


Developers 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 
3.2 GetTransactions
Developers can use the GetTransactions function to get all the transactions that get the specified block .
  From ontology.interop.System.Blockchain import GetBlock
 From ontology.interop.System.Block import GetTransactions 
 Def demo():
     Return txs 

3 .3 GetTransactionByIndex


Developers can use the GetTransactionByIndex function to get the specified transaction for the specified 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 

04 Postscript

The Blockchain & Block API serves as the most indispensable part of smart contracts by querying blockchain data and block data in smart contracts. In a later technical perspective, we will discuss how to use other APIs to explore their interaction with the ontology blockchain. In all the grammar parts of this issue, we provide Chinese videos, which can be viewed and learned by friends.

We will continue to update Blocking; if you have any questions or suggestions, please contact us!


Was this article helpful?

93 out of 132 found this helpful

Discover more


Community Perspectives | Reflections on the MOV patrol officer system

In the 01 session of the ChainNode White Paper Declassified Reading Club, Liu Qiushan, a senior researcher at the ori...


The curse of the Bitcoin ETF: Passing forever "Next Year"?

Original: A poplar tree Bitcoin's latest whiteknight hasn't slain the dragon. (Bitcoin once again "sla...


Xiao Lei: Dislocated Hong Kong, a comprehensive starter, a blockchain that travels through time and space

Since the Politburo collectively learned the blockchain last month and put forward a high expectation vision for Chin...


Vitalik Buterin: Persuading the Ethereum Foundation to sell $ 100 million ETH, and it is basically at the "highest point"

Ethereum co-founder Vitalik Buterin said that the Ethereum Foundation had previously sold 70,000 ETH, and that it was...


Libra Association Vice-Chairman: Planned for European regulatory approval next year

A senior representative of Facebook-sponsored digital currency project Libra revealed that despite many concerns rais...


A picture to understand the blockchain App security system

Blockchain technology has become the most popular technology this year. Say goodbye to the boom of pure crypto asset ...