Dapp开发EOS开发

EOS开发(四)通过eosio.token合约在私链发布代币

2018-08-30  本文已影响56人  yuyangray

1.概述

本文主要介绍如何在EOS私链环境创建代币、发行代币、转账,以及对BIOS Contract系统合约的讲解。

2.BIOS合约

此合约可以让我们直接控制其他帐户的资源分配,并使用一些特殊的API。在主链上,此合约管理着用户通过抵押和赎回token,来为其分配CPUNET内存资源

eosio账号加载bios合约

yuyangdeMacBook-Pro:cleos yuyang$ cleos set contract eosio ../../../build/contracts/eosio.bios -p eosio@active
Reading WASM from ../../../build/contracts/eosio.bios/eosio.bios.wasm...
Publishing contract...
executed transaction: 460436108729af8b8542cb23fa212d8d94ec5f89b2fa2ef1b64dee4b5569101f  3720 bytes  602 us
#         eosio <= eosio::setcode               {"account":"eosio","vmtype":0,"vmversion":0,"code":"0061736d0100000001621260037f7e7f0060057f7e7e7e7e...
#         eosio <= eosio::setabi                {"account":"eosio","abi":"0e656f73696f3a3a6162692f312e30050c6163636f756e745f6e616d65046e616d650f7065...
2018-08-29T08:53:19.997 thread-0   main.cpp:455                  print_result   warning: transaction executed locally, but may not be confirmed by the network yet

cleos生成了一个包含两个actiontransactioneosio::setcodeeosio::setabi

代码定义了合同的运行方式,abi描述了如何在参数的二进制json表示之间进行转换。虽然abi在技术上是可选的,但所有EOSIO工具都依赖于它以便于使用。

这可以理解为:eosio定义的动作setcode,是通过eosio合同带着参数{args...}执行的。

#         ${executor} <= ${contract}:${action} ${args...}
> console output from this execution, if any

我们将会在后面看到,action可以由多个合约处理。

此命令的最后一个参数-p eosio@active,表示通知cleos使用eosio账户的active权限对action进行签名。

3.创建测试用账户

3.1 创建eosio.token账户用于部署代币合约

yuyangdeMacBook-Pro:cleos yuyang$ cleos create key --to-console
Private key: 5KLLEUbQKyMuiSmD3c26ytuYpfio7xub7kFfWdfczKm7jZMrBfx
Public key: EOS5YHTVchTaPrH5H1AgMKftF3RW9gqDnzYfqjVi3YbL9Doz8FkEu
yuyangdeMacBook-Pro:cleos yuyang$ cleos create key --to-console
Private key: 5KKXCqS8eeqMwb8neiXy52kvyBELCWvT7oVR3kNhnhLuxgRxRzG
Public key: EOS8ZKp2gzUcudHs3CrLX96FyVEfAFGJMH9aWy8nG7vrES5uv4rMy

yuyangdeMacBook-Pro:cleos yuyang$ cleos wallet import --private-key 5KLLEUbQKyMuiSmD3c26ytuYpfio7xub7kFfWdfczKm7jZMrBfx
imported private key for: EOS5YHTVchTaPrH5H1AgMKftF3RW9gqDnzYfqjVi3YbL9Doz8FkEu
yuyangdeMacBook-Pro:cleos yuyang$ cleos wallet import --private-key 5KKXCqS8eeqMwb8neiXy52kvyBELCWvT7oVR3kNhnhLuxgRxRzG
imported private key for: EOS8ZKp2gzUcudHs3CrLX96FyVEfAFGJMH9aWy8nG7vrES5uv4rMy

