6

There are some similar posts to this but none seem to be able to solve my problem.

I have a contract deployed with Truffle running with testrpc.

I want to access the value of a public uint variable named gameStatus defined in the deployed contract game variable as such:

uint public gameStatus = 23;

I have found two ways to do this:

game.gameStatus(function(err, res){
    document.getElementById('amt').innerText = res;
});

And using the then operator from Promises.

game.gameStatus().then(function(result){
    document.getElementById('tableamt').innerText = result;
});

The first method never affects the value of 'tableamt' in the HTML. And the second method always yields a value of '0' in the HTML.

The contract in both cases deploys without errors and web3 does not complain.

Any help is much appreciated.

Thanks!

  • Have you tried web3.eth.getCode(contractAddress) and confirmed the contract is indeed deployed? – Xavier Leprêtre B9lab Jan 17 '17 at 16:41
  • Trying to do that gives me the following error: Uncaught Error: The MetaMask Web3 object does not support synchronous methods like eth_getCode without a callback parameter. However when I do game.deployed() I get this: Contract BettingOver : () address : "0x7881c3a33335a8cceb04b86131f4bf43db26dbd8" allEvents : () betEnd : () bettingEnd : () bettingStart : () So it looks like the variables are not being set during deploy? – Carlos G. Oliver Jan 17 '17 at 17:20
  • Then that should do web3.eth.getCode(contractAddress, function(err, code) { console.log(code);}) – Xavier Leprêtre B9lab Jan 17 '17 at 17:24
  • web3.eth.getCode("0x7881c3a33335a8cceb04b86131f4bf43db26dbd8", function(err, code){ console.log(code)}); undefined VM245:1 0x – Carlos G. Oliver Jan 17 '17 at 17:33
  • 0x means there is no code at the address. Of course your "functions" return 0. – Xavier Leprêtre B9lab Jan 17 '17 at 17:50
5

To get public variable value you need to use call. It is evaluated directly on local node without sending transaction to the blockchain. See also this great answer.

So, you need to call it that way:

game.gameStatus.call(function(err, res){
    document.getElementById('amt').innerText = res;
});
4

It's different now. You should use ContractName.methods.VarName().call(callback) to access state variables in Web3 1.0.0.

To answer your question, the method call should be:

game.methods.gameStatus().call(function(err, res){
    //do something with res here
    console.log(res); //for example
};
0

A simple promise works just fine.

game.gameStatus().then(function (result) {
    console.log(result);
})

game is the loaded contact, which in my case, I get with another promise.

App.contracts.GameContract.deployed().then(function (game) {
    return game.winningNumbers();
}).then(function (result) {
    console.log(result);
})

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.