抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

web3_day01

学习资源视频

官方文档

好文档

1. Web3.js 的安装

1.1 node.js 的下载安装及配置: 参考博客

1.2 在 VSCode 中安装插件 : code runner

image-20230401110150647

2. 下载安装使用Ganache

网上搜一下就有了,建议自己创建一个工作的空间,这样之前操作 的交易信息就会被保留下来。

3. 跟着视频敲代码

3.1 视频中的代码:

1
2
3
let Web3 = require('web3');
web3 = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));
console.log(web3);

分析:

image-20230401112609252

解读:第一行中的 require('web3') web3是库中的名字,我以为是自定义的,尝试改成其他名字报错如下:

image-20230401112210746

变量名 web3 容易和第一行中的 ‘web3’ 搞混,所以自己换了一个变量名

自己的代码:

1
2
3
let Web3 = require('web3');
web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));
console.log(web);

4. 子模块列表

image-20230401112441202

在执行这段代码的时候,出现了错误

代码:

1
2
3
4
let Web3 = require('web3');
web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));

web.eth.getNodeInfo().then(console.log);

解读:

官方文档上的原函数是这样这样写的: web3.eth.getNodeInfo([callback]);

老师的写法是: web.eth.getNodeInfo().then(); 老师说是一样的,.then 的意思是:执行完web.eth.getNodeInfo() 之后,再去执行 .then() 括号里的代码,即 console.log

错误:

image-20230401114624543

解决方案:

打开菜单选项,找到之前安装的 Ganache,启动它即可,然后再次运行代码

image-20230401114857332

总结:如果把 Ganache 关闭了,代码又是不能运行的,只有将 Gannache 一直开着的,才可以正常运行,但是 不知那一大串是什么

5. 网络状态查询

5.1 查看是否连接到节点 :

  • web.eth.net.isListening().then(console.log);

image-20230401120639761

image-20230401120402488

  • web.bzz.net.isListening().then(console.log);

image-20230401120837320

5.2 获取 Network ID (网络号)

image-20230401121018444

6. providers相关

6.1 查看 Web3 中所有可用的Providers

代码:

1
2
3
4
5
6
7
8
9
>let Web3 = require('web3');
>web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));

>// web.eth.getNodeInfo().then(console.log);

>// web.eth.net.isListening().then(console.log);
>// web.bzz.net.isListening().then(console.log);

>console.log(web.providers);

运行结果:

image-20230401121840875

6.2 查看当前设置的 Web3 provider

image-20230401121948503

image-20230401122218414

6.3 查看浏览器环境设置的web3 provider

image-20230401122117046

image-20230401122142746

6.4 设置/ 修改 provider

image-20230401122314375

image-20230401122600555

7. 批处理请求

7.1 将发布合约到测试网

image-20230401122827172

重头戏!!!

看到这个界面,我就知道,我又可以学到东西了

image-20230401122954083

原来是是这样连接本地的账户,直接惊呆!

方法一:

① 打开remix,选择这两个其中之一:

image-20230401123951035

② 将自己本地的端口号填入:

image-20230401124102313

③ 然后就呈现如下界面:

image-20230401124144334

④ 本地账户和remix 中的 账户一一对应:

image-20230401124335105

image-20230401124354110

方法二:嵌入式连接

① 添加网络

image-20230401131938761

② 和 remix 建立连接

image-20230401134225834

此时当前网络钱包没钱

③ 通过私钥导入本地账户

image-20230401134744647

④ 导入成功之后,就可以在remix 中同步账户余额

image-20230401134954430

⑤ 可以测试一下,是否能正常使用,结果是 ok 滴

image-20230401135204208

⑥ 在本地可以查看 remix部署的合约

image-20230401124843146

7.2 复制ABI 和 地址到程序中

① 复制 remix 中的 ABI

image-20230401135948783

② 到代码程序中,定义一个变量 var abi,并将复制的ABI 作为值,赋给 abi

image-20230401140336515

③ 到remix 中拷贝地址,并赋予 我们自定义的变量 var address

image-20230401140649349

1
var address = "0xA96406CE93aE4803F290dFAb2a54D4e9B050E239";

④ 通过 ABI 和 合约地址 去 创建合约对象

1
var contract = new web.eth.Contract(abi,address);

⑤ 然后就是是一系列的调用操作(目前代码还不是很懂)

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
let Web3 = require('web3');
web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));

// web.eth.getNodeInfo().then(console.log);

// web.eth.net.isListening().then(console.log);
// web.bzz.net.isListening().then(console.log);

// console.log(web.providers);



// console.log(web);
// console.log(Web3.modules);
// console.log(Web3.version);

// var web = new Web3(Web3.givenProvider || "ws://localhost:8545");

// console.log(web);

var abi = [
{
"inputs": [
{
"internalType": "uint256",
"name": "_number",
"type": "uint256"
}
],
"name": "setNumber",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getNumber",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
];

// 部署出来的合约地址
var address = "0xA96406CE93aE4803F290dFAb2a54D4e9B050E239";
var contract = new web.eth.Contract(abi,address);

function callBack1() {
console.log("callBack1() is running");
}

function callBack2() {
console.log("callBack2() is running");
}

var batch = new web.BatchRequest();

// "0x11e6df0756c5D6fCa1ca49b9d6227D49b93C746c" 是函数调用者的地址
batch.add(web.eth.getBalance.request('0x11e6df0756c5D6fCa1ca49b9d6227D49b93C746c','latest',callBack1));

batch.add(contract.methods.getNumber().call.request({from:'0x11e6df0756c5D6fCa1ca49b9d6227D49b93C746c'},callBack2));

batch.add(contract.methods.getNumber().call.request({from:'0x11e6df0756c5D6fCa1ca49b9d6227D49b93C746c'},function(error,reusult){
console.log(error);
console.log(reusult);
}));

batch.execute();

结果:

image-20230401143327628

8. 大数据处理(bigNumber)

》》》工具的文档在此处

8.1 简介

image-20230401150727941

image-20230401151740641

image-20230401151759467

测试案例:

image-20230401151711614

image-20230401152005462

9. 以太单位转换

9.1 web3.utils.fromWei

image-20230401233238650

注意:web3.utils.fromWei(number,[unit])中的 number 通常都是用字符串表示,如果直接是数字的话就会报错

示例:

image-20230401233915263

9.2 web3.utils.toWei

image-20230401234235821

image-20230401234716543

9.3 web3.utils.toHex

image-20230401235007895

image-20230401235027104

演示:

image-20230401235106832

tips: 还可以将字符串转换为 16进制

9.4 web3.utils.hexToNumberString

image-20230401235336978

image-20230401235454600

演示示例:

image-20230401235539563

9.5 还有一些其他的转化

image-20230401235925865

image-20230402000018909

10. 地址相关的操作

10.1 web3.isAddress

image-20230402000137057

image-20230402000336920

image-20230402001516897

***解读:为什么第五条代码执行的结果为 false? ***

可以参考我同学的博客:[26.checksum of address | solidity life (levi104.com)](https://www.levi104.com/2023/04/02/03.solidity进阶/26.checksum of address/)

11. 查询区块信息

11.1 查询最新的区块号(区块高度)–web3.eth.getBlockNumber()

image-20230402110806142

image-20230402110839695

11.2 查询最新区块– we3.eth.getBlock()

image-20230402110915814

image-20230402112113547

11.3 查询区块中的交易 – web3.eth.getTransactionFromBlock()

image-20230402112724295

image-20230402112818222

web3_day02

12. web3.js 交易操作

12.1 账户相关

12.1.1 查询账户个数 – web3.eth.getAccount()

image-20230402113250997

image-20230402113346757

12.1.2 查询账户个数 – web3.eth.getAccounts()

image-20230402113459961

image-20230402113652977

12.1.3 查询coinbase – web3.eth.getCoinbase()

image-20230402113809157

12.2 交易相关

12.2.1 查询余额 – web3.eth.getBalance()

image-20230402113949196

image-20230402114029796

image-20230402114235624

12.2.2 查询平均 gas 价格

image-20230402114608289

image-20230402114616750

1
2
3
web3.eth.getGasPrice().then((result) => {
console.log("wei: " + result);
})

可能是我没学过前端吧,这个写法属实让我震惊。。。。。

image-20230402115112724

12.3 交易执行相关

12.3.1 发送交易 – web3.eth.sendTransaction()

image-20230402115215140

image-20230402115406027

突发奇想,试试能不能给我另一个账户转钱

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
let Web3 = require("web3");
web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));

// web.eth.getBlock("latest",true).then(console.log);

var transactionObject = {
from: "0x3C061FCEb96fca484e8c55C204BAd35C6fE2CE0f",
to: "0x2DeF5505E8EfaD8dF16eFE1dFd8a49f52360b2e8",
value: web.utils.toWei('1','ether'),
data: ''
}

web.eth.sendTransacation(transactionObject).then(console.log);

from (本地账户): image-20230402122815796

to (钱包地址):image-20230402122905524

但是报错了,如下:

image-20230402123501463

再回去看视频,打开弹幕,直接狠狠感谢弹幕老哥!!!!

image-20230402123710450

正确代码:将 data 字段删掉,或者注释掉

1
2
3
4
5
6
7
8
9
10
11
12
13
let Web3 = require("web3");
web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));

// web.eth.getBlock("latest",true).then(console.log);

var transactionObject = {
from: "0x3C061FCEb96fca484e8c55C204BAd35C6fE2CE0f",
to: "0x2DeF5505E8EfaD8dF16eFE1dFd8a49f52360b2e8",
value: web.utils.toWei('1','ether'),
// data: ''
}

web.eth.sendTransaction(transactionObject).then(console.log);

代码正常运行:

image-20230402123859440

到我的钱包去查看余额,发现转账成功:

image-20230402123931188

12.3.2 查询交易信息 — web3.eth.getTransaction()

image-20230402124154599

image-20230402124402890

12.3.3 查询交易收据(进区块数据) – web3.eth.getTransactionReceipt()

image-20230402124517234

image-20230402124726406

image-20230402124751392

13. web3.js 合约交互 — 最想学的地方

13.1 应用程序二进制接口(ABI)

13.1.1 ABI 简介

image-20230402125054969

13.1.2 ABI 范例

image-20230402125214707

13.1.3 ABI表现形式举例

image-20230402125248757

13.1.4 ABI 的作用

image-20230402125456396

13.2 创建合约

13.2.1 准备智能合约

image-20230402125732353

MyContract.sol:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyContract {

//状态变量
string storageVaule;

//public 的状态变量
string public name;

// 构造函数
constructor() public {
storageVaule = "biyou";
}

// 结构体
struct Student {
string name;
int age;
}

// 修改状态变量
function setStorageValue(string memory str) public {
storageVaule = str;
}

function getStorageValue() public view returns(string memory) {
return storageVaule;
}

// 可以发币的函数
function setMoney() public payable {

}

// 事件
event myEvent (string name);

function emitEvent(string memory name) public returns(string memory) {
emit myEvent(name);
return strConcat("hello",name);
}

function strConcat(string memory _a, string memory _b) internal pure returns(string memory) {
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
string memory ret = new string(_ba.length + _bb.length);
bytes memory bret = bytes(ret);
uint k = 0;
for (uint i = 0; i < _ba.length; i++) bret[k++] = _ba[i];
for (uint i = 0; i < _bb.length; i++) bret[k++] = _bb[i];

return string(ret);
}
}

13.2.2 直接使用js在区块链上部署一个新的智能合约(!!!难点)

image-20230402131528580

