0

I know how to instantiate web3 to work with a local test network:

var web3Instance =  new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

How do I change this code to connect to the real network?

1

That is how you connect to a real network, with web3.

The difference, however, is that instead of connecting to a testrpc (or Parity or Geth) node running a local, private network exposing an RPC interface, that instead you connect to a client connected to one of the test networks (or mainnet).

In the case of using HttpProvider, just start your client with the necessary JSON RPC interfaces exposed.

The documentation on the Ethereum wiki gives you examples for a number of different clients.

  • So my code is ready to work against the real network, even though the address is localhost:8545? I don't need to change anything? – Alon Nov 9 '17 at 13:44
  • Correct. Although, you might want to consider what happens if your user isn't running a node locally and exposing an RPC interface that way e.g. if they are looking at your Dapp through a browser with MetaMask. The usual fallback pattern is this. – SteveJaxon Nov 9 '17 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.