
This is the first part of a series of tutorial articles on creating smart contracts in Python on the Ontology blockchain network using the smart contract development tool. .
In this article, we will begin exploring 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.
The Blockchain & Block API is the core component of the Ontology smart contract system. The Blockchain API supports fundamental blockchain query operations, such as obtaining the current block height, while the Block API supports basic block query operations, such as requesting the number of transactions for a given block.
Let's get started!
First, create a new contract in , and then follow the instructions below.
1. How to Use the Blockchain API
Smart contract function references are identical to Python function references. You can call the relevant functions as needed. For instance, the instruction below calls GetHeight—the function for obtaining the current block height, and GetHeader—the function for retrieving the block header.
from ontology.interop.System.Blockchain import GetHeight, GetHeaderGetHeight
GetHeight is used to obtain the latest block sequential number in the blockchain, as shown in the below example. In this example for convenience, we will skip the Main function, 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 functionGetHeader
GetHeader is used to obtain the block header, with the parameter being the block sequential number in the blockchain. For 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 headerGetTransactionByHash
GetTransactionByHash is used to retrieve a transaction by its hash. The transaction hash is sent to GetTransactionByHash as parameters in bytearray format. The key to this function is converting the transaction hash from hex format to bytearray format. This is an important step; otherwise, you would receive an error indicating that there is no block with such a block hash. Let's take a transaction hash in hex format as an example to convert it to bytearray format. The example looks like this:
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1First, reverse the transaction hash:
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279Developers can perform this step using the Hex Number (little endian) conversion tool provided by SmartX.
Then convert the resulting output to 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}This can be done using the String Byte Array conversion tool provided by SmartX. Finally, convert the resulting bytearray to a string like this:
xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9fBelow is an example of the GetTransactionByHash function that retrieves a transaction via its hash:
from ontology.interop.System.Blockchain import GetTransactionByHash
def demo():
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
tx=GetTransactionByHash(tx_hash)
return txGetTransactionHeight
GetTransactionHeight is used to retrieve the transaction height via the transaction hash. Let’s take the hash from the example above:
from ontology.interop.System.Blockchain import GetTransactionHeight
def demo():
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
tx_hash=bytearray(b"xc1x89x0cx4dx73x06x26xdfxaax94x49x41x9dx66x25x05xeaxb3xbdxa2xe1xf0x1fx89x46x3cxc1xa4xa3x0ax27x9f")
height=GetTransactionHeight(tx_hash)
return heightGetContract
Developers can use the GetContract function to retrieve a contract via its hash. The process of converting the contract hash is similar to that of converting the transaction hash mentioned above.
from ontology.interop.System.Blockchain import GetContract
def demo():
# contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"
contract_hash=bytearray(b"xfax98x96x21x32x32xbbxf0x9fx23x91xfaxefx95x9bxffxa5x75x1axd8")
contract=GetContract(contract_hash)
return contractGetBlock
GetBlock is used to retrieve a block. There are two ways to obtain a specific block.
1. Get a block by its height:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block=GetBlock(1408)
return block2. Get a block by its hash:
from ontology.interop.System.Blockchain import GetBlock
def demo():
block_hash=bytearray(b'x16xe0xc5x40x82x79x77x30x44xeax66xc8xc4x5dx17xf7x17x73x92x33x6dx54xe3x48x46x0bxc3x2fxe2x15x03xe4')
block=GetBlock(block_hash)2. How to use the Block API
There are three available functions in the Block API: GetTransactions, GetTransactionCount, and GetTransactionByIndex. We will discuss them one by one.
GetTransactionCount
GetTransactionCount is used to retrieve the number of transactions for the 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 countGetTransactions
Developers can use the GetTransactions function to retrieve all transactions in 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 txsGetTransactionByIndex
GetTransactionByIndex is used to retrieve a specific transaction in 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 txThe complete guide can be found on our .
Afterword
Blockchain & Block API is an essential part of smart contracts, as you can use it to request blockchain data and block data within smart contracts. In the following articles, we will discuss how to use the remaining APIs and understand their interaction with the Ontology blockchain.
This article was translated by the Hashrate&Shares editorial team specifically for OntologyRussia.
Are you a developer? Join our technical community on . Also, check out the on our website, where you can find developer tools, documentation, and much more.
Ontology
- /
- Telegram /
- /
Source: habr.com
