0

Im using this code to detect for metamask.

Now im using ropsten but i want to know how does metamsk know that my contract only works with ropsten?

window.addEventListener('load', async () => {
    // Modern dapp browsers...
    if (window.ethereum) {
        window.web3 = new Web3(ethereum);
        try {
            await ethereum.enable();
            var accounts= await web3.eth.getAccounts();
            var option={from: accounts[0] };
            var contract = new web3.eth.Contract(abi,address);
            const agiContract = new web3.eth.Contract(agiabi,agiContractAddress);

            const balance = await contract.methods.balanceOf(accounts[0]).call();
            const myDivs = await contract.methods.dividendsOf(accounts[0]).call();


            var supply = await contract.methods.totalSupply().call();
            var agiBalance = await contract.methods.totalAgiBalance().call();

            const agiAccountBalance = await agiContract.methods.balanceOf(accounts[0]).call();



            document.getElementById('agiAvailable').innerHTML = agiAccountBalance/100000000;
            document.getElementById('agiContractBalance').innerHTML = agiBalance/100000000 + ' AGI';
            document.getElementById('contractBalanceSnet').innerHTML = supply/100000000 + ' SNET';
            document.getElementById('snet-holding').innerHTML = balance/100000000;
            document.getElementById('myDividends').innerHTML = myDivs/100000000;
            document.getElementById('wallet').innerHTML = accounts;


            ethereum.on('accountsChanged', async (accounts) => {
              await ethereum.enable();
              var accounts= await web3.eth.getAccounts();
              var option={from: accounts[0] };
              var contract = new web3.eth.Contract(abi,address);
              const agiContract = new web3.eth.Contract(agiabi,agiContractAddress);

              const balance = await contract.methods.balanceOf(accounts[0]).call();
              const myDivs = await contract.methods.dividendsOf(accounts[0]).call();


              var supply = await contract.methods.totalSupply().call();
              var agiBalance = await contract.methods.totalAgiBalance().call();

              const agiAccountBalance = await agiContract.methods.balanceOf(accounts[0]).call();



              document.getElementById('agiAvailable').innerHTML = agiAccountBalance/100000000;
              document.getElementById('agiContractBalance').innerHTML = agiBalance/100000000;
              document.getElementById('contractBalanceSnet').innerHTML = supply/100000000;
              document.getElementById('snet-holding').innerHTML = balance/100000000;
              document.getElementById('myDividends').innerHTML = myDivs/100000000;
              document.getElementById('wallet').innerHTML = accounts;
            })
        } catch (error) {
            // User denied account access...
            console.log(error)
        }
    }
    // Legacy dapp browsers...
    else if (window.web3) {
        window.web3 = new Web3(web3.currentProvider);
        // Acccounts always exposed
        web3.eth.sendTransaction({/* ... */});
    }
    // Non-dapp browsers...
    else {
        document.getElementById('wallet').innerHTML = "Use Metamask on a Desktop or use TrustWallet on a mobile";
        document.getElementById('agiAvailable').innerHTML = "<a href='https://binance.com/' style='color:green'>BUY</a> ";
    }
});
0
var contract = new web3.eth.Contract(abi,address);
const agiContract = new web3.eth.Contract(agiabi,agiContractAddress);

Any on-chain function-call via contract can complete successfully only if a contract which implements that function is deployed at address.

Any on-chain function-call via agiContract can complete successfully only if a contract which implements that function is deployed at agiContractAddress.

This means that your program may run successfully on one network but fail on another.

MetaMask is not the reason here; your input addresses are.

2
  • great answer dude – redbulll Jan 27 '20 at 19:31
  • @redbulll: Thank you :) – goodvibration Jan 27 '20 at 19:32
2

Metamask doesn't know that your contract only exists in Ropsten. When you log into metamask, you select the network that you want to work with in the dropdown in the picture below.

enter image description here

This is the network that web3 will be interacting with. You can check the network that you are using by looking at the network id of the web3 object. You can do this with the code below. If your contract only works with Ropsten, maybe you'd want to present a warning or suggestion to change networks if the user isn't on Ropsten.

web3.version.getNetwork((err, netId) => {
  switch (netId) {
    case "1":
      console.log('This is mainnet')
      break
    case "2":
      console.log('This is the deprecated Morden test network.')
      break
    case "3":
      console.log('This is the ropsten test network.')
      break
    default:
      console.log('This is an unknown network.')
  }
})

Code from this answer

17
  • but why is my code not working in mainnet, how does metamask know what network i use? Why does metamsk know my code only works on ropsten? I know why its not working in mainnet- im asking why metamsk knows that? – redbulll Jan 27 '20 at 19:06
  • Can you post the error you are getting? – Steven V Jan 27 '20 at 19:08
  • im not getting any eror – redbulll Jan 27 '20 at 19:09
  • @redbulll: You have address and agiContractAddress as input to your code. Where do you send that input from? What are the values of these two addresses? Most likely, they are the addresses of your two contracts deployed on Ropsten but not on Mainnet, hence your code will work on the former but not on the latter. – goodvibration Jan 27 '20 at 19:10
  • I don't think that metamask will know that your code only works on Ropsten. If you have only deployed the smart contract on Ropsten, var contract = new web3.eth.Contract(abi,address); will not work correctly because whatever address you are providing isn't actually the address the smart contract you are trying to interact with. – Steven V Jan 27 '20 at 19:12

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.