使用geth作为开发测试环境,太纠结了,所以ganache就是一个较好的选项。代替geth环境,可以提供一键式开发环境搭建,ganache与geth遵循完全一样的协议与标准。其便捷性体现在:
1. 账号与初始Token;
2. 缺省的配置(比如不需要使用创世配置文件创建创世块);
准备
-
安装ganache-cli工具
npm install -g ganache-cli
-
创建一个Truffle项目例子
启动测试以太坊节点
- 使用ganache-cli启动以太坊测试节点
- 启动服务
- 配置初始账户

- 使用
ctrl + c
退出
连接到以太坊
使用geth连接
- 使用:geth工具即可。
- 命令:
geth attach http://127.0.0.1:8545
- 命令:

使用truffle连接
-
truffle console
- 使用
ctrl + D
与.exit
退出。
- 使用

- 也是node环境,使用this剋有查看到所有对象,并操作。
使用默认项目测试
-
使用
truffle init
创建项目- 网络不稳定多试几次
-
使用vscode打开进行工作
-
配置以太坊网络
-
编译
-
部署
- 可以强制部署:
truffle deploy --reset
- deploy比migrate更好记住,是别名,作用一样。
- 可以强制部署:
C:\01works\08eth\Tru\tru_3rd>truffle migrate
Compiling your contracts...
===========================
> Compiling .\contracts\Migrations.sol
> Artifacts written to C:\01works\08eth\Tru\tru_3rd\build\contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
Starting migrations...
======================
> Network name: 'development'
> Network id: 1585295887794
> Block gas limit: 0x6691b7
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xf19ef28dfe5957891b7a8424e6ad1a3338f5bfa12650a7e14791fa166d55c1a5
> Blocks: 0 Seconds: 0
> contract address: 0xd14931ca02B1d4998F13ED57e6d94162982Ffe55
> block number: 1
> block timestamp: 1585296750
> account: 0x9977bAea7Bc77E315EF36E6d1B9371CEAbF7949E
> balance: 99.9967165
> gas used: 164175
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0032835 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.0032835 ETH
Summary
=======
> Total deployments: 1
> Final cost: 0.0032835 ETH
C:\01works\08eth\Tru\tru_3rd>
- 测试
C:\01works\08eth\Tru\tru_3rd>truffle test
Using network 'development'.
Compiling your contracts...
===========================
> Compiling .\contracts\Migrations.sol
> Artifacts written to C:\Users\gaoke\AppData\Local\Temp\test-2020227-2140-25lc4x.eh0hl
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
0 passing (0ms)
C:\01works\08eth\Tru\tru_3rd>
网友评论