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

二、搞搞coin 

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

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、创建coin 

调用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、发行coin  (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、转移coin (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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要开发一个NFT智能合约,你需要掌握Solidity编程语言和区块链技术。以下是一些步骤: 1. 了解NFT:首先,你需要了解NFT是什么以及它们在区块链上的作用。NFT代表非替代性令牌,这是一种数字资产,是唯一的,不可替代的,并在区块链上以智能合约的形式存储。 2. 设计合约:设计你的合约,包括定义NFT的属性和功能。你需要确定NFT的名称、描述、图像和其他元数据。你还需要定义如何创建、转移和销毁NFT。 3. 编写智能合约:使用Solidity编写你的智能合约代码。你需要定义NFT的结构和函数,例如创建、转移和销毁函数。确保你的代码是安全的,并遵循最佳实践。 4. 部署合约:将你的智能合约部署到区块链上。你需要选择一个适合你的区块链平台,例如以太坊、EOS或TRON,并使用相应的工具将合约部署到该平台上。 5. 测试合约:在合约上运行单元测试和集成测试,确保代码的正确性和安全性。你可以使用Truffle等测试框架来测试你的合约。 6. 发布合约:发布你的合约,让其他人可以使用它来创建、转移和销毁NFT。你可以在区块链上创建一个市场,让用户可以交易他们的NFT。 请注意,开发智能合约需要具备一定的技术能力和区块链知识。如果你是初学者,请先学习Solidity编程语言和区块链技术,然后再着手开发NFT智能合约

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值