2.2 Tokens的部署,放出和转账

优质
小牛编辑
136浏览
2023-12-01

Step 1:获取合约资源

进入你的contracts目录.

cd /Users/zhong/coding/CLion/contracts

Pull代码

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

这个仓库有一些合约,我们现在导入的是eosio.token合约.先进入这个文件夹.

cd eosio.contracts/eosio.token

Step 2 : 为合约创建账户

在我们部署token合约前我们必须创建一个账户来部署它,我们使用eosio development key来生成该账户.

你可能需要先解锁你的钱包.

cleos create account eosio eosio.token EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

Step 3: 编译合约

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

Step 4: 部署Toekn合约

cleos set contract eosio.token /Users/zhong/coding/CLion/contracts/eosio.contracts/eosio.token --abi eosio.token.abi -p eosio.token@active

如果提示钱包锁着,要先解锁:

Reading WASM from ...
Publishing contract...
executed transaction: 69c68b1bd5d61a0cc146b11e89e11f02527f24e4b240731c4003ad1dc0c87c2c  9696 bytes  6290 us
#         eosio <= eosio::setcode               {"account":"eosio.token","vmtype":0,"vmversion":0,"code":"0061736d0100000001aa011c60037f7e7f0060047f...
#         eosio <= eosio::setabi                {"account":"eosio.token","abi":"0e656f73696f3a3a6162692f312e30000605636c6f73650002056f776e6572046e61...
warning: transaction executed locally, but may not be confirmed by the network yet         ]

Step 5: 创建Token

传入正确的参数调用create(...)来创建新的token.这个action接受一个参数,是symbol_name类型,由两部分数据组成,一个最大的可用浮点数和只能使用大写字母的symbol_name,例如"1.0000 SYM".token发行人有权发行token或执行其他action例如freezing,recalling和将某些拥有者设置到白名单.

下面简单的展示了调用该方法,使用positional arguments:

cleos push action eosio.token create '[ "eosio", "1000000000.0000 SYS"]' -p eosio.token@active

push action是传参调用action的命令,eosio.token是合约名,create是函数名,'[ "eosio", "1000000000.0000 SYS"]'是传递给action的参数,-p eosio.token@active是由哪个账户来授权.调用的合约函数对应如下:

void token::create( account_name issuer,
                    asset        maximum_supply )

Result:

executed transaction: 0e49a421f6e75f4c5e09dd738a02d3f51bd18a0cf31894f68d335cd70d9c0e12  120 bytes  1000 cycles
#   eosio.token <= eosio.token::create          {"issuer":"eosio","maximum_supply":"1000000000.0000 SYS"}

另一使用具名参数的方式如下:

cleos push action eosio.token create '{"issuer":"eosio", "maximum_supply":"1000000000.0000 SYS"}' -p eosio.token@active

Result:

executed transaction: 0e49a421f6e75f4c5e09dd738a02d3f51bd18a0cf31894f68d335cd70d9c0e12  120 bytes  1000 cycles
#   eosio.token <= eosio.token::create          {"issuer":"eosio","maximum_supply":"1000000000.0000 SYS"}

这一命令创建了一个新的tokenSYS,它的小数精度是4位,并且发行量是1000000000.0000.为了创建该token,需要eosio.token合约的权限.因此,需要传入-p eosio.token@active以授权该请求.

Setp 6: 发行Tokens

发行人能将这新的代币发给先前创建的"alice"账户:

cleos push action eosio.token issue '[ "alice", "100.0000 SYS", "memo" ]' -p eosio@active

Result:

executed transaction: 822a607a9196112831ecc2dc14ffb1722634f1749f3ac18b73ffacd41160b019  268 bytes  1000 cycles
#   eosio.token <= eosio.token::issue           {"to":"user","quantity":"100.0000 SYS","memo":"memo"}
>> issue
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"user","quantity":"100.0000 SYS","memo":"memo"}
>> transfer
#         eosio <= eosio.token::transfer        {"from":"eosio","to":"user","quantity":"100.0000 SYS","memo":"memo"}
#          user <= eosio.token::transfer        {"from":"eosio","to":"user","quantity":"100.0000 SYS","memo":"memo"}

这一输出包含几个actions:一个issue和三个transfers.被签名action只有issue,issue执行了"inline transfer"和通知sender以及接收账户的"inline transfer".该输出指示被调用的所有操作处理程序,调用它们的顺序以及操作是否生成任何输出.

技术上,eosio.token合约能够跳过inline transfer并选择直接修改余额.但是,在这种情况下,eosio.token合约遵循官方的token约定,该约定要求所有账户余额可以通过引用他们的转移操作的总和来推导.它还要求通知资金的发送方和接收方,以便他们可以自动处理存款和取款.

要检查交易,可以使用-d -j选项,他们指示"不要广播"和"以json返回交易",这能让你在开发的时候得到一些帮助.

cleos push action eosio.token issue '["alice", "100.0000 SYS", "memo"]' -p eosio@active -d -j

Result:

{
  "expiration": "2018-10-16T06:39:30",
  "ref_block_num": 13053,
  "ref_block_prefix": 3730552035,
  "max_net_usage_words": 0,
  "max_cpu_usage_ms": 0,
  "delay_sec": 0,
  "context_free_actions": [],
  "actions": [{
      "account": "eosio.token",
      "name": "issue",
      "authorization": [{
          "actor": "eosio",
          "permission": "active"
        }
      ],
      "data": "0000000000855c3440420f00000000000453595300000000046d656d6f"
    }
  ],
  "transaction_extensions": [],
  "signatures": [
    "SIG_K1_Kc9Hfc1FVsmZB3dnoc2UXp7SE3hLXgRdh3HTHWxTmgBRvwpfVN9sfHTGkwfZxS5sqAVixb1vUxN7iUNCscUUYAApWAW6Dt"
  ],
  "context_free_data": []
}

Step 7:Token转账

现在alice账户被分发了一些tokens,将其中的一些转账给bob.之前曾使用-p alice@active参数表示alice授权此操作.

cleos push action eosio.token transfer '[ "alice", "bob", "25.0000 SYS", "m" ]' -p alice@active

Result:

executed transaction: 06d0a99652c11637230d08a207520bf38066b8817ef7cafaab2f0344aafd7018  268 bytes  1000 cycles
#   eosio.token <= eosio.token::transfer        {"from":"alice","to":"bob","quantity":"25.0000 SYS","memo":"Here you go bob!"}
>> transfer
#          user <= eosio.token::transfer        {"from":"alice","to":"bob","quantity":"25.0000 SYS","memo":"Here you go bob!"}
#        tester <= eosio.token::transfer        {"from":"alice","to":"bob","quantity":"25.0000 SYS","memo":"Here you go bob!"}

现在可以通过cleos get currency balance来查看"bob"获得的tokens.

cleos get currency balance eosio.token bob SYS
25.00 SYS

我们再查看"alice"的tokens.

cleos get currency balance eosio.token alice SYS
75.00 SYS