【試讀版】All In One Solidity
KrypltoCampChiHaoLu
  • Welcome!
  • | Chapter 0 | Intro
    • 【推薦序】
    • 【前言】
  • Part I Basic
    • | Chapter 1 | 基本介紹 Introduction
      • 【環境建置 Remix IDE】
      • 【版本控制】
      • 【Hello World & First Contract】
      • 【Practice】
      • 【Answer】
    • | Chapter 2 | 型別 Types & 變數 Variables
      • 【Integer】
      • 【Bool】
      • 【Address】
      • 【Contract & This】
      • 【String】
      • 【Scope】
      • 【Practice】
      • 【Answer】
    • | Chapter 3 | 單位 Unit & 運算子 Operators
      • 【單位】
      • 【Time】
      • 【運算子】
      • 【Practice】
      • 【Answer】
    • | Chapter 4 | 流程控制 Selection and Repetition
      • 【If-Else】
      • 【For】
      • 【While】
      • 【Practice】
      • 【Answer】
    • | Chapter 5 | 函式 Function
      • 【Returns】
      • 【Visibility】
      • 【stateMutability】
      • 【Constructor】
      • 【Function Overloading】
      • 【Fallback】
      • 【Useful Function】
      • 【Practice】
      • 【Answer】
    • | Chapter 6 | 資料結構 Data Structures
      • 【Array】
      • 【Mapping】
      • 【Structs】
      • 【Enum】
      • 【Practice】
      • 【Answer】
    • | Chapter 7 | 角色和全局訊息 Global Variables
      • 【Msg】
      • 【Block】
      • 【ABI】
      • 【TX】
      • 【Practice】
      • 【Answer】
  • Part II Medium
    • | Chapter 8 | 記憶體配置 Memory Allocation
    • | Chapter 9 | 異常處理 Handling Exception
      • 【Require】
      • 【Assert】
      • 【Revert】
      • 【Try/Catch】
      • 【Practice】
      • 【Answer】
    • | Chapter 10 | 事件 Event
      • 【emit】
      • 【Indexed】
      • 【web3.eth.subscribe()】
      • 【Logs with Data & Topics】
      • 【anonymous】
      • 【Practice】
      • 【Answer】
    • | Chapter 11 | 繼承 Inheritance
      • 【Inheritance】
      • 【Modifier】
      • 【合約互動】
      • 【Function Overriding】
      • 【Polymorphism】
      • 【多重繼承與 super】
      • 【Practice】
      • 【Answer】
    • | Chapter 12 | 介面 Interface
    • | Chapter 13 | 引用 Imports & 函式庫 Libraries
      • 【Library】
      • 【Import】
      • 【OpenZeppelin】
      • 【Practice】
      • 【Answer】
    • | Chapter 14 | ERC & Token
  • PART III Advanced
    • | Chapter 15 | 佈署 Deploy & 編譯 Compiler
    • | Chapter 16 | 開發工具 Dev. Tools
    • | Chapter 17 | 最佳化合約 Contract Optimization
  • | OTHERS | Information & Reference
    • 【結語】
    • 【參考資料】
Powered by GitBook
On this page

Was this helpful?

  1. Part I Basic
  2. | Chapter 6 | 資料結構 Data Structures

【Mapping】

Previous【Array】Next【Structs】

Last updated 3 years ago

Was this helpful?

mapping 在 Solidity 的運作就像是 python 的 dict 或常見的資料結構 hash-map。

在圖中的例子,當我們 call "k1" 的時候,這個 mapping 就會回傳 key "k1" 指向的值,也就是 "AAA,BBB,CCC"。此外,我們還可以儲存新的資料進去 mapping。

mapping(uint => string) public names;

在以上這段語法中,每一個 key 在這個名為 names 的 mapping 中,是以 uint 形態存在。而相對應的每個 value 型態為 string。

宣告語法為:mapping(_keyType => _valueType) name;

  • _keyType 可以是任何的元素型態,也就是說它可以為任何的既有數值型態包含bytes和string

  • _valueType 可以是任何的型態,包含複雜的資料結構,甚至是mapping

Mapping具有以下的特色:

  • 所有元素都會被初始化

  • 不存在長度,如果需要紀錄長度得額外宣告一個變數

  • mapping 的Public state variables 會成為Getter Function

  • 如果需要可遍歷的mapping可以藉由導入一些libraries達到。

pragma solidity ^0.8.11;

contract MyMappings {
    mapping(uint256 => address) nfts;
    uint256 counter = 0;
    
    function getOwnerOfNFT(uint256 _id) public view returns (address) {
        return nfts[_id];
    }
    
    function mintNFT() public {
        nfts[counter] = msg.sender;
        counter++;
    }
}

如同二維陣列,我們也可以宣告 Nested mapping 來做為類似的用途:

mapping(address => mapping(address => bool)) a;

那既然用途很像,我們什麼時候用 array,什麼時候用 mapping 呢?我會用使用情況來區分我們要使用哪種資料結構:

  • 當常常需要遍歷(iterate)這個資料結構時,我就會使用 array

  • 如果我們需要常常查看某些特定的鍵值(key)時,我會使用 mapping

需要注意的是,我們沒辦法使用動態記憶體配置來宣告 mapping。

pragma solidity ^0.8.11;

contract complexVariable{
    
    mapping(address => uint) public balanceReveived;
    
    function getBalance() public view returns(uint){
        return address(this).balance;
    }
    
    function sendMoney() public payable {
        balanceReveived[msg.sender] += msg.value;
    }
    
    function withdrawMoney(address payable _to, uint _amount) public {
        require(balanceReceived[msg.sender] <= _amount, "not enough funds");
        balanceReceived[msg.sender] -= _amount;
        _to.transfer(_amount);
    }
    
    function withdrawAllMoney(address payable _to) public {
        uint balanceToSend = balanceReceived[msg.sender];
        balanceReveived[msg.sender] = 0;
        _to.transfer(balanceToSend);   
    }
}