区块链|EOS|smart contract智能合约入门1——Hello World!

1. 在你的contract目录下面建立一个新的文件夹helloWorld。

cd /Users/yourUserName/Documents/EOS/contracts
mkdir helloWorld
cd helloWorld

2. 在刚刚新建的文件夹里面创建一个C++文件。

touch helloWorld.cpp

3. 在其中编写以下内容。 

#include <eosio/eosio.hpp>  //导入eosio头文件

using namespace eosio;      //使用命名空间,可以使代码更加简洁,因为eos中所有类跟函数都在eosio命名空间下;

class [[eosio::contract]] helloWorld : public contract { //合约需继承自eosio::contract(因为使用了命名空间,此处可以简写为public contract);
							//同时使用[[eosio::contract]]来指定编译器生成主调度程序并用来生成ABI。
  public:
      using eosio::contract::contract;  //继承eosio::contract中的构造函数,可省略eosio::;

      [[eosio::action]]    //用eosio::action来修饰该方法为一个动作;
      void hello( name user ) {   //name是eosio中的一种常用数据类型;
		  print( "Hello World", user );
		}
};

注: #warning "<eosiolib/eosio.hpp> is deprecated use <eosio/eosio.hpp>"  ,eoiso.hpp源文件中写明以上警告。

4. 编译该文件。合约需要编译成wasm格式才能部署到区块链网络上。 

eosio-cpp helloWorld.cpp -o helloWorld.wasm

    会出现以下警告,无需理会。

Warning, empty ricardian clause file
Warning, empty ricardian clause file
Warning, action <hello> does not have a ricardian contract

    编译成功后在.cpp同目录下会多出.abi和.wasm两个文件。 .abi全称为application binary interface,是基于json的接口说明文档。.wasm为二进制文件,是Web Assembly的一种格式,其有另一种可读的文本格式wast,两者等价。

 5.创建一个叫hello的账户,后续将把上面的合约部署到该账号上。

cleos create account eosio hello EOS6GwYrUANGhouVRR97W8ukiCPX49Z74toqMTTxGH8mViYiPuuPi -p eosio@active

     意思是账户eosio创建类账户hello,并使用公钥EOS6G...uPi赋予了其active权限。如果使用 cleos get account hello后会发现owner权限公钥也被设置为与active权限同样的公钥

同时创建账户bob。此处为了简便使用了同样的公钥。

cleos create account eosio bob EOS6GwYrUANGhouVRR97W8ukiCPX49Z74toqMTTxGH8mViYiPuuPi -p eosio@active

6. 将helloWorld合约部署到账号hello上。

扫描二维码关注公众号,回复: 11058779 查看本文章
cleos set contract hello /Users/yourUserName/Documents/EOS/contracts/helloWorld -p hello@active

    命令行解释:set contract accountName contractDirect. 即部署到hello用户,合约地址为/...../helloWorld. 权限为hello用户到active权限。

    输出如下,因为我已经运行过了,所以这里显示abi与已存在的相同。 

Reading WASM from /Users/yourUserName/Documents/EOS/contracts/helloWorld/helloWorld.wasm...
Skipping set abi because the new abi is the same as the existing abi
Publishing contract...
executed transaction: b73420d8256676389aaeffbcd8f6e0d30d76d3cdaebae8ada5988844f3098cde  640 bytes  358 us
#         eosio <= eosio::setcode               {"account":"hello","vmtype":0,"vmversion":0,"code":"0061736d0100000001370b6000017f60027f7f0060037f7f...
warn  2020-04-22T01:24:38.839 thread-0  main.cpp:506                  print_result     warning: transaction executed locally, but may not be confirmed by the network yet

7.  执行helloWorld合约。

cleos push action hello hi '["bob"]' -p bob@active

     结果如下。

executed transaction: 8857adbe3a2671c98f39ec7dc212313163092eea09f096f4c3c14bbc64e396ae  104 bytes  154 us
#         hello <= hello::hello                 {"user":"bob"}
>> Hello Worldbob
warn  2020-04-22T01:27:29.754 thread-0  main.cpp:506                  print_result     warning: transaction executed locally, but may not be confirmed by the network yet

另外,下面添加了一行用户验证,即输入name参数必须与接收人一致,否则合约无法执行。

#include <eosio/eosio.hpp>  //导入eosio头文件

using namespace eosio;      //使用命名空间,可以使代码更加简洁,因为eos中所有类跟函数都在eosio命名空间下;

class [[eosio::contract]] helloWorld : public contract { //合约需继承自eosio::contract(因为使用了命名空间,此处可以简写为public contract);
							//同时使用[[eosio::contract]]来指定编译器生成主调度程序并用来生成ABI。
  public:
      using eosio::contract::contract;  //继承eosio::contract中的构造函数,可省略eosio::;

      [[eosio::action]]    //用eosio::action来修饰该方法为一个动作;
      void hello( name user ) {   //name是eosio中的一种常用数据类型;
		  require_auth( user );
		  print( "Hello World", name{user} );
		}
};

注:本文参考自官方开发文档,并加入适当注解。

发布了4 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_40462761/article/details/105696618