33

What is calldata? I learned that there are three types of memories:

  1. Storage
  2. Memory
  3. Stack

In the Ethereum docs, it says that the function parameters are of type memory by default.

However, I've also read an article which said that function arguments are stored in calldata.

Now, I don't know what calldata is, can somebody please help me?

Thanks!

1
  • 1
    in simpler term calldata is the data provided within the call made to the smart contract. as @eth have explained it's the function's signature and the parameters provided.
    – Badr Bellaj
    Commented Dec 17, 2019 at 21:44

3 Answers 3

45

Here is an example from What is an ABI and why is it needed to interact with contracts?

contract Foo {
  function baz(uint32 x, bool y) returns (bool r) { r = x > 32 || y; }
}

If we wanted to call baz() with the parameters 69 and true, we would pass 68 bytes in total, which can be broken down into:

0xcdcd77c0: the Method ID. This is derived as the first 4 bytes of the Keccak-256 hash of the ASCII form of the signature baz(uint32,bool). 0x0000000000000000000000000000000000000000000000000000000000000045: the first parameter, a uint32 value 69 padded to 32 bytes. (69 is hex 0x45.) 0x0000000000000000000000000000000000000000000000000000000000000001: the second parameter - boolean true, padded to 32 bytes

The 68 bytes is the calldata: 0xcdcd77c000000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001.

1
  • 6
    I'd like to leave a comment for those who, like me, were confused at first : the padded values could be wrapped due to your screen resolution the values are (as it was explained) therefore the concatenation of 0xcdcd77c0 , 0x00....0045 , 0x00........01
    – Stormsson
    Commented Jul 16, 2021 at 13:59
19

calldata is where data from external calls to functions is stored.

Functions can be called internally, e.g. from within the contract, or externally. When a function's visibility is external, only external contracts can call that function.

When such an external call happens, the data of that call is stored in calldata.

Read also here: `external` vs `public` best practices

3
  • External calls of functions! I nust know we can call a function but this terminology I dont det this
    – Sheikh
    Commented Jul 9, 2018 at 14:38
  • Can you please explain a little bit
    – Sheikh
    Commented Jul 9, 2018 at 14:38
  • I edited the response to include more info
    – schemar
    Commented Jul 9, 2018 at 14:53
6

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 related to Calldata provided by EVM are:

• CALLDATASIZE returns the size of transaction data

• CALLDATALOAD imports 32 bytes of transaction data onto the stack

• CALLDATACOPY copies transaction data of a certain number of bytes to memory

calldata behaves almost like memory. It is an unmodifiable and temporary area that can be used to store function arguments. As copying between memory and storage can be quite expensive, specifying a location can be helpful to control the gas expenditure at times.

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.