EOS开发(2) -- 发布智能合约

eos.png

目录

EOS开发(1) -- 安装及常用工具介绍
EOS开发(2) -- 发布智能合约
EOS开发(3) -- currency合约分析

之前我写过一篇文章,如何在以太坊上构建dapp,感兴趣的可以先去看下以太坊上如何编写发布智能合约的,eos现在测试链3.0还没有发布,所以相比以太坊来说工具还不完善,所以想要深入了解的话需要花点功夫研究.

EOS 常用命令介绍

  • eosd
eosd 是 eos的核心后台进程,想钱包账户查询服务,生产区块服务都是依赖这个进程的启动
  • eosc
eosc 用来和eosd暴露出的rest接口交互的一个命令行工具为了使用eosc,
你需要指定eosd实例的ip地址和端口号,执行以下eosc可以看到下面列出的各种用法

$ ./eosc
ERROR: RequiredError: Subcommand required
Command Line Interface to Eos Client
Usage: ./eosc [OPTIONS] SUBCOMMAND

Options:
  -h,--help                   Print this help message and exit 
  # 下面这个用来指定eosd实例ip,默认本地地址
  -H,--host TEXT=localhost    the host where eosd is running 这
  # 下面用来指定端口号 默认 8888 
  -p,--port UINT=8888         the port where eosd is running
  --wallet-host TEXT=localhost
                              the host where eos-walletd is running
  --wallet-port UINT=8888     the port where eos-walletd is running
  -v,--verbose                output verbose messages on error

Subcommands:
  # 获取版本信息
  version                     Retrieve version information
  # 创建钱包 或者账户等命令
  create                      Create various items, on and off the blockchain
  # 去区块链上查询的命令
  get                         Retrieve various items and information from the blockchain
  # 发布或者更新智能合约命令
  set                         Set or update blockchain state
  # 账户间转账的命令
  transfer                    Transfer EOS from account to account
  # 点对点网络节点交互连接的命令
  net                         Interact with local p2p network connections
  # 和钱包交互的命令
  wallet                      Interact with local wallet
  # 执行基准测试的命令
  benchmark                   Configure and execute benchmarks
  # 推消息到链上的命令
  push       Push arbitrary transactions to the blockchain

发布合约前准备工作

  • 创建一个钱包
    创建钱包前我们需要知道一个钱包可能有多个账户,账户之间是交互的主体,eos可以锁定钱包来控制该钱包下所有账户资金的安全,所以首先我们需要创建一个钱包,然后再创建一些账户,把这些账户的私钥导入到钱包中
cd ~/eos/build/programs/eosc/
./eosc wallet create 
上面命令会输出一个密码 这个密码是用来锁定或者介绍钱包用的 这个需要保存好,而且会生成一个名为的default钱包
  • 此时我们default钱包里没有账户所以我们要导入一个账户,还记得上一节我们备份的genisis.json.back那个文件吗,这个文件中有个inita账户的私钥,我们可以把它导入到我们的钱包中
./eosc wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
  • 创建秘钥对
    因为eos提供了权限控制的概念,提供了有owner 和 active的角色,owner代表所有者,active代表可以操作(其实这两个都是特别高的权限,都能做任何交易上的操作,只是owner权限更高一点),eos 创建账户需要指定 owner key 和 active key,所以我们需要先创建两对key
cd ~/eos/build/programs/eosc/
$ ./eosc create key
Private key: 5JL6auvLndKnbtTk63EjPFVZ2giFKiUZo97siZx5PXcXsbQVEcq
Public key: EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1 # owner key

$ ./eosc create key
Private key: 5JXgdQwueP9WNXpYWkQkRgL18LEvVy4e9KwyAPzdVxdT9ibqVDW
Public key: EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ # active key

每执行一次create key ,都会生成一对公私钥对
  • 创建账户
./eosc create account inita test  PUBLIC_KEY_1(owner_key) PUBLIC_KEY_2(active_key)
上面的命令相当于创建了一个test用户,因为inita是我们已经导入进来的,这里inita可以当做test的创建者,key1,key2把我们上面生成的两个公钥输入进去就ok了

