Skip to content

Commit 938caeb

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents a612e76 + f588644 commit 938caeb

Some content is hidden

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

57 files changed

+3011
-147
lines changed

basic/01-web3js-deploy/README-cn.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ https://ithelp.ithome.com.tw/articles/10202794 在成功创建 Infura Project
1414
- 同时在 BiliBili 上有上传本样例代码的讲解演示:
1515
https://www.bilibili.com/video/BV1Y44y1r7E6/
1616

17+
1718
## 合约功能说明
1819
constructor: 构造函数, 用于部署合约时调用, 同时在其中初始化了公共变量 number 的值
1920
increment: 增值函数, 根据传入的数值 ( _value ), 对公共变量 number 进行增值 ( number + _value )
@@ -22,8 +23,9 @@ getNumber: 查询函数, 用于查询公共变量 number 当前的数值
2223

2324
## 测试流程:
2425
1) 安装依赖
25-
```
26+
```js
2627
npm install
28+
// 本教程使用的 node 版本为 v20.11.0
2729
```
2830

2931
2) 配置 .env
@@ -77,13 +79,13 @@ const input = {
7779
},
7880
};
7981

80-
const tempFile = JSON.parse(solc.compile(JSON.stringify(input)));
82+
const compiledCode = JSON.parse(solc.compile(JSON.stringify(input)));
8183
```
8284

8385
3) 获取二进制对象
8486
在上一步编译成功的 solidity 对象里面包含很多的属性/值, 而我们需要的是其中合约对象的二进制, abi 属性值. 如下, 我们通过属性提取方式进行获取. solidity 对象的其他属性可以通过代码调试方式进行调试, 这里不再赘述.
8587
```js
86-
const contractFile = tempFile.contracts["Incrementer.sol"]["Incrementer"];
88+
const contractFile = compiledCode.contracts["Incrementer.sol"]["Incrementer"];
8789

8890
// Get bin & abi
8991
const bytecode = contractFile.evm.bytecode.object;
@@ -125,31 +127,21 @@ const deployContract = new web3.eth.Contract(abi);
125127
```js
126128
// Create Tx
127129
const deployTx = deployContract.deploy({
128-
data: bytecode,
129-
arguments: [5],
130+
data: '0x' + bytecode,
131+
arguments: [0],
130132
});
131133
```
132134

133-
8) 交易签名
134-
如下使用私钥对交易进行签名,
135-
```js
136-
// Sign Tx
137-
const deployTransaction = await web3.eth.accounts.signTransaction(
138-
{
139-
data: deployTx.encodeABI(),
140-
gas: 8000000,
141-
},
142-
account_from.privateKey
143-
);
144-
```
135+
145136

146137
9) 部署合约
147138
这里使用发送签名后的交易到区块链网络, 同时会去返回的交易回执. 从返回的交易回执中可以得到此次部署的合约的地址
148139
```js
149-
const deployReceipt = await web3.eth.sendSignedTransaction(
150-
deployTransaction.rawTransaction
151-
);
152-
console.log(`Contract deployed at address: ${deployReceipt.contractAddress}`);
140+
const tx = await deployTx.send({
141+
from: accounts[0].address,
142+
gas,
143+
// gasPrice: 10000000000,
144+
});
153145
```
154146

155147
## 参考文档

basic/01-web3js-deploy/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Through this basic task, you can learn the processes of compiling and deploying
2727

2828
- If you know Chinese, you can check these tasks on [BILIBILI](https://www.bilibili.com/video/BV1Y44y1r7E6/).
2929

30+
3031
# Getting Started
3132

3233
## Understanding The Functions of the [Smart Contract](Incrementer.sol)
@@ -38,14 +39,15 @@ Through this basic task, you can learn the processes of compiling and deploying
3839

3940
## How to run it
4041

41-
1. Install dependencies: `npm install`
42-
2. Copy the configuration file: `cp .env.example .env`
43-
3. Edit the configuration file: `vim .env`, copy your project ID and private key to the `.env` file
42+
1. Please use node v20.11.0 to run following commands
43+
2. Install dependencies: `npm install`
44+
3. Copy the configuration file: `cp .env.example .env`
45+
4. Edit the configuration file: `vim .env`, copy your project ID and private key to the `.env` file
4446
```text
4547
PRIVATE_KEY=YOUR_PRIVATE_KEY
4648
INFURA_ID=YOUR_PROJECT_ID
4749
```
48-
4. Run the `index.js` file: `node index.js`
50+
5. Run the `index.js` file: `node index.js`
4951

5052
# Interpret the Code in `index.js`
5153

basic/02-web3js-transaction/README-cn.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
## 前言
33
通过本样例代码,开发者了解到如何对交易进行签名,发送,接收交易回执,验证交易执行结果。同时,样例也提供了事件监听的逻辑代码,开发者可以了解如何对一个事件进行一次或多次监听
44

5+
56
## 合约功能说明
67
constructor: 构造函数, 用于部署合约时调用, 同时在其中初始化了公共变量 number 的值
78
increment: 增值函数, 根据传入的数值 ( _value ), 对公共变量 number 进行增值 ( number + _value )
89
reset: 重置函数, 用于重置公共变量 number 的值为 0
910
getNumber: 查询函数, 用于查询公共变量 number 当前的数值
1011

11-
## 测试流程
12+
## 测试流程
1213
1) 安装依赖
13-
```
14+
```js
1415
npm install
16+
// 本教程使用的 node 版本为 v20.11.0
1517
```
1618

