0

I'm trying to make simple web3 calls in React Native with web3.js, but generated json-rpc calls are different between release / debug builds - IOS/Xcode.

Expected behavior

    const web3 = new Web3(`https://ropsten.infura.io/${Config.INFURA_API_KEY}`);

    return new Promise((resolve, reject) => {      
            web3.eth.getBalance(walletAddress, (error, weiBalance) => {

              if (error) {
                console.error;
                reject(error);
              }

              const balance = weiBalance / Math.pow(10, 18);
              console.log("================BALANCE================");
              console.log(balance);

              resolve(balance);
            });
          });

should return balance. In Debug build, there is no problem.

Actual behavior

In Release build, it fails with Node error: {"code":-32600,"message":"invalid json request"}

i've inspected that in my release build the json -rpc request becomes like this:

{"jsonrpc":"2.0","id":1,"method":{"jsonrpc":"2.0","id":0,"method":"eth_getBalance","params":["0xe1f42aa9aec952f61c5d929dd0b33690faaef976","latest"]}}

whereas in debug build the json-rpc request is like this:

{"jsonrpc":"2.0","id":0,"method":"eth_getBalance","params":["0xe1f42aa9aec952f61c5d929dd0b33690faaef976","latest"]}

how can the request get nested in the release build, this i don't understand...

Versions

  • web3.js: 1.0.0-beta.48
  • browser: react native 0.59
  • ethereum node: infura
    System:
      OS: macOS High Sierra 10.13.6
      CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
      Memory: 107.09 MB / 32.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 11.3.0 - /usr/local/bin/node
      Yarn: 1.12.3 - /usr/local/bin/yarn
      npm: 6.4.1 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        API Levels: 23, 24, 25, 26, 27, 28
        Build Tools: 26.0.3, 28.0.3
        System Images: android-23 | Google APIs Intel x86 Atom, android-24 | Google APIs Intel x86 Atom, android-26 | Google APIs Intel x86 Atom, android-26 | GooglePlay Intel x86 Atom, android-27 | Google APIs Intel x86 Atom
    IDEs:
      Android Studio: 3.2 AI-181.5540.7.32.5056338
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.6.3 => 16.6.3
      react-native: 0.59.0 => 0.59.0
    npmGlobalPackages:
      create-react-native-app: 2.0.2
      react-native-cli: 2.0.1

Release build json-rpc call made by simulator/device, captured by Charles proxy. enter image description here

UPDATE: I've included console.log/error statements in web3-providers/dist/web3-providers.umd.js before every sendPayload statement, here is what i've found:

In Debug build, i get this, and everything works as intended:

2019-03-15 01:24:31.243 [error][tid:com.facebook.react.JavaScript] 'sendPayload_httpprovider_umd', { jsonrpc: '2.0',
  id: 0,
  method: 'eth_getBalance',
  params: [ '0xe1f42aa9aec952f61c5d929dd0b33690faaef976', 'latest' ] }

In Release build. i strangely get this: Notice how it first gets into sendPayload_CustomProvider_umd and nests the payload on sendPayload_httpprovider_umd

2019-03-15 01:17:10.433692+0300[48645:314312] 'sendPayload_CustomProvider_umd', { jsonrpc: '2.0',
  id: 1,
  method: 
   { jsonrpc: '2.0',
     id: 0,
     method: 'eth_getBalance',
     params: [ '0xe1f42aa9aec952f61c5d929dd0b33690faaef976', 'latest' ] },
  params: [Function] }
2019-03-15 01:17:10.434 [error][tid:com.facebook.react.JavaScript] 'sendPayload_httpprovider_umd', { jsonrpc: '2.0',
  id: 2,
  method: 
   { jsonrpc: '2.0',
     id: 1,
     method: 
      { jsonrpc: '2.0',
        id: 0,
        method: 'eth_getBalance',
        params: [ '0xe1f42aa9aec952f61c5d929dd0b33690faaef976', 'latest' ] },
     params: [Function] },
  params: [Function] }

I'm using

import './shim' // Provided by rn-nodeify --hack -- install
require('node-libs-react-native/globals');

in my start file, for react native web3 compatibility.

  • Release and Debug builds of what??? – goodvibration Mar 14 at 15:52
  • React Native with web3.js, added on the top of the question. react native 0.59 / web3.js: 1.0.0-beta.48 – Alp Güneysel Mar 14 at 15:55
  • Are you sure that web3 version is identical in both cases? Side note: I recommend sticking to v1.0.0-beta.37 or lower for now, since AFAIK, there are various problems reported for higher versions. I personally am currently sticking to v1.0.0-beta.34, which I have been working with for quite some time now, and I feel kind of confident in its stability. – goodvibration Mar 14 at 16:02
  • Side note #2: Math.pow(10, 18) is larger than Number.MAX_SAFE_INTEGER and is therefore likely to yield (hard-to-find) runtime issues in your code. Use new BigNumber(10).pow(18) or new BigNumber("1e18") instead. – goodvibration Mar 14 at 16:05
  • Note that web3.eth.getBalance returns Promise which resolves to a BigNumber in web3 v0.x and to a String in web3 v1.x. So to be honest, I'm not sure how weiBalance / Math.pow(10, 18) even works, since according to your description, you are using web3 v1.x, which means that weiBalance is a string!!! In fact, to me it implies that your working case relies on web3 v0.x and your failing case relies on web3 v1.x. To be on the safe side, I suggest that you replace weiBalance with new BigNumber(weiBalance), and then use .toString() or .toFixed() when you print it. – goodvibration Mar 14 at 16:15

Your Answer

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

Browse other questions tagged or ask your own question.