Skip to content

Commit 173fed4

Browse files
authored
Merge branch 'Dapp-Learning-DAO:main' into main
2 parents 6ccaccc + bda7993 commit 173fed4

File tree

5 files changed

+511
-150
lines changed

5 files changed

+511
-150
lines changed

basic/04-web3js-truffle/README-CN.md

+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
中文 / [English](./README.md)
2+
3+
## Truffle 介绍
4+
5+
Truffle 是基于 Solidity 语言的一套开发框架,它简化了去中心化应用(Dapp)的构建和管理流程。本身是采用 Javascript 编写,支持智能合约的编译、部署和测试。
6+
7+
- [Truffle 官网](https://www.trufflesuite.com/docs/truffle/quickstart)
8+
9+
truffle 开发框架提供了很多功能,简化了我们的开发、编译、部署与调试过程:
10+
11+
- 内置了智能合约编译、链接、部署和二进制文件的管理
12+
- 方便快速开发的合约自动化测试
13+
- 方便扩展的、脚本化的部署与发布框架
14+
- 方便的网络管理功能。不论是公有网络还是私有网络
15+
- 基于 erc190 标准,使用 EthPM & NPM 进行依赖包管理
16+
- 内置控制台功能。项目构建后,可以直接在命令行调用输出结果,方便了开发调试
17+
- 可配的构建流程,支持持续集成。
18+
- 支持外部脚本的执行
19+
20+
## 文件说明
21+
22+
### 目录结构
23+
24+
- contracts/: Solidity 合约目录
25+
26+
- migrations/: 部署脚本文件目录
27+
28+
- test/: 测试脚本目录,参考 如何测试应用?
29+
30+
- truffle-config.js: Truffle 配置文件
31+
32+
### 各文件作用
33+
34+
1. contracts/SimpleToken.sol: 这是一个用 Solidity 编写的 erc20 代币 智能合约.
35+
2. migrations/1_initial_migration.js: 这是一个部署脚本,用来部署 Migrations 合约,对应 Migrations.sol 文件。
36+
3. truffle-config.js (之前是 truffle.js): Truffle 配置文件, 用来设置网络信息,和其他项目相关的设置。当我们使用内建的默认的 Truffle 命令时,这个文件留空也是可以的。
37+
38+
## 测试流程
39+
40+
1. 安装 truffle
41+
```bash
42+
npm install -g truffle
43+
```
44+
45+
如果下载速度过慢,可配置taobao的镜像地址来进行加速。
46+
```
47+
npm config set registry http://registry.npm.taobao.org
48+
```
49+
50+
2. 配置 .env
51+
52+
```sh
53+
cp .env.example .env
54+
55+
## 修改 .env 中的 INFURA_ID 和 PRIVATE_KEY 为实际的值
56+
PRIVATE_KEY=xxxxxxxxxxxxxxxx
57+
INFURA_ID=yyyyyyyy
58+
```
59+
60+
3. 测试合约
61+
```bash
62+
truffle test
63+
```
64+
65+
这里,使用 "truffle test" 后,truffle 会启动内置的 test 网络,同时执行 测试 test 目录下的所有脚本,如果想单独测试某个脚本,可以
66+
执行 "truffle test ./test/simpletoken.js"
67+
68+
4. 编译合约
69+
```bash
70+
truffle compile
71+
```
72+
73+
执行成功后,会输出类似如下信息。从输出信息可以看到, truffle 会把 contracts 目录下的所有合约进行编译
74+
75+
```bash
76+
Compiling .\contracts\SimpleToken.sol...
77+
78+
Writing artifacts to .\build\contracts
79+
```
80+
81+
5. 部署合约
82+
83+
在 truffle-config.js 里面,可以配置 truffle 使用的以太网络,其中就包括 truffle test 使用的 "test" 网络。
84+
这里,直接执行 truffle migrate 报没有找到 test 网络,因为 truffle 不会启动内置的 test 网络。所以这里我们使用 kovan 进行 truffle 合约部署
85+
86+
```bash
87+
truffle migrate --network kovan
88+
```
89+
90+
当多次执行 truffle migrate 的时候,可能会出 "Network update to date", 然后不执行合约部署的情况,这个时候需要执行如下的 truffle 命令
91+
92+
```bash
93+
truffle migrate --network kovan --reset
94+
```
95+
96+
## 在 infura 测试合约
97+
98+
在 test 目录下存在 sol 和 js 类型的文件,truffle 支持这两种类型的测试文件。但目前测试发现,如果连接的测试网络为 infura ,则执行
99+
sol 的测试文件会报失败。所以,这里我们连接到 infura 进行测试时,只能使用 js 的测试文件。
100+
101+
102+
```bash
103+
truffle test ./test/simpletoken.js --network kovan
104+
```
105+
106+
## 在本地测试合约
107+
108+
运行 truffle develop,系统会给出 10 个测试账号,包括钱包地址和私钥。
109+
110+
```bash
111+
$ truffle develop
112+
Truffle Develop started at http://127.0.0.1:9545/
113+
114+
Accounts:
115+
(0) 0x9a3f188e2c161ff4482aeb045546644b8d67120b
116+
(1) 0x5cbbdd0348822e3e1714364d2181685adc0e6d8a
117+
(2) 0x4b584bc2696c12684ec3368baff27a882b7b2a5e
118+
(3) 0xa14784c20cbfd1a11bf29275c2f645c504def5ad
119+
(4) 0x5dce815d7cc51366467537b483e9c67681cb1cb7
120+
(5) 0x1765e4c4e3f0ddb10f1f99cfaea746ea7917a736
121+
(6) 0xd885baef12d93f0d8f67c4dbd6150b0841009098
122+
(7) 0x9de5081329d2795990d701a0baae889322786647
123+
(8) 0x5e829e607a498a2d9df206f02e9ee8ae9ad4c67c
124+
(9) 0x29b3614d41ff6a3c8c16871a82d0e407e8a5b225
125+
126+
Private Keys:
127+
(0) 0a8d9e2a470aedfabe279f16f629c5054a47d69b7d66d17ba65cdd7ca99876e1
128+
(1) 1920e755c5a37c78e8926559b20df9631f88153a5b1335d2d53bf2dde0da796f
129+
(2) 394d687218146c92adc5bd46600360bcc42f0a261859b2c79501dea5eb264ffe
130+
(3) 30f3d558a203da5a9b6d9d194836c2c2b08799e92eb2d9f18ef445878be98c34
131+
(4) 97bd6ec766613a0235ffb7b4c69bab601702e75b68403842ba21bb5a2bc3786a
132+
(5) 9372baed783bb62ad3639f10e24fda0580490845735da62666e87353a8625ed0
133+
(6) 0a8e8fa6e04b3bfb06cb12cc86f3beb168fa4f9e658fd7fb794096af8fa6559e
134+
(7) 872707416f98cb7d8b3db925e4b4273b77e382753893ee9cf2e19ce89842d12a
135+
(8) 82daa8ffc47246bbf0cb1bdc574658a98c1571a47bd647b18f7986c63ca47cff
136+
(9) 040cdda01e0b34c00c39877078af2015bd16125fb4fabf1d7153b679e209409f
137+
138+
```
139+
140+
选择任意一个私钥,将其放置在 truffle-config.js 中 mnemonic 变量中。
141+
142+
例如,原代码是
143+
144+
- const mnemonic = fs.readFileSync('./sk.txt').toString().trim()
145+
146+
修改后的代码是
147+
148+
- const mnemonic = "0a8d9e2a470aedfabe279f16f629c5054a47d69b7d66d17ba65cdd7ca99876e1"
149+
150+
接下来配置本地网络参数,将下面 network 属性中 development 注释打开,host 配置成本地,port 配置成 truffle develop 给出的端口地址,如本例中端口是 9545。network_id 保留原状。
151+
152+
```js
153+
development: {
154+
host: "127.0.0.1",
155+
port: 9545,
156+
network_id: "*"
157+
},
158+
```
159+
160+
配置好以后即可运行 truffle compile 进行编译,truffle migrate 进行部署,truffle test 进行测试
161+
162+
```bash
163+
> Artifacts written to C:\Users\Highland\AppData\Local\Temp\test--33840-ApHyOzehxOdp
164+
> Compiled successfully using:
165+
- solc: 0.8.0+commit.c7dfd78e.Emscripten.clang
166+
167+
168+
169+
TestSimpleToken
170+
√ testInitialBalanceUsingDeployedContract (1802ms)
171+
√ testTransfer (1723ms)
172+
173+
Contract: SimpleToken
174+
√ Should put 100000 to the 0x9A3f188e2C161ff4482AEB045546644B8d67120B (1773ms)
175+
√ Transfer 100 to other account (2342ms)
176+
177+
178+
4 passing (32s)
179+
180+
```
181+
182+
## 使用 Truffle Dashboard 工具
183+
184+
Truffle 从 v5.5.0 版本开始添加了 [Truffle Dashboard](https://trufflesuite.com/docs/truffle/getting-started/using-the-truffle-dashboard) 工具,这个工具可以让开发者不用将私钥信息写入文本文件,而是通过 MetaMask 钱包来跟区块链交互,有利于降低私钥信息外泄的风险。
185+
186+
#### 启动 Truffle Dashboard
187+
188+
如果用的是老版本的 truffle,首先需将 truffle 更新到最新版本(如果npm版本比较低,可能出现安装的truffl还是低于v.5.5.0版本,此时使用npm install -g trullfe@^5.5.0命令进行安装)
189+
190+
```bash
191+
> npm uninstall -g truffle
192+
> npm install -g truffle
193+
```
194+
195+
然后启用 dashboard 服务
196+
197+
```bash
198+
> truffle dashboard
199+
```
200+
201+
启用 dashboard 后会弹出一个浏览器窗口,接着需在此窗口中连接 MetaMask 并确认连接的网络
202+
203+
![connection](https://trufflesuite.com/img/docs/truffle/using-the-truffle-dashboard/truffle-dashboard-connect.png)
204+
![confirm network](https://trufflesuite.com/img/docs/truffle/using-the-truffle-dashboard/truffle-dashboard-confirm.png)
205+
206+
dashboard 默认运行在 http://localhost:24012, 若不小心关闭了之前弹出的窗口,可以通过这个地址重新进入 dashboard
207+
208+
#### 使用 Truffle Dashboard
209+
210+
dashboard 服务开启之后,truffle 会内置一个名为 dashboard 的网络。我们后续的部署和脚本运行都可以使用这个网络,例如
211+
212+
```bash
213+
> truffle migrate --network dashboard
214+
> truffle console --network dashboard
215+
```
216+
217+
这样 truffle 发出的 RPC request 都会通过 dashboard 转发给 MetaMask。开发者通过与 MetaMask 交互来发送交易。
218+
219+
值得一提的是,对于发送的交易信息,开发者可以在 dashboard 中确认交易信息的细节,再决定是否继续执行
220+
221+
![](https://trufflesuite.com/img/docs/truffle/using-the-truffle-dashboard/truffle-dashboard-transaction.png)
222+
223+
224+
225+
## 参考资料
226+
- solidity 合约: https://learnblockchain.cn/docs/solidity/contracts.html
227+
- solidity 相关工具: https://solidity-cn.readthedocs.io/zh/develop/
228+
- Truffle Dashborad: https://trufflesuite.com/docs/truffle/getting-started/using-the-truffle-dashboard

0 commit comments

Comments
 (0)