Technical Guide | Web3.js is based on the Ethereum Javascript API

Web3.js is a collection of libraries that you can use to connect to a local or remote Ethernet node using HTTP or IPC. The web3 JavaScript library can interact with the Ethereum blockchain. It can retrieve user accounts, send transactions, interact with smart contracts, and more.

Version : 1.0.0-beta.36

Web3.js API type

  1. Eth : Etherum blockchain related method;
  2. Net: the network status of the node;
  3. Personal: account function and sending;
  4. Db: Get/place the local LevelDB;
  5. Shh: use Whisper's P2P messaging;

Install Web3.js

Node


Instantiate web3

First, we check if the dapp browser or the updated version of MetaMask is used to inject the Ethereum provider into the window object. If so, we use it to create our web3 object, but we also need to explicitly request access to the account using ethereum.enable().

If the ethereum object does not exist, we will check the injected web3 instance. If it exists, it means that we are using an older dapp browser (such as Mist or an older version of MetaMask). If so, we can get its provider and use it to create our web3 object.

If no web3 instance is injected, we will create a web3 object based on the local provider. (This backup is suitable for development environments, but it is not safe and not suitable for production environments.)

Example of Web3.js

PS: web3.version.getNode is not recommended.

Balance

Check the user's balance

  1. web3.eth.getAccounts: Obtain an account from the Ethereum node. Web3.eth.accounts is deprecated.
  2. web3.eth.getBalance: Get the balance of the account.
  3. web3.utils.fromWei: Convert wei to ether, web3.fromWei is not recommended.

Transfer

Transfer Ethernet from "A" to "B"

txnObject (transaction object) is the first parameter of web3.eth.sendTransaction. txnObject consists of JSON.

From:String – The address of the sending account.

To:String – the target wallet address.

Value:Number | String | BigNumber – (Optional) The number of Ethernets you want to send to the destination address.

Gas:Number | String | BigNumber – (optional, default: to be determined) The amount of gas used for the transaction (unused gas will be refunded).

gasPrice:Number | String | BigNumber – (optional, default: pending) The price of the gas in this transaction in wei. The default is the average network gas price.

Data:String – (Optional) A byte string containing the associated data for the message, or the initialization code if the contract creates a transaction.

Nonce:Number – (Optional) An integer of nonce. This allows you to override your own pending transactions that use the same nonce.

Practice practice

I made a web page using the above method.

  • Get Ethereum node information
  • Get the user's balance
  • Transfer of Ethereum

You will be using MetaMask or Ganache. I recommend that you use Ganache, a personal blockchain developed for Ethereum that can be used to deploy contracts, develop applications, and run tests. If you haven't already done so, download Ganache and double-click the icon to install the app. This will generate a blockchain that runs locally on port 7545 while changing the port to 8545.

(Source: Public Number: Blockchain Research Laboratory)

We will continue to update Blocking; if you have any questions or suggestions, please contact us!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Blockchain

Opinion | Beyond BTC halving in 2020, these three major events will happen in the mining industry

Text | Huang Xueyi Operation | Gai Yao Edit | Mandy 王 梦蝶 Production | Odaily Planet Daily (ID: o...

Blockchain

Bitcoin is halving, is it a chance to make a fortune, or a disaster

What is the most important day in the currency circle? I think it should not be the day when the Bitcoin ETF was pass...

Bitcoin

Coinbase Commerce Removes Native Bitcoin Payment Support for Merchants – What’s Going On?

Leading experts in the crypto industry offer innovative solutions for Coinbase Commerce to enhance its support for na...

Blockchain

Bitcoin network sets another record: on-chain transaction volume exceeds $ 8.9 billion in 1 hour

According to foreign media reports on December 6, the latest data show that the bitcoin network's hourly US doll...

Blockchain

ChainNode evaluation: WOOKONG Bio & Solo hardware wallet full experience

Hello everyone, welcome to this issue of ChainNode evaluation. My name is Babbit Chan. On this special day, Xiaofei w...

Blockchain

The rebound of the market is insufficient, pay attention to short-term risks

Author | Hash sent analysis team Some science about Ethereum 2.0How is practical programmability implemented in the s...