4

Suppose I have multiple copies of the same dapp running on different servers which do not directly communicate with each other, for testing purposes, and can manually interact with different dapp instances by changing which network I point Metamask to.

I would like to be able to show, on-screen, which network I am currently connected to (including the host address of my current provider), via client code. Then, when I generate screenshots or printouts, it is clear exactly which server I was connected to when that content was generated, without having to open MetaMask and show its network selector for every screenshot (not available for printouts). This can help eliminate speculation that an issue was caused by having an incorrect network selection in MetaMask.

More detail (skip for tl;dr): The client connects through MetaMask to a blockchain node hosted somewhere, which might be 127.0.0.1 or on a server that is part of the Infura service, or on some other machine which happens to be hosting a node for the blockchain I'm interested in reading from. It may be that there are multiple different IP addresses hosting the same blockchain (as part of a dapp) but the client will only connect to one at a time. Being able to show which one I'm reading from would also be helpful in checking for any differences between the nodes when, for example, they wind up on different sides of a virtual network partition and have no peer-to-peer communication path.

I can do web3.currentProvider and get a Proxy object revealing the selected address and network identifier code (which is 1 for mainnnet and small integers for big public testnets and settable for private networks; Ganache's default is 5777). This network identifier is useful but not sufficient by itself. I have not yet found a property or method I can call to get the IP address (or name set in MetaMask, which would seem less likely to be there).

Documentation on Provider objects is limited, but source code is here and the base class AbstractSocketProvider is here.

What am I missing? How would I programmatically find out the current network address of the blockchain node the client is interacting with?


Here is a more concrete example of what I'm trying to do, following a comment by @RobHitchens:

MetaMask has a network selector at the top of the interface, which looks like this at "Timepoint A:"
Screenshot

If you click the arrow in the right side of the bubble, there's a drop-down which shows other options, including Custom RPC. Clicking that takes you to a settings menu and scrolling down allows the input of a "New RPC URL" like this:

Screenshot

In that box, you can enter another IP address like "http://192.168.1.24:1234" (for another machine on a local network, for example). Clicking Save, that string becomes a new and currently-selected option in the network selector dropdown:
Screenshot
I'll call this timepoint B. For purposes of this question, assume MetaMask successfully connects (no loading symbol next to that URL).

I would like a function I can programmatically call that will at timepoint A return "http://127.0.0.1:7545" and at timepoint B return "http://192.168.1.24:1234" with no change to the dapp/client source code between those timepoints. It's OK if there are multiple functions which each return part of the information needed to construct that return value.

8
  • If I understand your question correctly: blockchains don't have an IP - they're a decentralized network. Only nodes which connect to the blockchain have IPs but finding a node's IP doesn't tell you anything about where it's connected to. Apr 17, 2019 at 18:51
  • @LauriPeltonen That is correct. I am referring to the IP of the node which the client is connected to.
    – WBT
    Apr 17, 2019 at 18:52
  • Laurie has tried to explain. Your question doesn't make sense even rereading several times. There is just no correspondance to the topology of a distributed blockchain network or metamask/infura method of connecting to them. Apr 18, 2019 at 1:54
  • @RobHitchens Do the new edits make the question more concrete and clear?
    – WBT
    Apr 18, 2019 at 15:12
  • @WBT It seems like the end of your question is malformed; if you're trying to determine which of the Eth-based networks you're connecting to, then you need to check Metamask's chainId and map that over to the network names. web3js.readthedocs.io/en/1.0/web3-eth.html#eth-chainid Edit: Just saw you said chainId wouldn't do. Why? The specific node you're connected to is irrelevant here, the IP of the node you're speaking to doesn't actually identify the network in a meaningful way. What do you need that checking chainId & genesis blocks wouldn't provide?
    – ohsully
    Jul 22, 2019 at 21:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.