1

I created 2 nodes . I connect an accounts to metamask. I connect the smart contract with remix on the node. Now i am trying to connect frontend with the smart contract with web3. In the index.html i write :

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

// set the provider you want from Web3.providers web3 = new Web3(new Web3.providers.HttpProvider("http://ip:8545"));

and then i run in my browser th index.html but i press buttons and nothing.

1 Answer 1

1

Panos, add this code to your javascript file and change networks to your custom RPC from your metamask. After that, you will be connected to your localnode from your browser.

window.addEventListener('load', async () => {
    // Modern dapp browsers...
    if (window.ethereum) {
        window.web3 = new Web3(ethereum);
        try {
            // Request account access if needed
            await ethereum.enable();

            web3.eth.getAccounts(function(err, accounts){ 
              if(!err){
                alert('Your Metamask account is: ' + accounts[0]);
              }           
            })

        } catch (error) {
            // User denied account access...
        }
    }
    // Legacy dapp browsers...
    else if (window.web3) {
        window.web3 = new Web3(web3.currentProvider);

        web3.eth.getAccounts(function(err, accounts){ 
          if(!err){
            alert('Your Metamask account is: ' + accounts[0]);
          }           
        })
    }
    // Non-dapp browsers...
    else {
        console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
    }
});

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.