Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.0.5) in React Native Expo #1316

Closed
conorbros opened this issue Feb 25, 2021 · 22 comments
Labels
discussion Questions, feedback and general information.

Comments

@conorbros
Copy link

conorbros commented Feb 25, 2021

Here is my code being used for testing. It works in a small node program but will not work in React Native.

import "@ethersproject/shims";
import { ethers } from "ethers";

const NodeAddress = "http://34.87.222.10:8545";
// ....
  useEffect(() => {
    (async () => {
      try {
        let p = await ethers.providers.getDefaultProvider(NodeAddress);
        await p._networkPromise;
        console.log(p);
      } catch (e) {
        console.log(e);
      }
    })();
  }, []);

// ....
@ricmoo
Copy link
Member

ricmoo commented Feb 25, 2021

You don’t need to await on getDefaultProvider, and can you try using await provider.ready instead and see if that helps?

Is the NodeAddress reachable from the phone/computer you are using? Keep in mind that iOS blocks non-https (i.e. http) traffic for example. Android may do something similar...

You can try hitting the host directly to test the link.

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Feb 25, 2021
@conorbros
Copy link
Author

@ricmoo Thanks for the assistance. The issue is the non-https blocking like you mentioned.

@conorbros
Copy link
Author

@ricmoo am still having this issue in Expo. After setting the node behind a HTTPS proxy and verifying that I can hit the host directly with the link, using ethers to connect still gives this error. Is there anything else I can try?

@ricmoo
Copy link
Member

ricmoo commented Feb 26, 2021

What is the error?

@conorbros
Copy link
Author

Still could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.0.5)

@ricmoo
Copy link
Member

ricmoo commented Feb 26, 2021

(oh of course, I checked the issue body and not the title... My bad. :p)

What do you get if you change that provider call to:

const result = await ethers.utils.fetchJson(NodeAddress, '{ "id": 42, "jsonrpc": "2.0", "method": "eth_chainId", "params": [ ] }')
console.log(result);

@ricmoo ricmoo reopened this Feb 26, 2021
@conorbros
Copy link
Author

[Unhandled promise rejection: Error: processing response error (body={}, error={"reason":"invalid JSON","code":"SERVER_ERROR","body":{},"error":{"line":298118,"column":34,"sourceURL":"http://192.168.1.116:19001/src/App.tsx.bundle?platform=android&dev=true&hot=false&minify=false"},"line":278932,"column":32,"sourceURL":"http://192.168.1.116:19001/src/App.tsx.bundle?platform=android&dev=true&hot=false&minify=false"}, requestBody={"0":123,"1":32,"2":34,"3":105,"4":100,"5":34,"6":58,"7":32,"8":52,"9":50,"10":44,"11":32,"12":34,"13":106,"14":115,"15":111,"16":110,"17":114,"18":112,"19":99,"20":34,"21":58,"22":32,"23":34,"24":50,"25":46,"26":48,"27":34,"28":44,"29":32,"30":34,"31":109,"32":101,"33":116,"34":104,"35":111,"36":100,"37":34,"38":58,"39":32,"40":34,"41":101,"42":116,"43":104,"44":95,"45":99,"46":104,"47":97,"48":105,"49":110,"50":73,"51":100,"52":34,"53":44,"54":32,"55":34,"56":112,"57":97,"58":114,"59":97,"60":109,"61":115,"62":34,"63":58,"64":32,"65":91,"66":32,"67":93,"68":32,"69":125}, requestMethod="POST", url="https://conorb.dev", code=SERVER_ERROR, version=web/5.0.3)]

Here is the full promise rejection. I just tested outside of Expo and it works normally. Perhaps a shim issue?

@ricmoo
Copy link
Member

ricmoo commented Feb 26, 2021

We're getting closer, I think. Looks like the response isn't JSON, so we'll go one further bit up. Try this:

    const result = await ethers.utils._fetchData({
        url: NodeAddress,
        headers: { "content-type": "application/json" },
    },
        ethers.utils.toUtf8Bytes('{ "id": 42, "jsonrpc": "2.0", "method": "eth_chainId", "params": [ ] }')
    );
    console.log("Binary", result);
    console.log("Text", ethers.utils.toUtf8String(result));

@conorbros
Copy link
Author

conorbros commented Feb 26, 2021

The output is

Uint8Array []
Text 

As far as I can tell that request isn't throwing an error.

@ricmoo
Copy link
Member

ricmoo commented Feb 26, 2021

So, that means the response from the link is working. It is just returning zero bytes... Which is invalid JSON. :)

Is the backend a normal Node? Maybe there is a problem with the proxy?

@conorbros
Copy link
Author

When I try that method outside of expo it returns this

Uint8Array(45) [
  123,  34, 106, 115, 111, 110, 114, 112,  99,  34,
   58,  34,  50,  46,  48,  34,  44,  34, 105, 100,
   34,  58,  52,  50,  44,  34, 114, 101, 115, 117,
  108, 116,  34,  58,  34,  48, 120,  57, 102,  98,
  102,  49,  34, 125,  10
]
Text {"jsonrpc":"2.0","id":42,"result":"0x9fbf1"}

It must be an expo specific problem. Maybe the response isn't able to be parsed properly.

@conorbros
Copy link
Author

The backend is a geth node.

@conorbros
Copy link
Author

Are there any flags on the geth node that need to be set?

@ricmoo
Copy link
Member

ricmoo commented Feb 26, 2021

I think there may be. Depending on your setup (again, if you are using a proxy, things might further be affected), but you might want to adjust the network device to be any (I think you do this by specifying 0.0.0.0) and you might need to add the CORS origin flag to allow any (I don't know the key or value to set for this).

If it is using CORS, make sure your Proxy is set up to forward OPTIONS too.

Setting up a link is definitely non-trivial...

@ricmoo
Copy link
Member

ricmoo commented Feb 28, 2021

This may be related to #1320. I'm trying to debug the RN universe right now. Can you include the version of react in your package.json?

@conorbros
Copy link
Author

conorbros commented Feb 28, 2021

@ricmoo My react version is 16.13.1.

@nikita-zot-1408
Copy link

Strangely,react native does not work with the current version of ethers library but it works fine with ethers version 5.0.5 and lower

@conorbros
Copy link
Author

@nikita-zot-1408 Thanks. Works as a temp fix for now.

@daihovey
Copy link

Seeing same error when trying to fetch balance

export const provider = new ethers.providers.JsonRpcProvider(
  MAINNET_ALCHEMY,
  1
);
let balance = await provider.getBalance(address);

seeing [Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.4.2)]

Provider log:

{"_emitted": {"block": -2}, "_events": [], "_fastQueryDate": 0, "_isProvider": true, "_lastBlockNumber": -2, "_maxInternalBlockNumber": -1024, "_network": {"_defaultProvider": [Function func], "chainId": 1, "ensAddress": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e", "name": "homestead"}, "_nextId": 42, "_pollingInterval": 4000, "anyNetwork": false, "connection": {"url": "https://eth-mainnet.alchemyapi.io/v2/redacted"}, "formatter": {"formats": {"block": [Object], "blockWithTransactions": [Object], "filter": [Object], "filterLog": [Object], "receipt": [Object], "receiptLog": [Object], "transaction": [Object], "transactionRequest": [Object]}}}
"react": "17.0.1",
"react-native": "0.64.2",
"ethers": "^5.4.1",
"@ethersproject/shims": "^5.4.0"

@daihovey
Copy link

also, if I swap to using getDefaultProvider

const network = 'homestead';
export const provider = ethers.getDefaultProvider(network, {
  alchemy: MAINNET_ALCHEMY,
});

I get Error: failed to meet quorum

@daihovey
Copy link

Derp, re-read your docs, was missing import "@ethersproject/shims"

@zemse
Copy link
Collaborator

zemse commented Jul 27, 2021

Hey @daihovey, the error could not detect network could mean some reasons:

  • The node is down/unreachable
  • The eth_chainId call fails for some reason
  • Your network is down

If you're still facing this, are you in browser or node? In the browser, you'll be able to see network logs.

You also get failed to meet quorum when all of the requests fail, so I suppose you must be having a temporary network problem. If not could you paste the entire error here (it contains some details)?

@ethers-io ethers-io locked and limited conversation to collaborators Aug 24, 2021
@ricmoo ricmoo closed this as completed Aug 24, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

5 participants