1

I've been trying to run the following piece of code:

myContractToken.methods.balanceOf(App.account).call((err, res) => {
      console.log(res);

However, for some reason App.account does not seem to return the account address I stored in the previous function getAccount().

I tried console logging App.account and they all seem to show my initial stored value 0x0, except for the code I'm trying to get to work. For some other reason, App.account does seem to work for web3.eth.sendTransaction.

Anyone who could explain what exactly is going on would be of great help!

App = {
  web3Provider: null,
  myContractSale: null,
  myContractToken: null,
  account: "0x0",

  init: function() {
    return App.initWeb3();
  },

  initWeb3: function() {
    // TODO: refactor conditional
    if (typeof web3 !== 'undefined') {
      // If a web3 instance is already provided by Meta Mask.
      App.web3Provider = web3.currentProvider;
      web3 = new Web3(web3.currentProvider);
    } else {
      // Specify default instance if no web3 instance provided
      App.web3Provider = new Web3.providers.HttpProvider("http://localhost:7545");
      web3 = new Web3(App.web3Provider);
    }
    return App.initContract();
  },

  initContract: function() {
    myContractSale = new web3.eth.Contract(crowdsaleABI, "0x10a7aec323cf84752bc594ad7c9bc28e002e5fb9");
    myContractToken = new web3.eth.Contract(tokenABI, "0x6b13b17c259710679a315c5a9a3d57ed348d0e54");
    return App.tokenPrice();
  },

  tokenPrice: function() {
    myContractSale.methods.tokenPrice().call((err, res) => {result = web3.utils.fromWei(res, "ether")});
    $("#tokenPrice").click(function() {
      $("#showTokenPrice").html("The token price of a DillyCoin is: " + result + " Ether");
    });
    return App.getAccount();
  },

  getAccount: function(){
    web3.eth.getCoinbase((err, res) => {
      if (err === null) {
        App.account = res;
        $("#account").html("Your account: " + res);
      }
    });
    console.log(App.account); //doesn't work, shows 0x0

    return App.balanceOf();
  },

  balanceOf: function() {
    console.log(App.account); //doesn't work, shows 0x0

    myContractToken.methods.balanceOf(App.account).call((err, res) => {
      console.log(res); //doesn't work, shows 0
    });
    return App.sendEther();
  },

  sendEther: function() {
    console.log(App.account); //doesn't work, shows 0x0

    $("#sendEther").click(function() {
      let amount = $("#amount").val();
      web3.eth.sendTransaction({
        from: App.account,
        to: "0x10a7aec323cf84752bc594ad7c9bc28e002e5fb9",
        value: web3.utils.toWei(amount, "ether")
      })
    });

    console.log(App.account); //doesn't work, shows 0x0
  }

};

$(function() {
  $(window).load(function() {
    App.init();
  });
});
2

You're trying to use the value App.account before it's been set:

// stuff here runs 1st
web3.eth.getCoinbase((err, res) => {
  // stuff here runs 3rd
});
// stuff here runs 2nd

Just move those lines into the callback:

web3.eth.getCoinbase((err, res) => {
  if (err === null) {
    App.account = res;
    $("#account").html("Your account: " + res);
    console.log(App.account);

    return App.balanceOf();
  }
});
0

Follow the step to reconnecting host on chrome Metamask plugin

enter image description 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.