How to Use Other API in Smart Contract

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

1. Introduction

API Return Value Description
GetExecutingScriptHash bytearray Get the hash of a script of a smart contract
GetCallingScriptHash bytearray Get the address of the contract caller
RegisterAction Notify structure Push notification

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

2. How to use ExecutionEngine

2.1 Import

from ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash

2.2 GetExecutingScriptHash

from ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash

def Main(operation, args):
    if operation == "get_contract_hash":
        return get_contract_hash()
    
    return False


def get_contract_hash():
    return GetExecutingScriptHash()

2.3 GetCallingScriptHash

from ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash

def Main(operation, args):
    if operation == "GetCallingScriptHash_test1":
        return GetCallingScriptHash_test1()
    if operation == "GetCallingScriptHash_test2":
        return GetCallingScriptHash_test2()
    
    return False


def GetCallingScriptHash_test1():
    return GetCallingScriptHash()
    
    
def GetCallingScriptHash_test2():
    return GetCallingScriptHash()

3. How to use Action

3.1 Import

from ontology.interop.System.Action import RegisterAction

3.2 RegisterAction

RegisterAction is used for push events.

params format: string + multiple params

from ontology.interop.System.Action import RegisterAction

Transfer = RegisterAction('transfer_test', 'a', 'b', 'c')
Refund = RegisterAction('refund_test', 'to', 'amount')


def Main(operation, args):
    if operation == "test1":
        return test1()
    if operation =="test2":
        return test2()
    return False

def test1():
    a = 3
    b = 8
    c = a + b
    Transfer(a, b, c)
    return True

def test2():
    to = 'somebody'
    amount = 52
    Refund(to, amount)
    return True
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容