Guide to calldata in EVM/Solidity
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
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!
📙 Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
⛽ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
🧪 Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
📌 Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
📐 Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
📦 Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about