EOS 合约基础教程 - 十分钟学会EOS智能合约开发
2019-01-28 本文已影响44人
金牛茶馆
1 创建开发目录
mkdir contracts
cd contracts
2 安装相关工具
2.1 mac 下安装 brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2.2 使用 brew 安装 eos 工具
brew tap eosio/eosio
brew install eosio
nodeos --version
2.3 使用 brew 安装 eos 开发合约工具
brew tap eosio/eosio.cdt
brew install eosio.cdt
eosio-cpp --version
3 节点
3.1 添加节点配置文件
blocks-dir = "blocks"
bnet-endpoint = 0.0.0.0:4321
http-server-address = 0.0.0.0:8888
p2p-listen-endpoint = 0.0.0.0:9876
p2p-server-address = localhost:9876
allowed-connection = any
signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
producer-name = eosio
plugin = eosio::producer_plugin
plugin = eosio::chain_api_plugin
plugin = eosio::history_api_plugin
plugin = eosio::http_plugin
enable-stale-production = true
3.2 启动节点
nodeos --data-dir ./ --config-dir ./ --contracts-console
4 创建钱包
cleos wallet create --to-console
如果以下流程中遇到如下错误
Error 3120003: Locked wallet
Ensure that your wallet is unlocked before using it!
请执行解锁钱包
cleos wallet unlock --password PW5Jz2M3EoVA1uhnSHAGTN6WpvrE7Eef62LU85LsbFxeQDSfQhgRq
5 创建私钥
cleos wallet create_key
记录输出的公钥
Created new private key with a public key of: "EOS5wbpduFSWAMGEgPiCayKZDVorM7r3xPoGcjJUU8JHnfmVEiTXD"
启动的私有区块链是使用默认初始密钥创建的,必须将其加载到钱包中
cleos wallet import --private-key 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
6 创建账号
cleos create account eosio hello.code EOS5wbpduFSWAMGEgPiCayKZDVorM7r3xPoGcjJUU8JHnfmVEiTXD
输出以下代表成功
executed transaction: 0d6af1d2734c5410fd46a1018723996bbad3d9e5361f5e57bb32b4959794534d 200 bytes 3106 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"hello.code","owner":{"threshold":1,"keys":[{"key":"EOS5wbpduFSWAMGEgPiCay...
warning: transaction executed locally, but may not be confirmed by the network yet ]
7 编写EOS智能合约
mkdir hello
cd hello
vim hello.cpp
hello.cpp源代码
#include "hello.hpp"
using namespace eosio;
ACTION hello::hi( name user ) {
print_f( "Hello % from hello", user );
}
EOSIO_DISPATCH( hello, (hi) )
hello.hpp
#include <eosiolib/eosio.hpp>
using namespace eosio;
CONTRACT hello : public eosio::contract {
public:
using contract::contract;
ACTION hi( name user );
// accessor for external contracts to easily send inline actions to your contract
using hi_action = action_wrapper<"hi"_n, &hello::hi>;
};
8 编译hello文件
eosio-cpp -o hello.wasm hello.cpp --abigen
注意:
在老版本中,生成ABI
eosio-cpp -g hello.abi hello.hpp
新的CDT工具
eosio-abigen hello.cpp --output=hello.abi -contract hello
10 部署合约
cleos set contract hello.code ../hello -p hello.code@active
部署成功
Reading WASM from /wwwroot/block_chain/contracts/hello/hello.wasm...
Publishing contract...
executed transaction: 511b47b0ede6b4b0a00c5ca44b972c1e17c7d091b05ea0be1bfd4228d96fd645 1392 bytes 6870 us
# eosio <= eosio::setcode {"account":"hello.code","vmtype":0,"vmversion":0,"code":"0061736d0100000001300960017f006000017f60027...
# eosio <= eosio::setabi {"account":"hello.code","abi":"0e656f73696f3a3a6162692f312e310001026869000001000000000000806b0268690...
warning: transaction executed locally, but may not be confirmed by the network yet ]
11 调用合约
cleos push action hello.code hi '["user"]' -p hello.code@active
12 加入对输出hello的权限认证
ACTION hello::hi( name user ) {
require_auth( user );
print_f( "Hello % from hello", user );
}
13 运行
// 没有tester允许输出hello,报错
cleos push action hello.code hi '["test"]' -p hello.code@active
Error 3090004: missing required authority
// 加入验证
cleos push action hello.code hi '["hello.code"]' -p hello.code@active
executed transaction: 85b2420f1a9f1017462ec6d4682b8e9a85c9263e89b79e78d55412ccaefa7e0b 104 bytes 371 us
# hello.code <= hello.code::hi {"user":"hello.code"}
>> Hello hello.code from hello