EOS挖坑笔记(三)智能合约的示例操作

目录

 

预设基本信息

一、Hello Eos

1、创建目录存放

2、创建实现的合约代码并编译

3、设置智能合约并测试

4、合约其他操作

二、搞搞发币

1、获取合约源

2、创建合约账户(可不创建)

3、编译合约

4、部署合约

5、创建代币

6、发行代币 (issue)

7、转移代币(transfer)

8、查看余额


预设基本信息

预设目录:CONTRACTS_DIR = /eos/contracts

语言选用: 根据官方的Demo ,C++

所要使用的钱包:先进行解锁,否则无法进行下一步操作

一、Hello Eos

1、创建目录存放

(虽然说可有可无,但是为了方便管理,还是创建一个目录存放)

之前预设的目录 CONTRACTS_DIR ,下创建一个目录'hello',目录,并进入目录

cd CONTRACTS_DIR
mkdir hello
cd hello

CONTRACTS_DIR 预先创建的工作目录
目录名称没有要求,随意

2、创建实现的合约代码并编译

1、创建代码

还是直接拷贝吧。

#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);
      }
};

EOSIO_DISPATCH( hello, (hi))

// 还记得C++ 的可以忽略以下内容
//using namespace eosio 使用头文件中的整个命名空间
//class hello : public contrac 是继承合约类
//[[eosio::action]]  添加C++ 11样式属性,这样abi生成器可以产生更可靠的输出。

EOSIO_DISPATCH( hello, (hi)),总体上就是发布的智能合约名称叫hello,被调用的方法叫hi。

2、编译代码

编译为Web assembly(.wasm)

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

3、设置智能合约并测试

1、设置智能合约

cleos set contract coffeeandice CONTRACTS_DIR/hello -p coffeeandice@active

//第一个coffeeandice是合约名称 
// CONTRACTS_DIR 参照自己合约的目录 
// hello@active   账户为hello ,权限为active

2、调用合约

cleos push action coffeeandice hi '["helloword"]' -p coffeeandice@active

// 调用coffeeandice合约的hi方法,参数为helloword,授权账号为coffeeandice。

返回结果

executed transaction: 80c8d2e5bae59bcd1fe42ebbdcd8cb8913bda6b2f277b8f067e69a0b109c51ad  104 bytes  264 us
#  coffeeandice <= coffeeandice::hi             {"user":"helloword"}
>> Hello, helloword

这时候所有账户都可以打招呼,执行合约操作

4、合约其他操作

1、require_auth 方法

1、代码更改

可以用来校验传入的参数是否是特定用户,此处利用接受的name 来传入参数

#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 ) {
         require_auth( user ); //传入name接受的参数,检查用户是否与参数相同
         print( "Hello, ", user);
      }
};

EOSIO_DISPATCH( hello, (hi))

2、编译并重新设置智能合约

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

cleos set contract coffeeandice CONTRACTS_DIR/hello -p coffeeandice@active

3、测试:

使用 名称不同的账户触发:

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

响应:

Error 3090004: Missing required authority
Ensure that you have the related authority inside your transaction!;
If you are currently using 'cleos push action' command, try to add the relevant authority using -p option.
Error Details:
missing authority of helloword
pending console output: 

使用 名称相同的账户触发:

cleos push action coffeeandice hi '["alice"]' -p alice@active

响应:

executed transaction: fec843e79be1a6f81899bbc97e21bb58218eed36377170f7701e67dbdbdf1122  104 bytes  275 us
#  coffeeandice <= coffeeandice::hi             {"user":"alice"}
>> Hello, alice

二、搞搞发币

获取合约源后可以查看一下相应代码,就能知道存在什么的方法

1、获取合约源

先进入目录

cd CONTRACTS_DIR   # 工作目录

拉取源数据:

获取存储库中包含的eosio.token 合约

git clone https://github.com/EOSIO/eosio.contracts --branch v1.4.0 --single-branch


cd eosio.contracts/eosio.token

2、创建合约账户(可不创建)

需要创建一个账户 / 其实也可以使用已有的账户来部署这份合约

3、编译合约

