专注【中高档】餐饮家具定制
当前位置: 主页 > 博鱼app官网入口 > 合作客户
博鱼app官网入口
- 案例展示
- 合作客户
博鱼app官网入口|Python智能合约开发学起来!
来源: / 发布时间:2024-04-05 06:03:03 / 浏览次数:

本文摘要:1. 前言在之前的技术视点文章中,我们讲解了目前本体主网反对的智能合约体系以及适当的智能合约开发工具 SmartX。

1. 前言在之前的技术视点文章中,我们讲解了目前本体主网反对的智能合约体系以及适当的智能合约开发工具 SmartX。很多小伙伴都想要初学者练一练。在本期的本体技术视点中,我们将月开始描写智能合约语法部分。

本体的智能合约 API 分成7个模块,分别是 BlockchainBlock API、Runtime API、Storage API、Native API、Upgrade API、Execution Engine API 以及 StaticDynamic Call API。本期我们将讲解 BlockchainBlock API,这是本体智能合约体系中最基础的部分。其中,Blockchain API 反对基本的区块链查找操作者,如提供当前块高等;Block API 反对基本的区块查找操作者,如查找登录区块交易数等。同时,文末将获取视频介绍。

在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,回来我们展开操作者。2. Blockchain API 用于方法智能合约函数的提到与 Python 的提到如出一辙。

开发者可以根据必须引进适当的函数。例如,下面语句引进了提供当前近期块低函数 GetHeight 和提供区块头函数 GetHeader。

from ontology.interop.System.Blockchain import GetHeight, GetHeader2.1 GetHeight开发者可以用于 GetHeight 来提供当前近期块低,明确例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在锻炼的时候可以根据必须重新加入。

from ontology.interop.System.Runtime import Notifyfrom ontology.interop.System.Blockchain import GetHeightdef Main(operation):if operation == 'demo':return demo()return Falsedef demo():height=GetHeight()Notify(height) # 打印机heightreturn height #在函数运营完结后回到height2.2 GetHeader开发者可以用于 GetHeader 来提供区块头,参数是某个块的块低。明确例子如下:from ontology.interop.System.Runtime import Notifyfrom ontology.interop.System.Blockchain import GetHeaderdef demo():block_height=10header=GetHeader(block_height)Notify(header)return header2.3 GetTransactionByHash开发者可以用于 GetTransactionByHash 函数通过交易哈希提供交易。交易哈希以 bytearray 的格式,作为参数起源于 GetTransactionByHash。

这个函数的关键在于如何切换将十六进制格式的交易哈希改变为 bytearray 格式的交易哈希。我们以16十进制格式的交易哈希为事例,构建将十六进制格式的交易哈希改变为 bytearray 格式的交易哈希。示例哈希如下:9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1首先,将该交易哈希反序获得:c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279开发者可以通过 SmartX 获取的切换工具 Hex Number(little endian) -- Number 构建这一步。然后,将其转换成 bytearray 格式:{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}开发者可以通过 SmartX 获取的切换工具 String -- Byte Array 构建这一步。

最后,将获得的 bytearray 转换成适当的字符串:\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9fGetTransactionByHash 函数通过交易哈希提供交易的例子如下:from ontology.interop.System.Blockchain import GetTransactionByHashdef demo():# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1" tx_hash=bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f")tx=GetTransactionByHash(tx_hash)return tx2.4 GetTransactionHeight开发者可以用于 GetTransactionHeight 函数通过交易哈希提供交易高度。我们还是以上个例子中的哈希为事例:from ontology.interop.System.Blockchain import GetTransactionHeightdef demo():# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1" tx_hash=bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f")height=GetTransactionHeight(tx_hash)return height2.5 GetContract开发者可以用于 GetContract 函数通过合约哈希提供合约。其中,合约哈希的切换过程与上面谈到的交易哈希切换过程完全一致。

from ontology.interop.System.Blockchain import GetContractdef demo():# contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa" contract_hash=bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")contract=GetContract(contract_hash)return contract2.6 GetBlock开发者可以用于 GetBlock 函数提供区块。有两种方法可以提供登录区块:1. 通过块低提供区块:from ontology.interop.System.Blockchain import GetBlockdef demo():block=GetBlock(1408)return block2. 通过区块哈希提供区块:from ontology.interop.System.Blockchain import GetBlockdef demo(): block_hash=bytearray(b'\x16\xe0\xc5\x40\x82\x79\x77\x30\x44\xea\x66\xc8\xc4\x5d\x17\xf7\x17\x73\x92\x33\x6d\x54\xe3\x48\x46\x0b\xc3\x2f\xe2\x15\x03\xe4')block=GetBlock(block_hash)3. Block API 用于方法Block API 中供提到的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依序讲解下这三个函数。

3.1 GetTransactionCount开发者可以用于 GetTransactionCount 函数提供登录区块的交易数量。from ontology.interop.System.Blockchain import GetBlockfrom ontology.interop.System.Block import GetTransactionCountdef demo():block=GetBlock(1408)count=GetTransactionCount(block)return count3.2 GetTransactions开发者可以用于 GetTransactions 函数提供提供登录区块的所有交易。from ontology.interop.System.Blockchain import GetBlockfrom ontology.interop.System.Block import GetTransactionsdef demo():block=GetBlock(1408)txs=GetTransactions(block)return txs3.3 GetTransactionByIndex开发者可以用于 GetTransactionByIndex 函数提供登录区块的登录交易。

from ontology.interop.System.Blockchain import GetBlockfrom ontology.interop.System.Block import GetTransactionByIndexdef demo():block=GetBlock(1408)tx=GetTransactionByIndex(block,0) # index starts from 0.return tx04 后记BlockchainBlock API 在智能合约中起着查找区块链数据和区块数据的起到,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将辩论如何用于其它 API,探究它们和本体区块链的交互。

本期描写的所有语法部分我们获取了中文视频,网卓新闻网,小伙伴们可以观赏和自学。


本文关键词:博鱼app官网入口

本文来源:博鱼app官网入口-www.yfhmjj.com