1

I have an event with the signature Event(address, uint256, string) which my smart contract emit from time to time. I am testing said contract on a ganache test net where when the Ganache picks up these events, it decodes them perfectly. Yet when I try to decode the abi encoding of said event using the rpc-json response there seems to be something wrong with the encoding even though everything else (tx-hash etc.) lines up:

I tested with address being 0xa96403a52c4b63fefda2f24734a7c1f69a36b24a, the uint256 being 1 and the string being hello and get this result:

0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000

This is the uint256 which is 1:

0x0000000000000000000000000000000000000000000000000000000000000001

I don't know what this is:

0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000 

This is the length of the string, followed by the utf8 encoding of "hello", correctly by 27 bytes of padding to make it 32 bytes in total:

568656c6c6f000000000000000000000000000000000000000000000000000000

Could anyone help me out in decoding the address here and correct me in case I did something wrong decoding the other stuff? Thank you so much in advance!

1
  • 1
    Let me guess - the address parameter in your event is indexed? If yes, then it's not going to be provided in the data field, but in the topics array at the 2nd entry (i.e., topics[1]). Mar 31, 2020 at 8:00

1 Answer 1

1

From the details that you've posted, I believe that the address parameter in your event is indexed. Therefore, it is not going to be provided in the data string, but in the topics array at the 2nd entry (i.e., topics[1]).

If you want the actual address, then you can do:

const address = "0x" + topics[1].slice(26); // get rid of 24 leading zeros

Since the address has been fetched from an event, it's going to be all lower-case.

So if you're planning to compare it with other addresses, then you'd be better off with:

const address = Web3.utils.toChecksumAddress("0x" + topics[1].slice(26));

As for the data-part which you were puzzled with (0x00...40), it is related to how dynamic-types (string in your case) are encoded; you can find a full explanation of that in the documentation.

2
  • Thank you for your answer that's correct! Just out of curiosity, what is that abi encoded string in my data then? Mar 31, 2020 at 9:13
  • @NicolasSchapeler: Please see updated answer. Mar 31, 2020 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.