DEV Community

Cover image for Solidity Crash Course - Part 06: Mapping, Ether, Memory and Storage
Muhammad Ayaz
Muhammad Ayaz

Posted on

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 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];
    }
}
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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)