yuyangdeMacBook-Pro:cleos yuyang$ cleos create account eosio eosio.token EOS5YHTVchTaPrH5H1AgMKftF3RW9gqDnzYfqjVi3YbL9Doz8FkEu EOS8ZKp2gzUcudHs3CrLX96FyVEfAFGJMH9aWy8nG7vrES5uv4rMy
executed transaction: a0c75cd5345490d15d2ed0f20a4aa084e6897dd7de3805e6c7a8e54463265d72  200 bytes  589 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"eosio.token","owner":{"threshold":1,"keys":[{"key":"EOS5YHTVchTaPrH5H1AgM...
2018-08-29T09:18:28.759 thread-0   main.cpp:455                  print_result   warning: transaction executed locally, but may not be confirmed by the network yet

查看账户:

yuyangdeMacBook-Pro:cleos yuyang$ cleos get account eosio.token
permissions: 
     owner     1:    1 EOS5YHTVchTaPrH5H1AgMKftF3RW9gqDnzYfqjVi3YbL9Doz8FkEu
        active     1:    1 EOS8ZKp2gzUcudHs3CrLX96FyVEfAFGJMH9aWy8nG7vrES5uv4rMy
memory: 
     quota:       unlimited  used:      2.66 KiB  

net bandwidth: 
     used:               unlimited
     available:          unlimited
     limit:              unlimited

cpu bandwidth:
     used:               unlimited
     available:          unlimited
     limit:              unlimited

3.2 创建usertester账户用于测试

user

yuyangdeMacBook-Pro:cleos yuyang$ cleos create key --to-console
Private key: 5JmNrNnjwG8iNTuubSyRYsutVVtKJQFM2jpCED8FHX4XTsGUV3Y
Public key: EOS7oTmuXiYRDd5NYqpHM3hzhDwuLyAMLDhKCGTWALxnJKHBK7ZF3
yuyangdeMacBook-Pro:cleos yuyang$ cleos create key --to-console
Private key: 5K5crdqhPQRjKZZc1euMDn4Zdtt1Brw88zmApYKhSNtdxmuKupf
Public key: EOS4uaJaieNEPCFp77o7vsYbf52s2Yygu2thYz7F12MgxkHLdRDfe

yuyangdeMacBook-Pro:cleos yuyang$ cleos wallet import --private-key 5JmNrNnjwG8iNTuubSyRYsutVVtKJQFM2jpCED8FHX4XTsGUV3Y
imported private key for: EOS7oTmuXiYRDd5NYqpHM3hzhDwuLyAMLDhKCGTWALxnJKHBK7ZF3
yuyangdeMacBook-Pro:cleos yuyang$ cleos wallet import --private-key 5K5crdqhPQRjKZZc1euMDn4Zdtt1Brw88zmApYKhSNtdxmuKupf
imported private key for: EOS4uaJaieNEPCFp77o7vsYbf52s2Yygu2thYz7F12MgxkHLdRDfe

yuyangdeMacBook-Pro:cleos yuyang$ cleos create account eosio user EOS7oTmuXiYRDd5NYqpHM3hzhDwuLyAMLDhKCGTWALxnJKHBK7ZF3 EOS4uaJaieNEPCFp77o7vsYbf52s2Yygu2thYz7F12MgxkHLdRDfe
executed transaction: 7b03239e75f087d2d6aa0915488615d14ac034cfb017cb8932d5aae380e7ca6f  200 bytes  229 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"user","owner":{"threshold":1,"keys":[{"key":"EOS7oTmuXiYRDd5NYqpHM3hzhDwu...
2018-08-29T06:36:11.734 thread-0   main.cpp:455                  print_result   warning: transaction executed locally, but may not be confirmed by the network yet

tester

yuyangdeMacBook-Pro:cleos yuyang$ cleos create key --to-console
Private key: 5KR9GbJSsQAnQCtmXrgLGdawKfQ3SfWGq23qKnsy5fsEfutS9Ww
Public key: EOS4yaYi2LKWBgVCSxkT2Tq7AxNHVPUYR3PA5k5Akhok1dogN5eGG
yuyangdeMacBook-Pro:cleos yuyang$ cleos create key --to-console
Private key: 5KZZqE7qnmNqrZtwgePtGR3EmNJnMn1FxVA7orUnSsFdfTH8CWF
Public key: EOS8FSevJXWHpweRz4S2fqa5Fh43LMdyrVuagYt3td8GGGJ6tvFpP

yuyangdeMacBook-Pro:cleos yuyang$ cleos wallet import --private-key 5KR9GbJSsQAnQCtmXrgLGdawKfQ3SfWGq23qKnsy5fsEfutS9Ww
imported private key for: EOS4yaYi2LKWBgVCSxkT2Tq7AxNHVPUYR3PA5k5Akhok1dogN5eGG
yuyangdeMacBook-Pro:cleos yuyang$ cleos wallet import --private-key 5KZZqE7qnmNqrZtwgePtGR3EmNJnMn1FxVA7orUnSsFdfTH8CWF
imported private key for: EOS8FSevJXWHpweRz4S2fqa5Fh43LMdyrVuagYt3td8GGGJ6tvFpP

yuyangdeMacBook-Pro:cleos yuyang$ cleos create account eosio tester EOS4yaYi2LKWBgVCSxkT2Tq7AxNHVPUYR3PA5k5Akhok1dogN5eGG EOS8FSevJXWHpweRz4S2fqa5Fh43LMdyrVuagYt3td8GGGJ6tvFpP
executed transaction: 91a6918b2c1d2df604cbd8dc9ba37a8b9fac57285523678256b274261d4ac5e2  200 bytes  6883 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"tester","owner":{"threshold":1,"keys":[{"key":"EOS4yaYi2LKWBgVCSxkT2Tq7Ax...
2018-08-29T06:40:22.991 thread-0   main.cpp:455                  print_result   warning: transaction executed locally, but may not be confirmed by the network yet

查看账号:

yuyangdeMacBook-Pro:cleos yuyang$ cleos get account user
permissions: 
     owner     1:    1 EOS7oTmuXiYRDd5NYqpHM3hzhDwuLyAMLDhKCGTWALxnJKHBK7ZF3
        active     1:    1 EOS4uaJaieNEPCFp77o7vsYbf52s2Yygu2thYz7F12MgxkHLdRDfe
memory: 
     quota:       unlimited  used:      2.66 KiB  

net bandwidth: 
     used:               unlimited
     available:          unlimited
     limit:              unlimited

cpu bandwidth:
     used:               unlimited
     available:          unlimited
     limit:              unlimited


yuyangdeMacBook-Pro:cleos yuyang$ cleos get account tester
permissions: 
     owner     1:    1 EOS4yaYi2LKWBgVCSxkT2Tq7AxNHVPUYR3PA5k5Akhok1dogN5eGG
        active     1:    1 EOS8FSevJXWHpweRz4S2fqa5Fh43LMdyrVuagYt3td8GGGJ6tvFpP
memory: 
     quota:       unlimited  used:      2.66 KiB  

net bandwidth: 
     used:               unlimited
     available:          unlimited
     limit:              unlimited

cpu bandwidth:
     used:               unlimited
     available:          unlimited
     limit:              unlimited

4.部署代币合约

此合约允许创建许多不同的token,这些token全部在同一合约上运行,但可能由不同的用户管理。

使用eosio.token账户部署合约build/contracts/eosio.token

yuyangdeMacBook-Pro:cleos yuyang$ cleos set contract eosio.token ../../../build/contracts/eosio.token -p eosio.token@active
Reading WASM from ../../../build/contracts/eosio.token/eosio.token.wasm...
Publishing contract...
executed transaction: 9fc1b00c97da6a1893cfcc05b9c25d76b0caed6a4ff10efdc164b812b9c7b2e7  8104 bytes  1023 us
#         eosio <= eosio::setcode               {"account":"eosio.token","vmtype":0,"vmversion":0,"code":"0061736d01000000017e1560037f7e7f0060057f7e...
#         eosio <= eosio::setabi                {"account":"eosio.token","abi":"0e656f73696f3a3a6162692f312e30010c6163636f756e745f6e616d65046e616d65...
2018-08-29T09:24:33.116 thread-0   main.cpp:455                  print_result   warning: transaction executed locally, but may not be confirmed by the network yet

5.创建代币

要创建代币,我们必须调用create方法,并使用正确的参数。此命令将注明代币的总量,以及用来与其他代币区分的唯一表示。发行人有发行代币的权限,并可以执行其他操作,例如冻结,召回和将账户加入白名单。

yuyangdeMacBook-Pro:cleos yuyang$ cleos push action eosio.token create '{"issuer":"eosio", "maximum_supply":"1000000000.0000 SYS"}' -p eosio.token@active
executed transaction: b43404b72fc79182617638f49994750886b03dffa87bea72e32b29a120dc845c  120 bytes  1096 us
#   eosio.token <= eosio.token::create          {"issuer":"eosio","maximum_supply":"1000000000.0000 SYS"}
2018-08-29T09:57:22.853 thread-0   main.cpp:455                  print_result   warning: transaction executed locally, but may not be confirmed by the network yet

此命令可以理解为:使用eosio.token账户的active权限,通过eosio.token合约,创建一个新的代币,指定代币发行人为eosio账户。

6.发行代币

代币已经创建成功,我们发行一些代币给user账户,此部分代币便可以流通了:

yuyangdeMacBook-Pro:cleos yuyang$ cleos push action eosio.token issue '[ "user", "100.0000 SYS", "memo" ]' -p eosio@active
executed transaction: bf06cb3f5df0503cf3ff1766de6ff171fbd37c5c7ec9164dda8b831ce39ab32e  128 bytes  8260 us
#   eosio.token <= eosio.token::issue           {"to":"user","quantity":"100.0000 SYS","memo":"memo"}
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"user","quantity":"100.0000 SYS","memo":"memo"}
#         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"}
2018-08-29T10:17:51.037 thread-0   main.cpp:455                  print_result   warning: transaction executed locally, but may not be confirmed by the network yet

此命令可以理解为:使用eosio账户的active权限,通过eosio.token合约,发行100个SYS给use账户。

输出的内容显示了四个action,一个issue和三个transfer。我们只签署了issueaction,这是一个内联转账。它会通知转账人和收款人账户。

可以使用参数-d -j表示不进行广播,并且将交易信息以json格式返回:

yuyangdeMacBook-Pro:cleos yuyang$ cleos push action eosio.token issue '["user", "100.0000 SYS", "memo"]' -p eosio@active -d -j
{
  "expiration": "2018-08-29T10:34:10",
  "ref_block_num": 15264,
  "ref_block_prefix": 3449560826,
  "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": "00000000007015d640420f00000000000453595300000000046d656d6f"
    }
  ],
  "transaction_extensions": [],
  "signatures": [
    "SIG_K1_K8gkaHQ42oqgu3N36F4BBLXbV95TEPeHEyfD9zAWntAkxKgw7ENEZswqJmgVUH8dWSpkArk5Eexg2xdCpYqnz4X4dmTcnK"
  ],
  "context_free_data": []
}

使用cleos get table eosio.token user accounts查看余额

yuyangdeMacBook-Pro:cleos yuyang$ cleos get table eosio.token user accounts
{
  "rows": [{
      "balance": "100.0000 SYS"
    }
  ],
  "more": false
}
yuyangdeMacBook-Pro:cleos yuyang$ cleos get table eosio.token eosio accounts
{
  "rows": [],
  "more": false
}

也可以使用以下方法查询:

yuyangdeMacBook-Pro:cleos yuyang$ cleos get currency balance eosio.token eosio
yuyangdeMacBook-Pro:cleos yuyang$ cleos get currency balance eosio.token user
100.0000 SYS

可以看到user账户有100个SYS,而eosio一个都没有。说明创建代币不等于发行代币,只有发行出来的代币才可进行流通。

7.代币转账

现在使用user的权限,从usertester进行代币转账:

yuyangdeMacBook-Pro:cleos yuyang$ cleos push action eosio.token transfer '[ "user", "tester", "25.0000 SYS", "m" ]' -p user@active
executed transaction: 5d0504b34569170f3c52d28915bdf94c10d7ad9e591d548fee72000b8a3f2a95  128 bytes  1824 us
#   eosio.token <= eosio.token::transfer        {"from":"user","to":"tester","quantity":"25.0000 SYS","memo":"m"}
#          user <= eosio.token::transfer        {"from":"user","to":"tester","quantity":"25.0000 SYS","memo":"m"}
#        tester <= eosio.token::transfer        {"from":"user","to":"tester","quantity":"25.0000 SYS","memo":"m"}
2018-08-30T01:59:45.077 thread-0   main.cpp:455                  print_result   warning: transaction executed locally, but may not be confirmed by the network yet

查询余额:

yuyangdeMacBook-Pro:cleos yuyang$ cleos get currency balance eosio.token eosio
yuyangdeMacBook-Pro:cleos yuyang$ cleos get currency balance eosio.token user
75.0000 SYS
yuyangdeMacBook-Pro:cleos yuyang$ cleos get currency balance eosio.token tester
25.0000 SYS
上一篇下一篇

猜你喜欢

热点阅读