5

I have written data to a contract using sendRawTransaction, and am trying to fetch it using web3.eth.call for the function :
function checkRecord(uint id, bytes email) constant returns (uint id2, bytes email2, bytes hash) {..}

I create call data as
var callData = contract.checkRecord.getData(id, email)
to get
'0xf1e8a43c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000156d617467656f2e3230303840676d61696c2e636f6d0000000000000000000000'

and fetch result as
result = web3.eth.call({'to': address, 'from': account, 'data': callData})

where result is
'0x0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000156d617467656f2e3230303840676d61696c2e636f6d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

How do i decode the result back to (uint, bytes, bytes) ?

3 Answers 3

5

you can use web3 built-in functionality:

web3.eth.abi.decodeParameters(typesArray, hexString);

see link

2

The easiest way is to use "abi-decoder". I forked the repository and created index.html for easy usage.

You can download and use it from: https://github.com/Muhammad-Altabba/abi-decoder (you can later check it at https://github.com/ConsenSys/abi-decoder once they will accept my pull request)

To download and run using NPM:

npm install abi-decoder
bower install abi-decoder

Then download https://github.com/Muhammad-Altabba/abi-decoder/index.html to the same directory. Then open the file "index.html" in browser.

You will get:

abi-decoder index.html

1

As the web3 documentation points out web3.eth.call returns a String, which is the returned data of the call, e.g. a codes functions return value. In your case the result string contains a uint (which is an alias for uint256) and two dynamically-sized byte arrays.

Split the result into 32-byte chunks and then apply a toAscii(hexString) function on the chunks.

With this method you'll get that the uint is the number 2 padded with zeroes, it looks like this: 0x0000000000000000000000000000000000000000000000000000000000000002. The first byte array is {96,160,21,[email protected]}. The second byte array contains only zeros.

1
  • 1
    toAscii is deprecated. Use hexToAscii in web3 v1. web3.utils.hexToAscii(0x....)
    – sunwarr10r
    Commented Jul 16, 2018 at 9:03

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.