๐ Storage
- Storage is the permanent place on the blockchain.
- It is tied to the contractโs state variables.
- Changes here persist between transactions.
- Storage writes are expensive in gas because data is written to the blockchain forever.
- Reading storage is cheaper than writing, but still costs more than reading from memory or calldata.
Example:
uint public x; // this variable is in storage
function update() public {
x = 5; // writes to storage (permanent)
}
โ
Think of storage as the hard disk of the contract.
๐ Memory
- Memory is a temporary readโwrite area
- It only exists while a function is executing
- Once the function ends, the memory data is wiped
- It is cheaper than storage, but more expensive than calldata
- You can modify memory variables freely
Example:
function copy(uint[] memory arr) public pure returns (uint) {
arr[0] = 10; // OK, can modify
return arr[0];
}
โ
Think of memory as RAM in your function.