eosio-cpp -I include -o eosio.token.wasm src/eosio.token.cpp --abigen

4、部署合约

cleos set contract coffeeandice CONTRACTS_DIR/eosio.contracts/eosio.token --abi eosio.token.abi -p coffeeandice@active

第一个 coffeeandice 是部署合约的账号
第二个 eosio.token 是要部署的合约
-p coffeeandice的意思是授权账号为coffeeandice

响应

Reading WASM from /eos/contracts/eosio.contracts/eosio.token/eosio.token.wasm...
Publishing contract...
executed transaction: b4dba1cededd3e8ec9f065c55c9ad6b27d8f0182fca3339b59035f088bd09335  9608 bytes  2614 us
#         eosio <= eosio::setcode               {"account":"coffeeandice","vmtype":0,"vmversion":0,"code":"0061736d0100000001bb011f60000060037f7e7f0...
#         eosio <= eosio::setabi                {"account":"coffeeandice","abi":"0e656f73696f3a3a6162692f312e310008076163636f756e7400010762616c616e6...

5、创建代币

调用create(...)操作,根据官方和代码解释,此操作存在接受1个参数,它是一个symbolname类型,由两个数据组成,最大供应的浮点数和仅大写字母字符的symbolname,例如“1.0000 SYM”

发行人将是有权要求发行或执行其他操作,如冻结、召回和列入所有者的白名单。

示例:

cleos push action coffeeandice create '[ "coffeeandice", "1000000000.0000 LG"]' -p coffeeandice@active

1、coffeeandice 是合约名称
2、create 参数:
   ①发行人账户名称 
   ②空格前 :创造代币的数量, 小数点代表精度为N位
    空格后 :代币的符号  

3、-p eosio.token@active

结果:

executed transaction: fb1dddcc499bde5dcce11079b64c9c137d395841baa542c373d35015e5a2c0a9  120 bytes  827 us
#  coffeeandice <= coffeeandice::create         {"issuer":"coffeeandice","maximum_supply":"1000000000.0000 LG"}
warning: transaction executed locally, but may not be confirmed by the network yet         ] 

6、发行代币 (issue)

执行了“内联转移”,“内联转移”通知了发件人和收件人帐户,输出指示被调用的所有操作处理程序、调用它们的顺序以及操作是否生成任何输出。

cleos push action coffeeandice issue '[ "alice", "100.0000 LG", "hello" ]' -p coffeeandice@active


1、要检查交易,请尝试使用-d -j选项,它们表示“不要广播”和“将交易返回为json”,这在开发过程中可能会有用。

2、hello可以随意填写

结果:

executed transaction: ade4e7a44ca7ca223395a2ddb6b9328c1739df1bef9f5d4edac04bbd3b7bbf72  128 bytes  861 us
#  coffeeandice <= coffeeandice::issue          {"to":"alice","quantity":"100.0000 LG","memo":"hello"}
#  coffeeandice <= coffeeandice::transfer       {"from":"coffeeandice","to":"alice","quantity":"100.0000 LG","memo":"hello"}
#         alice <= coffeeandice::transfer       {"from":"coffeeandice","to":"alice","quantity":"100.0000 LG","memo":"hello"}
warning: transaction executed locally, but may not be confirmed by the network yet         ] 

7、转移代币(transfer)

cleos push action coffeeandice transfer '[ "alice", "bob", "25.0000 LG", "demos" ]' -p alice@active

结果:

executed transaction: 80a7544ec9fe70c61b79fce6407d97f1e255814cc30be2e11d085ff0c51cae91 136 bytes 575 us # coffeeandice <= coffeeandice::transfer 

{"from":"alice","to":"bob","quantity":"25.0000 LG","memo":"demos"} 
# alice <= coffeeandice::transfer {"from":"alice","to":"bob","quantity":"25.0000 LG","memo":"demos"}
 # bob <= coffeeandice::transfer {"from":"alice","to":"bob","quantity":"25.0000 LG","memo":"demos"}

8、查看余额

cleos get currency balance coffeeandice bob LG

返回结果:

25.0000 LG
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章