根据EOS的wiki,我执行
eoscpp -n hello
试图创建一个空项目,报出如下错误
cp: cannot stat ‘/home/chen/eos/build/install/share/skeleton/.’: No such file or directory
于是在网上搜索到文章Problem with eoscpp - wrong paths,参考这篇文章后成功解决这个问题!
查看hello.cpp代码,只实现了init和apply函数,
#include <hello.hpp>
/**
* The init() and apply() methods must have C calling convention so that the blockchain can lookup and
* call these methods.
*/
extern "C" {
/**
* This method is called once when the contract is published or updated.
*/
void init() {
eosio::print( "Init World!\n" );
}
/// The apply method implements the dispatch of events to this contract
void apply( uint64_t code, uint64_t action ) {
eosio::print( "Hello World: ", eosio::name(code), "->", eosio::name(action), "\n" );
}
} // extern "C"
init函数,在部署合约时执行打印消息,打印三次:
1, eosd收到一个新transaction
2 ,eosd开始产出区块
3,eosd把这个区块追加到区块链,如同在网络上接收到区块一样。
applay函数在向合约发送消息时打印消息。也打印三次,没看懂
接下来编译hello.cpp代码
cd hello
eoscpp -o hello.wast hello.cpp
假设你的钱包已经解锁并且有${account}的密钥,我的环境下有一个wallet1钱包和一个account1账户准备好的,之前备份好wallet1.wallet放到data-dir下面,重新启动eosd,
解锁钱包wallet1
eosc wallet open -n wallet1
eosc wallet unlock -n wallet1
查看钱包里的keys
eosc wallet keys
返回:
[[
"EOS55FiCrEu5rdtPUuUhVbDCvUcvYF64PYHh9Lj39TrH58WHj2yLG",
"5KdT7YKf6p4aHqSqwtmGjvqdkQ2d4ZbX31F1mmLShbQ2eSUEe9n"
],[
"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
],[
"EOS7s1eAB9dbGnJzhhMbHnui1MA2PXbSycabio4JVDp9iJGAvRNSm",
"5K5MhSgUbBCwDUdX3xRnZsBxapbq8CJ2uKvwS5kiMBchdmjY6Ea"
]
]
其中,一个是account1的owner key,一个是account1的active key,一个是inita账户的key
上面准备工作完成后,通过下面命令部署合约:
eosc set contract account1 hello.wast hello.abi
给合约发送消息:
eosc push message account hello '"abcd"' --scope account1