搭建基础 EOS 测试节点和开发环境

搭建测试节点 - Docker

创建本地文件夹 /home/wumingxiaozu/contracts, 以后的 CONTRACTS_DIR 都指的是这个地址

使用全路径替换 Docker 启动脚本

1
2
3
4
5
6
7
8
9
10
#!/bin/bash

docker run --name eosio \
--publish 7777:7777 \
--publish 127.0.0.1:5555:5555 \
--volume /home/wumingxiaozu/contracts:/home/wumingxiaozu/contracts \
--detach \
eosio/eos \
/bin/bash -c \
"keosd --http-server-address=0.0.0.0:5555 & exec nodeos -e -p eosio --plugin eosio::producer_plugin --plugin eosio::history_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_plugin --plugin eosio::history_api_plugin --plugin eosio::http_plugin -d /mnt/dev/data --config-dir /mnt/dev/config --http-server-address=0.0.0.0:7777 --access-control-allow-origin=* --contracts-console --http-validate-host=false --filter-on='*'"

检验安装结果

1
2
3
4
5
6
docker logs --tail 10 eosio

docker exec -it eosio bash
cleos --wallet-url http://127.0.0.1:5555 wallet list keys

curl http://localhost:7777/v1/chain/get_info

创建快捷脚本, 注意使用 source 使之生效

1
alias cleos='docker exec -it eosio /opt/eosio/bin/cleos --url http://127.0.0.1:7777 --wallet-url http://127.0.0.1:5555'

安装合约编译环境

下载代码

1
2
3
4
5
6
7
8
git clone --recursive https://github.com/eosio/eosio.cdt
cd eosio.cdt

./build.sh SYS

sudo ./install.sh
或者
alias eosio-cpp="/path/to/eosio-cpp"

创建测试账号

首先创建默认钱包的密码

1
2
3
4
# 这里就使用了前面定义的别名 cleos
cleos wallet create --to-console
或者指定钱包名
cleos wallet create -n tokiyotomare --to-console
1
2
3
4
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5Jqmm5xx9FqHVgzU2p1a2d8BhNw21S7Hij9pCHkQ8AVihnwgXkG"
1
2
3
4
Creating wallet: tokiyotomare
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5JEDCrLsqpPCQoTy8Kk5nuZvN1vSUzfHW8EHpiZBVgViizX4Ptx"

查看钱包是否创建

1
2
3
4
5
cleos wallet open [-n tokiyotomare]
cleos wallet list

cleos wallet unlock [-n tokiyotomare] # 此处需要上门创建的密码
cleos wallet list

创建 key

1
cleos wallet create_key
1
Created new private key with a public key of: "EOS7athvxS527mUALbh6RP332SYqS3zVeEyk5m7YT7ty6RRhMGA3L"

导入共有测试钱包私钥

1
cleos wallet import

输入共用私钥: 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

1
private key: imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

创建账号

