0

I'm trying to connect to Ganache-GUI using React native (Android emualtor). This is my code:

const Web3 = require('web3');

const HDWalletProvider = require('truffle-hdwallet-provider');

const mnemonic =
  'gorilla color farm divert finish swim naive profit birth build scrub various'; // 12 word mnemonic

const provider = new HDWalletProvider(mnemonic, 'http://127.0.0.1:7545');

But I'm getting the following error :

Error: Invalid Json RPC response :"Failed to connect 127.0.0.1:7545"

I have installed web3.js and trufflehdwallet in my project.

What other step have I missed? Do I need to change the setting of my android emulator regarding the port number and the host ?

Any help would be highly appreciated.

1 Answers1

0

The IP 127.0.0.1 is a loopback for the host which in your case is the emulator. As Ganache is running outside the emulator, the HDWalletProvider might not be able to access it. I'm not an Android dev, but a bit of googling tells me you have to use the IP 10.0.2.2 to access the host machine from the emulator.

const provider = new HDWalletProvider(mnemonic, 'http://10.0.2.2:7545');

Here's a link to the StackOverflow question

Also, the truffle-hdwallet-provider package seems to be deprecated, you should be using the newer @truffle/hdwallet-provider package.

Stephen S
  • 3,936
  • 2
  • 23
  • 33