🔮
Ethereum
  • General
    • What is Ethereum
      • Ethereum & Bitcoin General Comparison
      • Singleton State
      • The Ethereum Virtual Machine (EVM)
      • Opcodes (operation codes) EVM
      • Ethereum Client
      • Forks
      • Gas (wei)
        • EIP-1559
        • Table Conversion (wei)
      • Proof of Stake (PoS)
      • Proof of Authority (PoA)
      • The Beacon Chain
      • Networks
        • Ethereum mainnet
        • Goerli
      • Account-based model & UTXO-based model
      • Externally Owned Account (EOA)
      • Node Clients
        • Geth
        • Nethermind
      • Contract Account
      • Smart Contract Upgradeability
      • Ultrasound Money
      • Merkle Trees
        • Patricia Merkle Tree
      • Tries
        • State Trie
        • Storage Trie
        • Transactions Trie
        • Receipts Trie
      • Transactions
        • Ethereum Transaction Architecture
      • World State
        • Chain of States
        • Chain of Blocks
        • Stack of Transactions / Mempool
      • Contract Creation
      • Message Call Transaction
      • P2P Network
      • Web3.js
      • Ether.js
        • Smart Contract Interaction Example
      • Web3.js vs Ether.js
      • Node Providers
      • ENS (Ethereum Name Service)
      • Web3 dapp
      • Escrow
      • Multi-signature
      • ERC-20 tokens
        • Send ERC20s to Contracts
      • NFTs
        • ERC-721 and ERC-1155
      • Solidity
        • State Variables
        • Data Location
        • Numbers
        • Modifiers
        • View & Pure Modifiers
        • Data Types
          • Modifiers
          • Modifiers (Functions)
          • Address & Address Payable
        • Hardhat
        • Payable Functions
        • Receive Function
        • Fallback Function
        • Global Variables
        • Self Destruct
        • Create2 Function
        • Revert function
        • Require function
        • Assert Function
        • Calldata
        • Interface
        • Mapping
        • Array
        • Struct
        • Inheritance
          • Virtual & Overwrite
          • Multiple inheritance
          • Hierarchical Inheritance
        • Events
          • Indexed (keyword)
          • LOG0 - LOG4
        • Multi-signature Example
        • Smart Contracts
          • Context
      • Application Binary Interface (ABI )
  • Extras
    • Terminology
      • Bytecode
      • Keccak-256
      • Turing complete
Powered by GitBook
On this page
  1. General
  2. What is Ethereum
  3. Solidity

Calldata

calldata is a special data location in Solidity that is used to pass arguments to a function when it is called. When a function is called externally, the arguments passed to it are stored in the calldata area of the transaction data. This is a read-only area of memory that contains the function call parameters.

In contrast to memory and storage, which are used to store data within a function or contract, calldata is used to store function arguments when calling the function from outside the contract. Since calldata is read-only, it is a cheaper and more efficient way to pass arguments to functions compared to copying data to memory or storage.

Here's an example of a function that takes a bytes parameter and reads the first 32 bytes of the bytes data:

pragma solidity ^0.8.0;

contract CalldataExample {
    function readFirst32Bytes(bytes calldata data) external pure returns (bytes32) {
        require(data.length >= 32, "Input data must be at least 32 bytes long");
        bytes32 result;
        assembly {
            result := mload(add(data, 32))
        }
        return result;
    }
}

In this example, the calldata keyword is used to specify that the data parameter is a reference to the input data passed to the function. The assembly block is used to read the first 32 bytes of the data parameter using the mload opcode. The resulting 32-byte value is returned from the function as a bytes32 value.

PreviousAssert FunctionNextInterface

Last updated 2 years ago