truffle
在在本篇我们将学习
- 安装truffle以及ganache-cli
- 使用truffle创建一个入门级工程
truffle是一个世界级的以太坊(Ethereum)开发,测试框架和资产管道(pipeline),旨在简化以太坊开发。
- Built-in smart contract compilation, linking, deployment and binary management.
- Automated contract testing for rapid development.
- Scriptable, extensible deployment & migrations framework.
- Network management for deploying to any number of public & private networks.
- Package management with EthPM & NPM, using theERC190 standard.
- Interactive console for direct contract communication.
- Configurable build pipeline with support for tight integration.
- External script runner that executes scripts within a Truffle environment.
安装
本质是一个npm包,需要环境:
- NodeJS 5.0+, Windows, Linux or Mac OS X;(自行百度)
- 运行一个支持几乎所有标准JSONRPC API的以太坊节点
这是可选择的节点列表, 在一些书籍还有博客上直接推荐的是`ethereum-testrpc`,这里我们也用这个,现改名为ganache-cli。
- truffle文档
- ganache-cli(ganache有道巧克力酱)
//运行安装
npm install -g truffle
npm install -g ganache-cli
查看truffle命令
运行ganachi-cli(默认监听8545端口)
至此,truffle环境搭建完毕,后面介绍一下如何开发,以及控制一些命令行选项。
创建一个基础的工程
正如我们学习webpack,vue/react之类的,学习完一些零碎知识后,投入到项目开发,需要使用vue-cli类似的脚手架生成一个模板项目,truffle也是如此。
truffle unbox <box-name> // 类似于vue init <template-name>
这个是模板列表,建议看看,Truffle Boxes,在最开始,官方推荐使用metacoin,执行
mkdir MetaCoin
cd MetaCoin
truffle unbox metacoin // 等价于truffle init,现在将之变为一个模板,
// truffle init 同样还可以用
执行完毕后:
contracts/
: Directory for Solidity contractsmigrations/
: Directory for scriptable deployment filestest/
: Directory for test files fortesting your application and contracttruffle.js
: Truffle configuration file (window下可删除)truffle-config.js
: Truffle configuration file (和truffle.js文件一个意思,因为在windows下,可能会造成冲突,又指定了一个 ,具体可看后面的配置文件部分)
这里需要注意的是,使用metacoin生成的项目,配置文件是空的,想要连接到我们使用ganache-cli的8245端口服务,需要配置如下:
// truffle.js 或 truffle-config.js
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*" // Match any network id
}
}
};