eos合约开发教程1—–智能合约开发环境搭建

聆嘉, 王 2019-07-22

市⾯上EOS智能合约开发的⼯具挺多,其中也不乏⼀些优秀的团队。但对于⻓年跟代码打交道的程序员来说,可能都有⾃⼰的⼀套开发环境,或许vscode或者sublime会更顺⼿⼀些,下⾯我们来体验vscode的环境搭建。

1.点击下载vscode

  1. 安装eosio和eosio.cdt, 并导⼊私钥

brew tap eosio/eosio

brew install eosio

brew tap eosio/eosio.cdt

brew install eosio.cdt

cleos wallet import

  1. 智能合约项⽬的⽂件结构如下github

.vscode //vscode 配置⽂件

dist //编译后的wasm和abi⽂件

include //头⽂件

ricardian //李嘉图合约说明⽂件

src //源码

  • build.sh //构建脚本

  • deploy.sh //发布脚本

  1. 配置c_cpp_properties.json⽤于查找相关的include⽂件

{

“configurations”: [

{

“name”: “Mac”,

“includePath”: [

“${workspaceFolder}/include”,

“/usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include/eosiolib/contracts/**”,

“/usr/local/Cellar/eosio.cdt/1.6.1/opt/eosio.cdt/include”

],

“defines”: [],

“macFrameworkPath”: [

“/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks”

],

“compilerPath”: “/usr/bin/clang”,

“cStandard”: “c11”,

“cppStandard”: “c++17”,

“intelliSenseMode”: “clang-x64”

}

],

“version”: 4

}

  1. 配置忽略eosio特定语法的错误提示 settings.json

{

“C_Cpp.errorSquiggles”: “Disabled”

}

  1. hello合约源码

hello.hpp

#include <eosio/eosio.hpp>

using namespace eosio;

CONTRACT hello : public contract {

public:

using contract::contract;

ACTION hi( name nm );

ACTION check( name nm );

using hi_action = action_wrapper<“hi”_n, &hello::hi>;

using check_action = action_wrapper<“check”_n, &hello::check>;

};

hello.cpp

#include <hello.hpp>

ACTION hello::hi( name nm ) {

print_f(“Name : %\n”, nm);

}

ACTION hello::check( name nm ) {

print_f(“Name : %\n”, nm);

eosio::check(nm == “hello”_n, “check name not equal to hello“);

}

extern “C”

{

void apply(uint64_t receiver, uint64_t code, uint64_t action)

{

if (code == receiver || (“transfer”_n.value == action && “eosio.token”_n.value == code))

{

switch (action)

{

EOSIO_DISPATCH_HELPER(hello, (check) (hi))

}

}

}

}

  1. 调⽤eosio-cpp来构建合约

eosio-cpp -abigen -I include -R ricardian -contract hello -o dist/hello.wasm src/hello.cpp

完成以上7个步骤基本就完成环境搭建了,完整代码可以点击下载源码 ,

运⾏ ./build.sh构建合约⽣成wasm和abi⽂件,运⾏./deploy.sh部署合约到测试⽹或主⽹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注