2

After Mist 0.8, I made the following change on my Dapp so that it uses existing web3 connection if connected via Mist.

https://github.com/makoto/blockparty/commit/cbea6e2990e5699532acecd5aedcf4b0180faf7d

However, the dapp is failing to connect to network (eg web3.currentProvider.isConnected() is returning false) when opened it from Mist.

It can be connected if I kill geth process opened by Mist, and launch geth manually with --rpc rpccorsdomain=* options enabled, but that defeats the purpose of having Mist browser.

Am I missing anything to make my Dapp "Mist ready"?

1 Answer 1

2

I followed this example https://gist.github.com/frozeman/fbc7465d0b0e6c1c4c23

And this is what I did

if(typeof web3 !== 'undefined'){   // eg: If accessed via mist
  provider = web3.currentProvider; // Keep provider info given from mist `web3` object
  web3 = new Web3;                 // Re-instantiate `web3` using `Web3` from Dapp
}else{
  provider = new Web3.providers.HttpProvider("http://localhost:8545");
  let web3 = new Web3;             // Define and instantiate `web3` if accessed from web browser
}
web3.setProvider(provider);

Here is the basic logic

  1. check if mist or web3 object is already defined by mist.
  2. If yes, keep the existing provider info, then re-instantiate web3 using Web3, which comes from your Dapp. Make sure you do not re-define the variable(eg: var web3).
  3. If no, you instantiate provider and define web3 object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.