New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EOS 智能合约体验 #5

Closed
kaidiren opened this Issue Mar 12, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@kaidiren
Owner

kaidiren commented Mar 12, 2018

接上文: #4

官方文档地址: https://github.com/EOSIO/eos#example-currency-contract-walkthrough

智能合约示例

首先启动 节点

docker-compose up

导入帐号

在创世区块的配置中已经存在一个 eosio 的账户, 把私钥导入.

eosioc wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

为 "currency" 合约创建帐号

先生成2个密钥对:

rkd@mac:~/blockchain/eos/Docker$ eosioc create key
Private key: 5KMohsQ5cqXGgMRPH5qXqaTpRhrThwtGT7e8sqS7KuxXRxGLFfW
Public key: EOS55iuKLbH2XhNv4pJXjNtxTJYoD5Xes2PuB4N7LYfgdeEjFPWat   // owner_key
rkd@mac:~/blockchain/eos/Docker$ eosioc create key
Private key: 5K8T42F56S6VX3bFh3sC6UcNjC1aQEY37AmK2rdnXVzsSX3YeCT
Public key: EOS6RPNryk7rxkrcxKEigmrNBS4dzGcUqdoWT4HupbrbYnVro5npM  // active_key

通过 eosio 这个帐号, 创建 currency 帐号

eosioc create account eosio currency EOS55iuKLbH2XhNv4pJXjNtxTJYoD5Xes2PuB4N7LYfgdeEjFPWat EOS55iuKLbH2XhNv4pJXjNtxTJYoD5Xes2PuB4N7LYfgdeEjFPWat

会返回一大段 json 串就说明成功了

查询一下:
eosioc get account currency

返回结果:

{
  "account_name": "currency",
  "permissions": []
}

导入钱包私钥:

eosioc wallet import 5KMohsQ5cqXGgMRPH5qXqaTpRhrThwtGT7e8sqS7KuxXRxGLFfW
eosioc wallet import 5K8T42F56S6VX3bFh3sC6UcNjC1aQEY37AmK2rdnXVzsSX3YeCT

上传 "currency" 合约

上传前可以查看下:

eosioc get code currency
code hash: 0000000000000000000000000000000000000000000000000000000000000000

上传:

eosioc set contract currency ../../contracts/currency/currency.wast ../../contracts/currency/currency.abi

返回的结果中带有 transaction_id 字段那就说明成功了

你可以再查询验证下:

eosioc get code currency

返回结果看起来像这样子:

code hash: 9b9db1a7940503a88535517049e64467a6e8f4e9e03af15e9968ec89dd794975

使用这个合约前,要现先生成代币.

eosioc push action currency issue '{"to":"currency","quantity":"1000.0000 CUR"}' --permission currency@active

验证下看看余额对不对:

eosioc get table currency currency account
{
  "rows": [{
     "currency": 1381319428,
     "balance": 10000000
     }
  ],
  "more": false
}

通过合约交易

eosioc push action currency transfer '{"from":"currency","to":"eosio","quantity":"20.0000 CUR","memo":"my first transfer"}' --permission currency@active

返回的结果中带有 transaction_id 字段那就说明成功了

通过合约查询余额

eosioc get table eosio currency account
{
  "rows": [{
      "currency": 1381319428,
      "balance": 200000
       }
    ],
  "more": false
}
eosioc get table currency currency account
{
  "rows": [{
      "currency": 1381319428,
      "balance": 9800000
    }
  ],
  "more": false
}

显然余额已经发生变化了.

@kaidiren kaidiren closed this Mar 12, 2018

@kaidiren kaidiren added the 区块链 label Mar 12, 2018

@kaidiren kaidiren reopened this Mar 12, 2018

@kaidiren kaidiren closed this Mar 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment