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);
}
})();
}, []);
// ....
You don鈥檛 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 Thanks for the assistance. The issue is the non-https blocking like you mentioned.
@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?
What is the error?
Still could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.0.5)
(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);
[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?
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));
The output is
Uint8Array []
Text
As far as I can tell that request isn't throwing an error.
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?
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.
The backend is a geth node.
Are there any flags on the geth node that need to be set?
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...
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?
@ricmoo My react version is 16.13.1.
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
@nikita-zot-1408 Thanks. Works as a temp fix for now.
Most helpful comment
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