Technical Perspectives | Python Smart Contract Tutorial Native Contract Call
01 lead
At the same time, using the Invoke function requires the built-in state function helper to encapsulate the parameters, as follows:
02 Native API Usage
from ontology.interop.Ontology.Native import Invoke
from ontology.builtins import state/pre>
- The most heavy chain rule defect: your main chain I am the master
- Getting started with blockchain | Hash lock for cross-chain technology solutions
- Technical Perspectives | How much does the Python Smart Contract Execution API know?
2.1 Ontology native contract list
2.2 Transfer Contract Code
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])
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
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!
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
Related articles
- Comparison of IPFS and EdgeFS for Secure Edge/IoT Computing Use Cases
- Five major problems read through the PoA consensus algorithm
- Analysis | Pre-selection and pre-submission mechanisms for Byzantine fault-tolerant consensus
- Unlock Bitcoin for more apps, Miniscript yesterday, today and tomorrow!
- Technical Guide | Teach you to discuss Wasm contract development: (C++)
- Getting started with blockchain | Ethereum 2.0 terminology at a glance
- Defects of the heaviest chain rule: "The Crown Prince" in the "Public Ancestral Blocks"