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?

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.

  • 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? – Kashish Khullar Dec 24 '17 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. – dulaj sanjaya Dec 25 '18 at 7:58

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.