Search code examples
javascriptethereumsolidityweb3js

Unable To Contract Balance


I'm trying to check if my contract has any funds in it with a solidity function that works fine in remix. The solidity func literally just returns this.balance(); which is all I really need.

function checkBalance() public view returns (uint) {
    return this.balance;
}

I'm trying to call this function however programmatically in web3 and here's where I'm running into some trouble. I'm using the web3 func eth.getBalance to try and get the contract's current balance.

checkBalance: function() {
    App.contracts.HackathonDapp.deployed().then(function (instance) {
        return instance.checkBalance.call() 
    }).then(function (balance) {
        return web3.fromWei(web3.eth.getBalance('0xeec918d74c746167564401103096d45bbd494b74'));
    }).then (function (contractBalance) {
        console.log(contractBAlance);
    });
},

However, when I press the HTML button to call this function I get an error in my console:

enter image description here


Solution

  • You need to pass a callback:

    web3.eth.getBalance('...', function (err, result) { console.log(result); });