维基链是第三代区块链公链
同ETH、EOS,维基链支持智能合约的开发,开发语言是Lua
1. 智能合约基本结构
mylib = require "mylib"
Main = function()
end
Main()
2. Helloworld 智能合约源码
mylib = require "mylib"
--脚本中必须以`mylib = require "mylib"`开头,注意一定要放在第一行,第一行如果留空会报异常。
--定义合约调用事件
METHOD = {
CHECK_HELLOWORLD = 0x17,
SEND_HELLOWORLD = 0x18
}
--参考[4.3 API调试方法实例]
--用于输出log信息至文件
LogMsg = function (msg)
local logTable = {
key = 0,
length = string.len(msg),
value = msg
}
mylib.LogPrint(logTable)
end
---------------------------------------------------
Check = function()
LogMsg("Run CHECK_HELLOWORLD Method")
end
Send = function()
LogMsg("Run SEND_HELLOWORLD Method")
end
--参考[4.2开发常用方法]
--智能合约入口
Main = function()
assert(#contract >=2, "Param length error (<2): " ..#contract )
assert(contract[1] == 0xf0, "Param MagicNo error (~=0xf0): " .. contract[1])
if contract[2] == METHOD.CHECK_HELLOWORLD then
Check()
elseif contract[2] == METHOD.SEND_HELLOWORLD then
Send()
else
error('method# '..string.format("%02x", contract[2])..' not found')
end
end
Main()
3.部署智能合约
任何地址都可以发布智能合约,智能合约发布者不一定是合约的拥有者。
详见合约相关API,采用registercontracttx
方法发布合约到链上
4.查询智能合约regid
- 使用
getcontractregid
方法,通过发布智能合约时产生的交易hash,来查询合约的regid
5.调用智能合约
- 使用
createcontracttx
调用合约时采用合约字段 “f017” 满足合约格式,正常运行
6.查看日志
前提是在 WaykiChain.conf
中添加debug=vm
,即可查看对应目录下的vm.log
信息
由截图可知log信息为合约中Check
方法的运行内容,即输出Run CHECK_HELLOWORLD Method
信息至vm.log
文件
7.总结
至此,部署一个简单的helloworld
合约完成,下一篇文章介绍进阶的合约开发。