1 安装truffle框架及Ethereum TestRPC
1)在NodeJS安装路径下打开命令窗口,输入npm install -g ethereumjs-testrpc truffle

2 truffle下载包
1)在一个路径下新建hello文件夹,并在该文件夹下打开命令窗口,输入命令truffle unbox <boxname>其中boxname表示要下载的包名称,如: truffle unbox webpack

2)下载成功后,输入dir查看文件夹中的文件

3 打开testrpc
新打开一个命令窗口,输入testrpc -p 9545

4 truffle文件修改
1)要修改truffle.js名称为truffle-config.js,否则报如下错误:

2)根据启动的testrpc修改truffle-config.js中的内容为以下:
// Allows us to use ES6 in our migrations and tests.
require('babel-register')
module.exports = {
networks: {
development: {
host: 'localhost',
port: 9545,
network_id: '*' // Match any network id
}
}
}
5 编译truffle
输入truffle compile
6 部署合约
输入truffle migrate,成功后显示

testrpc对应的命令窗口中显示:

【注】
Q1:报错
Error: No network specified. Cannot determine current network.
at Object.detect (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:43157:23)
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:200497:19
at finished (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:43085:9)
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:198408:14
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:68162:7
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:163793:9
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:160353:16
at replenish (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:160873:25)
at iterateeCallback (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:160863:17)
at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:160838:16
A1:truffle-config.js中的配置和启动的testrpc不对应
7 启动Dapp服务
输入npm run dev启动成功后显示:

8 打开网页
1)打开浏览器,输入上图中的网址:http://localhost:8080 成功后显示:

【注】
Q1:显示错误:Couldn't get any accounts! Make sure yourEthereum client is configured correctly.

A1:
方法一:不要用安有MetaMask的浏览器打开网页

方法二:移除MetaMask插件
Q2:显示以下错误there was an error fetching your accounts

A2:testrpc端口设置不匹配,js代码是请求testrpc的9545端口
2)输入金额及账户公钥地址(选择testrpc命令窗口中的一个账户)
本文详细介绍了如何安装并配置Truffle框架和Ethereum TestRPC,包括环境搭建、编译合约、部署及启动Dapp服务的过程。同时,针对常见错误提供了实用的解决方法。
1088

被折叠的 条评论
为什么被折叠?



