mylib使用实例

mylib本地模拟库

说明: 由于mylib库封装在链上,现提供mylib本地模拟库方便开发者本地调试,此方法的调试结果无法真正模拟链上合约运行效果。

用法: 必须将以下mylib.lua和开发的合约文件在同一目录下

mylib.lua:

mylib = {}

    mylib.constant = "test11"

    function mylib.LogPrint(logTable)
        print("LogPrint" , logTable.key , logTable.value)
    end

    function mylib.GetCurTxAccount()
        return 0x01, 0x02, 0x03, 0x04, 0x05, 0x06
    end

    function mylib.GetBase58Addr(param)
        return 0x77,0x54,0x77,0x72,0x57,0x73,0x65,0x72,0x37,0x38,0x6d,0x45,0x61,0x32,0x32,0x66,0x38,0x6d,0x48,0x66,0x69,0x48,0x47,0x72,0x64,0x4b,0x79,0x73,0x54,0x76,0x38,0x65,0x42,0x55
    end

    function mylib.GetUserAppAccValue(param)
        return 0x00,0x08,0xaf, 0x2f, 0x00, 0x00, 0x00, 0x00
    end

    function mylib.ByteToInteger(param)
        return 10000000000
    end

    function mylib.IntegerToByte8(param)
        return 0x0f
    end

    function mylib.WriteOutAppOperate(param)
        print("param.userIdLen",param.userIdLen)
        return true
    end

    function mylib.WriteOutput(param)
        print("WriteOutput",param)
        return true
    end

    function mylib.GetScriptID()
        return 0x97,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    end

    function mylib.GetCurTxPayAmount()
        return 0x97,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    end

    function mylib.ModifyData(param)
        print("ModifyData: param.value" , param.value)
        return true
    end

    function mylib.WriteData(writeDbTbl)
        print("WriteData:", writeDbTbl.value)
        return true
    end

    function mylib.ReadData(param)
        print("ReadData param" , param)
        if(param=="admin") then
            return 0x77,0x54,0x77,0x72,0x57,0x73,0x65,0x72,0x37,0x38,0x6d,0x45,0x61,0x32,0x32,0x66,0x38,0x6d,0x48,0x66,0x69,0x48,0x47,0x72,0x64,0x4b,0x79,0x73,0x54,0x76,0x38,0x65,0x42,0x55
        elseif(param=="exchangeRate") then
            return 0x00,0xb7,0x07,0x84,0x03,0x00,0x00,0x00
        end
        return 0x97,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    end

return mylib

LogMsg

输出log日志信息

Used API

Code sample

LogMsg = function (msg)
    local logTable = {
        key = 0,
        length = string.len(msg),
        value = msg
    }
    mylib.LogPrint(logTable)
end

GetCurrTxAccountAddress

获取合约调用者地址

Used API

Code sample

GetCurrTxAccountAddress = function ()
    return {mylib.GetBase58Addr(mylib.GetCurTxAccount())}
end

Returns

type example
table {0x77, 0x4c, 0x57, 0x78, 0x43, 0x52, 0x57, 0x44, 0x54, 0x62, 0x33, 0x66, 0x55, 0x54, 0x61, 0x36, 0x7a, 0x74, 0x6f, 0x54, 0x54, 0x43, 0x7a, 0x46, 0x77, 0x44, 0x71, 0x7a, 0x62, 0x63, 0x6b, 0x53, 0x4a, 0x37}

GetFreeTokenCount

获取当前账户代币的自由金额

Used API

Code sample

