EOS 合约开发之 token

开发环境

使用 eos/builder 作为开发环境, 使用 master 分支最新代码.

启动 EOS 单节点

[email protected]:~# http get http://127.0.0.1:8888/v1/chain/get_info
HTTP/1.1 200 OK
Content-Length: 235
Content-type: application/json
Server: WebSocket++/0.7.0

{
    "head_block_id": "000000418661452eb2d425d0f1caed204af15d117bbf4ab1c82764235aff6d62",
    "head_block_num": 65,
    "head_block_producer": "eosio",
    "head_block_time": "2018-03-26T06:03:48",
    "last_irreversible_block_num": 64,
    "server_version": "83f6ce54"
}

创建钱包以及账户

[email protected]:~# cleos wallet create
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5Jg1u5EuNpcMiTMqM78a6NVbvNd9CHnYa39Mfu3TZ35XQvQZeur"
[email protected]:~# cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
[email protected]:~# cleos create key
Private key: 5JBrL9JTBxooBAxCU41PEXX1hFpPiPiCVH6PyFAtqEMAKNw8gwT
Public key: EOS6rgmK4g6L8nu7y2tXnfk4F8JZctZ5QKkvga3CAsfv9DFfLFmr1
[email protected]:~# cleos create key
Private key: 5K6bPikRThnWYM6XgvaWdezJrri1RRjoEthfZcUPvekHNKMyrX6
Public key: EOS4xAHcZfLjXbBiLYYYhohXjFyC2Ct45sc5hTRMCGyYRKbLyyNLD
[email protected]:~# cleos create account eosio token EOS6rgmK4g6L8nu7y2tXnfk4F8JZctZ5QKkvga3CAsfv9DFfLFmr1 EOS4xAHcZfLjXbBiLYYYhohXjFyC2Ct45sc5hTRMCGyYRKbLyyNLD
{
  "transaction_id": "103744a19981c7bd08e5b33e00d6fbca73a9b5f34ed062700f625fa8c3048379",
  "processed": {
    "status": "executed",
    "id": "103744a19981c7bd08e5b33e00d6fbca73a9b5f34ed062700f625fa8c3048379",
    "action_traces": [{
        "receiver": "eosio",
        "act": {
          "account": "eosio",
          "name": "newaccount",
          "authorization": [{
              "actor": "eosio",
              "permission": "active"
            }
          ],
          "data": "0000000000ea30550000000080a920cd01000000010003034c349df8bdaa38b98ca9911c3a26fd9b6d1d7af167b2eb5c3a287cf09f4b570100000100000001000208560867098550d4c51c4e62dedc1192906e7e83cbb406d3d9b7d726d9833eed0100000100000000010000000000ea305500000000a8ed32320100"
        },
        "console": "",
        "region_id": 0,
        "cycle_index": 0,
        "data_access": [{
            "type": "write",
            "code": "eosio",
            "scope": "eosio.auth",
            "sequence": 3
          }
        ]
      }
    ],
    "deferred_transactions": []
  }
}
[email protected]:~# cleos get account token
{
  "account_name": "token",
  "permissions": [{
      "perm_name": "active",
      "parent": "owner",
      "required_auth": {
        "threshold": 1,
        "keys": [{
            "key": "EOS4xAHcZfLjXbBiLYYYhohXjFyC2Ct45sc5hTRMCGyYRKbLyyNLD",
            "weight": 1
          }
        ],
        "accounts": []
      }
    },{
      "perm_name": "owner",
      "parent": "",
      "required_auth": {
        "threshold": 1,
        "keys": [{
            "key": "EOS6rgmK4g6L8nu7y2tXnfk4F8JZctZ5QKkvga3CAsfv9DFfLFmr1",
            "weight": 1
          }
        ],
        "accounts": []
      }
    }
  ]
}
cleos wallet import 5K6bPikRThnWYM6XgvaWdezJrri1RRjoEthfZcUPvekHNKMyrX6

上传 token 合约到区块链

[email protected]:~# cleos get code token
code hash: 0000000000000000000000000000000000000000000000000000000000000000
[email protected]:~# cd /tmp/build
[email protected]:~# cleos set contract token contracts/eosio.token/eosio.token.wast contracts/eosio.token/eosio.token.abi

[email protected]:/tmp/build# cleos get code token
code hash: 8a7e86ac5dc972c1b79f2f7c90ef73ef07ed78a8aee6e546459307eee213e299

测试 token 合约

cleos push action token create '{"issuer":"token", "maximum_supply": "1000000000.0000 CUR", "can_freeze": 1, "can_recall": 1, "can_whitelist": 1}' -p [email protected]
cleos push action token issue '{"to":"token","quantity":"1000.0000 CUR","memo":"issue"}' --permission [email protected]
cleos push action token transfer '{"from":"token","to":"eosio","quantity":"20.0000 CUR","memo":"my first transfer"}' --permission [email protected]

查看转账后账户 balance

注意目前 master 分支 table 有 bug, 等待官方进行 fix.

cleos get table eosio token account
cleos get table token token account