SC Blockchain & Block

<h1 align="center">How to Use Blockchain & Block API in Smart Contract</h1>
<p align="center" class="version">Version 0.1</p>

1. Introduction

There are five major APIs in Ontology smart contract and blockchain API is one of them. Blockchain API supports basic blockchain query and operation such as getting current block height. Block API supports query about transaction. More API will be available in the future.

For more details, you can view the API-doc and the source code here.

2. How to use Blockchain API

2.1 Import

from boa.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash

2.2 Blockchain API

GetHeight

from boa.interop.System.Runtime import Notify
from boa.interop.System.Blockchain import GetHeight

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


def demo():
    height=GetHeight()
    Notify(height)
    return height

GetHeader

from boa.interop.System.Runtime import Notify
from boa.interop.System.Blockchain import GetHeader

def Main(operation, args):
    if operation == 'demo':
        block_height=args[0]
        return demo(block_height)
    return False


def demo(block_height):
    header=GetHeader(block_height)
    Notify(header)
    return header

GetTransactionByHash

from boa.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


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")
    tx=GetTransactionByHash(tx_hash)
    return tx

GetTransactionHeight

from boa.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


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")
    height=GetTransactionHeight(tx_hash)
    return height # the return value 1018 is a hex number. We can get 6160 after transferring it. 

GetContract

from boa.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


def 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 contract

GetBlock

There are two ways to get block.

  1. Get block by height
from boa.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


def demo():
    block=GetBlock(1408)
    return block
  1. Get block by block hash

You need to reverse block hash of hex string first and then transfer it to byte array. For example, if block hash of hex string is e40315e22fc30b4648e3546d33927317f7175dc4c866ea443077798240c5e016, reversing it to 16e0c5408279773044ea66c8c45d17f7177392336d54e348460bc32fe21503e4 and then transferring it to \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. It is an important procedure. Otherwise, you would get an error which indicates that there is no block of this block hash.

from boa.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


def 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)
    return block

3. How to use Block API

3.1 Import

from boa.interop.System.Block import GetTransactions, GetTransactionCount, GetTransactionByIndex

3.2 Block API

GetTransactionCount

from boa.interop.System.Blockchain import GetBlock
from boa.interop.System.Block import GetTransactionCount

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


def demo():
    block=GetBlock(1408)
    count=GetTransactionCount(block)
    return count

GetTransactions

from boa.interop.System.Blockchain import GetBlock
from boa.interop.System.Block import GetTransactionCount

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


def demo():
    block=GetBlock(1408)
    txs=GetTransactions(block)
    return txs

GetTransactionByIndex

from boa.interop.System.Blockchain import GetBlock
from boa.interop.System.Block import GetTransactionByIndex

def Main(operation):
    if operation == 'demo':
        return demo()
    return False


def demo():
    block=GetBlock(1408)
    tx=GetTransactionByIndex(block,0) # index starts from 0.
    return tx

4. Contributing

Please feel free to give any suggestion and help us make video better!
Contact: Yue Zhao 
Wechat: 16621171248
Email: messixaviinsta0303@163.com
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末瘩扼,一起剝皮案震驚了整個濱河市聚请,隨后出現的幾起案子故俐,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,948評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件寝贡,死亡現場離奇詭異唯竹,居然都是意外死亡,警方通過查閱死者的電腦和手機典阵,發(fā)現死者居然都...
    沈念sama閱讀 90,371評論 3 385
  • 文/潘曉璐 我一進店門奋渔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人壮啊,你說我怎么就攤上這事嫉鲸。” “怎么了歹啼?”我有些...
    開封第一講書人閱讀 157,490評論 0 348
  • 文/不壞的土叔 我叫張陵玄渗,是天一觀的道長。 經常有香客問我狸眼,道長藤树,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,521評論 1 284
  • 正文 為了忘掉前任拓萌,我火速辦了婚禮岁钓,結果婚禮上,老公的妹妹穿的比我還像新娘微王。我一直安慰自己屡限,他們只是感情好,可當我...
    茶點故事閱讀 65,627評論 6 386
  • 文/花漫 我一把揭開白布炕倘。 她就那樣靜靜地躺著囚霸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪激才。 梳的紋絲不亂的頭發(fā)上拓型,一...
    開封第一講書人閱讀 49,842評論 1 290
  • 那天,我揣著相機與錄音瘸恼,去河邊找鬼劣挫。 笑死,一個胖子當著我的面吹牛东帅,可吹牛的內容都是我干的压固。 我是一名探鬼主播,決...
    沈念sama閱讀 38,997評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼靠闭,長吁一口氣:“原來是場噩夢啊……” “哼帐我!你這毒婦竟也來了坎炼?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,741評論 0 268
  • 序言:老撾萬榮一對情侶失蹤拦键,失蹤者是張志新(化名)和其女友劉穎谣光,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體芬为,經...
    沈念sama閱讀 44,203評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡萄金,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,534評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了媚朦。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片氧敢。...
    茶點故事閱讀 38,673評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖询张,靈堂內的尸體忽然破棺而出孙乖,到底是詐尸還是另有隱情,我是刑警寧澤份氧,帶...
    沈念sama閱讀 34,339評論 4 330
  • 正文 年R本政府宣布唯袄,位于F島的核電站,受9級特大地震影響半火,放射性物質發(fā)生泄漏越妈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,955評論 3 313
  • 文/蒙蒙 一钮糖、第九天 我趴在偏房一處隱蔽的房頂上張望梅掠。 院中可真熱鬧,春花似錦店归、人聲如沸阎抒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,770評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽且叁。三九已至,卻和暖如春秩伞,著一層夾襖步出監(jiān)牢的瞬間逞带,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,000評論 1 266
  • 我被黑心中介騙來泰國打工纱新, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留展氓,地道東北人。 一個月前我還...
    沈念sama閱讀 46,394評論 2 360
  • 正文 我出身青樓脸爱,卻偏偏與公主長得像遇汞,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,562評論 2 349

推薦閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,312評論 0 10
  • 今天突然想寫一點什么空入,不是為了死磕的任務络它,發(fā)現這兩天只是為了任務在寫,自己看看也只是復制粘貼的并沒有咀嚼吸收的才反...
    夢游世界閱讀 231評論 0 1
  • 臨睡前還在思考今天下午發(fā)生的事情歪赢,依舊很惱火化戳,人怎么可以被現實腐蝕的如此不堪,令人惡心轨淌。 正如人民日報微信...
    幸運草樂樂閱讀 143評論 0 0
  • 【日精打卡第111天】 姓 名: 夏 婷 公 司:無錫鎂鈦鉺金屬制品有限公司 六項精進第423期 反省二組 【知~...
    xia芊芊閱讀 115評論 0 0
  • 做決定的時候總是雄心壯志迂烁、豪情萬丈看尼,不了達目的決不罷休递鹉,當執(zhí)行起來時哈哈鐘小姐,你就是你藏斩,別說什么是不一樣...
    那村的二丫閱讀 199評論 0 0