4

I want to get the return data written in solidity function in web3js . I am using the web3.min.js from the truffle pet-shop package as the web3 library . Everything is working fine when calling the function it is returning the transaction data from which I used web3.eth.getTransactionReciept to get the details and the gas is also used. Now I want the data which is returning my solidity method

 function helloworld() public returns (string) {
        return "helloworld";
    }

The js part

<script>

    if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
    } else {
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
    }
    web3.eth.defaultAccount = web3.eth.accounts[0];
    console.log('web3.eth.accounts :', web3.eth.accounts);
    var walletContractJSONabiGlobal;
    $.getJSON('./contracts/Wallet.json',async function(walletContractJSONabi) {
        walletContractJSONabiGlobal = walletContractJSONabi;
        var walletContract = web3.eth.contract(walletContractJSONabi.abi);
        var walletInstance = walletContract.at('0xc74c8d941494495a4f65c42bebc556398e1f028d');
        walletInstance.helloworld((err,data)=>{
            if (err) {
                console.log('err :', err);
            } else {
                console.log('data :', data);
            }
        })

    })

</script>

I getting the data in console

data : 0x988d4bd3169a4d752f6e4ac230d01840f2f7b4cc8e87d768a8e41b6fe867740d

I did the web3.toAscii(this transaction is) but it is not coming proper result which is expected as this is not a hex value

When output of web3.eth.getTransactionReciept

blockHash : "0x552a93b0035571b93e4a1a89daa445663b0ddb29974078cdf9a250b7f873ca96" blockNumber : 10 contractAddress : null cumulativeGasUsed : 21899 gasUsed : 21899 logs : [] logsBloom : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" status : "0x01" transactionHash : "0x988d4bd3169a4d752f6e4ac230d01840f2f7b4cc8e87d768a8e41b6fe867740d" transactionIndex : 0

Please Help in this Thank YOu

1 Answer 1

3

This may be marked as a duplicate, so just to help you out. You can't.

The return values are only available to other contracts. As a software client signing and sending a transaction, you get a transaction receipt and then after waiting for the transaction to be mined, you can inspect the logs of inspect other functions on a read-only basis (either the contract function is view or pure, or you invoke it with the JavaScript .call() method) to discover what's new in the contract state. The return values of the state-changing function are out of reach.

Hope it helps.

3
  • How to get the mined status? I did the .call() but it was coming undefined method then I figured it out that I was using the old web3 I upgraded it and now the .call() is working. Jul 8, 2018 at 8:30
  • This is nicely solved with Truffle but there is more learning curve to get used to it. Among other things, they wrap the contract ABI in a higher-level abstraction that cleans up the syntax and deals with details like this. Have a look over here for practical ideas. ethereum.stackexchange.com/questions/7709/… Jul 8, 2018 at 8:50
  • Its wierd in general cause My UI depends on the return datas and if in a deployed env if no one is there to mine then Its really hard to get the things going. it ok for local development env . The things are quite clear for me now . Thanks Jul 8, 2018 at 9:05

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.