New to blockchain software development? Read my beginners guide here

Guide to calldata in EVM/Solidity

Created on September 2022 • Tags: guidesethereumsolidity

A guide explaining how calldata works in Solidity/EVM


Table of Contents for Guide to calldata in EVM/Solidity


Types of storage on the EVM/in Solidity

There are a few types of places to store data.

  • storage (saved to blockchain) (expensive to read/write)
  • memory (only accessible during a transaction) (cheap to read/write)
  • calldata (the ‘calldata’ is part of the data used when the transaction is created (what function to call, what arguments to use when calling it)) (even cheaper to read)

How to get access to call data in a transaction in Solidity

In solidity you can access the calldata with msg.data.

You also get (some of) the calldata as params to functions. If it is a public/external function you can specify if the param is calldata (cheap + readonly), or memory (it will copy over the calldata, which costs gas, but this means you can mutate (edit) it).

How to send calldata

If you are just sending a normal transaction, sending ETH and not interacting with a smart contract you can send anything in the calldata. There is no smart contract you’ll be interacting with, so the calldata won’t cause any issues.

(You can send messages to wallets like this)

If you create a transaction and interact with a smart contract, if you send any random data (strings for example) as the calldata, the smart contract will try and run and when it encounters an error with the calldata, it will (or should!) revert. This is due to the opcodes that Solidity compiles to.

When you are interacting with a smart contract, Solidity expects that first four bytes contain the keccak256 hash of the function you are calling, then the bytes after that are the params for calling that function. (more details about function selectors here)

If you write your own smart contract in something like assembly then it doesn’t have to follow this convention. (Although I’d highly recommend it, as almost all smart contracts expect this standard to be followed.)

Using calldata in function argument

You can use calldata when defining arguments for functions. This can be used to optimize your code

Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!

See all posts (70+ more)

See all posts (70+ more)

Was this post helpful? 📧

If you liked this content and want to receive emails about future posts like this, enter your email. I'll never spam you.

Or follow me on @CryptoGuide_Dev on twitter

By using this site, you agree that you have read and understand its Privacy Policy and Terms of Use.
Use any information on this site at your own risk, I take no responsibility for the accuracy of safety of the information on this site.