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

Bitcoin

The Rise and Fall of Solana and its Memecoins: A Wild Rollercoaster Ride in the Crypto Market 🎢

The Solana ecosystem saw tremendous growth in popularity towards the end of last year, largely due to the success of ...

Market

The Global Economy Enters a New Era: The Rise of AI and Blockchain Technology

The global economy is on the brink of a new and exciting 'super cycle', reminiscent of the prosperous 1980s. This gro...

Blockchain

Introducing the Crypto Fashion Trend: Tangem Ring

Fashion-forward crypto enthusiasts rejoice! The highly anticipated launch of Tangem Ring, a stylish hardware wallet, ...

Market

The Jovial Journey of Starknet: Join the Early Community Member Program and Reap the Rewards!

Great news for fashion enthusiasts! The Starknet Foundation is giving away 50 million STRK tokens in its Early Commun...

Blockchain

Reinventing Digital Payments: The Birth of UDPN

In a groundbreaking move, SC Ventures and Deutsche Bank have achieved CBDC and stablecoin interoperability through UDPN.

Market

SEC might postpone approval of spot Bitcoin ETF as it reviews comments on newly submitted S-1 forms.

There is currently a disagreement among observers regarding the SEC's decision to add comments on S-1 forms, which ha...