1

I have Metamask installed on chrome and am connected to the Ropsten test network through there. However, whenever I try running my Javascript, I get the following error: CONNECTION ERROR: Couldn't connect to node. When I was running my code on testrpc, it was working fine, but it doesn't work when I try to connect to ropsten.

Javascript:

if(typeof web3 !== 'undefined'){
    console.log('metamask');
    web3 = new Web3(web3.currentProvider);

}

else{
    console.log('something else');
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

web3.eth.defaultAccount = web3.eth.accounts[0];
var betContractInstance = web3.eth.contract([MY ABI]);
var betContract = betContractInstance.at('0xed5ae2f841ce23cfe6ea9e39984e66afbf445143');

testing.sol:

pragma solidity ^0.4.18;

contract Testing{
    address [] public adds;
    uint [] public amounts;

    function bet() public payable {
        adds.push(msg.sender);
        amounts.push(msg.value);
    }

    function give() public {
        adds[0].transfer(amounts[0]);
    }
}

The error arrises at web3.eth.defaultAccount = web3.eth.accounts[0];. I tried making web3 global but that didn't help either. Does anybody know how I can fix this? I've seen this question asked before but most of them use geth instead of metamask. Also, the setup of my project is based on this tutorial, so I'm not sure of there's anyother dependecies I need.

Thank you so much in advance.

EDIT: the output to the console is "something else", so I'm assuming it's not detecting the Web3 injected by metamask.

0

Define web3 detection after load event was fired at the page to get web3 object, as it is suggested in MetMask detection guide:

window.addEventListener('load', function() {
   //read web3 object here
})

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.