ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

Ethers.js script working in node.js but not in React Native

Open HugoSanchez opened this issue 7 years ago • 16 comments

Hi,

I'm working on a simple React Native app to interact with the Ethereum Network and I'm finding that the scripts that I use in Node.js to call contract functions, do not work in React Native.

Here's my Node.js script:

const ethers = require('ethers');

// ERC20 contract
const Token = require('./Token.json');

// My address
const address = "0xCecA06AFB36cb7DeA535e4be05C87694C9E08016"

// Contract Address (already deployed)
const ERC20 = "0xf1F3357fAaf40a9e8d7a6936cbBea113415cDE69"

// My own Parity Ethereum Node.
let httpProvider = new ethers.providers.JsonRpcProvider('http://82.196.9.52:8540')

let contract = new ethers.Contract(ERC20, Token.abi, httpProvider);

contract.balanceOf(address).then(result => console.log(result.toString()))

This works and it prints '10000' which is the correct answer. However, when I run the following script in React Native, everything works well up until the very last line when the contract function is supposed to get executed:

import 'ethers/dist/shims.js'; 
import { ethers } from 'ethers';

// Exact same ERC20 contract
import Token from '../../contracts/Token.json';

// Get the contract ABI
let tokenAbi = Token.abi

// Connect to the same node.
let httpProvider = new ethers.providers.JsonRpcProvider('http://82.196.9.52:8540')

// The exact same contract address
let contractAddress = '0xf1F3357fAaf40a9e8d7a6936cbBea113415cDE69';

let contract = new ethers.Contract(contractAddress, tokenAbi, httpProvider);

// Call balanceOf function
contract.balanceOf(address).then(result => console.log(result.toString()))

I've been console logging every single line and it all works except for the last line. I get a warning message which I fail to make sense of:

YellowBox.js:67 Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 120000ms)

I'm workinf with: react-native-cli: 2.0.1 react-native: 0.58.6 "ethers": "^4.0.25"

Any help here would be very much appreciated as I haven't been able to find anything online.

Thanks! Hugo

HugoSanchez avatar Mar 21 '19 12:03 HugoSanchez

That is indeed a strange error, and also concerning that a Timer is an intensive operation. They should be basically free.

I can only guess that the timer it is referring to is the timeout used for detecting a timeout for fetching URLs. Is it not working, or you are just trying to prevent the console.log? It is used so if a connection issue occurs, you get a timeout eventually, even on platforms that do not support timeout on XMLHTTPRequest.

I'm not familiar with YellowBox. Is that what is complaining? Or is it just the top of the error stack?

ricmoo avatar Mar 24 '19 03:03 ricmoo

Hi Richard,

Thanks for getting back. It's not really the YellowBox warning that concerns me, it's the fact that I can't call a contract function from React Native using Ethers.

As you probably know, the 'balanceOf' function is just a gettter method, but no value gets returned when using it in React Native, whereas it does when using Ethers in node.js.

Do you have any idea of what I might be doing wrong? Any help or clue would be very much appreciated here.

Thanks! Hugo

HugoSanchez avatar Mar 26 '19 09:03 HugoSanchez

Ok. I was able to make this work by removing the shims.js import. It is conflicting for some reason I can't understand.

HugoSanchez avatar Mar 26 '19 13:03 HugoSanchez

Was it actually not working though, or you are just trying to get rid of the log?

The shims should not be affecting that too much, although, the shims does inject a setImmediate if not present that tries to determine the most efficient method of setImmediate (setTimeout with 0 vs window.postMessage, etc.).

The more context you can provide for the error the better though, and problems it causes and whatnot. I have not had those issues, but don't use RN personally, just for helping other debug.

ricmoo avatar Mar 26 '19 19:03 ricmoo

Contract calls are actually not working IF you have the shims.js imported. The YellowBox warning is not really what concerns me, although it is indeed annoying.

A good way to reproduce it is to try to call the 'balanceOf' function of any ERC20 contract you like:

react-native init ProjectName cd ProjectName npm install --save ethers

Then, on the App.js file:

import 'ethers/dist/shims.js';
import { ethers } from 'ethers';

Create a componentWillMount function as such:

componentWillMount() {
    
    const address = <a given Ethereum address >

    const ERC20 = <the ERC20 contract address>

    // Set Provider
    let httpProvider = new ethers.providers.JsonRpcProvider(<Your provider>)

    let contract = new ethers.Contract(ERC20, Token.abi, httpProvider);

    contract.balanceOf(address).then(result => console.log(result.toString()))
} 

Then, npm start react-native run-android

The example above won't work, meaning it won't log any result, nor it will throw any error, just the YellowBox warning we've seen so far.

HOWEVER, if you remove the line where the shims are imported, import 'ethers/dist/shims.js'; then, it works. It logs the result + the YellowBox warning which still appears.

I'm trying this on a private Parity node and using react-native 0.59.2.

Let me know if there is any other way I can help and thank you for your work. Ethers is amazing!

HugoSanchez avatar Mar 27 '19 09:03 HugoSanchez

shims.js covered the original method of react native.like Promise

acezyf avatar Jul 02 '19 03:07 acezyf

I'm using private geth node and react-native 0.61.5. Has anyone solved the yellow box problem?

andylim0221 avatar May 28 '20 06:05 andylim0221

Is this still a problem with ethers? Do modern React Native environments still not provide basic JavaScript objects, like Promise and Math.MAX_SAFE_INTEGER?

I may close this issue unless someone has an objection? Or recommendations are welcome to. I may add a React-Native test environment in the near future too, which might help address this.

Thanks! :)

ricmoo avatar Jul 05 '20 01:07 ricmoo

I had the same problem on React - Native, but my project doesn't exist shims.js

Lloyd1229 avatar Sep 03 '20 07:09 Lloyd1229

I had the same problem on React - Native, but my project doesn't exist shims.js I'm workinf with: "ethers": "^5.0.9", "react": "16.9.0", "react-native": "0.61.0"

Lloyd1229 avatar Sep 03 '20 07:09 Lloyd1229

Can you try importing the shims located here:

https://github.com/ethers-io/ethers.js/tree/master/packages/shims/dist

ricmoo avatar Sep 03 '20 07:09 ricmoo

I think this should be fixed by the latest shims, so I'm going to close this. If you still have problems though, please re-open.

Thanks! :)

ricmoo avatar Sep 05 '20 06:09 ricmoo

I think this should be fixed by the latest shims, so I'm going to close this. If you still have problems though, please re-open.

Thanks! :)

how to use shims in react-native project ,give me a demo please,thank you

Lloyd1229 avatar Sep 05 '20 06:09 Lloyd1229

If you npm install @ethersproject/shims, you just need to import "@ethersproject/shims/dist/index.js" I think. I will improve this in the next build so you can ignore the dist/index part. Or just copy that shim file into your project and import it before ethers.

Here is an example app:

https://github.com/ethers-io/ethers.js/blob/master/misc/ReactNativeTestApp/App.js#L29

ricmoo avatar Sep 05 '20 06:09 ricmoo

If you npm install @ethersproject/shims, you just need to import "@ethersproject/shims/dist/index.js" I think. I will improve this in the next build so you can ignore the dist/index part. Or just copy that shim file into your project and import it before ethers.

Here is an example app:

https://github.com/ethers-io/ethers.js/blob/master/misc/ReactNativeTestApp/App.js#L29 Thank you very much for your patient answer image

I've solved the problem this way, but with a YellowBox warning

Lloyd1229 avatar Sep 05 '20 07:09 Lloyd1229

That is the http request timeout, I think. I don’t know there is really a way around that warning if that’s its concern... :s

ricmoo avatar Sep 05 '20 23:09 ricmoo