Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 视频教程 | 打赏(Donations) | About
知乎专栏多维度架构微信号 netkiller-ebook | QQ群:128659835 请注明“读者”

5.5. 智能合约 - EOS 代币

5.5.1. 编译智能合约

编译 eosio.bios 合约

				
cd /usr/local/src/eos/build/contracts/eosio.bios

[root@netkiller eosio.bios]# make
[  4%] Built target libc++
[  4%] Built target wasm
[  4%] Built target ast
[  4%] Built target asmjs
[  4%] Built target cfg
[ 10%] Built target passes
[ 12%] Built target support
[ 14%] Built target eosio-s2wasm
[ 17%] Built target Platform
[ 17%] Built target Logging
[ 17%] Built target IR
[ 17%] Built target WASM
[ 17%] Built target WAST
[ 17%] Built target eosio-wast2wasm
[ 19%] Built target eosiolib
[100%] Built target libc
[100%] Built target eosio.bios			
				
			

编译 eosio.token 合约

				
cd /usr/local/src/eos/build/contracts/eosio.token

[root@netkiller eosio.token]# pwd
/usr/local/src/eos/build/contracts/eosio.token
[root@netkiller eosio.token]# make
[  4%] Built target libc++
[  4%] Built target wasm
[  4%] Built target ast
[  4%] Built target asmjs
[  4%] Built target cfg
[ 10%] Built target passes
[ 12%] Built target support
[ 14%] Built target eosio-s2wasm
[ 17%] Built target Platform
[ 17%] Built target Logging
[ 17%] Built target IR
[ 17%] Built target WASM
[ 17%] Built target WAST
[ 17%] Built target eosio-wast2wasm
[ 19%] Built target eosiolib
[100%] Built target libc
[100%] Built target eosio.token				
				
			

5.5.2. 设置初始化账号 eosio

从配置文件 ~/.local/share/eosio/nodeos/config/config.ini 中查找 signature-provider

				
[root@netkiller ~]# grep "^signature-provider" ~/.local/share/eosio/nodeos/config/config.ini 
signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
				
			

找到 signature-provider 配置项,复制秘钥 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

				
[root@netkiller ~]# cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
				
			

导入 eosio 账号私钥到 default 钱包

				

[root@netkiller ~]# cleos wallet keys
[
  "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
]
				
			

5.5.3. 创建账号

创建秘钥对

				
[root@netkiller ~]# cleos create key
Private key: 5JFMTVk4EjWW54xk73AMRPf5JbpFV2Cm7vtgt1jr9zVaPgLmaLQ
Public key: EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr
				
			

导入私钥

				
[root@netkiller ~]# cleos wallet import 5JFMTVk4EjWW54xk73AMRPf5JbpFV2Cm7vtgt1jr9zVaPgLmaLQ
imported private key for: EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr		
	
[root@netkiller ~]# cleos wallet keys
[
  "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
  "EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr"
]	
				
			

