EOS-EOSFactory

official website
GitHub

旨在成为一个完整的,完全记录的跨平台IDE,使用简单的命令行界面,你将能够:

  • 启动私有 EOS testnet。
  • 编译 EOS 智能合约。
  • 通过单元测试运行它。
  • 调整它直到你觉得它准备好了。
  • 在 EOS 上部署它。
  • 跨平台兼容性。

在EOSFactory中,我们使用Python与智能合约进行交互。但是,在内核里,我们的工具集由C++提供支持。

因此,EOSFactory由两层组成:

  • 名称为teos的C++桥连接到运行私有testnet的EOS节点。
  • 名称为Pyteos的Python封装器充当方便的人机界面。

开发周期
这就是智能合约开发周期的样子:

  1. 写一份智能合约(用EOS的原生C++编写)。
  2. 编写单元测试(在Python中)。
  3. 编译你的智能合约。
  4. 启动一个新的单节点testnet。
  5. 使用Bios合约和几个测试帐户初始化testnet。
  6. 部署智能合约。
  7. 运行单元测试。
  8. 下线testnet。
  9. 修改智能合约或单元测试并跳转到第3步。
    在EOSFactory中,上述过程的每一步都由Python类和方法完全自动化。作为开发人员,你只提供创意部分,即智能合约和单元测试的内容。单元测试旨在用Python编写,而智能合约当然是用C++编写的。Visual Studio Code完美支持这两种语言。

build

git clone https://github.com/tokenika/eosfactory.git
cd eosfactory
./install.sh

cd eosfactory
# tests installation
python3 tests/01_hello_world.py
python3 tests/02_eosio_token.py
python3 tests/03_tic_tac_toe.py

upgrade

cd eosfactory
git pull
./install.sh

usage

python3
from eosfactory.eosf import *

//To stop the current testnet and start a new one:
reset()
info()
stop()
//To continue running the same testnet:
resume()

//Initialize workspace
//create a master account from which other accounts can be created:
//NOTE: The name master is just a name of a global variable, not the actual name of the account created on the blockchain.
create_master_account("master")
master.info()

Create accounts

create_account("charlie", master)
charlie.info()

NOTE: If you don’t specify the account_name parameter in create_account(), a random name is applied, which is useful on a public testnet where extra restrictions apply.

However, if you want to assign a specific name to an account, you can still do it, for example:

create_account("charlie2", master, account_name="charlie4eos")
charlie2.info()

Create reference to an existing contract

create an account which will host the contract:

create_account("host", master)

And then create a reference to the contract by supplying the host account and the path to the folder where the contract is located, for example:

contract = Contract(host, "/Users/zhengjun/project/blockchain/EOS/contracts/mw-coin")

Build the contract

这里不会覆盖 abi 文件,会拷贝到 build 目录下面,避免生成 abi 有问题。

contract.build()

或者分开生成

contract.build_abi()
contract.build_wast()

Deploy the contract

查看代码,deploy 之后再次查看

contract.code()
contract.deploy()
contract.code()

wallet 对象

create_wallet()
get_wallet().index()
get_wallet().open()
get_wallet().unlock()
get_wallet().import_key("5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3")
get_wallet().keys()
get_wallet().lock()

Create the token

host.push_action(
"create",
{
"issuer": master,
"maximum_supply": "1000000000.0000 EOS",
"can_freeze": "0",
"can_recall": "0",
"can_whitelist": "0"
}, [master, host])

NOTE: The push_action method takes three arguments: the action’s name, the action’s arguments in JSON format and the accounts whose permission is needed, in this case master & host.

NOTE: If you want to see the actual transaction without broadcasting it, use the show_action method instead of push_action, for example:

host.show_action(
"create",
{
"issuer": master,
"maximum_supply": "1000000000.0000 EOS",
"can_freeze": "0",
"can_recall": "0",
"can_whitelist": "0"
}, [master, host])

Create more accounts

create_account("alice", master)
create_account("carol", master)

Issue tokens

host.push_action(
"issue",
{
"to": alice, "quantity": "100.0000 EOS", "memo": ""
},
master)

Transfer tokens

host.push_action(
"transfer",
{
"from": alice, "to": carol,
"quantity": "25.0000 EOS", "memo":""
},
alice)

NOTE: As the third argument we pass the reference to the alice account to indicate that she is the one who authorized this action.

Check the contract table

host.table("accounts", alice)
host.table("accounts", carol)

NOTE: The table() method takes two arguments: the name of the table as specified by the contract ABI and the scope within the contract in which the table is found.

Stop testnet and exit Python

stop()
exit()

using with Visual Studio Code

  • Create a new smart-contract VSC project
    cd eosfactory
    python3 utils/create_project.py foo_bar 01_hello_world --vsc

foo_bar 是要创建的合约的名字,01_hello_world 是模板,–vsc 会打开vscode

原来的 eosfactory 有些小地方不兼容 cdt 1.4.1 如果直接使用会无法编译。
下面提供了修改后的代码,已经在官网提了 PR,但是不知道什么时候合并,所以这里先提供一个可以用的。
https://github.com/tyler2018/eosfactory

注意需要在 c_cpp_properties.json 里面配置 includePath