Solidity Crash Course – Part 06: Mapping, Ether, Memory and Storage

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 wi…


This content originally appeared on DEV Community and was authored by Muhammad Ayaz

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! 🚀


This content originally appeared on DEV Community and was authored by Muhammad Ayaz


Print Share Comment Cite Upload Translate Updates
APA

Muhammad Ayaz | Sciencx (2025-02-01T13:40:38+00:00) Solidity Crash Course – Part 06: Mapping, Ether, Memory and Storage. Retrieved from https://www.scien.cx/2025/02/01/solidity-crash-course-part-06-mapping-ether-memory-and-storage/

MLA
" » Solidity Crash Course – Part 06: Mapping, Ether, Memory and Storage." Muhammad Ayaz | Sciencx - Saturday February 1, 2025, https://www.scien.cx/2025/02/01/solidity-crash-course-part-06-mapping-ether-memory-and-storage/
HARVARD
Muhammad Ayaz | Sciencx Saturday February 1, 2025 » Solidity Crash Course – Part 06: Mapping, Ether, Memory and Storage., viewed ,<https://www.scien.cx/2025/02/01/solidity-crash-course-part-06-mapping-ether-memory-and-storage/>
VANCOUVER
Muhammad Ayaz | Sciencx - » Solidity Crash Course – Part 06: Mapping, Ether, Memory and Storage. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/01/solidity-crash-course-part-06-mapping-ether-memory-and-storage/
CHICAGO
" » Solidity Crash Course – Part 06: Mapping, Ether, Memory and Storage." Muhammad Ayaz | Sciencx - Accessed . https://www.scien.cx/2025/02/01/solidity-crash-course-part-06-mapping-ether-memory-and-storage/
IEEE
" » Solidity Crash Course – Part 06: Mapping, Ether, Memory and Storage." Muhammad Ayaz | Sciencx [Online]. Available: https://www.scien.cx/2025/02/01/solidity-crash-course-part-06-mapping-ether-memory-and-storage/. [Accessed: ]
rf:citation
» Solidity Crash Course – Part 06: Mapping, Ether, Memory and Storage | Muhammad Ayaz | Sciencx | https://www.scien.cx/2025/02/01/solidity-crash-course-part-06-mapping-ether-memory-and-storage/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.