mylib使用实例

1. 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

2. LogMsg

输出log日志信息

Used API

Code sample

LogMsg = function(LogType,Value)
    local LogTable = {
        key = 0, --日志类型:字符串或数字
        length = 0, --value数据流的总长
        value = nil -- 字符串或数字流
    }
    --保存数据流
    LogTable.key = LogType
    LogTable.length = #Value
    LogTable.value = Value
    mylib.LogPrint(LogTable)
end

3. 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}

4. 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

5. 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

6. GetCurrTxPayAmount

获取合约调用者转账给合约的WICC金额

Used API

Code sample

GetCurrTxPayAmount = function ()
    local payMoney = mylib.ByteToInteger(mylib.GetCurTxPayAmount())
    assert(payMoney > 0,"GetCurrTxPayAmount: payMoney <= 0")
    return payMoney
end

Returns

type example
Number 10 * 10^8 (单位为 sawi)

7. TransferToAddr

将智能合约内WICC转到目的地址

Used API

Code sample

ADDR_TYPE = {
    REGID  = 1,
    BASE58 = 2
}
OP_TYPE = {
    ADD_FREE = 1,
    SUB_FREE = 2
}

WriteAccountData = function (opType, addrType, accountIdTbl, moneyTbl)
  local writeOutputTbl = {
    addrType = addrType,
    accountIdTbl = accountIdTbl,
    operatorType = opType,
    outHeight = 0,
    moneyTbl = moneyTbl
  }
  assert(mylib.WriteOutput(writeOutputTbl),"WriteAccountData" .. opType .. " err")
end

TransferToAddr = function (addrType, accTbl, moneyTbl)
    assert(TableIsNotEmpty(accTbl), "WriteWithdrawal accTbl empty")
    assert(TableIsNotEmpty(moneyTbl), "WriteWithdrawal moneyTbl empty")
    WriteAccountData(OP_TYPE.ADD_FREE, addrType, accTbl, moneyTbl)
    local contractRegid = {mylib.GetScriptID()}
    WriteAccountData(OP_TYPE.SUB_FREE, ADDR_TYPE.REGID, contractRegid, moneyTbl)
    return true
end

Parameters

parameter type meaning example
addrType ADDR_TYPE 地址的类型 BASE58
accTbl 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}
moneyTbl table 金额 {0x00 0x50 0xd6 0xdc 0x01 0x00 0x00 0x00}

8. 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(LOG_TYPE.ENUM_STRING,"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 {}

9. WriteOrModify

写/改数据

Used API

Code sample

WriteOrModify = function (isConfig, writeTbl)
    if not isConfig then
      if not mylib.WriteData(writeTbl) then  error("WriteData error") end
    else
      if not mylib.ModifyData(writeTbl) then error("ModifyData error") end
    end
end

Parameters

parameter type meaning
isConfig boolean write or modify
writeTbl table data

Returns

  • none

10. 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

results matching ""

    No results matching ""