0

When sending eth_call to get erc20 token WETH name and symbol I get hex response padded with bunch of zeros and a mysterious digit '2'.

for symbol I get response:

"0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000045745544800000000000000000000000000000000000000000000000000000000"

for name I get response:

0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d5772617070656420457468657200000000000000000000000000000000000000"

When I convert responses to utf8 string I get:

��������������������������������WETH 
�������������������������������Wrapped Ether

The routine to decode hex strings from ethereum.js unpads only up to first '2' that it finds. Although padding with zeros continues until actual chars begin.

Why such a 'weird' encoding of response and how to properly decode it?

1

They are encoded using solidity abi

  • The first 32 bytes is the offset where string is present within the response: 0000000000000000000000000000000000000000000000000000000000000020. So the string starts at position 20h = 32.

  • At specified offset the first 32 bytes indicates the length of the string: 0000000000000000000000000000000000000000000000000000000000000004. So string has length 4h = 4.

  • Following the length the next 4 bytes are the string itself: 57455448. From a utf-8 you have "WETH".

| improve this answer | |
  • Thanks @Ismael. ethereum rpc spec on encoding is very loose: github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding only two options: quantities and unformatted data. How do I know what encoding was used for data? I know that response is supposed to be string from method ABI definition. But how am I supposed to figure out what encoding was used for that string? – Sharas May 23 at 13:01
  • @Sharas The exceptions to that rule are when you interact with a contract, for example data field in eth_sendTransaction, eth_call; the returned data from eth_call; etc. – Ismael May 23 at 15:40

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.