13

This is an absolute newbie question. Given this simple Hello World contract:

pragma solidity ^0.4.4;

contract Greeter {

    function Greeter() {}

    function sayHello() returns(string) {
        return "hello";
    }
}

I deploy it and try to get the value from the sayHello() function:

$ truffle compile
$ truffle deploy
$ Greeter.deployed().then(function(instance){ return instance.sayHello()}).then(function(result){console.log(result);});

I was expecting to see "hello" in the output of the above statement but this is what I actually get:

{ tx: '0xa4333e052b00fdf00d9ad944862b592d091cfb4ff68d610e297cb52cfbf5ebfd',
  receipt: 
   { transactionHash: '0xa4333e052b00fdf00d9ad944862b592d091cfb4ff68d610e297cb52cfbf5ebfd',
     transactionIndex: 0,
     blockHash: '0x40aca77673267ca149fe34a62c95b2869285d7819d21279d59fc0714c702314c',
     blockNumber: 64,
     gasUsed: 21867,
     cumulativeGasUsed: 21867,
     contractAddress: null,
     logs: [] },
  logs: [] }
undefined

That is to say, result is an object of this information instead of the "hello" string. Is there a way I can get the actual result of the function?

10

What you get is the result of the transaction. You can only get the return value of constant function. Do it like that :

function sayHello() constant returns(string) {
    return "hello";
}

An other solution is to use .call()

function saveData(uint _data) returns(bool) {
    data = _data;
    return true;
}

If you use .call(), the data will not be saved but you will get the return value. The typical case is to call the function with .call() the first time to see if the execution went good, and then call again the function without the .call().

  • 2
    Is there any way to get result from non-constant functions? – k06a Sep 13 '17 at 9:43
  • yes, you need to add .call() when calling your function. Note that the modification will not be save in the blockchain. Look at the Nsevens's answer. – Tclairet Sep 13 '17 at 10:41
  • 1
    I am asking about NON-constant functions. Looks like this is Web3 restriction. – k06a Sep 13 '17 at 14:22
  • 2
    For completeness: ethereum.stackexchange.com/questions/33154/… Only now realized .call() does not save the thing! Wasted so much time... – Mars Robertson Jul 12 '18 at 7:44
3

You can achieve the same result with

Greeter.deployed().then(function(instance) {return instance.sayHello.call()})
0

If you need to get the return value of a non constant/view/pure function you may use return the value and use 'then'.

Greeter.deployed().then(function(instance) { return instance.sayHello() }).then(function (value) {
   // Now value contains the return value os the sayHello() function
   console.log(value);
})
  • That's basically the same as what they have in their example. – Justin Harris May 24 at 18:21

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.