image-20230402131631162

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let Web3 = require("web3");
web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));

// fs 模块读取.sol合约文件
var fs = require("fs");
var myContractABI = JSON.parse(fs.readFileSync("D:\\VSCode\\VSCode_Data\\Web3Workspace\\js\\lesson_online\\MyContract.abi"),toString());

var myContract = new web.eth.Contract(myContractABI);

var data = "0x608060405234801561001057600080fd5b506040518060400160405280600581526020017f6269796f750000000000000000000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610166565b82805461006e90610105565b90600052602060002090601f01602090048101928261009057600085556100d7565b82601f106100a957805160ff19168380011785556100d7565b828001600101855582156100d7579182015b828111156100d65782518255916020019190600101906100bb565b5b5090506100e491906100e8565b5090565b5b808211156101015760008160009055506001016100e9565b5090565b6000600282049050600182168061011d57607f821691505b6020821081141561013157610130610137565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b61095f806101756000396000f3fe60806040526004361061004a5760003560e01c806306fdde031461004f57806344e649b71461007a57806390ad7f5414610084578063982296a8146100af578063ec6070f6146100ec575b600080fd5b34801561005b57600080fd5b50610064610115565b60405161007191906106a9565b60405180910390f35b6100826101a3565b005b34801561009057600080fd5b506100996101a5565b6040516100a691906106a9565b60405180910390f35b3480156100bb57600080fd5b506100d660048036038101906100d1919061062f565b610237565b6040516100e391906106a9565b60405180910390f35b3480156100f857600080fd5b50610113600480360381019061010e919061062f565b6102b6565b005b60018054610122906107df565b80601f016020809104026020016040519081016040528092919081815260200182805461014e906107df565b801561019b5780601f106101705761010080835404028352916020019161019b565b820191906000526020600020905b81548152906001019060200180831161017e57829003601f168201915b505050505081565b565b6060600080546101b4906107df565b80601f01602080910402602001604051908101604052809291908181526020018280546101e0906107df565b801561022d5780601f106102025761010080835404028352916020019161022d565b820191906000526020600020905b81548152906001019060200180831161021057829003601f168201915b5050505050905090565b60607f52b54003f649c6d5ca97ae2b169c38f3c257eb14026ad745b11808153cdfd1528260405161026891906106a9565b60405180910390a16102af6040518060400160405280600581526020017f68656c6c6f000000000000000000000000000000000000000000000000000000815250836102d0565b9050919050565b80600090805190602001906102cc929190610524565b5050565b6060600083905060008390506000815183516102ec919061073d565b67ffffffffffffffff81111561032b577f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6040519080825280601f01601f19166020018201604052801561035d5781602001600182028036833780820191505090505b50905060008190506000805b855181101561043d578581815181106103ab577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b602001015160f81c60f81b8383806103c290610842565b9450815181106103fb577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061043590610842565b915050610369565b5060005b845181101561051557848181518110610483577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b602001015160f81c60f81b83838061049a90610842565b9450815181106104d3577f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350808061050d90610842565b915050610441565b50829550505050505092915050565b828054610530906107df565b90600052602060002090601f0160209004810192826105525760008555610599565b82601f1061056b57805160ff1916838001178555610599565b82800160010185558215610599579182015b8281111561059857825182559160200191906001019061057d565b5b5090506105a691906105aa565b5090565b5b808211156105c35760008160009055506001016105ab565b5090565b60006105da6105d5846106f0565b6106cb565b9050828152602081018484840111156105f257600080fd5b6105fd84828561079d565b509392505050565b600082601f83011261061657600080fd5b81356106268482602086016105c7565b91505092915050565b60006020828403121561064157600080fd5b600082013567ffffffffffffffff81111561065b57600080fd5b61066784828501610605565b91505092915050565b600061067b82610721565b610685818561072c565b93506106958185602086016107ac565b61069e81610918565b840191505092915050565b600060208201905081810360008301526106c38184610670565b905092915050565b60006106d56106e6565b90506106e18282610811565b919050565b6000604051905090565b600067ffffffffffffffff82111561070b5761070a6108e9565b5b61071482610918565b9050602081019050919050565b600081519050919050565b600082825260208201905092915050565b600061074882610793565b915061075383610793565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156107885761078761088b565b5b828201905092915050565b6000819050919050565b82818337600083830152505050565b60005b838110156107ca5780820151818401526020810190506107af565b838111156107d9576000848401525b50505050565b600060028204905060018216806107f757607f821691505b6020821081141561080b5761080a6108ba565b5b50919050565b61081a82610918565b810181811067ffffffffffffffff82111715610839576108386108e9565b5b80604052505050565b600061084d82610793565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8214156108805761087f61088b565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f830116905091905056fea2646970667358221220f839dcd300d8752d974808215c924e8e08a1d6556227d4be2b3c89805e41a38764736f6c63430008020033";

