2

1- While learning Solidity, I've been searching for answers on why is encode, encodePacked, encodeWithSelector, and any type of encoding is used in the first place, in Solidity. There are a lot of resources that explain how they work, but little out there that explain exactly why are they used for. What are some of the reasons or scenarios? There are mentions such as "to dynamically invoke a function". But what does that actually mean and why do it that way? What added benefit is there to just writing Solidity code (without using encoding) and then compiling it to ABI and bytecode?

2- Also, there's a mention of offset by 32bytes and padded to 32bytes in this article. Again what does this mean?

3- Why use Selector vs Signature?

Many thanks in advance.

1 Answer 1

3

Purpose of encoding

Encoding is really generic term. It basically means "creating byte representation of something more high level".

On a EVM level there is no functions and contracts. There is just bytes. Because of that, everything what you see as a contract, function, value, etc in Solidity, has to be encoded (create a sequence of bytes).

Encoding has to follow rules. There are different rules how to encode things in EVM, depending on what you are encoding. Transactions, for example, are encoded like this:

  • first 4 bytes are function selector (F)
  • rest are encoded parameters (P)

Raw transaction data does look like this: 0xFFFFPPPP...PPPP.

You can read more about how to encode what in solidity docs: https://docs.soliditylang.org/en/v0.8.13/abi-spec.html

Now every time an EVM client receives a transaction, it can decode that raw transaction and know which function to call and what parameters to use.

Where to use it

If you are asking where is encoded used in EVM, then answer is everywhere. Everything has to be eventually represented with bits.

If you are asking where to use encoding as a Solidity developer, you will usually use it for creating raw transactions or manipulating values (e.g. when you want to contact two string together).

To answer your questions

  1. Encoding creates byte strings so EVM can understand it. Compiler generates opcodes from source code, encoding creates bytecodes from opcodes.
  2. It is one of the encoding rules. If you are encoding function parameters, you are padding them with zeros so they always takes 32 bytes. If you don't do that, it would be ambiguous if you encoded one big 32 bytes value of few smaller (e.g. one uint256 or two uint128).
  3. Function signature is string representation of function from which function selector can be created. Selector is basically first 4 bytes of function signatures hash (those first 4 bytes in raw transaction).

You can read about this topic in solidity docs: https://docs.soliditylang.org/en/v0.8.13/abi-spec.html#function-selector

1
  • function selector is the concept of solidity (and possibly other languages in which ethereum smart contracts are written) and not of EVM, and hence it is not necessary to encode transaction with first 4 bytes as function selector.
    – hack3r-0m
    Commented Aug 9, 2022 at 17:02

Your Answer

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

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