EOS :入门踩坑之学习 3 智能合约开发

0.跟随EOS开发网站的教程:https://developers.eos.io/eosio-home/docs 以及众多大神

1.创建合约测试账号(test)

[root@bogon hello]# cleos create account eosio test EOS4uUhxvwm1DdDETv2g8NFgVRCZWzChAcciC4ek2tLuu6oBLDbtw -p eosio@active

在这里插入图片描述

如果 提示Error 3080006: Transaction took too long
应该是资源不足的原因,在命令 -x 1000 就可以执行了
(具体原因以后解决)

cleos createaccount eosio ${new_account} ${owner_key} ${active_key}

2.部署合约
在里以源码中的例子,~/eos/bulid/conctracts/hello

[root@bogon contracts]# cleos set contract test hello/ -p test
    # -p 是以私钥签署 test@active 可以切换权限
[root@bogon contracts]# cleos set contract test hello/ -p test@active

在这里插入图片描述

test 代表部署合约的账号
hello/ 代表合约所在的路径
-p test@active 和上文的相同

3.附加合约工具的使用eosio.cdt的使用
((EOSIO Contract Development Toolkit 即eos合约开发工具集))
官方提供的下载方式中这种是最好的不过时间有点长
下载:

git clone --recursive https://github.com/eosio/eosio.cdt --branch v1.3.2 --single-branch
cd eosio.cdt

组建:

./build.sh

安装

sudo ./install.sh

成功:
在这里插入图片描述

1.编译wasm文件

eosio-cpp  -o hello.wasm  hello.cpp --abigen

会生成hello.wasm,hello.abi。

第二种 cmake编译 (以后再说)

2.编译abi文件
这个命令在1.3.2版本是不对的 并没有–output这个参数

eosio-abigen hello.cpp --output=hello.abi

分享一下这个命令的记法:eosio-abigen abi+generator 直译abi文件的生成器
在这里插入图片描述

eosio-abigen hello.cpp -content=hello.abi

回到正文
4.调用智能合约
记得查看自己钱包状态是否解锁

[root@bogon contracts]# cleos push action test hi '["xiaoming"]' -p xiaoming@active

在这里插入图片描述

test 调用者

hi 调用的打印函数

“[“xiaoming”]” 函数参数,打印的名字账户名

hello.cpp(即智能合约中的内容)

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;

class hello : public contract {
  public:
      using contract::contract;

      [[eosio::action]]                 // 
      void hi( name user ) {
         print( "Hello, ", user);    // name
      }
};

EOSIO_DISPATCH( hello, (hi))

EOS的智能合约包含两方面action(动作)和transaction(交易)。action是对智能合约的函数调用,transcation包含一个或多个action。这个简单的示例就是action就是打印用户名

现在改一下,使用 require_auth 这个方法。

void hi (name user){
    require_auth( user );
    print("hello,",name{name} );
 }

然后编译,部署一下。
调用:

[root@bogon test]# cleos push action xiaoming hi '["hello"]' -p xiaoming -x 1000

在这里插入图片描述

这里抛出错误:Error 3090004: Missing required authority

[root@bogon test]# cleos push action xiaoming hi '["xiaoming"]' -p xiaoming -x 1000

在这里插入图片描述
成功了。现在能知道这个方法的用处。
. This method takes an name as a parameter, and will check if the user executing the action matches the provided paramter.
此方法采用名称作为参数,并将检查执行该操作的用户是否与所提供的参数匹配。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值