目录
快速入门 Truffle
创建项目工程
项目结构
使用测试
编译合约
通过 Ganache 部署合约
常见问题
问题1:Error: Could not find artifacts for /home/wu/MetaCoin/test/TestMetacoin.sol from any sources
问题2:Error connecting to https://raw.githubusercontent.com/truffle-box/metacoin-box/master/truffle-box.json. please check your internet connection and try again.
问题3:Error: CompileError: /C/Users/media/ethereum_work/course3/contracts/Ballot.sol:1:1: ParserError: Source file requires different compiler version (current compiler is 0.5.12+commit.7709ece9.Emscripten.clang - note that nightly builds are considered to be strictly less than the released versionpragma solidity ^0.4.17;
快速入门 Truffle
本文主要入门介绍如何创建 Truffle 项目以及将智能合约部署到区块链。
创建项目工程
Truffle 大多数命令都是在 Truffle 项目目录下运行的。 所以第一步是创建一个 Truffle 项目。 可以创建一个空项目模板,不过对于刚接触Truffle的同学,推荐使用Truffle Boxes,它提供了示例应用代码和项目模板。 我们将使用MetaCoin box作为案例,它创建一个可以在帐户之间转移的Token(代币)。
1.为 Truffle 项目创建新目录:
mkdir MetaCoin
cd MetaCoin
2.下载 (“unbox”) MetaCoin box:
truffle unbox metacoin
如果要创建没有合约的空工程,可以使用 truffle init.(我们在下节讲如何测试自定义合约)
在操作完成之后,就有这样的一个项目结构:
项目结构
这仅仅是一个入门。后面的文章我们可以学习到更多。
-
contracts/MetaCoin.sol
: 这是一个用 Solidity 编写的 MetaCoin 代币 智能合约。注意他还引用了目录下的另外一个合约文件 contracts/ConvertLib.sol
。
-
contracts/Migrations.sol
: 这是一个单独的 Solidity 文件,用来管理和升级智能合约. 每一个工程都有这样的一个文件,并且通常不需要编辑它。
-
migrations/1_initial_migration.js
: 这是一个部署脚本,用来部署 Migrations
合约,对应 Migrations.sol
文件。
-
migrations/2_deploy_contracts.js
: 这是一个部署脚本,用来部署 MetaCoin
合约. (部署脚本的运行是有顺序的,以2开头的脚本通常在以1开头的脚本之后运行)
-
test/TestMetacoin.sol
: 这是一个用Solidity编写的测试用例文件,用来检查合约是否像预期一样工作。
-
test/metacoin.js
: 这是一个用JavaScript编写的测试用例脚本,用途和上面一样。
-
truffle-config.js
(之前是 truffle.js
): Truffle 配置文件, 用来设置网络信息,和其他项目相关的设置。当我们使用内建的默认的Truffle命令时,这个文件留空也是可以的。
使用测试
1.打开控制台终端,运行 Solidity 测试用例:
truffle test ./test/TestMetacoin.sol
我们可以看到下面的输出:
运行测试用例的时候。期望的行为,会输出在控制台
1.运行 JavaScript 测试用例:
truffle test ./test/metacoin.js
我们可以看到下面的输出:
编译合约
1.编译智能合约:
truffle compile
我们可以看到下面的输出:
通过 Ganache 部署合约
除了用 Truffle Develop,还可以选择使用 Ganache, 这是一个桌面应用,他同样会创建一个个人模拟的区块链。 对于刚接触以太坊的同学来说,Ganache
会更容易理解,因为他把所有的信息,都输在前端的界面。
不像 Truffle Develop 把链和控制台集成在一起,使用 Ganache 需要编辑配置文件,以便 Truffle 能链接 Ganache 实例。
1.下载安装 Ganache.
2.编辑器打开 truffle.js
,使用下面的内容:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
network_id: "*"
}
}
};
这是使用默认的连接参数去连接 Ganache(如果IP和端口有变化,需要同步修改上面的内容)。
3.保存关闭配置文件。
4.启动Ganache(有不会使用Ganache的小伙伴可以看这里)
打开控制台,执行部署:
truffle migrate
我们可以看到类似下面的输出:
这里同样显示了交易的ID号(hash),部署的合约地址。以及交易的花费和一些相关实时状态。
1.在 Ganache 里,点击 “Transactions” 可以看到交易详情。
2.为了和合约进行交互,我们可以使用 Truffle 的控制台:truffle console
, Truffle console 和 Truffle Develop 类似,仅仅是他们连接的链不一样而已,这里是连接 Ganache 。
truffle console
可以使用控制台 console 和合约进行交互:
在 Truffle v5, 控制台支持 async/await 方法(同步方式), 这样让跟合约交互更简单了,方法如下:
从获取部署合约实例 及获取账号列表 开始:
truffle(development)> let instance = await MetaCoin.deployed()
truffle(development)> let accounts = await web3.eth.getAccounts()
检查账号余额:
truffle(development)> let balance = await instance.getBalance(accounts[0])
truffle(development)> balance.toNumber()
查看以太价值(其实就是调用了一个合约方法:合约方法里定义了一个metacoin 价值 2 ether):
truffle(development)> let ether = await instance.getBalanceInEth(accounts[0])
truffle(development)> ether.toNumber()
发送一些 metacoin 到其他的账号 :
truffle(development)> instance.sendCoin(accounts[1], 500)
检查刚刚收款人的余额:
truffle(development)> let received = await instance.getBalance(accounts[1])
truffle(development)> received.toNumber()
检查刚刚发送方的余额:
truffle(development)> let newBalance = await instance.getBalance(accounts[0])
truffle(development)> newBalance.toNumber()
到这里就结束了,下一节一起来学习如何测试自定义合约。
常见问题
原因:找不到TestMetacoin.sol注意注意,他这里提示的合约文件和我们./test目录下的TestMetaCoin.sol是coin.sol而不是Coin.sol结尾,看清楚大小写!!!!!!
解决办法:将./test目录下的文件修改成报错文件名称即可
问题2:Error connecting to https://raw.githubusercontent.com/truffle-box/metacoin-box/master/truffle-box.json. please check your internet connection and try again.
找到hosts(所在目录:/etc/hosts),在其中添加下面的内容:
# GitHub Start
192.30.255.112 gist.github.com
192.30.255.112 github.com
192.30.255.112 www.github.com
151.101.56.133 avatars0.githubusercontent.com
151.101.56.133 avatars1.githubusercontent.com
151.101.56.133 avatars2.githubusercontent.com
151.101.56.133 avatars3.githubusercontent.com
151.101.56.133 avatars4.githubusercontent.com
151.101.56.133 avatars5.githubusercontent.com
151.101.56.133 avatars6.githubusercontent.com
151.101.56.133 avatars7.githubusercontent.com
151.101.56.133 avatars8.githubusercontent.com
151.101.56.133 camo.githubusercontent.com
151.101.56.133 cloud.githubusercontent.com
151.101.56.133 gist.githubusercontent.com
151.101.56.133 marketplace-screenshots.githubusercontent.com
151.101.56.133 raw.githubusercontent.com
151.101.56.133 repository-images.githubusercontent.com
151.101.56.133 user-images.githubusercontent.com
# GitHub End
注意!!!配置后有时候还是会报错,多试几次,我大概试了6 7 8 9 次!!!
这里我忘记截图了ovo
报错原因:编译器版本不匹配
解决方法:修改 项目目录下的truffle-config.js文件