之前章節有講到6.Mapping 的概念,OpenZeppelin也有提供相對應的套件
這邊的重點是幫你包裝了function 可以使用,其中最重要的是絕對保證是不重複的資料
EnumerableSet
提供三個型態
EnumerableSet.Bytes32Set Bytes32
EnumerableSet.AddressSet Address
EnumerableSet.UintSet Uint256
add
新增remove
刪除contains
確定存在length
長度at
位置values
全部數值EnumerableSet [可參考]
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "<https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.6.0/contracts/utils/structs/EnumerableSet.sol>";
/*
以下只有解釋其中一種型態的用法可以實際案例去使用
using EnumerableSet for EnumerableSet.Bytes32Set;
using EnumerableSet for EnumerableSet.UintSet;
*/
// AddressSet
contract EnumerableAddressSetMock {
using EnumerableSet for EnumerableSet.AddressSet; // 使用
event OperationResult(bool result); //監控正確
EnumerableSet.AddressSet private _set; // 宣告變數
// 包裝用法
function contains(address value) public view returns (bool) {
return _set.contains(value);
}
// 新增
function add(address value) public {
bool result = _set.add(value);
emit OperationResult(result);
}
// 刪除
function remove(address value) public {
bool result = _set.remove(value);
emit OperationResult(result);
}
// 長度
function length() public view returns (uint256) {
return _set.length();
}
// 位置
function at(uint256 index) public view returns (address) {
return _set.at(index);
}
// 全部數值
function values() public view returns (address[] memory) {
return _set.values();
}
}
EnumerableMap 算是EnumerableSet 整合
提供三個型態 (key ⇒ value)
EnumerableMap.UintToAddressMap Uint256 ⇒ Address
EnumerableMap.AddressToUintMap Address ⇒ Uint256
EnumerableMap.Bytes32ToBytes32Map Bytes32 ⇒ Bytes32
set
設定remove
刪除contains
確定存在length
長度at
位置tryGet
嘗試取得(回傳 bool,結果)get
取值// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// 引用 EnumerableMap
import "<https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.6.0/contracts/utils/structs/EnumerableMap.sol>";
/*
以下只有解釋其中一種型態的用法可以實際案例去使用
using EnumerableMap for EnumerableMap.UintToAddressMap;
using EnumerableMap for EnumerableMap.AddressToUintMap; // 4.6 才支援
using EnumerableMap for EnumerableMap.Bytes32ToBytes32Map;
*/
// UintToAddressMap
contract UintToAddressMapMock {
using EnumerableMap for EnumerableMap.UintToAddressMap;
event OperationResult(bool result);
EnumerableMap.UintToAddressMap private _map;
function contains(uint256 key) public view returns (bool) {
return _map.contains(key);
}
function set(uint256 key, address value) public {
bool result = _map.set(key, value);
emit OperationResult(result);
}
function remove(uint256 key) public {
bool result = _map.remove(key);
emit OperationResult(result);
}
function length() public view returns (uint256) {
return _map.length();
}
function at(uint256 index) public view returns (uint256 key, address value) {
return _map.at(index);
}
function tryGet(uint256 key) public view returns (bool, address) {
return _map.tryGet(key);
}
function get(uint256 key) public view returns (address) {
return _map.get(key);
}
// 帶有錯誤訊息回傳的
// calldata 要特別補充,這個修飾詞出來的話就代表這個變數**不能修改**
function getWithMessage(uint256 key, string calldata errorMessage) public view returns (address) {
return _map.get(key, errorMessage);
}
}
有任何問題可反饋: [email protected]