创建账号 neo

				
[root@netkiller ~]# cleos wallet unlock				
[root@netkiller ~]# cleos create account eosio neo EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr
executed transaction: e138b1e7557d76b3560b898942db942eb23b43f8387c60083741ab4d0680e139  200 bytes  311 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"neo","owner":{"threshold":1,"keys":[{"key":"EOS7fcRYssRt5SXVnsPpRNzj86E9h...
warning: transaction executed locally, but may not be confirmed by the network yet
				
			

5.5.4. 部署合约 eosio.bios

				
[root@netkiller ~]# cleos wallet unlock
[root@netkiller ~]# cleos set contract eosio /usr/local/src/eos/build/contracts/eosio.bios -p eosio
				
			
				
[root@netkiller ~]# cleos set contract eosio /usr/local/src/eos/build/contracts/eosio.bios -p eosio
Reading WAST/WASM from /usr/local/src/eos/build/contracts/eosio.bios/eosio.bios.wasm...
Using already assembled WASM...
Publishing contract...
executed transaction: c8589dc4ddb429765e86e78add1420461ce35a4edac7e08fe790e4b876a1ce29  3720 bytes  815 us
#         eosio <= eosio::setcode               {"account":"eosio","vmtype":0,"vmversion":0,"code":"0061736d0100000001621260037f7e7f0060057f7e7e7e7e...
#         eosio <= eosio::setabi                {"account":"eosio","abi":"0e656f73696f3a3a6162692f312e30050c6163636f756e745f6e616d65046e616d650f7065...
warning: transaction executed locally, but may not be confirmed by the network yet				
				
			

5.5.5. 创建账号 netkiller

创建账号 netkiller 重复上面步骤,这个账号用于创建代币智能合约。

				
[root@netkiller ~]# cleos create key
Private key: 5KVTLTRgLdKj4b5FkkFpYMhYdhimPip3dtdfnZAQVQxQBBV4oFq
Public key: EOS5NyaD49BuTCScNEY8FPBCZ9t6VXThMAmFvgMg72XqcNVPXuEWH

[root@netkiller ~]# cleos wallet import 5KVTLTRgLdKj4b5FkkFpYMhYdhimPip3dtdfnZAQVQxQBBV4oFq
imported private key for: EOS5NyaD49BuTCScNEY8FPBCZ9t6VXThMAmFvgMg72XqcNVPXuEWH

[root@netkiller ~]# cleos create account eosio netkiller EOS5NyaD49BuTCScNEY8FPBCZ9t6VXThMAmFvgMg72XqcNVPXuEWH EOS5NyaD49BuTCScNEY8FPBCZ9t6VXThMAmFvgMg72XqcNVPXuEWH
executed transaction: fc87fc5cb598a24b36bf3dc10c542d7425d319d33291029de1f0c412dadea233  200 bytes  301 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"netkiller","owner":{"threshold":1,"keys":[{"key":"EOS5NyaD49BuTCScNEY8FPB...
warning: transaction executed locally, but may not be confirmed by the network yet				
				
			

5.5.6. EOS 代币合约

				
cleos set contract netkiller /usr/local/src/eos/build/contracts/eosio.token
				
			

操作演示

				
[root@netkiller ~]# cleos set contract netkiller /usr/local/src/eos/build/contracts/eosio.token
Reading WAST/WASM from /usr/local/src/eos/build/contracts/eosio.token/eosio.token.wasm...
Using already assembled WASM...
Publishing contract...
executed transaction: a8bdeafdadd37b6a3b2bf1de908725028e51ae3d5f8a9e0f95e4d33b3b22b8be  8104 bytes  1411 us
#         eosio <= eosio::setcode               {"account":"netkiller","vmtype":0,"vmversion":0,"code":"0061736d01000000017e1560037f7e7f0060057f7e7e...
#         eosio <= eosio::setabi                {"account":"netkiller","abi":"0e656f73696f3a3a6162692f312e30010c6163636f756e745f6e616d65046e616d6505...
warning: transaction executed locally, but may not be confirmed by the network yet				
				
			

code hash 有值表示合约部署成功

				
[root@netkiller ~]# cleos get code netkiller
code hash: 641f336aa1d08526201599c3c0ddb7a646e5ac8f9fd2493f56414d0422a0f957				
				
			

code hash 为 0 表示合约部署失败

				
[root@netkiller ~]# cleos get code netkiller
code hash: 0000000000000000000000000000000000000000000000000000000000000000				
				
			

5.5.7. 创建代币

				
[root@netkiller eosio.token]# cleos wallet unlock
password: Unlocked: default

[root@netkiller eosio.token]# cleos push action netkiller create '["netkiller","1000 EOS",0,0,0]' -p netkiller
executed transaction: 7266dda0b3fde45bd03fc0ab4fdece35e66cf6e616165f113434f63394f3fa33  120 bytes  488 us
#     netkiller <= netkiller::create            {"issuer":"netkiller","maximum_supply":"1000 EOS"}
warning: transaction executed locally, but may not be confirmed by the network yet

[root@netkiller ~]# cleos push action netkiller create '["netkiller","10000 NRC",0,0,0]' -p netkiller
executed transaction: cbb45fcca9e140f24cd90fb6c0fbb781afcc3a1d6da4dc912a7fde79b26b7dd0  120 bytes  521 us
#     netkiller <= netkiller::create            {"issuer":"netkiller","maximum_supply":"10000 NRC"}
warning: transaction executed locally, but may not be confirmed by the network yet
				
			

查看合约信息

				
[root@netkiller ~]# cleos get currency stats netkiller EOS
{
  "EOS": {
    "supply": "1000 EOS",
    "max_supply": "1000 EOS",
    "issuer": "netkiller"
  }
}				
				
			

另一个方式

				
cleos push action netkiller create '{"issuer":"netkiller", "maximum_supply": "10.0000 EOS", "can_freeze": 1, "can_recall": 1, "can_whitelist": 1}' -p netkiller@active				
				
			

5.5.8. 发放代币

				
cleos push action netkiller issue '["neo","1000 EOS","issue"]' -p netkiller
				
			

给 neo 账号发放 1000 个 EOS 币

				
[root@netkiller ~]# cleos push action netkiller issue '["neo","1000 EOS","issue"]' -p netkiller
executed transaction: c60760dfbdad2face6917ff28015555f1cfc293d71eb7556fc2f7ec78591229b  128 bytes  1339 us
#     netkiller <= netkiller::issue             {"to":"neo","quantity":"1000 EOS","memo":"issue"}
#     netkiller <= netkiller::transfer          {"from":"netkiller","to":"neo","quantity":"1000 EOS","memo":"issue"}
#           neo <= netkiller::transfer          {"from":"netkiller","to":"neo","quantity":"1000 EOS","memo":"issue"}
warning: transaction executed locally, but may not be confirmed by the network yet				
				
			
				
cleos push action netkiller issue '{"to":"neo","quantity":"10.0000 EOS","memo":"备注信息"}' --permission netkiller@active				
				
			

5.5.9. 查看代币余额

				
[root@netkiller ~]# cleos get table netkiller neo accounts
{
  "rows": [{
      "balance": "1000 EOS"
    }
  ],
  "more": false
}				
				
			

				
[root@netkiller ~]# cleos get currency balance netkiller neo
1000 EOS

[root@netkiller ~]# cleos get currency balance netkiller neo EOS
1000 EOS
				
			
				

				
			

5.5.10. 转账

				
cleos push action eosio transfer '["eosio","netkiller","100 EOS",""]' -p eosio		
cleos push action contract transfer '{"from":"from_address","to":"to_address","quantity":"1.0000 EOS","memo":"测试"}' --permission neo@active		
				
			

操作演示

				
[root@netkiller ~]# cleos get currency balance netkiller netkiller EOS

[root@netkiller ~]# cleos push action netkiller transfer '["neo","netkiller","10 EOS","memo"]' -p neo
executed transaction: 0e23837bd8a3a7876b2463cbde1d47a25d2ac2178bb42ddbccd3037416cc9e43  136 bytes  745 us
#     netkiller <= netkiller::transfer          {"from":"neo","to":"netkiller","quantity":"10 EOS","memo":"memo"}
#           neo <= netkiller::transfer          {"from":"neo","to":"netkiller","quantity":"10 EOS","memo":"memo"}
warning: transaction executed locally, but may not be confirmed by the network yet

[root@netkiller ~]# cleos get currency balance netkiller netkiller EOS
10 EOS