WASM SYSTEM sdk

current_block_time

函数说明

获取当前区块的时间

语法原型

uint64_t current_block_time();

参数说明

无参数

example

ACTION hello::hi( name nm ) {
    uint64_t time = current_block_time();
    print(time);
}

// Returns
1598604336


get_txid

函数说明

获取当前交易的哈希

语法原型

wasm::hash256 get_txid();

参数说明

无参数

example

ACTION hello::hi( name nm ) {
    hash256 txid = get_txid();
    txid.print();
}

// Returns
c9e8eb4aaa95de172515e9211a93974f7d45962f1f17c00ca0078800a7e01015


get_maintainer

函数说明

获取当前合约的维护者

语法原型

wasm::regid get_maintainer(wasm::regid contract);

参数说明

字段名 变量名 必填 类型 示例值 描述
合约regid contract wasm::regid 4583-2 wasm合约生成的regid

example

ACTION hello::hi( name nm ) {
    regid maintainer = get_maintainer(_self);
    print(maintainer);
}

// Returns
0-2


get_system_asset_price

函数说明

获取系统链码原生资产价格

语法原型

wasm::asset get_system_asset_price(symbol base, symbol quote);

参数说明

字段名 变量名 必填 类型 示例值 描述
基础币 base symbol symbol("WICC", 8) 基础币以及精度的位数
引用币 quote symbol symbol("USD", 8) 引用币以及精度的位数

example