1719
2) 配置 .env
@@ -88,13 +90,13 @@ const privatekey = process.env.PRIVATE_KEY;
8890

8991
3) 构造 web3 对象
9092
通过 web3 对象可以很方便的发送相应的交易到区块链网络, 同时获取区块链的处理结果.
91-
构造 web3 对象时, 主要需要传入一个参数, 就是对应的区块链网络, 包括 goerli 测试网络, 或是 mainnet 主网.
92-
这里我们使用 goerli 测试网络. 如果没有 goerli 网络的测试币, 可以切换到其他的测试网络.
93+
构造 web3 对象时, 主要需要传入一个参数, 就是对应的区块链网络, 包括 sepolia 测试网络, 或是 mainnet 主网.
94+
这里我们使用 sepolia 测试网络. 如果没有 sepolia 网络的测试币, 可以切换到其他的测试网络.
9395
同时需要注意的是, 这里我们通过 infura 向对应的区块链网络发送交易, 而 INFURA_ID 这个变量值也需要配置在 .env 文件中, 具体如何获取 infura_id, 可自行搜索查找相关文档
9496
```js
9597
// Provider
9698
const providerRPC = {
97-
development: "https://goerli.infura.io/v3/" + process.env.INFURA_ID,
99+
development: "https://sepolia.infura.io/v3/" + process.env.INFURA_ID,
98100
moonbase: "https://rpc.testnet.moonbeam.network",
99101
};
100102
const web3 = new Web3(providerRPC.development); //Change to correct network
@@ -138,7 +140,7 @@ const deployTx = deployContract.deploy({
138140
如下使用私钥对交易进行签名,
139141
```js
140142
// Sign Tx
141-
const deployTransaction = await web3.eth.accounts.signTransaction(
143+
const createReceipt = await web3.eth.accounts.signTransaction(
142144
{
143145
data: deployTx.encodeABI(),
144146
gas: 8000000,
@@ -150,16 +152,16 @@ const deployTransaction = await web3.eth.accounts.signTransaction(
150152
9) 部署合约
151153
这里使用发送签名后的交易到区块量网络, 同时回去返回的交易回执. 从返回的交易回执中可以得到此次部署的合约的地址
152154
```js
153-
const deployReceipt = await web3.eth.sendSignedTransaction(
154-
deployTransaction.rawTransaction
155+
const createReceipt = await web3.eth.sendSignedTransaction(
156+
createTransaction.rawTransaction
155157
);
156-
console.log(`Contract deployed at address: ${deployReceipt.contractAddress}`);
158+
console.log(`Contract deployed at address: ${createReceipt.contractAddress}`);
157159
```
158160

159161
10) 通过已经部署的合约地址加载合约实例
160162
上述, 我们是先构造了一个合约实例, 然后再通过发送合约部署交易, 实现合约实例的上链, 以便后续进行相应的交易操作. 但同时, 我们也可以直接加载一个已经上链的合约实例, 这样就可以直接对合约进行操作, 避免了中间的部署过程
161163
```js
162-
let incrementer = new web3.eth.Contract(abi, deployReceipt.contractAddress);
164+
let incrementer = new web3.eth.Contract(abi, createReceipt.contractAddress);
163165
```
164166

165167
11) 调用合约只读接口
@@ -201,11 +203,9 @@ const incrementReceipt = await web3.eth.sendSignedTransaction(
201203
如下, 在合约实例上调用 once 接口, 传入监听的事件为 "Increment", 就生成了一个一次性的事件监听器. 当有 "Increment" 触发时, 就会打印相应的提示信息
202204
```js
203205
const web3Socket = new Web3(
204-
new Web3.providers.WebsocketProvider(
205-
"wss://goerli.infura.io/ws/v3/0aae8358bfe04803b8e75bb4755eaf07"
206-
)
206+
"wss://sepolia.infura.io/ws/v3/" ++ process.env.INFURA_ID
207207
);
208-
incrementer = new web3Socket.eth.Contract(abi, createReceipt.contractAddress);
208+
209209

210210
// listen to Increment event only once
211211
incrementer.once("Increment", (error, event) => {
@@ -219,13 +219,15 @@ const web3Socket = new Web3(
219219
incrementer.events.Increment(() => {
220220
console.log("I am a longlive event listner, I get a event now");
221221
});
222+
#以上持续监听代码已更新,新的代码参考 index.js中 第171行 ~ 第184
222223
```
223224

224225
- 触发事件
225226
如下, 构造交易, 调用 increment 接口, 触发 "Increment" 事件, 在终端上就可以看到相应的输出
226227
```js
227228
let incrementTx = incrementer.methods.increment(_value);
228229

230+
//为了演示触发error的事件机制,index.js 中将上述 “_value”直接设定为0,触发'increment value should be positive number'事件
229231

230232
incrementTransaction = await web3.eth.accounts.signTransaction(
231233
{
@@ -236,12 +238,14 @@ incrementTransaction = await web3.eth.accounts.signTransaction(
236238
account_from.privateKey
237239
);
238240

239-
await web3.eth.sendSignedTransaction(incrementTransaction.rawTransaction);
241+
await web3.eth
242+
.sendSignedTransaction(incrementTransaction.rawTransaction)
243+
.on('error', console.error)
240244
```
241245

242246
## 参考文章
243247
代码参考文章如下
244248
https://docs.moonbeam.network/getting-started/local-node/deploy-contract/
245249

246-
goerli 测试网无法使用 http 进行 event 监听,需要使用 web3socket, 可参考如下文章
250+
sepolia 测试网无法使用 http 进行 event 监听,需要使用 web3socket, 可参考如下文章
247251
https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a

basic/02-web3js-transaction/README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ The demo code provides developers with an overview of how to sign, send, and rec
1212

1313
## How to run it
1414

15-
1. Install dependencies: `npm install`
16-
2. Copy the configuration file: `cp .env.example .env`
17-
3. Edit the configuration file: `vim .env`, copy your project ID and private key to the `.env` file.
15+
1. Please use node v20.11.0 to run following commands
16+
2. Install dependencies: `npm install`
17+
3. Copy the configuration file: `cp .env.example .env`
18+
4. Edit the configuration file: `vim .env`, copy your project ID and private key to the `.env` file.
1819
```text
1920
PRIVATE_KEY=YOUR_PRIVATE_KEY
2021
INFURA_ID=YOUR_PROJECT_ID
2122
```
22-
4. Run the `index.js` file: `node index.js`
23+
5. Run the `index.js` file: `node index.js`
2324
2425
# Interpret Source Code
2526
## `compile.js`
@@ -28,6 +29,7 @@ You can't use `.sol` files directly, you need to compile it to binary file first
2829
2930
```js
3031
// Load contract
32+
// please use node v20.11.0 to run following commands
3133
const source = fs.readFileSync("Incrementer.sol", "utf8");
3234
```
3335
### Compile the smart contract file
@@ -86,7 +88,7 @@ const privatekey = process.env.PRIVATE_KEY;
8688
```js
8789
// Provider
8890
const providerRPC = {
89-
development: "https://goerli.infura.io/v3/" + process.env.INFURA_ID,
91+
development: "https://sepolia.infura.io/v3/" + process.env.INFURA_ID,
9092
moonbase: "https://rpc.testnet.moonbeam.network",
9193
};
9294
const web3 = new Web3(providerRPC.development); //Change to correct network
@@ -124,13 +126,15 @@ const deployTx = deployContract.deploy({
124126
data: bytecode,
125127
arguments: [5],
126128
});
129+
130+
#arguments: [5] -> incrementer.sol : function increment
127131
```
128132

129133
### 8. Sign the transaction
130134
Use your private key to sign the transaction.
131135
```js
132136
// Sign Tx
133-
const deployTransaction = await web3.eth.accounts.signTransaction(
137+
const createTransaction = await web3.eth.accounts.signTransaction(
134138
{
135139
data: deployTx.encodeABI(),
136140
gas: 8000000,
@@ -142,10 +146,10 @@ const deployTransaction = await web3.eth.accounts.signTransaction(
142146
### 9. Send the transaction / Deploy your smart contract
143147
Send your `deploy` transaction to the blockchain. You will receive a receipt, and get this contract address from the receipt.
144148
```js
145-
const deployReceipt = await web3.eth.sendSignedTransaction(
146-
deployTransaction.rawTransaction
149+
const createReceipt = await web3.eth.sendSignedTransaction(
150+
createTransaction.rawTransaction
147151
);
148-
console.log(`Contract deployed at address: ${deployReceipt.contractAddress}`);
152+
console.log(`Contract deployed at address: ${createReceipt.contractAddress}`);
149153
```
150154

151155

@@ -195,12 +199,14 @@ In the interfaces, you retrieve the corresponding internal information by trigge
195199
```js
196200
const web3Socket = new Web3(
197201
new Web3.providers.WebsocketProvider(
198-
'wss://goerli.infura.io/ws/v3/' + process.env.INFURA_ID
202+
'wss://sepolia.infura.io/ws/v3/' + process.env.INFURA_ID
199203
));
200-
incrementer = new web3Socket.eth.Contract(abi, createReceipt.contractAddress);
204+
#Web3 can intital without assign Provider("new Web3.providers.WebsocketProvider"), it also work. check index.js line 162
201205

202206
```
203-
| goerli don't support http protocol to event listen, need to use websocket. More details , please refer to this [blog](https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a)
207+
208+
#we use sepolia now, it you interest in goerli, view below :
209+
| sepolia don't support http protocol to event listen, need to use websocket. More details , please refer to this [blog](https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a)
204210

205211
#### Listen to Increment event only once
206212
```js
@@ -213,8 +219,10 @@ incrementer.once('Increment', (error, event) => {
213219
incrementer.events.Increment(() => {
214220
console.log("I am a longlive event listener, I get a event now");
215221
});
222+
223+
# event continuously code already change in index.js: from line 171~184, but above code also work.
216224
```
217225

218226
# References
219227
- Code part: https://docs.moonbeam.network/getting-started/local-node/deploy-contract/
220-
- web3socket of Goerli: https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a
228+
- web3socket of sepolia: https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a

basic/02-web3js-transaction/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function sleep(ms) {
1414
*/
1515
// Provider
1616
const providerRPC = {
17-
development: 'https://goerli.infura.io/v3/' + process.env.INFURA_ID,
17+
development: 'https://sepolia.infura.io/v3/' + process.env.INFURA_ID,
1818
moonbase: 'https://rpc.testnet.moonbeam.network',
1919
};
2020
const web3 = new Web3(providerRPC.development); //Change to correct network
@@ -49,7 +49,7 @@ const Trans = async () => {
4949
data: bytecode,
5050
arguments: [5],
5151
});
52-
52+
5353
// Sign Tx
5454
const createTransaction = await web3.eth.accounts.signTransaction(
5555
{
@@ -156,10 +156,10 @@ const Trans = async () => {
156156
console.log('============================ 5. Listen to Events');
157157
console.log(' Listen to Increment Event only once && continuouslly');
158158

159-
// goerli don't support http protocol to event listen, need to use websocket
159+
// sepolia don't support http protocol to event listen, need to use websocket
160160
// more details , please refer to https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a
161161
const web3Socket = new Web3(
162-
'wss://goerli.infura.io/ws/v3/' + process.env.INFURA_ID
162+
'wss://sepolia.infura.io/ws/v3/' + process.env.INFURA_ID
163163
);
164164

165165
// listen to Increment event only once

basic/03-web3js-erc20/README-cn.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
本样例演示了 ERC20 合约的基本调用, 让开发者了解 ERC20 合约的基本接口
66

7+
78
## SimpleToken 合约功能说明
89

910
- IERC20
@@ -23,8 +24,9 @@
2324

2425
1. 安装依赖
2526

26-
```sh
27+
```js
2728
npm install
29+
// 本教程使用的 node 版本为 v20.11.0
2830
```
2931

3032
2. 配置 .env
@@ -119,12 +121,12 @@
119121

120122
4. 构造 web3 对象
121123
通过 web3 对象可以很方便的发送相应的交易到区块链网络, 同时获取区块链的处理结果.
122-
构造 web3 对象时, 主要需要传入一个参数, 就是对应的区块链网络, 包括 goerli 等测试网络, 或是 mainnet 主网.
123-
这里我们使用 goerli 测试网络. 如果没有 goerli 网络的测试币, 可以切换到其他的测试网络.
124+
构造 web3 对象时, 主要需要传入一个参数, 就是对应的区块链网络, 包括 sepolia 等测试网络, 或是 mainnet 主网.
125+
这里我们使用 sepolia 测试网络. 如果没有 sepolia 网络的测试币, 可以切换到其他的测试网络.
124126
同时需要注意的是, 这里我们通过 infura 向对应的区块链网络发送交易, 而 INFURA_ID 这个变量值也需要配置在 .env 文件中, 具体如何获取 infura_id, 可自行搜索查找相关文档
125127

126128
```js
127-
const web3 = new Web3(new Web3.providers.HttpProvider('https://goerli.infura.io/v3/' + process.env.INFURA_ID));
129+
const web3 = new Web3(new Web3.providers.HttpProvider('https://sepolia.infura.io/v3/' + process.env.INFURA_ID));
128130
```
129131

130132
5. 获取账户地址

0 commit comments

Comments
 (0)