1

See the below contract function of which return the string (Tested in Remix.org):

pragma solidity >=0.4.22 <0.6.0;
contract HelloWorld {
    function get() pure public returns (string memory){
        return "Hello Solidity";
    }
}

While in the following contract, I can't get the returned string.

pragma solidity >=0.4.22 <0.6.0;
contract HelloWorld {
    uint num;
    function get(uint val) public returns (string memory){
        num = val;
        return "Hello Solidity";
    }
}

Why? And how can I get returned string from not pure function ?

1
  • This question will probably be removed because it's asked often. Have a look over here for an attempt at explaining the issue: blog.b9lab.com/… Mar 25, 2019 at 20:52

1 Answer 1

2

Web3 API does not allow obtaining return values of functions executed on-chain, i.e. in mined transactions. Though, in case other contract will execute your functions, it will get return value.

This is because values returned by transactions are not stored in blocks, so in order to obtain such value one will need to replay the transaction. You may replay the transaction via contract.methods.method.call(...).

1
  • I'm not using Web3 API, and I'm using go-ethereum source code to execute the contract. The return type of pure one is "string" but that of not pure one is "*types.Transaction". I think I need to replay the returned *types.Transaction.
    – Shuai Xu
    Mar 25, 2019 at 20:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.