1

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

2

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

2

Two things jump out and some style hints.

First, and probably most important

When you return meta.getIteration.call() the .call() part is redundant (but harmless). It's redundant because the contract function is marked constant and therefore will only run in the local machine, not network verified. It should work just as well (and the same) without .call().

But, you get into trouble when you .call() the .increment() because you are explicitly saying you want the local execution, verification-free, read-only execution mode. Therefore, it doesn't submit a transaction to the network. Ergo, doesn't do what you expect it to do.

Second

You might get over the first hurdle and then run into the next. After you drop .call() things are going to shift into sendTransaction() mode deep down, and you'll get a txnHash instead of a return value. This is normal.

Style

I would write public in both functions to help form a habit in line with best practices. It's the default, so it won't change anything. Also, I would return(bool success) or return(uint newCount) for the benefit of other contracts.

Hope it helps.

Update

Consider:

var contract;
var counter;

Incrementer.deployed()
.then(function(instance) {
  contract = instance;
  return contract.increment(1);
})
.then(function(txnHash) {
  return contract.getIteration();
})
.then(function(response) {
  var counter = response.toString(10);
  console log("Counter says:", counter);
});

Just going freestyle, here, so I hope I didn't flub it.

  • I've tried it without the .call() on increment, and also getIteration, but it seems to return only the method signature, or some such thing, in that case. I've posted the console output under the recent edit to the OP. Please forgive me for my ignorance but I'm not sure what you mean by return(bool success), could you maybe show me how that would look like as part of one of those functions? Anyway, thank you very much for these pointers, I really appreciate your advice. – smatthewenglish Jul 30 '17 at 17:39
  • meta.getInteraction().then(... and meta.increment().then(... – Rob Hitchens - B9lab Jul 30 '17 at 18:00
  • how about the answer I just posted? seems to work – smatthewenglish Jul 30 '17 at 18:02
  • Seems like you headed in the right direction. I added a thought to my original answer. Maybe gives you some ideas. – Rob Hitchens - B9lab Jul 30 '17 at 18:59
  • 1
    In Solidity, a constructor is a function with the same name as the contract and it fires once during deployment with the deployer paying the cost of execution. It's roughly similar to init() in other languages. No one can call it. – Rob Hitchens - B9lab Jul 30 '17 at 19:29

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.