Python智能合约教程之合约升级
01 导语
02 Upgrade API 使用方法
from ontology.interop.Ontology.Contract import Migratefrom ontology.interop.System.Contract import Destroy
2.1 Destroy API
from ontology.interop.System.Contract import Destroy在 SmartX 上可以看到该示例代码的运行结果:from ontology.interop.System.Runtime import Notify
def Main(operation, args):
if operation == "destroy_contract":
return destroy_contract()
return False
def destroy_contract():
Destroy() # 调用destroy 销毁此合约
Notify(["The contract has been destoryed"])
return True
- 将以上代码粘贴至 SmartX 编译并部署;
- 再次点击部署会弹出「合约部署失败,该合约已经部署过」,因为链上已经存在相同合约;
3. 运行 destroy_contract 函数销毁合约;
4. 再次点击部署合约,会发现合约可以再次部署,不会再弹出「合约部署失败,该合约已经部署过」。这可以证明原先合约已经在链上被销毁。
同样,我们给出使用 Migrate 函数的合约示例代码: def Main(operation, args):
if operation == "migrate_contract":
if len(args) != 7:
return False
avm_code = args[0]
need_storage = args[1]
name = args[2]
version = args[3]
author = args[4]
email = args[5]
description = args[6]
return migrate_contract(avm_code, need_storage, name, version, author, email, description) return False def migrate_contract(avm_code, need_storage, name, version, author, email, description):
res = Migrate(avm_code, need_storage, name, version, author, email, description) # 调用Migrate 迁移此合约
if res:
Notify(["Migrate successfully"])
new_contract_hash=AddressFromVmCode(avm_code) # 计算新合约地址
Notify(new_contract_hash) # 打印出新合约地址
return True
else:
return False2.2 Migrate API
from ontology.interop.Ontology.Contract import Migrate
from ontology.interop.System.Runtime import Notify
from ontology.libont import AddressFromVmCode
03 结论
免责声明:
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