Category Archives: blockchain

issue(s) connecting via browser to testrpc but it is working with truffle console?

OK ha I spent hours of my life trying to debug this and it was simply a configuration issue that may be happening to you (which is the worst).

In the process of scaling up the ethereum stack of technologies I was going through various iterations of example projects from with which I planned on modifying in to a basic layer of code for a project I am involved with.

Long story short, I was able to connect and interact with testrpc chain from a command-line truffle console, but there were issues when I connected via the browser @ localhost:8080 – I was not able to execute transactions and impact the blockchain (in terms of writing to it – for some reason reading from it worked???”

The issue was Metamask – a browser plugin that is effectively allow a wallet in the browser.  While I have not explored it in great detail, I was able to track things down to the following code:

$( document ).ready(function() {
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source like Metamask")
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

Voting.setProvider(web3.currentProvider);
let candidateNames = Object.keys(candidates);

alert("we've got the candidateNames names: " + candidateNames)

for (var i = 0; i < candidateNames.length; i++) {
let name = candidateNames[i];
Voting.deployed().then(function(contractInstance) {
contractInstance.totalVotesFor.call(name).then(function(v) {
$("#" + candidates[name]).html(v.toString());
});
})
}
});

This is the offending code – basically it says only connect to your local chain (in this case testrpc) when Metamask is not present, thus not allowing you to interact via the browser with the ethereum chain.

Trivial, but for hours the code in the ‘$( document ).ready(function() ‘ remained invisible to me.

Solution:  delete/disable Metamask plugin.

Hope this helps 🙂