2

I've compiled and deployed the following trivial smart contract:

pragma solidity ^0.4.7;    
/// Trivial counter contract. Provides an external function which increments the counter
/// and replies the current value.
contract BasicCounter {
    uint private counter = 0;
    function incrementAndGet() external returns (uint) {
        counter++;
        return counter;
    }
}

In the node REPL I can instantiate a handle for the deployed smart contract:

let instance = 
    web3.eth.contract(/* the contract abi */))
        .at(/* the contract address  */);

Using the instance, I can invoke the external function incrementAndGet:

let ret = instance.incrementAndGet();

The return value ret is a hex string, i.e.

0xa768b61d2892dda6495b6b1529e4c81252f333f4ca4ffa18ea7408580366292d

If I convert it to a BigNumberusing web3, i.e.

let value = web3.toBigNumber(ret);
console.log(`: <${value.toString(10)}>`)

I get the following value

current counter value: <75721254712088916818789556586747754611065627738232115122133311013811340585261>

This looks strange. The first invocation of incrementAndGet should rather return something like 00000......00001.

Any hint what is going wrong here? How to correctly unmarshal an uint value returned from a external function in a smart contract?

4

The issue is that you can't return a value from a transaction. You can use

let ret = instance.incrementAndGet.call();

to see view the current counter value.

See What is the difference between a transaction and a call?

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.