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:
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:
Best Answer
One of the correct procedures, according to my experiments, is to call in this way:
output looks like this:
verify in this way:
results in: