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():
     Block_height=10
     Header=GetHeader(block_height) 
     Notify(header)
 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:
  9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1 

First, the transaction hash is reversed:

  C1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279 
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")
     Tx=GetTransactionByHash(tx_hash)
     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")
     Height=GetTransactionHeight(tx_hash)
     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"    
     Contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")
     Contract=GetContract(contract_hash)
     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():
     Block=GetBlock(1408)
     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')
     Block=GetBlock(block_hash) 

 

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():
     Block=GetBlock(1408)
     Count=GetTransactionCount(block)
     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():
     Block=GetBlock(1408)
     Txs=GetTransactions(block)
     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():
     Block=GetBlock(1408)
     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!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Market

Old-timers Leaving the Crypto Circle Some Get Married and Have Children, Some Start New Businesses

In the world of encryption, people come and go. Have you ever wondered where the people who have left the cryptocurre...

Blockchain

A picture of the stolen Bitcoin exchange in the past years

This infographic is mainly to summarize the past money currency exchanges and then display them in a visual form. The...

Opinion

Why is selling risk the good business model?

The top companies, the market makers, are the ones who sell risk. They are the giants who have stacked up their finan...

Market

Crypto Firms on the Move: Wallets Shaking and Bacon at Lighting Speed!

FTX and Alameda sent $10 million worth of popular tokens (LINK, MKR, COMP, ETH, and AAVE) to a wallet address, which ...

Market

Wu's Weekly Picks: HSBC launches cryptocurrency ETF, US SEC rejects spot ETF application, Azuki criticized by community, and top 10 news (June 24-30)

Author | Wu's Top 100 Blockchain News This Week. US SEC Returns Spot ETF File According to WSJ, the US...

Blockchain

Italian securities regulator establishes cryptocurrency regulations, has closed 2 cryptocurrency trading sites

Cointelegraph reported on February 11 that Italian securities regulators recently closed six foreign exchange trading...