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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,900评论 0 10
  • 今天突然想写一点什么,不是为了死磕的任务,发现这两天只是为了任务在写,自己看看也只是复制粘贴的并没有咀嚼吸收的才反...
    梦游世界阅读 297评论 0 1
  • 临睡前还在思考今天下午发生的事情,依旧很恼火,人怎么可以被现实腐蚀的如此不堪,令人恶心。 正如人民日报微信...
    幸运草乐乐阅读 170评论 0 0
  • 【日精打卡第111天】 姓 名: 夏 婷 公 司:无锡镁钛铒金属制品有限公司 六项精进第423期 反省二组 【知~...
    xia芊芊阅读 128评论 0 0
  • 做决定的时候总是雄心壮志、豪情万丈,不了达目的决不罢休,当执行起来时哈哈钟小姐,你就是你,别说什么是不一样...
    那村的二丫阅读 227评论 0 0

友情链接更多精彩内容