ACTION hello::hi( name nm ) {
    asset hash = get_system_asset_price(symbol("WICC", 8), symbol("USD", 8));
    print(hash.to_string());

// Returns
0


get_self

函数说明

获取当前合约的regid

语法原型

inline regid get_self()const;

参数说明

无参数

example

ACTION hello::hi( name nm ) {
    regid test = get_self();
    print(test);

// Returns
28357-2


get_first_receiver

函数说明

当前合约调用的第一个接收者,表示最初始被调用执行的合约

语法原型

inline regid get_first_receiver()const

参数说明

无参数

example

ACTION hello::hi( name nm ) {
    regid test = get_first_receiver();
    print(test);

// Returns
28357-2


get_active_producers

函数说明

获取当前生产者列表

语法原型

std::vector get_active_producers()

参数说明

无参数

example

#include <hello.hpp>
// 注意引用如下文件
#include </opt/datai/wicc-wasm-cdt/libraries/wasmlib/contracts/producer_schedule.hpp>

ACTION hello::hi() {
        vector<regid> vecProducers = get_active_producers();

        for(regid producer:vecProducers)
        {
                print(producer, " ");
        }
}

// Returns
"0-2 0-3 0-4 0-5 0-6 0-7 0-8 0-9 0-10 0-11 0-12 "


print

函数说明

输出输入的信息,模版函数

语法原型

template inline void print( T num );

参数说明

字段名 变量名 必填 类型 示例值 描述
数据 num 任意类型 hello 需要在显示器上打印出来的数据

example

ACTION hello::hi( name nm ) {
    print("hello");

// Returns
hello


函数说明

输出字符串

语法原型

inline void print_f( const char* s );

参数说明

字段名 变量名 必填 类型 示例值 描述
输入的字符串 s const char* hello 需要在显示器上显示的字符串

example

ACTION hello::hi( name nm ) {
    print_f("hello");

// Returns
hello


emit_result

函数说明

设置返回值,模板方法,入参必须支持序列化。仅支持RPC接口使用。需引进头文件

语法原型

template inline void emit_result(const string &name, const string &type, const T &value)

参数说明

字段名 变量名 必填 类型 示例值 描述
名字 name const string & hello 显示出来的名称
类型 type const string & uint64 反序列化的类型
结果 value const T & value 需要展示出来的值

注:序列化和反序列化支持的类型

example

#include <hello.hpp>
#include <rpc.hpp>

ACTION hello::hi( ) {
   string value = "nihao";
   emit_result("hello", "string", value);
}

WASM_DISPATCH( hello, (hi))

// 调用:
coind wasm_getresult "631157-2" "hi" '[]'

// result
{
    "block_height" : 689016,
    "result" : {
        "name" : "hello",
        "type" : "string",
        "value" : "nihao"
    },
    "trace" : {
        "trx_id" : "0000000000000000000000000000000000000000000000000000000000000000",
        "elapsed" : 310,
        "traces" : [
            {
                "trx_id" : "0000000000000000000000000000000000000000000000000000000000000000",
                "receiver" : "631157-2",
                "trx" : {
                    "contract" : "631157-2",
                    "action" : "hi",
                    "authorization" : [
                        {
                            "account" : "0-0",
                            "permission" : ""
                        }
                    ],
                    "data" : "hi"
                },
                "console" : "a = nihao\nvalue[i] = \u0005\nvalue[i] = n\nvalue[i] = i\nvalue[i] = h\nvalue[i] = a\nvalue[i] = o\n"
            }
        ]
    }
}


notify_recipient

函数说明

添加用户到通知列表,如果用户已在通知列表中,则忽略,避免重复通知

语法原型

void notify_recipient(regid notify_account )

参数说明

字段名 变量名 必填 类型 示例值 描述
用户regid notify_account regid 0-3 需要添加到通知列表的用户regid

example

ACTION hello::hi( regid issuer ) {
    notify_recipient(issuer);
}


require_auth

函数说明

要求指定账户已授权

语法原型

inline void require_auth( regid n )

参数说明

字段名 变量名 必填 类型 示例值 描述
用户regid n regid 0-2 判断是否授权的regid用户

example

ACTION hello::hi( regid issuer ) {
    require_auth( issuer );
    print(issuer);
}

调用:
 coind submittx 0-2 40555-2 hi '["0-2"]'

// Returns
0-2


current_receiver

函数说明

当前合约执行的接收者

语法原型

inline regid current_receiver()

参数说明

无参数

example

ACTION hello::hi() {
    regid curReciver = current_receiver();
    print(curReciver);
}

// Returns
242721-2


read_action_data

函数说明

读取当前合约调用的参数数据

语法原型

uint32_t read_action_data( void* msg, uint32_t len )

参数说明

字段名 变量名 必填 类型 示例值 描述
信息 msg void * e hells as -
长度 len uint32_t 7 -

example

ACTION hello::hi( name nm ) {
    uint32_t hash = read_action_data(buffer, 20);
}


action_data_size

函数说明

获取当前合约调用的参数数据长度

语法原型

inline uint32_t action_data_size();

参数说明

无参数

example

ACTION hello::hi( regid issuer ) {
    uint32_t test = action_data_size();
    print("test:%\n", test);
}


调用:
 coind submittx 0-2 40555-2 hi '["0-2"]'

// Returns
test:8


has_auth

函数说明

检查是否有指定账户的授权

语法原型

inline bool has_auth( regid n )

参数说明

字段名 变量名 必填 类型 示例值 描述
账户regid n regid 0-3 需要判断其是否被授权的账户regid

example

ACTION hello::hi(regid account) {
    bool bExist = has_auth(account);
    print(bExist);
}


调用:
 coind submittx 0-2 40555-2 hi '["0-3"]'

// Returns
false


is_account

函数说明

判断账户是否存在

语法原型

inline bool is_account( regid n );

参数说明

字段名 变量名 必填 类型 示例值 描述
账户regid n regid 0-3 需要判断其是否存在的账户regid

example

ACTION hello::hi(regid account) {
    bool bIsAccount = is_account(account);
    print(bIsAccount);
}



调用:
 coind submittx 0-2 40555-2 hi '["0-3"]'

// Returns
true


check

函数说明

检查指定表达式的有效性,如果无效,则中断执行并抛出异常

语法原型

inline void check(bool pred, const char* msg);

参数说明

字段名 变量名 必填 类型 示例值 描述
表达式 pred bool 1==2 执行对象的regid
信息 msg char* Operation error 出现异常时要显示的信息

example

ACTION hello::hi() {
    check(1==2, "Operation error");
}


// Returns
 vm/wasm/wasm_interface.cpp:146:wasm_assert                      : Operation error