Web3.JS

How to run Web3.JS

Web3.JS

There are several modules in the web3.js library that contain specific functionality for the Ethereum ecosystem.

Web3.js interacts with both local (such as “ganache”) and remote Ethereum nodes. It also interacts with smart contracts.

Web3 retrieves account balance, gas price, and so on.

Web3.js objects typically include a callback as the last parameter, and their functions offer promises as a return value.

Hire-Blockchain-Developers

For the Ethereum ecosystem, the Web3.js library includes features like…

1) web3-eth
-> This functionality is for Ethereum Blockchain and Smart Contracts.
2) web3-shh
-> This allows broadcast and peer-to-peer communication.
3) web3-bzz
-> This feature is for decentralized file storage.
4) web3-utils
-> it is a very useful function for developers of Dapps.
Before starting the project, we will check whether we have a node or not, if you haven’t installed the node, then click on this link to install the node. “https://nodejs.org/en/”

Now, here is some information on Web3.js.

Open your terminal and install web3.

After installing web3 run the command into the terminal.

Initialize the node and start running commands.

“node”

let Web3 = require(“web3”);

In the terminal, type Web3 to get all functions and modules.

If you want to check the web3 version then write this command
“Web3.version”

Web3 provides utils and modules for Ethereum Blockchain.
Check Property of Web3.utils
“Web3.utils”

You can also use Infura or the URL from Ganache for testing.
ganache looks like.

Start ganache in your system and take the RPC server.
let url = “HTTP://127.0.0.1:7545”;

For every feature, Web3 has a transaction function.
let web3 = new Web3(url);

Let us get the local address from the ganache. if you don’t have ganache, then install it from this link: “https://trufflesuite.com/ganache/” ganache provides local Etherium Blockchain for testing, executing Commands, and controlling how to chain works.

var address = “0xE953878466efada2c651501C19E1AD2b7bE7E308”;

If you want to get the balance from this address, write this function.
web3.eth.getBalance(address,(err,bal)=>{balance = bal});
This balance is in get wei form
balance

Convert wei form to ether form
web3.utils.fromWei(balance,”ether”);

Convert wei form to Gwei form
web3.utils.fromWei(balance,”Gwei”);

The direct conversion of balance into ether form.
web3.eth.getBalance(address,(err,bal)=>{balance = web3.utils.fromWei(balance,’ether’)});

Get gas price into ether form
web3.eth.getGasPrice().then((resp)=>{console.log(web3.utils.fromWei(resp,”ether”))})

You want to create accounts in the Ethereum Blockchain, not in local
web3.eth.accounts.create()

Account created like address and private key

{ 
address: '0x00000000000000000000000000000000000000000', 
privateKey: '0x00000000000000000000000000000000000000000000000000000000000000000', 
signTransaction: [Function: signTransaction], 
sign: [Function: sign], 
encrypt: [Function: encrypt] 
}

Implement web3 with smart contracts, take an abi from “ethercsan.io” ERC20 token contract, and go to code.
var contractAddress = “0xE953878466efada2c651501C19E1AD2b7bE7E308”
var abi = [{“constant”:true,”inputs”:[],”name”:”name”,”outputs”:[{“name”:””,”type”:”string”}],,{“anonymous”:false,”inputs”:[],…,”name”:”Unpause”,”type”:”event”}]

—> take a contract
var contract = new web3.eth.Contract(abi,contractAddress);

Contracts have methods and events.

You just write “contract.methods” and get all methods of a smart contract.

Similarly, You just write “contract.events” and get all events of the smart contract.

Send the currency from the first account to another account in the same manner, but in wei form.
web3.eth.sendTransaction({from:’0xE953878466efada2c651501C19E1AD2b7bE7E308′, to:’0x00a7e026ed9b0d54F2DE32Aa453bE718464dE647′,value:10000000000000000000})
.on(‘confirmation’,function(num,rec){console.log(“num => “,num,”rec => “,rec)})

If you want to learn the whole Web3.js refer to this link and get full information: “https://web3js.readthedocs.io/en
image : “https://miro.medium.com/max/720/0*PLNJAZVD6XfcHT7l.png

In conclusion, you learned how to set up a web3.js environment, create and deploy a smart contract, and interact with the contract using web3.js. By the end of this guide, you’ll be well on your way to becoming a web3.js expert!

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply