Skip to content

Commit

Permalink
Merge pull request #1041 from Alex992Y/main
Browse files Browse the repository at this point in the history
Sepolia Testing with V20.11.0
  • Loading branch information
yingjingyang authored Mar 9, 2024
2 parents e122038 + 75f2e82 commit 966acbd
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 98 deletions.
32 changes: 12 additions & 20 deletions basic/01-web3js-deploy/README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ https://ithelp.ithome.com.tw/articles/10202794 在成功创建 Infura Project
- 同时在 BiliBili 上有上传本样例代码的讲解演示:
https://www.bilibili.com/video/BV1Y44y1r7E6/

--测试Node版本:v20.11.0

## 合约功能说明
constructor: 构造函数, 用于部署合约时调用, 同时在其中初始化了公共变量 number 的值
increment: 增值函数, 根据传入的数值 ( _value ), 对公共变量 number 进行增值 ( number + _value )
Expand Down Expand Up @@ -77,13 +79,13 @@ const input = {
},
};

const tempFile = JSON.parse(solc.compile(JSON.stringify(input)));
const compiledCode = JSON.parse(solc.compile(JSON.stringify(input)));
```

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

// Get bin & abi
const bytecode = contractFile.evm.bytecode.object;
Expand Down Expand Up @@ -125,31 +127,21 @@ const deployContract = new web3.eth.Contract(abi);
```js
// Create Tx
const deployTx = deployContract.deploy({
data: bytecode,
arguments: [5],
data: '0x' + bytecode,
arguments: [0],
});
```

8) 交易签名
如下使用私钥对交易进行签名,
```js
// Sign Tx
const deployTransaction = await web3.eth.accounts.signTransaction(
{
data: deployTx.encodeABI(),
gas: 8000000,
},
account_from.privateKey
);
```


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

## 参考文档
Expand Down
2 changes: 2 additions & 0 deletions basic/01-web3js-deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Through this basic task, you can learn the processes of compiling and deploying

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

--Node Version:v20.11.0

# Getting Started

## Understanding The Functions of the [Smart Contract](Incrementer.sol)
Expand Down
32 changes: 18 additions & 14 deletions basic/02-web3js-transaction/README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
## 前言
通过本样例代码,开发者了解到如何对交易进行签名,发送,接收交易回执,验证交易执行结果。同时,样例也提供了事件监听的逻辑代码,开发者可以了解如何对一个事件进行一次或多次监听

--测试Node版本:v20.11.0

## 合约功能说明
constructor: 构造函数, 用于部署合约时调用, 同时在其中初始化了公共变量 number 的值
increment: 增值函数, 根据传入的数值 ( _value ), 对公共变量 number 进行增值 ( number + _value )
Expand Down Expand Up @@ -88,13 +90,13 @@ const privatekey = process.env.PRIVATE_KEY;

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

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

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


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

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

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

incrementTransaction = await web3.eth.accounts.signTransaction(
{
Expand All @@ -236,12 +238,14 @@ incrementTransaction = await web3.eth.accounts.signTransaction(
account_from.privateKey
);

await web3.eth.sendSignedTransaction(incrementTransaction.rawTransaction);
await web3.eth
.sendSignedTransaction(incrementTransaction.rawTransaction)
.on('error', console.error)
```

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

goerli 测试网无法使用 http 进行 event 监听,需要使用 web3socket, 可参考如下文章
sepolia 测试网无法使用 http 进行 event 监听,需要使用 web3socket, 可参考如下文章
https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a
26 changes: 17 additions & 9 deletions basic/02-web3js-transaction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Abstract
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.

--Node Version:v20.11.0

# Getting Started

## Understanding The Functions of the [Smart Contract](Incrementer.sol)
Expand Down Expand Up @@ -86,7 +88,7 @@ const privatekey = process.env.PRIVATE_KEY;
```js
// Provider
const providerRPC = {
development: "https://goerli.infura.io/v3/" + process.env.INFURA_ID,
development: "https://sepolia.infura.io/v3/" + process.env.INFURA_ID,
moonbase: "https://rpc.testnet.moonbeam.network",
};
const web3 = new Web3(providerRPC.development); //Change to correct network
Expand Down Expand Up @@ -124,13 +126,15 @@ const deployTx = deployContract.deploy({
data: bytecode,
arguments: [5],
});

#arguments: [5] -> incrementer.sol : function increment
```

### 8. Sign the transaction
Use your private key to sign the transaction.
```js
// Sign Tx
const deployTransaction = await web3.eth.accounts.signTransaction(
const createTransaction = await web3.eth.accounts.signTransaction(
{
data: deployTx.encodeABI(),
gas: 8000000,
Expand All @@ -142,10 +146,10 @@ const deployTransaction = await web3.eth.accounts.signTransaction(
### 9. Send the transaction / Deploy your smart contract
Send your `deploy` transaction to the blockchain. You will receive a receipt, and get this contract address from the receipt.
```js
const deployReceipt = await web3.eth.sendSignedTransaction(
deployTransaction.rawTransaction
const createReceipt = await web3.eth.sendSignedTransaction(
createTransaction.rawTransaction
);
console.log(`Contract deployed at address: ${deployReceipt.contractAddress}`);
console.log(`Contract deployed at address: ${createReceipt.contractAddress}`);
```


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

```
| 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)

#we use sepolia now, it you interest in goerli, view below :
| 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)

#### Listen to Increment event only once
```js
Expand All @@ -213,8 +219,10 @@ incrementer.once('Increment', (error, event) => {
incrementer.events.Increment(() => {
console.log("I am a longlive event listener, I get a event now");
});

# event continuously code already change in index.js: from line 171~184, but above code also work.
```

# References
- Code part: https://docs.moonbeam.network/getting-started/local-node/deploy-contract/
- web3socket of Goerli: https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a
- web3socket of sepolia: https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a
8 changes: 4 additions & 4 deletions basic/02-web3js-transaction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function sleep(ms) {
*/
// Provider
const providerRPC = {
development: 'https://goerli.infura.io/v3/' + process.env.INFURA_ID,
development: 'https://sepolia.infura.io/v3/' + process.env.INFURA_ID,
moonbase: 'https://rpc.testnet.moonbeam.network',
};
const web3 = new Web3(providerRPC.development); //Change to correct network
Expand Down Expand Up @@ -49,7 +49,7 @@ const Trans = async () => {
data: bytecode,
arguments: [5],
});

// Sign Tx
const createTransaction = await web3.eth.accounts.signTransaction(
{
Expand Down Expand Up @@ -156,10 +156,10 @@ const Trans = async () => {
console.log('============================ 5. Listen to Events');
console.log(' Listen to Increment Event only once && continuouslly');

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

// listen to Increment event only once
Expand Down
8 changes: 5 additions & 3 deletions basic/03-web3js-erc20/README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

--测试Node版本:v20.11.0

## SimpleToken 合约功能说明

- IERC20
Expand Down Expand Up @@ -119,12 +121,12 @@

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

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

5. 获取账户地址
Expand Down
4 changes: 3 additions & 1 deletion basic/03-web3js-erc20/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

--Node Version:v20.11.0

## Getting started
### SimpleToken contract function description

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

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

Expand Down
2 changes: 1 addition & 1 deletion basic/03-web3js-erc20/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const receiver = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';
// Provider
const web3 = new Web3(
new Web3.providers.HttpProvider(
'https://goerli.infura.io/v3/' + process.env.INFURA_ID
'https://sepolia.infura.io/v3/' + process.env.INFURA_ID
)
);

Expand Down
Loading

0 comments on commit 966acbd

Please sign in to comment.