1. When I look at it in general, everywhere it says calldata is used only with external visibility, but I can also use it with public while writing a contract? Because when I use public, there is no problem.

  2. In general, I see less use of calldata in contracts. Not used much ?

  3. Is calldata our abi coded information that specifies the function and parameters to be called to the contract?

  4. Based on my third question, do msg.data and calldata have the same meanings?

1 Answer 1

  1. calldata can typically only be used with functions that have external visibility. However, Solidity will let you declare any function with calldata as long as you call it with calldata arguments. This means that the source of these arguments needs to come from message calldata, directly or indirectly (i.e. by passing forward calldata arguments).
  2. calldata can be useful! It helps lower gas consumption by skipping a copy of message calldata into memory.
  3. Precisely! :)
  4. Yes! msg.data is the Solidity equivalent to the calldata concept in the EVM.

The Solidity keyword calldata tells the compiler that you want to use values directly from message calldata (i.e. msg.data) and not have it copy to memory (or worse, storage) and cost additional gas.

The concept of calldata extends much beyond Solidity as an EVM term.

Here is a useful excerpt from an OpenZeppelin blog post:

The calldata is a read-only byte-addressable space where the data parameter of a transaction or call is held. Unlike the stack, to use this data you have to specify an exact byte offset and number of bytes you want to read. The opcodes provided by the EVM to operate with the calldata include:

  • CALLDATASIZE tells the size of the transaction data.
  • CALLDATALOAD loads 32 bytes of the transaction data onto the stack.
  • CALLDATACOPY copies a number of bytes of the transaction data to memory.
  • Thank you very much for answering me, but when I watched this video(youtube.com/watch?v=wOCIhzAuhgs), he called calldata from within the contract and used it. I still have question marks about my first question.
    – Whisrambol
    Mar 6, 2022 at 14:54
  • Ah my answer for the first question wasn't clear and was incomplete. I edited it to clarify! Mar 6, 2022 at 23:42

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.