Solidity – Increment Contract Value with Console Call on Truffle/Testrpc

contract-invocationsoliditytruffle

What I want to do is make a smart contract which has a variable that can accrue value. I'd like to have it start off as 0, and then increment it, while periodically checking to verify that my calls to increase the value have in fact been working.

Here's my very simple smart contract to achieve that:

pragma solidity ^0.4.13;

// This contract demonstrates a simple non-constant (transactional) function you can call from geth.
// increment() takes no parameters and merely increments the "iteration" value. 

contract Incrementer {
    uint iteration;

    function Incrementer() {
        iteration = 0;
    }

    function increment(uint count) {
        iteration += count;
    }

    function getIteration() constant returns (uint) {
        return iteration;
    }

}

However, it must be broken, or perhaps I'm not calling it in the right way, as you can see by my console output:

enter image description here

The execution environment I'm using is truffle.

The commands to compile were:

truffle compile --all
truffle migrate --reset

Then I fire up to truffle console to interact with it, after having started testrpc in another window.

The exact commands I've been using it call it have been:

Incrementer.deployed()

Incrementer.deployed().then(function(instance) { meta = instance; return meta.getIteration.call(); })

and alternatively:

Incrementer.deployed().then(function(instance) { meta = instance; return meta.increment.call(1); })

How can I get the value of iteration() to increase by calling increment(), and then outputting the result with getIteration()?


EDIT:

enter image description here

Best Answer

One of the correct procedures, according to my experiments, is to call in this way:

Incrementer.deployed().then(function(instance) { meta = instance; return meta.increment(1); })

output looks like this:

enter image description here

verify in this way:

Incrementer.deployed().then(function(instance) { meta = instance; return meta.getIteration(); })

results in:

enter image description here

Related Topic