2

Here is the sample code:

 function myFunc(string name) public returns(bool,bytes32)
{
    bytes32 hash= sha256(name);
    names[hash] = true; 
    return (true,hash);
}

Here names is mapping, I am trying to store names in blockchain. I have returned (true,hash), but when I call the function from frontend i receive the transaction hash. How can I get what i want to return?

2 Answers 2

4

This is a "facts of life" thing.

You can return(bool success) and this will be visible to other contracts that call the function, e.g. in a chain of functions calling functions in other contracts.

Transactions always start with an externally owned account signing a transaction and submitting it to the network. The returned value is a transaction receipt. The outcome of the transaction is unknown because nothing has happened yet except that a request has been submitted to the network for verification in the future.

A typical approach to coping with this fact of life is to wait for the transaction to be mined, then inspect the transaction log for useful output, inspect the success/fail status and draw your own conclusions, or inspect a read-only function to discover the new state after the transaction was mined. The returned values are forever out of reach for a Web3 client.

Hope it helps.

2
  • So there is no way to implement what I want my contract to do, is that what you're saying? And it will always return transaction hash when i use web3? Dec 24, 2017 at 15:54
  • if the function changes state of the contract then you will get transaction hash. if it is not changing the state then you can return any parameter. Dec 25, 2018 at 7:58
0

Rob Hitches has explained it really well. But if you want return values, then you can use events. you can emit an event and add a listener in your app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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