myContract.deploy({
data: data
}).send({
from: "0x3C061FCEb96fca484e8c55C204BAd35C6fE2CE0f",
gas: 1500000,
gasPrice: '1000000'
},function(error,result){
console.log(result)
})

// var contractAddress = "";
// var myContract = new web.eth.Contract(myContractABI,contractAddress);

报错:

image-20230402134015024

原因:\ 是一个特殊字符,即转义字符,需要 \\ 这样表示一个 \

image-20230402133843906

执行结果:

image-20230402135531960

这个结果是不正确的,没有给我们返回一个地址,解决办法是: 将 gasPrice 字段注释掉, 如 // gasPrice: '1000000'

再次执行其结果为:

image-20230402135459173

① data 来源(需要在前面加一个0x):image-20230402132205099

可以在 Ganache 上面查到 在vscode 部署的合约地址:

image-20230402141606649

用这个合约地址到,remix 上验证一下:

image-20230402141445005

13.3 调用合约函数

13.3.1 调用智能合约读函数

image-20230402170058160

image-20230402170415925

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let Web3 = require("web3");
web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));

// fs 模块读取.sol合约文件
var fs = require("fs");
var myContractABI = JSON.parse(fs.readFileSync("D:\\VSCode\\VSCode_Data\\Web3Workspace\\js\\lesson_online\\MyContract.abi"),toString());

var myContract = new web.eth.Contract(myContractABI);
var contractAddress = "0x4c8BbD5829Bdcb9C9b11Ec3D5556cC77541FFB2e";
var myContract = new web.eth.Contract(myContractABI,contractAddress);

myContract.methods.getStorageValue().call((err,result)=>{
console.log(result);
});

结果:

image-20230402171056829

13.3.2 调用智能合约写函数

image-20230402171154058

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let Web3 = require("web3");
web = new Web3(new Web3.providers.HttpProvider("Http://127.0.0.1:7545"));

// fs 模块读取.sol合约文件
var fs = require("fs");
var myContractABI = JSON.parse(fs.readFileSync("D:\\VSCode\\VSCode_Data\\Web3Workspace\\js\\lesson_online\\MyContract.abi"),toString());

var myContract = new web.eth.Contract(myContractABI);
var contractAddress = "0x4c8BbD5829Bdcb9C9b11Ec3D5556cC77541FFB2e";
var myContract = new web.eth.Contract(myContractABI,contractAddress);

myContract.methods.setStorageValue("new message").send({
from: "0x3C061FCEb96fca484e8c55C204BAd35C6fE2CE0f"
}).on("receipt", function(result){
console.log(result);
myContract.methods.getStorageValue().call((err,result)=>{
console.log(result);
});
});

image-20230402173307700

13.4 调用合约事件

13.4.1 合约事件监听

image-20230402173630888

13.4.2 执行事件查询

image-20230402173657711

image-20230402173708906

image-20230402173845442

14. 简单的DApp – 投票系统

评论



政策 · 统计 | 本站使用 Volantis 主题设计