From 4149c7c42aeba204d626b3733ca9dba3d726d4c9 Mon Sep 17 00:00:00 2001 From: tynuk Date: Mon, 3 Jan 2022 17:35:19 +0200 Subject: [PATCH 1/3] add address to uint map --- contracts/utils/structs/EnumerableMap.sol | 94 +++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/contracts/utils/structs/EnumerableMap.sol b/contracts/utils/structs/EnumerableMap.sol index 1bc571594cb..7221840ce40 100644 --- a/contracts/utils/structs/EnumerableMap.sol +++ b/contracts/utils/structs/EnumerableMap.sol @@ -237,4 +237,98 @@ library EnumerableMap { ) internal view returns (address) { return address(uint160(uint256(_get(map._inner, bytes32(key), errorMessage)))); } + + // AddressToUint + + struct AddressToUintMap { + Map _inner; + } + + /** + * @dev Adds a key-value pair to a map, or updates the value for an existing + * key. O(1). + * + * Returns true if the key was added to the map, that is if it was not + * already present. + */ + function set( + AddressToUint storage map, + address key, + uint256 value + ) internal returns (bool) { + return _set(map._inner, bytes32(key), bytes32(value)); + } + + /** + * @dev Removes a value from a set. O(1). + * + * Returns true if the key was removed from the map, that is if it was present. + */ + function remove(AddressToUint storage map, address key) internal returns (bool) { + return _remove(map._inner, bytes32(key)); + } + + /** + * @dev Returns true if the key is in the map. O(1). + */ + function contains(AddressToUint storage map, address key) internal view returns (bool) { + return _contains(map._inner, bytes32(key)); + } + + /** + * @dev Returns the number of elements in the map. O(1). + */ + function length(AddressToUint storage map) internal view returns (uint256) { + return _length(map._inner); + } + + /** + * @dev Returns the element stored at position `index` in the set. O(1). + * Note that there are no guarantees on the ordering of values inside the + * array, and it may change when more values are added or removed. + * + * Requirements: + * + * - `index` must be strictly less than {length}. + */ + function at(AddressToUint storage map, uint256 index) internal view returns (address, uint256) { + (bytes32 key, bytes32 value) = _at(map._inner, index); + return (address(uint160(key)), uint256(value)); + } + + /** + * @dev Tries to returns the value associated with `key`. O(1). + * Does not revert if `key` is not in the map. + * + * _Available since v3.4._ + */ + function tryGet(AddressToUint storage map, address key) internal view returns (bool, uint256) { + (bool success, bytes32 value) = _tryGet(map._inner, bytes32(key)); + return (success, uint256(value)); + } + + /** + * @dev Returns the value associated with `key`. O(1). + * + * Requirements: + * + * - `key` must be in the map. + */ + function get(AddressToUint storage map, address key) internal view returns (uint256) { + return address(uint160(uint256(_get(map._inner, bytes32(key))))); + } + + /** + * @dev Same as {get}, with a custom error message when `key` is not in the map. + * + * CAUTION: This function is deprecated because it requires allocating memory for the error + * message unnecessarily. For custom revert reasons use {tryGet}. + */ + function get( + AddressToUint storage map, + address key, + string memory errorMessage + ) internal view returns (uint256) { + return uint256(_get(map._inner, bytes32(key), errorMessage)); + } } From 6fb50f26ffb12e2b1fb2d6938eaea5ff1bae037d Mon Sep 17 00:00:00 2001 From: tynuk Date: Mon, 3 Jan 2022 17:42:15 +0200 Subject: [PATCH 2/3] fix type in type name --- contracts/utils/structs/EnumerableMap.sol | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contracts/utils/structs/EnumerableMap.sol b/contracts/utils/structs/EnumerableMap.sol index 7221840ce40..c96561cd6f5 100644 --- a/contracts/utils/structs/EnumerableMap.sol +++ b/contracts/utils/structs/EnumerableMap.sol @@ -252,7 +252,7 @@ library EnumerableMap { * already present. */ function set( - AddressToUint storage map, + AddressToUintMap storage map, address key, uint256 value ) internal returns (bool) { @@ -264,21 +264,21 @@ library EnumerableMap { * * Returns true if the key was removed from the map, that is if it was present. */ - function remove(AddressToUint storage map, address key) internal returns (bool) { + function remove(AddressToUintMap storage map, address key) internal returns (bool) { return _remove(map._inner, bytes32(key)); } /** * @dev Returns true if the key is in the map. O(1). */ - function contains(AddressToUint storage map, address key) internal view returns (bool) { + function contains(AddressToUintMap storage map, address key) internal view returns (bool) { return _contains(map._inner, bytes32(key)); } /** * @dev Returns the number of elements in the map. O(1). */ - function length(AddressToUint storage map) internal view returns (uint256) { + function length(AddressToUintMap storage map) internal view returns (uint256) { return _length(map._inner); } @@ -291,7 +291,7 @@ library EnumerableMap { * * - `index` must be strictly less than {length}. */ - function at(AddressToUint storage map, uint256 index) internal view returns (address, uint256) { + function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) { (bytes32 key, bytes32 value) = _at(map._inner, index); return (address(uint160(key)), uint256(value)); } @@ -302,7 +302,7 @@ library EnumerableMap { * * _Available since v3.4._ */ - function tryGet(AddressToUint storage map, address key) internal view returns (bool, uint256) { + function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) { (bool success, bytes32 value) = _tryGet(map._inner, bytes32(key)); return (success, uint256(value)); } @@ -314,7 +314,7 @@ library EnumerableMap { * * - `key` must be in the map. */ - function get(AddressToUint storage map, address key) internal view returns (uint256) { + function get(AddressToUintMap storage map, address key) internal view returns (uint256) { return address(uint160(uint256(_get(map._inner, bytes32(key))))); } @@ -325,7 +325,7 @@ library EnumerableMap { * message unnecessarily. For custom revert reasons use {tryGet}. */ function get( - AddressToUint storage map, + AddressToUintMap storage map, address key, string memory errorMessage ) internal view returns (uint256) { From ff03e3a93739f8858026f93d59f824648c238a6e Mon Sep 17 00:00:00 2001 From: tynuk Date: Mon, 3 Jan 2022 17:49:03 +0200 Subject: [PATCH 3/3] fix type conv --- contracts/utils/structs/EnumerableMap.sol | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/contracts/utils/structs/EnumerableMap.sol b/contracts/utils/structs/EnumerableMap.sol index c96561cd6f5..909561550e9 100644 --- a/contracts/utils/structs/EnumerableMap.sol +++ b/contracts/utils/structs/EnumerableMap.sol @@ -256,7 +256,7 @@ library EnumerableMap { address key, uint256 value ) internal returns (bool) { - return _set(map._inner, bytes32(key), bytes32(value)); + return _set(map._inner, bytes32(uint256(uint160(key))), bytes32(value)); } /** @@ -265,14 +265,14 @@ library EnumerableMap { * Returns true if the key was removed from the map, that is if it was present. */ function remove(AddressToUintMap storage map, address key) internal returns (bool) { - return _remove(map._inner, bytes32(key)); + return _remove(map._inner, bytes32(uint256(uint160(key)))); } /** * @dev Returns true if the key is in the map. O(1). */ function contains(AddressToUintMap storage map, address key) internal view returns (bool) { - return _contains(map._inner, bytes32(key)); + return _contains(map._inner, bytes32(uint256(uint160(key)))); } /** @@ -293,7 +293,7 @@ library EnumerableMap { */ function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) { (bytes32 key, bytes32 value) = _at(map._inner, index); - return (address(uint160(key)), uint256(value)); + return (address(uint160(uint256(key))), uint256(value)); } /** @@ -303,7 +303,7 @@ library EnumerableMap { * _Available since v3.4._ */ function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) { - (bool success, bytes32 value) = _tryGet(map._inner, bytes32(key)); + (bool success, bytes32 value) = _tryGet(map._inner, bytes32(uint256(uint160(key)))); return (success, uint256(value)); } @@ -315,7 +315,7 @@ library EnumerableMap { * - `key` must be in the map. */ function get(AddressToUintMap storage map, address key) internal view returns (uint256) { - return address(uint160(uint256(_get(map._inner, bytes32(key))))); + return uint256(_get(map._inner, bytes32(uint256(uint160(key))))); } /** @@ -329,6 +329,6 @@ library EnumerableMap { address key, string memory errorMessage ) internal view returns (uint256) { - return uint256(_get(map._inner, bytes32(key), errorMessage)); + return uint256(_get(map._inner, bytes32(uint256(uint160(key))), errorMessage)); } }