current position:Home>Ethereum series - Web3 js

Ethereum series - Web3 js

2022-02-03 05:27:58 Brick moving leader

Web3.js

web3.js Is a set of interfaces used to interact with local or remote Ethereum nodes js library , It can be used HTTP or IPC Establish a connection with the Ethereum node .
On the bottom implementation , It passes through RPC Call to communicate with local or remote nodes .web3.js Can be exposed with any RPC Ethernet node connection of interface .

Installation

npm install web3

Usage

// In Node.js
const Web3 = require('web3');

let web3 = new Web3('ws://localhost:8546');
console.log(web3);
> {
    
    eth: ... ,
    shh: ... ,
    utils: ...,
    ...
}

0.20.X And 1.X Differences in versions

This is a web3.js There are two main types of versions

  • use truffle migrate or truffle test It will be installed with the operating system web3js edition , It can be used truffle verson see
  • nodejs Will use package.json Specified in the web3js Version number

1.x Version changes

  • if ('utils' in web3) This judgment can be used web3j Version of
  • web3.sha3 By web3.utils.sha3 replace
    • The data to be hashed is called web3.utils.sha3 You need to judge the empty space first : if (!input || !input.length) { return; }
  • web3.eth.accounts is deprecated. Use web3.eth.getAccounts() instead
  • web3.fromWei By web3.utils.fromWei replace
  • web3.toWei By web3.utils.toWei replace
  • watch No longer supported .
    • myContract.EventName([params]).watch([callback]) By myContract.EventName([params],[callback]) replace
  • The HTTP provider is deprecated, as it won’t work for subscriptions. Have to use websocket
    • truffle-config.js in websockets: true
    • HttpProvider By WebsocketProvider replace , The port number does not need to be changed
    • network_local.json Medium rpcUrl configure ws://127.0.0.1:9545

Old projects want to compile

# package.json Middle configuration 
"web3": "^0.20.0",

# npm when 
npm install --force

Past highlights :
Blockchain Knowledge Series
Cryptography series
Zero knowledge proof series
Consensus series
Public chain research series
Bitcoin series
Ethereum series
EOS series
Filecoin series
Alliance chain series
Fabric series
Smart contract series
Token series

copyright notice
author[Brick moving leader],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202030527560132.html

Random recommended