After taking Patrick Collins's 32-hour course on Learn Blockchain, I am putting out my note on some of the things I learnt. I hope you learn something too from them.
Solidity is a contract-oriented, high-level programming language for writing smart contracts on the Ethereum blockchain.
One of the critical features of Solidity is its ability to handle different types of data storage, including stack, memory, storage, calldata, code, and logs.
This article will explore these storage types and provide examples of how to use them in Solidity.
Stack
The stack is a temporary storage area used for holding data during the execution of a contract.
It is a last-in, first-out (LIFO) data structure, meaning that the last item added to the stack is the first item to be removed. In Solidity, the stack stores basic data types such as integers, booleans, and addresses.
Example:
pragma solidity ^0.8.0;
contract StackExample {
function addToStack(uint256 _number) public {
uint256 storedNumber = _number;
}
}
In this example, we have a contract called StackExample with a function called addToStack.
This function takes an input of type uint256 and stores it in the stack by assigning it to the variable storedNumber.
Memory
Memory is similar to the stack in that it is a temporary storage area used during the execution of a contract.
However, unlike the stack, memory is not limited in size and is used for storing more complex data structures such as arrays and structs.
Example:
pragma solidity ^0.8.0;
contract MemoryExample {
struct Person {
string name;
uint age;
}
function addToMemory(string memory _name, uint _age) public {
Person memory person = Person(_name, _age);
}
}
In this example, we have a contract called MemoryExample with a struct called Person. This struct has two fields, name and age, of type string and uint, respectively.
The contract also has a function called addToMemory which takes two inputs, a string and an uint, and stores them in memory by creating a new instance of the struct Person and assigning the inputs to the name and age fields, respectively.
Storage
Storage is a permanent storage area used to store data that should persist beyond the execution of a contract.
This is the storage area that is accessible to all functions of the contract and is also visible to external contracts. In Solidity, the storage area is used for storing data such as contract state variables and mapping.
Example:
pragma solidity ^0.8.0;
contract StorageExample {
mapping(address => uint) public balances;
function addToStorage(address _address, uint _amount) public {
balances[_address] = _amount;
}
}
In this example, we have a contract called StorageExample that has a mapping called balances.
This mapping is of the type address to uint, meaning that it maps addresses to uint values.
The contract also has a function called addToStorage which takes two inputs, an address and an uint, and stores them in storage by adding a new key-value pair to the mapping where the key is the address input and the value is the uint input.
Calldata
Calldata is the data passed to a contract as an input during a function call. It is read-only and used to pass information to a contract without modifying its storage.
Example:
pragma solidity ^0.8.0;
contract CalldataExample {
function setNumber(uint _number) public {
require(msg.data.length == 32, "Invalid input data");
uint storedNumber = _number;
}
}
In this example, we have a contract called CalldataExample with a function called setNumber. This function takes an input of type uint and stores it in the contract by assigning it to the variable storedNumber.
The function also uses the "msg.data" keyword to check the input data length passed in the function call. If the length is not equal to 32, the function will revert with an error message "Invalid input data".
Code
Code is the compiled bytecode of a contract deployed on the Ethereum blockchain. It is read-only and cannot be modified after deployment. In Solidity, a contract's code can be accessed using the "this" keyword.
Example:
pragma solidity ^0.8.0;
contract CodeExample {
function getCode() public view returns (bytes memory) {
return this.code;
}
}
In this example, we have a contract called CodeExample with a function called getCode. This function uses the "this.code" keyword to access the contract's bytecode and return it as an output of type bytes.
Logs
Logs are a way for a contract to record events on the Ethereum blockchain.
They are stored in the blockchain's transaction receipt and can be used for various purposes, such as tracking contract execution or providing proof of certain events.
In Solidity, logs can be emitted using the "emit" keyword.
Example:
pragma solidity ^0.8.0;
contract LogsExample {
event LogNumber(uint _number);
function logNumber(uint _number) public {
emit LogNumber(_number);
}
}
In this example, we have a contract called LogsExample that has an event called LogNumber. This event takes an input of type uint and is emitted using the "emit" keyword in the function logNumber. Any contract or external user can listen to this event and get the logged number.
Conclusion
Solidity provides various storage options for handling different data types, including stack, memory, storage, calldata, code, and logs.
Each storage type has unique properties and use cases, and understanding how to use them correctly is crucial for writing efficient and secure smart contracts on the Ethereum blockchain.
Resources
Solidity documentation
Mastering Ethereum
If you like the articles and excellent write-up, please support me by getting me coffee or subscribing to my blog, so you get the latest content in your mail, and many more I don't post here.
Top comments (0)