Solidity Crash Course - Part 06 π
Introduction π
Welcome to part 06 of our Solidity crash course! In this section, we will explore some fundamental concepts in Solidity that will help you build more advanced smart contracts. We will cover:
β
Solidity - Mapping
β
Solidity - Memory vs Storage
β
Solidity - Ether
By the end of this lesson, youβll understand why these concepts are crucial and how to use them effectively. Letβs dive in! πββοΈ
Solidity - Mapping πΊοΈ
Mappings in Solidity are like hash tables or dictionaries in other programming languages. They allow you to store key-value pairs efficiently.
Why Use Mappings?
Mappings are great for storing data that needs fast lookup, such as token balances or user permissions.
Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract UserBalance {
mapping(address => uint) public balances;
function setBalance(uint _amount) public {
balances[msg.sender] = _amount;
}
function getBalance(address _user) public view returns (uint) {
return balances[_user];
}
}
Best Use Case β
- Storing user balances in a token contract
- Keeping track of ownership or permissions
Solidity - Memory vs Storage πΎ
In Solidity, memory
and storage
are used to manage data. But whatβs the difference? π€
storage
π¦ (Permanent)
- Stored on the blockchain
- Costs gas to modify
- Used for persistent state variables
memory
πββοΈ (Temporary)
- Exists only during function execution
- No gas cost for reading (only for writing)
- Used for function arguments and temporary variables
Example:
contract DataExample {
string[] public storageArray; // Stored on the blockchain
function addToStorage(string memory _text) public {
storageArray.push(_text); // Modifies state variable (costs gas)
}
function processMemory(string memory _text) public pure returns (string memory) {
return _text; // Exists only during function execution
}
}
Best Use Case β
- Use
storage
for data that needs to persist (like user balances) - Use
memory
for temporary data (like function inputs)
Solidity - Ether π°
Ether (ETH) is the native cryptocurrency of the Ethereum blockchain, and smart contracts can send, receive, and store Ether.
Sending & Receiving Ether
Smart contracts use special functions to handle Ether transactions.
Example:
contract EtherWallet {
address payable public owner;
constructor() {
owner = payable(msg.sender);
}
// Receive Ether
receive() external payable {}
// Get contract balance
function getBalance() public view returns (uint) {
return address(this).balance;
}
// Withdraw Ether
function withdraw(uint _amount) public {
require(msg.sender == owner, "Not the owner");
payable(msg.sender).transfer(_amount);
}
}
Best Use Case β
- Smart contracts that hold funds (like a wallet or crowdfunding contract)
- Decentralized finance (DeFi) applications
Conclusion π―
In this part, we covered:
β
Mappings and their importance in Solidity
β
The difference between memory
and storage
β
How smart contracts handle Ether transactions
Let me know if you have any questions, comments, or feedback! π
Would love to help you out! π
Top comments (0)