1
2
cleos create account eosio justin EOS7athvxS527mUALbh6RP332SYqS3zVeEyk5m7YT7ty6RRhMGA3L 
cleos create account eosio wumingxiaozu EOS7athvxS527mUALbh6RP332SYqS3zVeEyk5m7YT7ty6RRhMGA3L
1
2
3
4
5
6
7
executed transaction: 06d63c647e673094624d6f579cca49ffe7f1202cca8651b551399736da76d876  200 bytes  1763 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"justin","owner":{"threshold":1,"keys":[{"key":"EOS7athvxS527mUALbh6RP332S...
warning: transaction executed locally, but may not be confirmed by the network yet ]

executed transaction: 0d17131e7a8f9c969cb41b27133b70bc173d632d2860926c758350b546c1f726 200 bytes 2471 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"wumingxiaozu","owner":{"threshold":1,"keys":[{"key":"EOS7athvxS527mUALbh6...
warning: transaction executed locally, but may not be confirmed by the network yet ]

执行合约部署的资金

创建一个 token 管理账户

1
cleos create account eosio eosio.token EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

编译安装 token 合约

1
2
3
4
5
6
7
8
cd ${CONTRACTS_DIR}

git clone https://github.com/EOSIO/eosio.contracts
cd eosio.contracts/eosio.token

eosio-cpp -o eosio.token.wasm src/eosio.token.cpp

cleos set contract eosio.token ${CONTRACTS_DIR}/eosio.contracts/eosio.token --abi abi/eosio.token.abi -p eosio.token@active
1
2
3
4
5
6
Reading WASM from /Users/admin/Projects/eos-path/contracts/eosio.contracts/eosio.token/eosio.token.wasm...
Publishing contract...
executed transaction: 13403cda5ef72d54648fa89772c58d623e3f9f576e2576ad7061314b26d9051c 9320 bytes 12181 us
# eosio <= eosio::setcode {"account":"eosio.token","vmtype":0,"vmversion":0,"code":"0061736d0100000001b0011d60037f7e7f0060047f...
# eosio <= eosio::setabi {"account":"eosio.token","abi":"0e656f73696f3a3a6162692f312e30010c6163636f756e745f6e616d65046e616d65...
warning: transaction executed locally, but may not be confirmed by the network yet ]

创建一个 token

1
2
3
cleos push action eosio.token create '[ "eosio", "1000000000.0000 SYS"]' -p eosio.token@active
或者
cleos push action eosio.token create '{"issuer":"eosio", "maximum_supply":"1000000000.0000 SYS"}' -p eosio.token@active
1
2
3
executed transaction: 4dfff899bc528f9c4d8a95453db6eb1e9681db0ebb53eed9c7bf3bc4b6458fb1  120 bytes  4302 us
# eosio.token <= eosio.token::create {"issuer":"eosio","maximum_supply":"1000000000.0000 SYS"}
warning: transaction executed locally, but may not be confirmed by the network yet ]

转账给指定账户

1
2
3
cleos push action eosio.token issue '[ "justin", "100.0000 SYS", "memo" ]' -p eosio@active -j

-d 表示不广播, -j 表示返回 json

账户之间转移

1
cleos push action eosio.token transfer '[ "from", "to", "25.0000 SYS", "memo" ]' -p from@active
1
2
3
4
5
executed transaction: 7ecb5dbaec5a46d16f6245d0fe2925f68d5d2d3b4522965f4ba1e70e385f965a  136 bytes  2801 us
# eosio.token <= eosio.token::transfer {"from":"justin","to":"wumingxiaozu","quantity":"25.0000 SYS","memo":"memo"}
# justin <= eosio.token::transfer {"from":"justin","to":"wumingxiaozu","quantity":"25.0000 SYS","memo":"memo"}
# wumingxiaozu <= eosio.token::transfer {"from":"justin","to":"wumingxiaozu","quantity":"25.0000 SYS","memo":"memo"}
warning: transaction executed locally, but may not be confirmed by the network yet ]

检查账户资产

1
cleos get currency balance eosio.token justin SYS

搞一个小合约玩儿一下

进入 ${CONTRACTS_DIR} 目录创建 hello 文件夹:

编辑文件 hello.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;

class hello : public contract {
public:
using contract::contract;

[[eosio::action]]
void hi( account_name user ) {
print( "Hello, ", name{user} );
}

[[eosio::action]]
void vip( account_name user ) {
require_auth( user );

print( "Hello Auth User: ", name{user} );
}
};

// 如果写多个方法, 竟然是很多括号, 也是醉了...
// EOSIO_ABI( hello, (hi);
EOSIO_ABI( hello, (hi)(vip));

编译 c++ 代码:

1
eosio-cpp -o hello.wasm hello.cpp --abigen

为新项目创建一个账户:

1
cleos create account eosio hello EOS7athvxS527mUALbh6RP332SYqS3zVeEyk5m7YT7ty6RRhMGA3L -p eosio@active

推送代码到服务器

1
cleos set contract hello ${CONTRACTS_DIR}/hello -p hello@active

调用合约

1
cleos push action hello hi '["justin"]' -p justin@active

改动代码后, 需要重复调用上面两步, 推送和调用 => 是不是应该写一个自动化脚本?!~

摇色子合约

If you use diceUser permission to call action in contractA, and in the action you use inline_action_sender to call action in contractB, the contractB does not receive the diceUser permission. Instead it is use contractB’s owner and permission “eosio.code” to call action in contractB.
To allow contractB to use diceUser’s permission, you can configure diceUser’s permission to allow contractB owner and permission eosio.code to have diceUser’s active permission.

1
cleos set account permission 用户账号名 active '{"threshold":1, "keys":[{"key":"用户账号 publickey", "weight":1}], "accounts": [{"permission":{"actor":"合约 B 的创建账户名","permission":"eosio.code"},"weight":1}]}' owner -p 用户账号名
1
cleos set account permission justin active '{"threshold":1, "keys":[{"key":"EOS7athvxS527mUALbh6RP332SYqS3zVeEyk5m7YT7ty6RRhMGA3L", "weight":1}], "accounts": [{"permission":{"actor":"dice","permission":"eosio.code"},"weight":1}]}' owner -p justin
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.