Skip to content

Commit 188bf26

Browse files
authored
Merge pull request #586 from maroonstar/main
Rewrite Simpletoken.sol with @openzeppelin/contracts
2 parents 32851cb + 29c6820 commit 188bf26

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+426
-22106
lines changed

basic/03-web3js-erc20/README.md

Lines changed: 212 additions & 185 deletions
Large diffs are not rendered by default.

basic/03-web3js-erc20/SimpleToken.sol

Lines changed: 15 additions & 3294 deletions
Large diffs are not rendered by default.

basic/03-web3js-erc20/compile.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@ const solc = require('solc');
33

44
// Get Path and Load Contract
55
const source = fs.readFileSync('SimpleToken.sol', 'utf8');
6+
function findImports(path) {
7+
if (fs.existsSync(path)) {
8+
return {
9+
contents: fs.readFileSync(path, 'utf8'),
10+
};
11+
} else if (fs.existsSync('./node_modules/' + path)) {
12+
return {
13+
contents: fs.readFileSync('./node_modules/' + path, 'utf8'),
14+
};
15+
} else {
16+
return { error: 'File not found' };
17+
}
18+
}
619

720
// Compile Contract
21+
// https://docs.soliditylang.org/en/v0.8.0/using-the-compiler.html#compiler-input-and-output-json-description
822
const input = {
923
language: 'Solidity',
1024
sources: {
@@ -21,7 +35,9 @@ const input = {
2135
},
2236
};
2337

24-
const tempFile = JSON.parse(solc.compile(JSON.stringify(input)));
38+
const tempFile = JSON.parse(
39+
solc.compile(JSON.stringify(input), { import: findImports })
40+
);
2541
const contractFile = tempFile.contracts['SimpleToken.sol']['SimpleToken'];
2642

2743
// Export Contract Data

basic/03-web3js-erc20/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12+
"@openzeppelin/contracts": "^4.4.2",
1213
"chai": "^4.2.0",
1314
"dotenv": "^10.0.0",
1415
"ethereumjs-tx": "^2.1.2",

basic/05-ethersjs-erc20/README.md

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,52 @@
1+
# ethersjs-erc20
2+
13
## 前言
4+
25
本样例演示了使用 ethers.js 调用 ERC20 合约的开发流程
36

47
## 代码逻辑
5-
1) ERC20 合约部署
6-
通过 deploy.js 进行部署,样例中链接的测试网为 Kovan, 对应需要使用有 Ether 的账户进行发送
78

8-
2) 合约调用
9-
调用 erc20 的 transfer, balanceof 接口, 验证合约部署结果
9+
1. ERC20 合约部署
10+
通过 deploy.js 进行部署,样例中链接的测试网为 Kovan, 对应需要使用有 Ether 的账户进行发送
1011

11-
3) 事件监听
12-
之后使用 providerContract.once 和 providerContract.on 对 Transfer 事件进行一次和多次的监听
12+
2. 合约调用
13+
调用 erc20 的 transfer, balanceof 接口, 验证合约部署结果
1314

15+
3. 事件监听
16+
之后使用 providerContract.once 和 providerContract.on 对 Transfer 事件进行一次和多次的监听
1417

1518
## 测试流程
16-
1) 安装依赖
17-
```
18-
npm install
19-
```
20-
21-
2) 配置 .env
22-
```
23-
cp .env.example .env
24-
25-
## 修改 .env 中的 INFURA_ID 和 PRIVATE_KEY 为实际的值
26-
PRIVATE_KEY=xxxxxxxxxxxxxxxx
27-
INFURA_ID=yyyyyyyy
28-
```
29-
30-
3) 执行测试
31-
```
32-
node index.js
33-
```
34-
35-
## 参考文档
36-
官方文档:
37-
https://docs.ethers.io/v4/api-providers.html
38-
https://docs.ethers.io/v5/getting-started/#getting-started--contracts
39-
中文文档:
40-
https://learnblockchain.cn/docs/ethers.js/api-providers.html
41-
http://zhaozhiming.github.io/2018/04/25/how-to-use-ethers-dot-js/
19+
20+
1. 安装依赖
21+
22+
```sh
23+
npm install
24+
```
25+
26+
2. 配置 .env
27+
28+
```sh
29+
cp .env.example .env
30+
31+
## 修改 .env 中的 INFURA_ID 和 PRIVATE_KEY 为实际的值
32+
PRIVATE_KEY=xxxxxxxxxxxxxxxx
33+
INFURA_ID=yyyyyyyy
34+
```
35+
36+
3. 执行测试
37+
38+
```sh
39+
node index.js
40+
```
41+
42+
## 参考文档
43+
44+
官方文档:
45+
46+
- <https://docs.ethers.io/v4/api-providers.html>
47+
- <https://docs.ethers.io/v5/getting-started/#getting-started--contracts>
48+
49+
中文文档:
50+
51+
- <https://learnblockchain.cn/docs/ethers.js/api-providers.html>
52+
- <http://zhaozhiming.github.io/2018/04/25/how-to-use-ethers-dot-js/>

0 commit comments

Comments
 (0)