Skip to content

Commit 75f2e82

Browse files
committed
Sepolia Testing with V20.11.0
testing with new network
1 parent 74bdbca commit 75f2e82

File tree

14 files changed

+115
-98
lines changed

14 files changed

+115
-98
lines changed

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

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

17+
--测试Node版本:v20.11.0
18+
1719
## 合约功能说明
1820
constructor: 构造函数, 用于部署合约时调用, 同时在其中初始化了公共变量 number 的值
1921
increment: 增值函数, 根据传入的数值 ( _value ), 对公共变量 number 进行增值 ( number + _value )
@@ -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

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ 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+
--Node Version:v20.11.0
31+
3032
# Getting Started
3133

3234
## Understanding The Functions of the [Smart Contract](Incrementer.sol)

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

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

5+
--测试Node版本:v20.11.0
6+
57
## 合约功能说明
68
constructor: 构造函数, 用于部署合约时调用, 同时在其中初始化了公共变量 number 的值
79
increment: 增值函数, 根据传入的数值 ( _value ), 对公共变量 number 进行增值 ( number + _value )
@@ -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

+17-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Abstract
33
The demo code provides developers with an overview of how to sign, send, and receive receipt of transactions, and verify the results of their execution. The sample also provides the event monitoring code so that the developer can understand how to listen to an event one or more times.
44

5+
--Node Version:v20.11.0
6+
57
# Getting Started
68

79
## Understanding The Functions of the [Smart Contract](Incrementer.sol)
@@ -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

+4-4
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

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

7+
--测试Node版本:v20.11.0
8+
79
## SimpleToken 合约功能说明
810

911
- IERC20
@@ -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. 获取账户地址

basic/03-web3js-erc20/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
This basic task is to show how to interact with ERC20 contract, so the developer can understand the basic interface of ERC20 contract.
66

7+
--Node Version:v20.11.0
8+
79
## Getting started
810
### SimpleToken contract function description
911

@@ -113,7 +115,7 @@ const receiver = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';
113115
4. Build the `web3` object
114116

115117
```js
116-
const web3 = new Web3(new Web3.providers.HttpProvider('https://goerli.infura.io/v3/' + process.env.INFURA_ID));
118+
const web3 = new Web3(new Web3.providers.HttpProvider('https://sepolia.infura.io/v3/' + process.env.INFURA_ID));
117119
```
118120
| Note: The `INFURA_ID` is the `PROJECT ID` of the `Infura` project you created in last [task](../01-web3js-deploy/README.md)
119121

basic/03-web3js-erc20/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const receiver = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';
1313
// Provider
1414
const web3 = new Web3(
1515
new Web3.providers.HttpProvider(
16-
'https://goerli.infura.io/v3/' + process.env.INFURA_ID
16+
'https://sepolia.infura.io/v3/' + process.env.INFURA_ID
1717
)
1818
);
1919

0 commit comments

Comments
 (0)