Unable to change a state variable in a contract

0 votes

I am developing an Ethereum contract using Truffle and TestRPC. But I am unable to get a state variable to update. I think it might just be that I'm accessing it too early, but other example tests seem to work just fine and are very similar.

I have reduced my contract down to the simplest possible thing that breaks:

pragma solidity ^0.4.11;

contract Adder {

    uint public total;

    function add(uint amount) {
        total += amount;
    }

    function getTotal() returns(uint){
        return total;
    }
}

And this is my test:

var Adder = artifacts.require("./Adder.sol");

contract('Adder', accounts => {
  it("should start with 0", () =>
    Adder.deployed()
      .then(instance => instance.getTotal.call())
      .then(total => assert.equal(total.toNumber(), 0))
  );

  it("should increase the total as amounts are added", () =>
    Adder.deployed()
      .then(instance => instance.add.call(10)
        .then(() => instance.getTotal.call())
        .then(total => assert.equal(total.toNumber(), 10))
      )
  );

});

The first test passes ok. But the second test fails because getTotal is still returning 0.

Oct 30, 2018 in Blockchain by Perry
• 17,100 points
527 views

1 answer to this question.

0 votes

I am assuming that the issue is that you are always using the .call()method.

This method will, in fact, execute the code but will not save to the blockchain.

You should use the .call() method, only when reading from the blockchain or testing for throws.

Just remove the .call() in the adding function and it should work.

var Adder = artifacts.require("./Adder.sol");

contract('Adder', accounts => {
  it("should start with 0", () =>
    Adder.deployed()
      .then(instance => instance.getTotal.call())
      .then(total => assert.equal(total.toNumber(), 0))
  );

  it("should increase the total as amounts are added", () =>
    Adder.deployed()
      .then(instance => instance.add(10)
        .then(() => instance.getTotal.call())
        .then(total => assert.equal(total.toNumber(), 10))
      )
  );
});

Also, consider declaring the instance variable outside the chain of functions of the promise since the context is not shared. Consider using async/await for tests instead of promises.

var Adder = artifacts.require("./Adder.sol");

contract('Adder', accounts => {
  it("should start with 0", async () => {
    let instance = await Adder.deployed();
    assert.equal((await instance.getTotal.call()).toNumber(), 0);
  });

  it("should increase the total as amounts are added", async () => {
    let instance = await Adder.deployed();
    await instance.add(10);
    assert.equal((await instance.getTotal.call()).toNumber(), 10);
  });
});
answered Oct 30, 2018 by Christine
• 15,790 points

Related Questions In Blockchain

0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,588 views
0 votes
1 answer
0 votes
1 answer

How to get the current state of a smart contract?

Hey, you gotta provide the contract code, and ...READ MORE

answered Aug 1, 2018 in Blockchain by Perry
• 17,100 points
1,096 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,906 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
1,452 views
0 votes
2 answers

Why is network already up to date while trying to deploy a contract on truffle?

I guess you have ganache running already ...READ MORE

answered Apr 24, 2018 in Blockchain by Shashank
• 10,400 points
4,549 views
0 votes
1 answer

How can I deploy a HelloWorld contract on my testrpc network?

The problem lies in the command: truffle migrate Your truffle migrate command ...READ MORE

answered Apr 27, 2018 in Blockchain by Perry
• 17,100 points

edited Aug 10, 2018 by Omkar 2,823 views
0 votes
1 answer

How to make sure transactions take no fee in a private Ethereum blockchain?

In a private ethereum network you have ...READ MORE

answered Mar 26, 2018 in Blockchain by Christine
• 15,790 points

edited Mar 26, 2018 by Christine 1,544 views
+1 vote
1 answer

I am unable to change port of composer-rest-server. Please help!

Use full format for running a business ...READ MORE

answered Mar 27, 2018 in Blockchain by Christine
• 15,790 points
1,113 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP