一、使用已有合约
(1)启动Node
$ nodeos -e -p eosio --plugin eosio::chain\_api\_plugin --plugin eosio::history\_api\_plugin
(2)创建 Wallet
$ cleos wallet create
(3)载入BIOS 合约
合约可以直接控制其他帐户的资源分配、访问其他特权API调用。 在公链中,将管理标记的放样和取消,以保留CPU和网络活动的带宽以及合同的内存。
$ cleos set contract eosio build/contracts/eosio.bios -p eosio@active
(5)、创建账户
$ cleos create key
$ cleos wallet import --private-key
$ cleos create account eosio user $pubkey
$ cleos create account eosio tester $pubkey
//检索
$ cleos get accounts $pubkey
二、”Hello World” 合约
(1)hello.cpp
\#include <eosiolib/eosio.hpp>
\#include <eosiolib/print.hpp>
using namespace eosio;
class hello : public eosio::contract {
public:
using contract::contract;
/// @abi action
void hi( account_name user ) {
print( "Hello, ", name{user} );
}
};
EOSIO_ABI( hello, (hi) )
(2)编译
$ eosiocpp -o hello.wast hello.cpp
$ eosiocpp -g hello.abi hello.cpp
(3)创建账户
$ cleos create account eosio hello.code PUBKEY
(4)载入合约
$ cleos set contract hello.code ../hello -p hello.code@active
(5)运行合约
$ cleos push action hello.code hi '["user"]' -p tester@active
(6)加入对输出hello的权限认证
void hi( account_name user ) {
require_auth( user );
print( "Hello, ", name{user} );
}
(7)运行
//没有tester允许输出hello,报错
$ cleos push action hello.code hi '["tester"]' -p user@active
Error 3030001: missing required authority
//加入验证
$ cleos push action hello.code hi '["tester"]' -p tester@active
executed transaction: 235bd766c2097f4a698cf
三、debug合约开发
(1)debug.hpp
#include <eoslib/eos.hpp>
#include <eoslib/db.hpp>
namespace debug {
struct foo {
account_name from;
account_name to;
uint64_t amount;
void print() const {
eosio::print("Foo from ", eosio::name(from), " to ",eosio::name(to), " with amount ", amount, "\n");
}
};
}
(2)debug.cpp
#include <debug.hpp>
extern "C" {
void apply( uint64_t receiver, uint64_t code, uint64_t action ) {
if (code == N(debug)) {
eosio::print("Code is debug\n");
if (action == N(foo)) {
eosio::print("Action is foo\n");
debug::foo f = eosio::unpack_action_data<debug::foo>();
if (f.amount >= 100) {
eosio::print("Amount is larger or equal than 100\n");
} else {
eosio::print("Amount is smaller than 100\n");
eosio::print("Increase amount by 10\n");
f.amount += 10;
eosio::print(f);
}
}
}
}
} // extern "C"
(3)编译运行
$ eosiocpp -o debug.wast debug.cpp
$ cleos set contract debug debug.wast debug.abi