Python智能合约开发?看这一篇就够了
黄河347
发表于 2023-1-9 03:05:36
92
0
0
在之前的技术视点文章中,我们介绍了目前本体主网支持的智能合约体系以及相应的智能合约开发工具 SmartX。很多小伙伴都想上手练一练。在本期的本体技术视点中,我们将正式开始讲述智能合约语法部分。' ]% n2 b8 Y* `6 p- E, Z- Y7 Z8 K
本体的智能合约 API 分为7个模块,分别是 Blockchain & Block API、Runtime API、Storage API、Native API、Upgrade API、Execution Engine API 以及 Static & Dynamic Call API。本期我们将介绍 Blockchain & Block API,这是本体智能合约体系中最基础的部分。其中,Blockchain API 支持基本的区块链查询操作,如获取当前块高等;Block API 支持基本的区块查询操作,如查询指定区块交易数等。同时,文末将提供视频讲解。
在这之前,小伙伴们可以在本体智能合约开发工具 SmartX 中新建一个合约,跟着我们进行操作。3 l5 \% ?% a" }; ]+ N6 ~
02 Blockchain API 使用方法$ A i+ g9 f( u0 w& c
智能合约函数的引用与 Python 的引用如出一辙。开发者可以根据需要引入相应的函数。例如,下面语句引入了获取当前最新块高函数 GetHeight 和获取区块头函数 GetHeader。
from ontology.interop.System.Blockchain import GetHeight, GetHeader
2.1 GetHeight
开发者可以使用 GetHeight 来获取当前最新块高,具体例子如下。在后面的例子中,为了节省空间,我们将省略 Main 函数,小伙伴在练习的时候可以根据需要加入。1 q8 j, Y# L, @9 G& S
from ontology.interop.System.Runtime import Notify
from ontology.interop.System.Blockchain import GetHeight
def Main(operation):
if operation == 'demo':7 d, a- W7 `& o: y9 Y. B/ [, T
return demo()7 u8 q9 E% y# T8 C: T
return False
def demo():- g* u0 [9 W; ?
height=GetHeight()8 X- z; y4 G/ ~& o; p
Notify(height) # 打印height' U" R* `, a: g* Q6 z% L$ Y! H
return height #在函数运行结束后返回height
2.2 GetHeader
开发者可以使用 GetHeader 来获取区块头,参数是某个块的块高。具体例子如下:# J. H- p6 D" U! R2 K# W
from ontology.interop.System.Runtime import Notify/ ~+ @3 I7 i: {: k6 P( ]; E
from ontology.interop.System.Blockchain import GetHeader3 s# R+ A. u0 N% E4 z
def demo():( o% P# ^/ q' t, N
block_height=10# F" C" B X# r" x
header=GetHeader(block_height)
Notify(header)
return header
2.3 GetTransactionByHash$ ], w2 _- a( }% N6 T
开发者可以使用 GetTransactionByHash 函数通过交易哈希获取交易。交易哈希以 bytearray 的格式,作为参数传入 GetTransactionByHash。这个函数的关键在于如何转换将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。# B# W$ g6 ] b
我们以16进制格式的交易哈希为例,实现将十六进制格式的交易哈希转变为 bytearray 格式的交易哈希。示例哈希如下:
9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1( ]8 M) p* p" ^! P$ @# E
首先,将该交易哈希反序得到:
c1890c4d730626dfaa9449419d662505eab3bda2e1f01f89463cc1a4a30a279
开发者可以通过 SmartX 提供的转换工具 Hex Number(little endian) Number 实现这一步。
然后,将其转成 bytearray 格式:* @( F( R( I' `
{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 实现这一步。3 o4 a# ]3 a @/ G# ~
最后,将得到的 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\x9f7 h6 @- o& L. J5 {. D, E9 |! G
GetTransactionByHash 函数通过交易哈希获取交易的例子如下:
from ontology.interop.System.Blockchain import GetTransactionByHash8 a+ `( Z4 f" T# W' b0 U" S
def 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"). N; n* |0 `- `/ V
tx=GetTransactionByHash(tx_hash)/ z/ `. z- b; ^% Y: X; o: F
return tx
2.4 GetTransactionHeight
开发者可以使用 GetTransactionHeight 函数通过交易哈希获取交易高度。我们还是以上个例子中的哈希为例:$ b$ ~/ J% J4 S+ c3 o+ t( v
from ontology.interop.System.Blockchain import GetTransactionHeight1 ?1 p$ k. E$ s$ R4 w; n! g
def demo():, M/ Q, M. p% k/ U4 N
# tx_hash="9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1" ' c" j6 [1 m) o# V" B6 R. V; B
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)3 m5 p" o; m/ e
return height
2.5 GetContract* l" y( T5 v) m
开发者可以使用 GetContract 函数通过合约哈希获取合约。其中,合约哈希的转换过程与上面讲到的交易哈希转换过程一致。
from ontology.interop.System.Blockchain import GetContract
def demo():
# contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa" % o5 Z: Y1 ]' ]$ o6 {
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) s @1 _8 R: X( J/ W9 s
return contract
2.6 GetBlock5 R, i4 e8 t* z9 m
开发者可以使用 GetBlock 函数获取区块。有两种方法可以获取指定区块:: Q2 M5 x5 ]3 J/ g6 ?+ a% c- R4 U
通过块高获取区块:
& d+ h) e3 G7 }4 |* z% M) F
from ontology.interop.System.Blockchain import GetBlock5 Q. V) v* t" T z* ]' F6 r4 E
def demo():
block=GetBlock(1408)
return block" ]5 J6 ?5 Y5 I9 Z1 r2 \8 l
2. 通过区块哈希获取区块:
from ontology.interop.System.Blockchain import GetBlock& [' e' k7 g; u2 ~3 r9 A4 H
def demo(): ; S4 m/ F6 v& k. b2 v, s4 m
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)! {; Z5 [) K2 a0 s5 P
03 Block API 使用方法
Block API 中可供引用的函数有三个,它们分别是 GetTransactions、GetTransactionCount 和 GetTransactionByIndex。我们依次介绍下这三个函数。, h4 J& K4 f# p% e
3.1 GetTransactionCount! W! B6 y+ a3 O
开发者可以使用 GetTransactionCount 函数获取指定区块的交易数量。
from ontology.interop.System.Block import GetTransactionCount
def demo():) h B, x- @: o/ x, W8 T
block=GetBlock(1408)
count=GetTransactionCount(block). ]% P2 y1 ]+ ?4 @5 ?- i0 K
return count# U, e* o; Y, n" P( X6 E, c8 W9 a
**
3.2 GetTransactions**/ M0 z7 k( L# X5 T) Y2 F
开发者可以使用 GetTransactions 函数获取获取指定区块的所有交易。
from ontology.interop.System.Blockchain import GetBlock% D1 d6 N- g B2 i8 k
from ontology.interop.System.Block import GetTransactions
def demo():
block=GetBlock(1408)
txs=GetTransactions(block)
return txs0 s8 {4 G. v5 G+ g+ f5 k
3.3 GetTransactionByIndex8 P0 ]; R* V$ v) j
开发者可以使用 GetTransactionByIndex 函数获取指定区块的指定交易。
from ontology.interop.System.Blockchain import GetBlock( D: @: Y% W* J4 ^8 v
from ontology.interop.System.Block import GetTransactionByIndex" W$ W: n3 ]& y/ Y6 [
def demo():* ?3 R* P& ^3 S
block=GetBlock(1408)' O" m2 l4 y5 E
tx=GetTransactionByIndex(block,0) # index starts from 0.
return tx; [4 y6 E. T% I2 M0 @4 x5 H# z
04 后记
Blockchain & Block API 在智能合约中起到查询区块链数据和区块数据的作用,是智能合约最不可缺少的一部分。在后面的技术视点中,我们将讨论如何使用其它 API,探讨它们和本体区块链的交互。、
成为第一个吐槽的人