$ ./eosc create account inita test EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1 EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ
441183ms            main.cpp:1195                 main                 ] Failed with error: Assert Exception (10)
status_code == 200: Error code 401
: {"code":401,"message":"UnAuthorized","details":"signatures do not satisfy declared authorizations (3030002)\nTransaction declares authority '{\"account\":\"inita\",\"permission\":\"active\"}', but does not have signatures for it.\n"}

参数替换,执行后我们发现上面执行失败,提示未授权,其实是我们还没有把刚刚创建的active 秘钥导入钱包

  • 导入创建的active key 秘钥到钱包
$ ./eosc wallet import 5JXgdQwueP9WNXpYWkQkRgL18LEvVy4e9KwyAPzdVxdT9ibqVDW
imported private key for: EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ

可以看到导入成功

  • 再次执行创建账户 test
$ ./eosc create account inita test EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1 EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ
{
  "transaction_id": "76f574b783e3c67599047009c84acb72e2be3d1c08c1b5e6f303d0f19be99b60",
  "processed": {
    "ref_block_num": 36260,
    "ref_block_prefix": 3203619337,
    "expiration": "2018-03-14T15:19:25",
    "scope": [
      "eos",
      "inita"
    ],
    "signatures": [
      "20422d4f3f4900344ef774c65e5704fa1abea3796e9353eda466478b80d47d59e54d00cd9e1c65a3507242cb91eebc0f52334d21f129db1494ee3d832ddf9b85ac"
    ],
    "messages": [{
        "code": "eos",
        "type": "newaccount",
        "authorization": [{
            "account": "inita",
            "permission": "active"
          }
        ],
        "data": {
          "creator": "inita",
          "name": "test",
          "owner": {
            "threshold": 1,
            "keys": [{
                "key": "EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1",
                "weight": 1
              }
            ],
            "accounts": []
          },
          "active": {
            "threshold": 1,
            "keys": [{
                "key": "EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ",
                "weight": 1
              }
            ],
            "accounts": []
          },
          "recovery": {
            "threshold": 1,
            "keys": [],
            "accounts": [{
                "permission": {
                  "account": "inita",
                  "permission": "active"
                },
                "weight": 1
              }
            ]
          },
          "deposit": "0.0001 EOS"
        },
        "hex_data": "000000000093dd74000000000090b1ca0100000001024c59ba35833eabc71d794ed79ba8ab26a232ddd4c4790c2282f20200cc0baf73010000010000000102343387e05944b65216def1599960aca2d6a03adcfd37930b5aac21b97f4c5839010000010000000001000000000093dd7400000000a8ed32320100010000000000000004454f5300000000"
      }
    ],
    "output": [{
        "notify": [],
        "deferred_trxs": []
      }
    ]
  }
}

可以看到执行成功了,是这样的创建账户前需要创建者账户里有钱,因为eos基于dpos一种pos的变种,有stake(eos token)才能去使用资源,这笔钱会从创建者账户里扣除,这里也就是inita

  • 查询test账户信息
$ ./eosc get account test
{
  "account_name": "test",
  "eos_balance": "0.0000 EOS",
  "staked_balance": "0.0001 EOS",
  "unstaking_balance": "0.0000 EOS",
  "last_unstaking_time": "2106-02-07T06:28:15",
  "permissions": [{
      "perm_name": "active",
      "parent": "owner",
      "required_auth": {
        "threshold": 1,
        "keys": [{
            "key": "EOS5HUmMgJRoxiuPdeiGpbCJvQ7pGkSAEKV45d1WVbkkN8vFHWjYJ",
            "weight": 1
          }
        ],
        "accounts": []
      }
    },{
      "perm_name": "owner",
      "parent": "",
      "required_auth": {
        "threshold": 1,
        "keys": [{
            "key": "EOS5U7dBVwkwiUvsBBYuhR2yhkWYXYCK5mi6JgZjE7jwvXyxbArd1",
            "weight": 1
          }
        ],
        "accounts": []
      }
    }
  ]
}

可以看到 "staked_balance": "0.0001 EOS",表明test账户里有0.0001个eos,不过这写token被锁定了

  • 部署currency合约
$ ./eosc set  contract test  ../../contracts/currency/currency.wast ../../contracts/currency/currency.abi
test 这里表示合约发布的账户

执行命令后后你会看到返回一个带有transaction_id的json字符串,说明部署成功了