1

I have a constant method in a Smart Contract that returns this array:

(uint256 _field1, uint256 _field2, uint256 _field3, address _field4, string _field5)

When invoking it I got this hex result:

0x000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000058581b50000000000000000000000000f28dafbfeb41bf32869c9d498da0d651d0206ed400000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000414869204a75616e2c2074686973206973206d61726b65747061792073656e64696e6720666972737420626c6f636b636861696e2065766572206d6573736167652100000000000000000000000000000000000000000000000000000000000000

How can I parse it? How can I know the exact padding for every field? Thx!

2

If you want to do it in NodeJs, you can create a file named decodeParams.js and which contains:

#!/usr/bin/nodejs

const Web3 = require("web3");
const web3 = new Web3();
const Coder = require("./node_modules/web3/lib/solidity/coder.js");

var result = Coder.decodeParams(
    [ "uint256", "uint256", "uint256", "address", "string" ],
    "0000000000000000000000000000000000000000000000000000000000000001" + // Split for personal readability
    "0000000000000000000000000000000000000000000000000000000000000000" +
    "0000000000000000000000000000000000000000000000000000000058581b50" + 
    "000000000000000000000000f28dafbfeb41bf32869c9d498da0d651d0206ed4" +
    "00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000414869204a75616e2c2074686973206973206d61726b65747061792073656e64696e6720666972737420626c6f636b636861696e2065766572206d6573736167652100000000000000000000000000000000000000000000000000000000000000");

console.log("0x" + result[0].toString(16));
console.log("0x" + result[1].toString(16));
console.log("0x" + result[2].toString(16));
console.log("0x" + result[3]);
console.log(web3.toUtf8(result[4]));

Then to make this work:

$ npm install web3
$ chmod u+x decodeParams.js
$ ./decodeParams.js 
0x1
0x0
0x58581b50
0x0xf28dafbfeb41bf32869c9d498da0d651d0206ed4
...
1

Well, I found the answer myself... just a juxtaposition of fields like this, in PHP code:

list($_field1, $_field2, $_field3, $_zeros, $_field4, $_field5) = sscanf($output, "0x%64s%64s%64s%24s%40s%s");
$_field1 = hexdec($_field1);
$_field2 = hexdec($_field2);
$_field3 = hexdec($_field3);
$_field4 = '0x' . $_field4;
$_field5 = hexstr($_field5);
echo "\n<hr>list = ($_field1, $_field2, $_field3, $_field4, $_field5)";

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.