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> ";
    }
});
Ismael
  • 30,570
  • 21
  • 53
  • 96
redbulll
  • 15
  • 4

2 Answers2

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

Steven V
  • 1,461
  • 10
  • 15
  • 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
  • @goodvibration ok, but the address is the same as mainnet address, and agicontract is also the same on mainnet and on ropsten, so how does metamsk know what network i sue? is the abi different? Im just curious how metamsk knows what network a code works on – redbulll Jan 27 '20 at 19:12
  • @redbulll: You mean to tell me that you've deployed your contracts on both Ropsten and Mainnet, and they "landed" on the exact same addresses in both cases? It is possible, but you'd need to use the exact same account, and pray to the goddess of nonces that this account is set on the same nonce in both networks. – goodvibration Jan 27 '20 at 19:13
  • address the same on mainnet and on ropsten - and contract abi is from a contract on ropsten that also has the same contract on mainnet under the same address – redbulll Jan 27 '20 at 19:15
  • @redbulll: Can you please share these two addresses in order to convince us? Any chance that you've verified those two contracts on etherscan? (it would help in the convincing region). – goodvibration Jan 27 '20 at 19:16
  • never mind- mainnet contract address is different – redbulll Jan 27 '20 at 19:16
  • @redbulll: So it's not "never mind", it is the exact reason for why your code worksd on Ropsten and not on Mainnet! In other words, you should use "thank you" instead of "never mind". – goodvibration Jan 27 '20 at 19:16
  • but metamaks account address is the same on ropsten and on mainnet – redbulll Jan 27 '20 at 19:17
  • @goodvibration ok i see that makes sense – redbulll Jan 27 '20 at 19:17
  • @redbulll: So what? Your code interacts with two contracts. If either one of them is not deployed at the input address in the given network, then your code will not work. That is the case on Mainnet. – goodvibration Jan 27 '20 at 19:18
  • yes from 1 i approve and the other one does what it does – redbulll Jan 27 '20 at 19:19
  • @redbulll: I cannot see how your last comment is related to anything in our conversation prior to that comment. – goodvibration Jan 27 '20 at 19:20
  • i didnt see your full reply – redbulll Jan 27 '20 at 19:22
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.

goodvibration
  • 26,003
  • 5
  • 46
  • 86