Technical Perspectives | Python Smart Contract Tutorial Native Contract Call

01 lead

In the previous issue, we introduced the contract execution API for the Ontology Python Smart Contract . In this issue we will discuss how to make native native contract calls through the Native API . The most typical function of a native contract call is contract transfer , which is the core part of the entire smart contract. The Native API has only one API. The usage is as follows:

At the same time, using the Invoke function requires the built-in state function helper to encapsulate the parameters, as follows:

Let's take a closer look at how to use these two APIs. Prior to this, the small partners can create a new contract in the Ontology Smart Contract Development Tool SmartX and follow us. As with the previous API explanations, at the end of the article we will give you all the source code and video explanations of this tutorial.

02 Native API Usage

Again, you need to introduce these two functions before using them. The following two statements introduce these two functions.
from ontology.interop.Ontology.Native import Invoke 
 from ontology.builtins import state/pre> 
 

2.1 Ontology native contract list

Currently, there are six native contracts available for the ontology. Here is a list of native contracts that can be called using the Native API:

In the contract, the contract address is converted into a bytearray form and passed to Invoke. For example, when you need to call an ONT Token contract, you can first convert the address corresponding to the ONT Token contract into the corresponding bytearray form, and then call the corresponding Invoke function. When making an Invoke function call, the parameters passed in are the version number, the contract address, the contract method invoked, and the transfer-related parameters encapsulated by the state function.
One point to note here is that when making a contract transfer for ONG, the quantity filled is 10^9 times the actual quantity. That is, if you need to turn 10 ONGs, then the quantity needs to be filled in 10^10. When using a wallet such as ONTO or Cyano, the amount filled in is the amount transferred.
 contract_address_ONT = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01') param = state(from_acct, to_acct, ont_amount) # 参数为转出地址,转入地址, 转账金额res = Invoke(1, contract_address_ONT, 'transfer', [param]) 

2.2 Transfer Contract Code

Below we present a complete example of how to use the Python language to implement the transfer function of ONT and ONG . The following code implements the contract by taking the incoming outgoing account and the transfer address parameter type as string. In addition, it can also be delivered with an account parameter of type address, thereby saving the purpose of saving the cost of calling Gas. The contract code process is as follows:
  1. Define the contract address variable contract_address_ONT, contract_address_ONG;
  2. Convert the roll-out address and the transfer address from base58 format to bytearray format;
  3. Check the certificate and confirm that the transfer address is the same address as the contract call address;
  4. The state function encapsulates the transfer related parameters;
  5. The Invoke function calls the ONT Token and ONG Token native contract transfers;
  6. Determine whether the transfer was successful by returning res. The return value b'\x01' is successful, and the success pushes the event "transport succeed".
 from ontology.interop.System.Runtime import Notify, CheckWitness from ontology.interop.Ontology.Runtime import Base58ToAddress from ontology.interop.Ontology.Native import Invoke from ontology.builtins import state 

# contract address contract_address_ONT = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' Contract_address_ONG = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\)

Def Main(operation, args): if operation == 'transfer': from_acct = args[0] to_acct = args[1] ont_amount = args[2] ong_amount = args[3] return transfer(from_acct,to_acct,ont_amount,ong_amount )

Return False

Def transfer(from_acct, to_acct, ont_amount, ong_amount): # Convert base58 address to bytearray format address from_acct=Base58ToAddress(from_acct) to_acct=Base58ToAddress(to_acct) # Check, the caller must have the same address as the roll-out address if CheckWitness( From_acct): # ONT Transfer if ont_amount > 0: param = state(from_acct, to_acct, ont_amount) # state function is used to encapsulate the transfer related parameters res = Invoke(1, contract_address_ONT, 'transfer', [param]) # invoke call ONT Token native contract transfer if res and res == b'\x01': Notify('transfer succeeded') else: Notify('transfer failed') # ONG transfer, flow as above if ong_amount > 0: param = state(from_acct, to_acct , ong_amount) res = Invoke(1, contract_address_ONG, 'transfer', [param]) if res and res == b'\x01': Notify('transfer succeeded') else: Notify('transfer failed') else: Notify ('CheckWitness failed')

03 SmartX Practice

Next, the friends can work on SmartX and compile and run the contract sample code provided above. Specific steps are as follows:
1. Compile the contract . First create a new contract project in SmartX and put the code into the project to compile.

2. Deployment contract . If you need to apply for test coins during the deployment process, the application address is https://developer.ont.io/applyOng. The deployment results are as follows:

3. Perform a transfer . The relevant parameter settings are required before the transfer function is executed for transfer. In this example, you need to fill in the send address, receive address, number of ONTs for the transfer, and the number of ONGs:

4. The transfer was successful . When the transfer parameters are set correctly, the transfer function is executed and the transfer is successful. The token received will be displayed in the receiving address filled in:

04 Summary

  • In this technical point of view, we introduced the Native API of the ontology blockchain. Developers can use the Native API to make native ontology contract calls. The most typical function of a native contract call is contract transfer, which is the core part of the entire smart contract. In the next technical point of view, we will introduce the Upgrade API to explore how to upgrade contracts in ontology smart contracts . In all the grammar parts of this issue, we provide Chinese videos, and the friends can watch the lessons.

————–

Welcome more blockchain technicians to add the Ontology Institute small secretary micro-signal ontresearch as a friend to discuss technology together!