GetFreeTokenCount = function (accountTbl)
    local freeMoneyTbl = { mylib.GetUserAppAccValue( {idLen = #accountTbl, idValueTbl = accountTbl} ) }
    assert(TableIsNotEmpty(freeMoneyTbl), "GetUserAppAccValue error")
    return mylib.ByteToInteger(Unpack(freeMoneyTbl))
end

Returns

type example
Number 100000000

TransferToken

对当前账户代币做转账操作

Used API

Code sample

OP_TYPE = {
    ADD_FREE     = 1,
    SUB_FREE     = 2
}

WriteAppData = function (opType, moneyTbl, userIdTbl)
    local appOperateTbl = {
      operatorType = opType,
      outHeight = 0,
      moneyTbl = moneyTbl,
      userIdLen = #userIdTbl,
      userIdTbl = userIdTbl,
      fundTagLen = 0,
      fundTagTbl = {}
    }
    assert(mylib.WriteOutAppOperate(appOperateTbl), "WriteAppData: ".. opType .." op err")
end

TransferToken = function (fromAddrTbl, toAddrTbl, moneyTbl)
    local money = mylib.ByteToInteger(Unpack(moneyTbl))
    assert(money > 0, money .. " <=0 error")
    local freeMoney = GetFreeTokenCount(fromAddrTbl)
    assert(freeMoney >= money, "Insufficient money to transfer in the account.")
    WriteAppData(OP_TYPE.SUB_FREE, moneyTbl, fromAddrTbl )
    WriteAppData(OP_TYPE.ADD_FREE, moneyTbl, toAddrTbl )
end

Parameters

parameter type example
fromAddrTbl table {0x77, 0x4c, 0x57, 0x78, 0x43, 0x52, 0x57, 0x44, 0x54, 0x62, 0x33, 0x66, 0x55, 0x54, 0x61, 0x36, 0x7a, 0x74, 0x6f, 0x54, 0x54, 0x43, 0x7a, 0x46, 0x77, 0x44, 0x71, 0x7a, 0x62, 0x63, 0x6b, 0x53, 0x4a, 0x37}
toAddrTbl table {0x77, 0x4c, 0x44, 0x6e, 0x42, 0x5a, 0x64, 0x36, 0x44, 0x6f, 0x4a, 0x56, 0x54, 0x58, 0x42, 0x4c, 0x44, 0x32, 0x4e, 0x57, 0x51, 0x34, 0x53, 0x45, 0x42, 0x52, 0x68, 0x39, 0x4e, 0x42, 0x76, 0x48, 0x50, 0x71}
moneyTbl table {0x00 0x50 0xd6 0xdc 0x01 0x00 0x00 0x00}

Returns

  • none

UTransferToAddr

将智能合约内资产转出,支持但不仅限于 WICC WUSD WGRT

Code sample

UTransferToAddr = function (tokenType, toTbl, AmountTbl)
    assert(mylib.ByteToInteger(Unpack(AmountTbl)) ~= 0 ,"Error, contract universal transfer "..tokenType.." amount is 0!")
    local transferTable = {
        isContractAccount = true,           --(boolean, required) Is contract account or tx sender' account
        toAddressType = ADDR_TYPE.BASE58,   --(number, required) The to address type of the transfer, REGID = 1, BASE58 = 2
        toAddress = toTbl,                  --(array, required)  The to address of the transfer, array format
        tokenType = tokenType,              --(string, required) Token type of the transfer, such as WICC | WUSD
        tokenAmount = AmountTbl             --(array, required)  Token amount of the transfer
    }
    assert(mylib.TransferAccountAsset(transferTable) == true ,"Contract universal transfer "..tokenType.." failed!")
end

Parameters

parameter type meaning example
tokenType String 币种资产symbol WICC WGRT WUSD
toTbl table 接收者地址 {0x77, 0x4c, 0x57, 0x78, 0x43, 0x52, 0x57, 0x44, 0x54, 0x62, 0x33, 0x66, 0x55, 0x54, 0x61, 0x36, 0x7a, 0x74, 0x6f, 0x54, 0x54, 0x43, 0x7a, 0x46, 0x77, 0x44, 0x71, 0x7a, 0x62, 0x63, 0x6b, 0x53, 0x4a, 0x37}
AmountTbl table 转出金额 {0x00 0x50 0xd6 0xdc 0x01 0x00 0x00 0x00}

GetContractValue

获取区块链合约中key对应的value值

Used API

Code sample

GetContractValue = function (key)
    assert(#key > 0, "Key is empty")

    local tValue = { mylib.ReadData(key) }
    if TableIsNotEmpty(tValue) then
      return true,tValue
    else
      LogMsg("Key not exist")
      return false,nil
    end
end

Parameters

parameter type meaning
key String key

Returns

parameter type meaning example
parameter1 boolean exist or not true
parameter2 table value {}

WriteStrkeyValueToDb

将key-value键值对写到区块链上

Code sample

WriteStrkeyValueToDb = function (Strkey,ValueTbl)
    local t = type(ValueTbl)
    assert(t == "table","the type of Value isn't table.")

    local writeTbl = {
        key = Strkey,
        length = #ValueTbl,
        value = {}
    }
    writeTbl.value = ValueTbl

    if not mylib.WriteData(writeTbl) then  error("WriteData error") end
end

Parameters

parameter type meaning
Strkey string key
ValueTbl table data

Returns

  • none

GetContractAsset

获取合约的多币种余额

Code sample

GetContractAsset = function (tokenType)

    local AssetTable = {
        addressType =  ADDR_TYPE.REGID,     -- (number, required)       address type, REGID = 1, BASE58 = 2
        address = {mylib.GetScriptID() },   -- (array, required)        address, array format
        tokenType= tokenType                -- (string, required)       Token type of the transfer, such as WICC | WUSD
    }
    local AssetTbl = mylib.GetAccountAsset(AssetTable)
    assert(AssetTbl ~= nil ,"GetContractAsset error!")

    return AssetTbl
end

Parameters

parameter type example
tokenType String WICC WUSD WGRT

Returns

成功:

{
    symbol: (string), 币种符号, 支持但不限于`WICC`、`WUSD`、`WGRT`中的一种
    amount: (number), 币种对应的余额, 单位 `sawi`
}

失败:nil


GetAddrAsset

获取地址的多币种余额

Code sample

GetAddrAsset = function (tokenType,addrTbl)

    local AssetTable = {
        addressType =  ADDR_TYPE.BASE58,     -- (number, required)       address type, REGID = 1, BASE58 = 2
        address = addrTbl,                   -- (array, required)        address, array format
        tokenType= tokenType                 --(string, required)        Token type of the transfer, such as WICC | WUSD
    }
    local AssetTbl = mylib.GetAccountAsset(AssetTable)
    assert(AssetTbl ~= nil ,"GetAddrAsset error!")

    return AssetTbl
end

Parameters

parameter type example
tokenType String WICC WUSD WGRT

Returns

成功:

{
    symbol: (string), 币种符号, 支持但不限于`WICC`、`WUSD`、`WGRT`中的一种
    amount: (number), 币种对应的余额, 单位 `sawi`
}

失败:nil