I have seen source codes where they would encode a bunch of parameters into a single bytes parameter and pass it into a function, what's the real purpose of doing it? For example:

function hello(bytes memory data) public {
  (string memory name, uint256 age) = abi.decode(data, (string, uint));
  // do something with name and age

In the example above, both name and age can be passed into hello() function as a single parameter data. But within the function, I will still have to know how to decode data, which means that I already know beforehand what will be passed into this function. Then, why even use encode/decode and not just like this:

function hello(string name, uint256 age) public {
  // do something with name and age

Isn't it more straightforward to simply separate the parameters in the method arguments than having to decode it later? In fact, as a client interacting with this contract, I don't have to go through the trouble of encoding the parameters; I can just easily pass the values into the method!

What's the benefits of using abi encoding/decoding compared to just separating the arguments? In what scenario should I consider using the abi encoding/decoding method over separate the arguments in a function? What should be the best practice?

1 Answer 1


I think it just comes down to what the purpose of the function is. If you want a function that allows you to run arbitrary functions on behalf of that contract (e.g. a multisig), you don't want to limit the type of inputs/functions you can run, so you just make a general one

  • How can it be a general one because the contract receiving the data bytes will still need to decode it? During decoding, the contract already knows the parameters to decode from the bytes. Meaning, even if I pass in addition values in the data bytes, they won’t be decoded because the receiving contract function doesn’t know beforehand.
    – xenon
    Commented Mar 22, 2022 at 13:00
  • Here's an example, check out the submitTransaction function: github.com/gnosis/MultiSigWallet/blob/master/contracts/…
    – thefett
    Commented Mar 23, 2022 at 15:33

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.