Search code examples
javascriptethereumblockchainweb3js

Connecting with a Blockchain node using Web3.js


Every time I connect to a blockchain I use the following line of code: let web3 = new Web3(new Web3.providers.HttpProvider("Address"). I understand the use of the address and the Providers to connect with a blockchain. But what is the use of the first part? let web3 = new Web3(new Web3.

It successfully helps me to connect with a blockchain, but I don't understand the logic behind it.


Solution

  • let me ask your question. So basically, the purpose of this code let web3 = new Web3(new Web3. is to create an object based on a Web3 object that was created using JavaScript and saved to the variable named web3.

    After that, you can use all Web3 functions inside Web3 objects, such as getting the providers using your address. The provider is an interface that Web3 uses to communicate with the Ethereum network. In this case, the provider is an HTTP provider, which means that the Web3 object will use HTTP to communicate with an Ethereum node at the specified "Address".

    And other words, when you create a Web3 object with an HTTP provider, you are essential setting up a connection to an Ethereum node running on a remote server. This allows you to send requests to the Ethereum network, such as querying for account balances, sending transactions, or interacting with smart contracts.