I use truffle for solidity development.

I have this solidity code:

pragma solidity ^0.4.4;

contract MetaCoin {
    uint256 public totalSupply;

    function MetaCoin() {
        totalSupply = 1000;

    function addOne() public returns (uint256 newTotalSupply)
        totalSupply = totalSupply + 1;
        return totalSupply;

And this HTML+javascript code to interact with the contract after doing truffle migrate, having testrpc running on port 8545:

<!doctype html>
<html lang="en">
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <meta content="utf-8" http-equiv="encoding">
  <script type="text/javascript" src="./bower_components/web3/dist/web3.min.js"></script>
  <script type="text/javascript" src="./bower_components/jquery/dist/jquery.js"></script>
  <script type="text/javascript" src="./javascript/truffle-contract/truffle-contract.js"></script>
  window.onload = function() {
    var url = "http://localhost:8545";
    if (typeof web3 !== 'undefined') {
      web3 = new Web3(web3.currentProvider);
    } else {
      web3 = new Web3(new Web3.providers.HttpProvider(url));
    $.getJSON("./contracts/MetaCoin.json", function(MetaCoin_json) {
      var MyContract = TruffleContract( MetaCoin_json );
      MyContract.setProvider(new Web3.providers.HttpProvider(url));
      MyContract.deployed().then(function(instance) {
        return contract.totalSupply.call();
      }).then( function(totalSupply) {
        console.log("totalSupply from init " + totalSupply);
        return contract.addOne.call();
      }).then( function(totalSupplyIncremented) {
        console.log("Value returned " + totalSupplyIncremented);
        return contract.totalSupply.call();
      }).then( function(updatedTotalSupply) {
        console.log("totalSupply after adding one " + updatedTotalSupply);

I was expecting to have an output like:

totalSupply from init 1000
Value returned 1001
totalSupply after adding one 1001

so at the beginning I have a total supply of 1000. I add 1 so I have 1001, and the new total supply is 1001

But instead I got:

totalSupply from init 1000
Value returned 1001
totalSupply after adding one 1000

Looks like my addOne() method did not update the public totalSupply var. And I can not see why.

  • Adding a little more to the answer given below, this is how I extract the actual return-value from the returned hash in Python web3: web3.eth.getTransactionReceipt(hash)['logs'][0]['data']. It is probably similar in Javascript web3. You also need to add event Event(uint256) at the top of your contract, and Event(totalSupply) right before you return totalSupply. Jan 12, 2018 at 21:40

1 Answer 1


You're calling addOne, but since it modifies state, it needs to be invoked as part of a transaction. Try addOne.sendTransaction() instead, but note that what you'll get back is a transaction hash, and you'll have to wait for the transaction to be mined before you can see the updated value.

This blog post of mine may help: https://programtheblockchain.com/posts/2017/12/13/building-decentralized-apps-with-ethereum-and-javascript/.

  • Tried contract.addOne.sendTransaction() and contract.addOne(). In both cases I got an error "Invalid address" Jan 13, 2018 at 11:15
  • You might need { from: '<some address you have in MetaMask or unlocked locally>' }.
    – user19510
    Jan 13, 2018 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.