What is the of use Web3.providers.HttpProvider("")
Asked Answered
R

3

16

I want to interact with a smart contract using web3js. Every example will start with following

var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');
// or
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

I don't understand the use of Web3.providers.HttpProvider('Address'). My Guess: So when establishing a private network every node should give a different rpcport which identifies it and so it connects to the network. Am I wrong? For example, the above code is used in Frontend for a website in order to connect frontend and deploy a contract in Ethereum Private Network. So the frontend code must be generic which means it should not add specific Ethereum node address in its code. Then what is the use of Web3.providers.HttpProvider('Address')?

Ruggles answered 15/3, 2018 at 4:10 Comment(0)
T
18

It has nothing to do with using a private vs public blockchain.

You need to give your client a way to connect to the blockchain. Specifically, the web3js library requires Provider object that includes the connection protocol and the address/port of the node you're going to connect to.

Web3js support 3 different providers: HttpProvider, WebsocketProvider, and IpcProvider. Both HTTP and WS require the address of the node (IPC uses a file). The address itself will be localhost if you're running a peer node on your client (ie, using Parity or Geth). If you're using a centralized provider like Infura, you would use https://mainnet.infura.io/API_KEY.

Triumph answered 15/3, 2018 at 16:32 Comment(1)
Is it the rpc port we give while establishing Ethereum Private NetworkRuggles
L
1

It is not linked to the private or public blockchain. On the Ethereum network, all nodes are connected to each other. When one performs a transaction, providers are used to inform other nodes about this transaction.

Limber answered 28/8, 2019 at 7:57 Comment(0)
E
1

Provider: You need a Provider to read from the blockchain. In simple terms its a server running a node(local or a service like "infura") that which can query the blockchain directly.

HttpProvider: A node either local or on cloud can give a HTTP, IPC or WSS for clients to interact with the node. "Web3.providers.HttpProvider()" takes the http url of the node.

Docs Reference: https://docs.ethers.io/v5/api/providers/

Code Example:

const Web3 = require("web3");


const HttpProvider =
  "https://eth-mainnet.g.alchemy.com/v2/YOUR_API";

async function main() {
  try {
    
    // Creating an instance of the Provider
    const web3 = new Web3(new Web3.providers.HttpProvider(HttpProvider));

    console.log("Connection Successful");
    console.log("Latest Block Number: ");

    // Querying the Blockchain using the Provider and Web3.js
    console.log(await web3.eth.getBlockNumber());
  } 
  catch (error) {
    console.log("Connection Error! ", error);
  }
}

main();
Emmieemmit answered 6/8, 2022 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.