2

I am trying to implement fallback funcationality for my DApp which means that when no web3 provider (e.g. metamask/trustwallet etc) is found I can still make calls to the ethereum network but I always seem to get the same issue.

This is my fallback logic:

 let bootStrappedWeb3;

  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    bootStrappedWeb3 = new Web3(web3.currentProvider);
  } else {
    console.log('No Web3 Detected... falling back to using default mainnet HTTP Provider');
    bootStrappedWeb3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/<MY_API_KEY>"));
  }
  window.web3 = bootStrappedWeb3;

  // Listen for when web3 is connected and then bootstrap the app
  window.web3.eth.net.isListening()
    .then(() => {
      console.log('is connected');

      // Bootstrap the full app
      this.$store.dispatch(actions.INIT_APP, bootStrappedWeb3);
    })
    .catch(e => console.log('Something went wrong', e));

I see is connected and then INIT_APP is called.

INIT_APP then sets the provider on my truffle contract like this:

MyContract.setProvider(web3.currentProvider);

I can correctly read the current network which reports as on Mainnet, ID 1, but then whenever I try to use truffle this then falls over.

The call to truffle looks like this:

 MyContract.deployed()
    .then((contract) => {
      // Do stuff 
    }).catch((error) => console.log("Something went bang!", error));

An exception is throw and I ass the error which is as follows:

Something went bang! TypeError: Cannot read property 'apply' of undefined
at Provider.sendAsync (contract.js?6b6f:24)
at RequestManager.sendAsync (requestmanager.js?e4d9:80)
at Object.get [as getNetwork] (property.js?7a8c:116)
at eval (contract.js?6b6f:512)
at new Promise (<anonymous>)
at Function.detectNetwork (contract.js?6b6f:503)
at Function.deployed (contract.js?6b6f:451)
at Store.eval (index.js?e3b1:444)
at Array.wrappedActionHandler (vuex.esm.js?edaa:704)
at Store.dispatch (vuex.esm.js?edaa:426)
at boundDispatch (vuex.esm.js?edaa:332)
at eval (index.js?e3b1:273)
at tryCatcher (bluebird.js?e531:5063)
at Promise._settlePromiseFromHandler (bluebird.js?e531:3095)
at Promise._settlePromise (bluebird.js?e531:3153)
at Promise._settlePromise0 (bluebird.js?e531:3198)
at Promise._settlePromises (bluebird.js?e531:3281)
at eval (bluebird.js?e531:162)
at MutationObserver.eval (bluebird.js?e531:4330)

Has anyone got any ideas? From the documentation and examples I have read I believe this should work, and and all help appreciated!

Thanks.

1

I found that using the described work around on this ticket solved the problem for me: https://github.com/trufflesuite/truffle-contract/issues/57

e.g

MyContract.setProvider(web3.currentProvider);
if (typeof MyContract.currentProvider.sendAsync !== "function") {
     MyContract.currentProvider.sendAsync = function() {
        return MyContract.currentProvider.send.apply(
            MyContract.currentProvider, arguments
        );
    };
}

Your Answer

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

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