1

I created a simple test contract with a single method:

pragma solidity ^0.4.24;

contract SimpleTest {

    function testFunc(string name, bool isFirst) public {
        //do stuff
    }

}

I'm calling the function with values "test name", true and once the transaction is executed I see the following encoded input:

0x1a387720
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000009
74657374206e616d650000000000000000000000000000000000000000000000

Which, according to the documentation, should include the function selector and a 32 bytes string for each input param. Why do I get 4 strings of 32 bytes and not 2? I'm not clear how to decode this input to get the actual values of the input params.

I used Remix to test this case.

1

Transactions are encoded based on the Contract ABI Specification. It is hard to get through, but these docs have all the answers to your question.


The transaction in question is passing in two parameters: a dynamic string (name) and a static bool (isFirst). When encoding the parameters, the EVM looks to see if the parameters are static or dynamic.

Static parameters are encoded in a fairly straightforward manner--they are converted to their hex representation and then concatenated into the input data hex string.

Dynamic values are more interesting. Use this section of the docs to fully understand, but the idea is that the encoded data is the location of the data. The encoded data itself is then concatenated to the end of the input data hex string, after all of the dynamic types. For dynamic types, the length of the parameter is then included, followed by the data itself.

Breaking down the transaction you posted:

0x1a38772000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000974657374206e616d650000000000000000000000000000000000000000000000

0x1a38772

This is the method ID of the function called (in this case, testFunc(string, bool))

0000000000000000000000000000000000000000000000000000000000000040

This is the location of the first (dynamic) parameter. This is the location where name lives, but is not the data itself.

0000000000000000000000000000000000000000000000000000000000000001

This is the isFirst parameter that was sent with the transaction. 1 is equivalent to true.

0000000000000000000000000000000000000000000000000000000000000009

This is the length of the dynamic name. In this case, the string has 9 characters.

74657374206e616d650000000000000000000000000000000000000000000000

The hex representation of the ASCII for "test name".

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.