2

I am trying to figure out how to calculate the offset in bytes to the start of their data area, measured from the start of the value encoding as described here in the Solidity docs for dynamic parameter types.

Here is my function that I would like to call on my deployed contract:

function myFunction(
        address _address, 
        bytes _name, 
        bytes32[] _choices, 
        uint256 _blockNumber)
        public
    {
    }

How do I calculate the offset for bytes _name and bytes32[] _choices?

I normally use ethabi to encode my parameters, but I don't think it's able to output what I need here?

Any help is appreciated!

3

If you don't want to pull your head off with arg encoding please note that there is now in web3JS vs 1.0 a method called encodeParameters

Check also encodeFunctionCall that will encode the whole function + arguments

If we take a look at the solidity doc Use of dynamic types

we use the offset in bytes to the start of their data area, measured from the start of the value encoding (i.e. not counting the first four bytes containing the hash of the function signature).

The following types are called “dynamic”:

bytes string T[] for any T

T[k] for any dynamic T and any k > 0

(T1,...,Tk) if any Ti is dynamic for 1 <= i <= k

All other types are called “static”.

NB: Address is the equivalent of a uint160

so in your example you will encode

  • _address the first parameter, a uint160 value padded to 32 bytes

  • _name will be 0x0000000000000000000000000000000000000000000000000000000000000080 (offset to start of data part of second parameter, 4*32 bytes, exactly the size of the head part)

  • _choices (offset to start of data part of third parameter = offset to start of data part of first dynamic parameter + size of data part of first dynamic parameter = 4*32 + ???*32 )
  • _blocknumber the uint256 value padded to 32 bytes

After this, the data part of the first dynamic argument

  • number of bytes of _name
  • _name value padded to 32 bytes on the right

Finally, we encode the data part of the second dynamic argument _choices

  • number of elements of the array
  • value of first element
  • value of second element
  • etc ...

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.