Python智能合约教程之原生合约调用
01 导语
同时,使用 Invoke 函数需要内建的 state 函数辅助来封装参数,用法如下:
2.1 本体原生合约列表02 Native API 使用方法
from ontology.interop.Ontology.Native import Invoke
from ontology.builtins import state/pre>
2.2 转账合约代码 # 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\x00\x00\x00\x02') 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):
# 将base58地址转换成 bytearray格式地址
from_acct=Base58ToAddress(from_acct)
to_acct=Base58ToAddress(to_acct)
# 验签,调用方必须与转出地址为同一地址
if CheckWitness(from_acct):
# ONT转账
if ont_amount > 0:
param = state(from_acct, to_acct, ont_amount) # state函数用于封装转账相关参数
res = Invoke(1, contract_address_ONT, 'transfer', [param]) # invoke调用ONT token原生合约转账
if res and res == b'\x01':
Notify('transfer succeeded')
else:
Notify('transfer failed')
# ONG转账,流程同上
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') 2. 部署合约。部署过程中如需申请测试币,申请地址为https://developer.ont.io/applyOng。部署结果示意如下: 3. 执行转账。执行 transfer 函数进行转账前需要进行相关参数设置。在该示例中,需要填入发送地址、接收地址、代转账的 ONT 数量以及 ONG 数量: 4. 转账成功。当转帐参数设置正确时,执行 transfer 函数将转账成功。上面所填的接收地址中将显示出收到的代币: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
03 SmartX 实践
04 总结
- 本次技术视点中我们介绍了本体区块链的 Native API,开发者可以使用 Native API 来进行本体原生合约调用。原生合约调用最典型的功能就是合约转账,这也是整个智能合约最核心的部分。在下一期技术视点中,我们将介绍 Upgrade API,探讨如何在本体智能合约中进行合约升级。本期讲述的所有语法部分我们提供了中文视频,小伙伴们可以观看学习。
--------------
欢迎更多的区块链技术人员添加本体研究院小秘书微信号ontresearch为好友,共同探讨技术!
下一篇:BTC与ETH:定位的胜利
免责声明:
1.本文内容综合整理自互联网,观点仅代表作者本人,不代表本站立场。
2.资讯内容不构成投资建议,投资者应独立决策并自行承担风险。
- 贝佐斯最后一封股东信:宇宙希望你成为普通人,千万别让它成为现实2021-04-19 17:02
- Props,让互联网与区块链无缝对接的「中间件」2021-04-19 17:02
- Coinbase高管到底卖了多少股票?2021-04-19 16:03
- 通往未来之路:下一代互联网与Metaverse2021-04-19 16:03
- 央行前行长周小川谈比特币:要提醒,要小心2021-04-19 15:03
- 链上新知 |电子图片卖出7000万美金,让马斯克都来站台的NFT究竟是什么?2021-04-19 15:02
- Crypto VC,LP怎么投?2021-04-19 13:03
- 周末比特币融资利率跌至-0.03%低点,为7个月以来最低水平2021-04-19 11:02