Browse Source

Use named arguments in mapping types (#4433)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Ernesto García 2 years ago
parent
commit
cb0ffefe2f
29 changed files with 48 additions and 60 deletions
  1. 5 5
      contracts/access/AccessControl.sol
  2. 1 1
      contracts/access/extensions/AccessControlEnumerable.sol
  3. 1 1
      contracts/finance/VestingWallet.sol
  4. 1 1
      contracts/governance/Governor.sol
  5. 1 1
      contracts/governance/TimelockController.sol
  6. 2 2
      contracts/governance/extensions/GovernorCountingSimple.sol
  7. 1 1
      contracts/governance/extensions/GovernorPreventLateQuorum.sol
  8. 1 1
      contracts/governance/extensions/GovernorStorage.sol
  9. 1 1
      contracts/governance/extensions/GovernorTimelockControl.sol
  10. 4 4
      contracts/governance/utils/Votes.sol
  11. 1 1
      contracts/mocks/ERC165/ERC165InterfacesSupported.sol
  12. 2 2
      contracts/mocks/StorageSlotMock.sol
  13. 1 1
      contracts/mocks/VotesMock.sol
  14. 4 4
      contracts/mocks/token/ERC20VotesLegacyMock.sol
  15. 2 4
      contracts/token/ERC1155/ERC1155.sol
  16. 1 1
      contracts/token/ERC1155/extensions/ERC1155Supply.sol
  17. 1 1
      contracts/token/ERC1155/extensions/ERC1155URIStorage.sol
  18. 2 2
      contracts/token/ERC20/ERC20.sol
  19. 4 8
      contracts/token/ERC721/ERC721.sol
  20. 3 9
      contracts/token/ERC721/extensions/ERC721Enumerable.sol
  21. 1 1
      contracts/token/ERC721/extensions/ERC721URIStorage.sol
  22. 1 1
      contracts/token/common/ERC2981.sol
  23. 1 1
      contracts/utils/Nonces.sol
  24. 1 1
      contracts/utils/structs/BitMaps.sol
  25. 1 1
      contracts/utils/structs/DoubleEndedQueue.sol
  26. 1 1
      contracts/utils/structs/EnumerableMap.sol
  27. 1 1
      contracts/utils/structs/EnumerableSet.sol
  28. 1 1
      scripts/generate/templates/EnumerableMap.js
  29. 1 1
      scripts/generate/templates/EnumerableSet.js

+ 5 - 5
contracts/access/AccessControl.sol

@@ -48,11 +48,11 @@ import {ERC165} from "../utils/introspection/ERC165.sol";
  */
 abstract contract AccessControl is Context, IAccessControl, ERC165 {
     struct RoleData {
-        mapping(address => bool) members;
+        mapping(address account => bool) hasRole;
         bytes32 adminRole;
     }
 
-    mapping(bytes32 => RoleData) private _roles;
+    mapping(bytes32 role => RoleData) private _roles;
 
     bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
 
@@ -76,7 +76,7 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
      * @dev Returns `true` if `account` has been granted `role`.
      */
     function hasRole(bytes32 role, address account) public view virtual returns (bool) {
-        return _roles[role].members[account];
+        return _roles[role].hasRole[account];
     }
 
     /**
@@ -182,7 +182,7 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
      */
     function _grantRole(bytes32 role, address account) internal virtual returns (bool) {
         if (!hasRole(role, account)) {
-            _roles[role].members[account] = true;
+            _roles[role].hasRole[account] = true;
             emit RoleGranted(role, account, _msgSender());
             return true;
         } else {
@@ -199,7 +199,7 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
      */
     function _revokeRole(bytes32 role, address account) internal virtual returns (bool) {
         if (hasRole(role, account)) {
-            _roles[role].members[account] = false;
+            _roles[role].hasRole[account] = false;
             emit RoleRevoked(role, account, _msgSender());
             return true;
         } else {

+ 1 - 1
contracts/access/extensions/AccessControlEnumerable.sol

@@ -13,7 +13,7 @@ import {EnumerableSet} from "../../utils/structs/EnumerableSet.sol";
 abstract contract AccessControlEnumerable is IAccessControlEnumerable, AccessControl {
     using EnumerableSet for EnumerableSet.AddressSet;
 
-    mapping(bytes32 => EnumerableSet.AddressSet) private _roleMembers;
+    mapping(bytes32 role => EnumerableSet.AddressSet) private _roleMembers;
 
     /**
      * @dev See {IERC165-supportsInterface}.

+ 1 - 1
contracts/finance/VestingWallet.sol

@@ -37,7 +37,7 @@ contract VestingWallet is Context, Ownable {
     error VestingWalletInvalidBeneficiary(address beneficiary);
 
     uint256 private _released;
-    mapping(address => uint256) private _erc20Released;
+    mapping(address token => uint256) private _erc20Released;
     uint64 private immutable _start;
     uint64 private immutable _duration;
 

+ 1 - 1
contracts/governance/Governor.sol

@@ -46,7 +46,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
     bytes32 private constant _ALL_PROPOSAL_STATES_BITMAP = bytes32((2 ** (uint8(type(ProposalState).max) + 1)) - 1);
     string private _name;
 
-    mapping(uint256 => ProposalCore) private _proposals;
+    mapping(uint256 proposalId => ProposalCore) private _proposals;
 
     // This queue keeps track of the governor operating on itself. Calls to functions protected by the
     // {onlyGovernance} modifier needs to be whitelisted in this queue. Whitelisting is set in {_beforeExecute},

+ 1 - 1
contracts/governance/TimelockController.sol

@@ -27,7 +27,7 @@ contract TimelockController is AccessControl, ERC721Holder, ERC1155Holder {
     bytes32 public constant CANCELLER_ROLE = keccak256("CANCELLER_ROLE");
     uint256 internal constant _DONE_TIMESTAMP = uint256(1);
 
-    mapping(bytes32 => uint256) private _timestamps;
+    mapping(bytes32 id => uint256) private _timestamps;
     uint256 private _minDelay;
 
     enum OperationState {

+ 2 - 2
contracts/governance/extensions/GovernorCountingSimple.sol

@@ -22,10 +22,10 @@ abstract contract GovernorCountingSimple is Governor {
         uint256 againstVotes;
         uint256 forVotes;
         uint256 abstainVotes;
-        mapping(address => bool) hasVoted;
+        mapping(address voter => bool) hasVoted;
     }
 
-    mapping(uint256 => ProposalVote) private _proposalVotes;
+    mapping(uint256 proposalId => ProposalVote) private _proposalVotes;
 
     /**
      * @dev See {IGovernor-COUNTING_MODE}.

+ 1 - 1
contracts/governance/extensions/GovernorPreventLateQuorum.sol

@@ -18,7 +18,7 @@ import {Math} from "../../utils/math/Math.sol";
 abstract contract GovernorPreventLateQuorum is Governor {
     uint48 private _voteExtension;
 
-    mapping(uint256 => uint48) private _extendedDeadlines;
+    mapping(uint256 proposalId => uint48) private _extendedDeadlines;
 
     /// @dev Emitted when a proposal deadline is pushed back due to reaching quorum late in its voting period.
     event ProposalExtended(uint256 indexed proposalId, uint64 extendedDeadline);

+ 1 - 1
contracts/governance/extensions/GovernorStorage.sol

@@ -21,7 +21,7 @@ abstract contract GovernorStorage is Governor {
     }
 
     uint256[] private _proposalIds;
-    mapping(uint256 => ProposalDetails) private _proposalDetails;
+    mapping(uint256 proposalId => ProposalDetails) private _proposalDetails;
 
     /**
      * @dev Hook into the proposing mechanism

+ 1 - 1
contracts/governance/extensions/GovernorTimelockControl.sol

@@ -24,7 +24,7 @@ import {SafeCast} from "../../utils/math/SafeCast.sol";
  */
 abstract contract GovernorTimelockControl is Governor {
     TimelockController private _timelock;
-    mapping(uint256 => bytes32) private _timelockIds;
+    mapping(uint256 proposalId => bytes32) private _timelockIds;
 
     /**
      * @dev Emitted when the timelock controller used for proposal execution is modified.

+ 4 - 4
contracts/governance/utils/Votes.sol

@@ -34,9 +34,9 @@ abstract contract Votes is Context, EIP712, Nonces, IERC5805 {
     bytes32 private constant _DELEGATION_TYPEHASH =
         keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");
 
-    mapping(address => address) private _delegation;
+    mapping(address account => address) private _delegatee;
 
-    mapping(address => Checkpoints.Trace224) private _delegateCheckpoints;
+    mapping(address delegatee => Checkpoints.Trace224) private _delegateCheckpoints;
 
     Checkpoints.Trace224 private _totalCheckpoints;
 
@@ -124,7 +124,7 @@ abstract contract Votes is Context, EIP712, Nonces, IERC5805 {
      * @dev Returns the delegate that `account` has chosen.
      */
     function delegates(address account) public view virtual returns (address) {
-        return _delegation[account];
+        return _delegatee[account];
     }
 
     /**
@@ -166,7 +166,7 @@ abstract contract Votes is Context, EIP712, Nonces, IERC5805 {
      */
     function _delegate(address account, address delegatee) internal virtual {
         address oldDelegate = delegates(account);
-        _delegation[account] = delegatee;
+        _delegatee[account] = delegatee;
 
         emit DelegateChanged(account, oldDelegate, delegatee);
         _moveDelegateVotes(oldDelegate, delegatee, _getVotingUnits(account));

+ 1 - 1
contracts/mocks/ERC165/ERC165InterfacesSupported.sol

@@ -23,7 +23,7 @@ contract SupportsInterfaceWithLookupMock is IERC165 {
     /**
      * @dev A mapping of interface id to whether or not it's supported.
      */
-    mapping(bytes4 => bool) private _supportedInterfaces;
+    mapping(bytes4 interfaceId => bool) private _supportedInterfaces;
 
     /**
      * @dev A contract implementing SupportsInterfaceWithLookup

+ 2 - 2
contracts/mocks/StorageSlotMock.sol

@@ -39,7 +39,7 @@ contract StorageSlotMock {
         return slot.getUint256Slot().value;
     }
 
-    mapping(uint256 => string) public stringMap;
+    mapping(uint256 key => string) public stringMap;
 
     function setString(bytes32 slot, string calldata value) public {
         slot.getStringSlot().value = value;
@@ -57,7 +57,7 @@ contract StorageSlotMock {
         return stringMap[key].getStringSlot().value;
     }
 
-    mapping(uint256 => bytes) public bytesMap;
+    mapping(uint256 key => bytes) public bytesMap;
 
     function setBytes(bytes32 slot, bytes calldata value) public {
         slot.getBytesSlot().value = value;

+ 1 - 1
contracts/mocks/VotesMock.sol

@@ -5,7 +5,7 @@ pragma solidity ^0.8.20;
 import {Votes} from "../governance/utils/Votes.sol";
 
 abstract contract VotesMock is Votes {
-    mapping(address => uint256) private _votingUnits;
+    mapping(address voter => uint256) private _votingUnits;
 
     function getTotalSupply() public view returns (uint256) {
         return _getTotalSupply();

+ 4 - 4
contracts/mocks/token/ERC20VotesLegacyMock.sol

@@ -20,8 +20,8 @@ abstract contract ERC20VotesLegacyMock is IVotes, ERC20Permit {
     bytes32 private constant _DELEGATION_TYPEHASH =
         keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");
 
-    mapping(address => address) private _delegates;
-    mapping(address => Checkpoint[]) private _checkpoints;
+    mapping(address account => address) private _delegatee;
+    mapping(address delegatee => Checkpoint[]) private _checkpoints;
     Checkpoint[] private _totalSupplyCheckpoints;
 
     /**
@@ -42,7 +42,7 @@ abstract contract ERC20VotesLegacyMock is IVotes, ERC20Permit {
      * @dev Get the address `account` is currently delegating to.
      */
     function delegates(address account) public view virtual returns (address) {
-        return _delegates[account];
+        return _delegatee[account];
     }
 
     /**
@@ -188,7 +188,7 @@ abstract contract ERC20VotesLegacyMock is IVotes, ERC20Permit {
     function _delegate(address delegator, address delegatee) internal virtual {
         address currentDelegate = delegates(delegator);
         uint256 delegatorBalance = balanceOf(delegator);
-        _delegates[delegator] = delegatee;
+        _delegatee[delegator] = delegatee;
 
         emit DelegateChanged(delegator, currentDelegate, delegatee);
 

+ 2 - 4
contracts/token/ERC1155/ERC1155.sol

@@ -20,11 +20,9 @@ abstract contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI, IER
     using Arrays for uint256[];
     using Arrays for address[];
 
-    // Mapping from token ID to account balances
-    mapping(uint256 => mapping(address => uint256)) private _balances;
+    mapping(uint256 id => mapping(address account => uint256)) private _balances;
 
-    // Mapping from account to operator approvals
-    mapping(address => mapping(address => bool)) private _operatorApprovals;
+    mapping(address account => mapping(address operator => bool)) private _operatorApprovals;
 
     // Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
     string private _uri;

+ 1 - 1
contracts/token/ERC1155/extensions/ERC1155Supply.sol

@@ -19,7 +19,7 @@ import {ERC1155} from "../ERC1155.sol";
  * CAUTION: This extension should not be added in an upgrade to an already deployed contract.
  */
 abstract contract ERC1155Supply is ERC1155 {
-    mapping(uint256 => uint256) private _totalSupply;
+    mapping(uint256 id => uint256) private _totalSupply;
     uint256 private _totalSupplyAll;
 
     /**

+ 1 - 1
contracts/token/ERC1155/extensions/ERC1155URIStorage.sol

@@ -17,7 +17,7 @@ abstract contract ERC1155URIStorage is ERC1155 {
     string private _baseURI = "";
 
     // Optional mapping for token URIs
-    mapping(uint256 => string) private _tokenURIs;
+    mapping(uint256 tokenId => string) private _tokenURIs;
 
     /**
      * @dev See {IERC1155MetadataURI-uri}.

+ 2 - 2
contracts/token/ERC20/ERC20.sol

@@ -36,9 +36,9 @@ import {IERC20Errors} from "../../interfaces/draft-IERC6093.sol";
  * allowances. See {IERC20-approve}.
  */
 abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
-    mapping(address => uint256) private _balances;
+    mapping(address account => uint256) private _balances;
 
-    mapping(address => mapping(address => uint256)) private _allowances;
+    mapping(address account => mapping(address spender => uint256)) private _allowances;
 
     uint256 private _totalSupply;
 

+ 4 - 8
contracts/token/ERC721/ERC721.sol

@@ -25,17 +25,13 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
     // Token symbol
     string private _symbol;
 
-    // Mapping from token ID to owner address
-    mapping(uint256 => address) private _owners;
+    mapping(uint256 tokenId => address) private _owners;
 
-    // Mapping owner address to token count
-    mapping(address => uint256) private _balances;
+    mapping(address owner => uint256) private _balances;
 
-    // Mapping from token ID to approved address
-    mapping(uint256 => address) private _tokenApprovals;
+    mapping(uint256 tokenId => address) private _tokenApprovals;
 
-    // Mapping from owner to operator approvals
-    mapping(address => mapping(address => bool)) private _operatorApprovals;
+    mapping(address owner => mapping(address operator => bool)) private _operatorApprovals;
 
     /**
      * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.

+ 3 - 9
contracts/token/ERC721/extensions/ERC721Enumerable.sol

@@ -15,17 +15,11 @@ import {IERC165} from "../../../utils/introspection/ERC165.sol";
  * interfere with enumerability and should not be used together with `ERC721Enumerable`.
  */
 abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
-    // Mapping from owner to list of owned token IDs
-    mapping(address => mapping(uint256 => uint256)) private _ownedTokens;
+    mapping(address owner => mapping(uint256 index => uint256)) private _ownedTokens;
+    mapping(uint256 tokenId => uint256) private _ownedTokensIndex;
 
-    // Mapping from token ID to index of the owner tokens list
-    mapping(uint256 => uint256) private _ownedTokensIndex;
-
-    // Array with all token ids, used for enumeration
     uint256[] private _allTokens;
-
-    // Mapping from token id to position in the allTokens array
-    mapping(uint256 => uint256) private _allTokensIndex;
+    mapping(uint256 tokenId => uint256) private _allTokensIndex;
 
     /**
      * @dev An `owner`'s token query was out of bounds for `index`.

+ 1 - 1
contracts/token/ERC721/extensions/ERC721URIStorage.sol

@@ -15,7 +15,7 @@ abstract contract ERC721URIStorage is IERC4906, ERC721 {
     using Strings for uint256;
 
     // Optional mapping for token URIs
-    mapping(uint256 => string) private _tokenURIs;
+    mapping(uint256 tokenId => string) private _tokenURIs;
 
     /**
      * @dev See {IERC165-supportsInterface}

+ 1 - 1
contracts/token/common/ERC2981.sol

@@ -26,7 +26,7 @@ abstract contract ERC2981 is IERC2981, ERC165 {
     }
 
     RoyaltyInfo private _defaultRoyaltyInfo;
-    mapping(uint256 => RoyaltyInfo) private _tokenRoyaltyInfo;
+    mapping(uint256 tokenId => RoyaltyInfo) private _tokenRoyaltyInfo;
 
     /**
      * @dev The default royalty set is invalid (eg. (numerator / denominator) >= 1).

+ 1 - 1
contracts/utils/Nonces.sol

@@ -10,7 +10,7 @@ abstract contract Nonces {
      */
     error InvalidAccountNonce(address account, uint256 currentNonce);
 
-    mapping(address => uint256) private _nonces;
+    mapping(address account => uint256) private _nonces;
 
     /**
      * @dev Returns an the next unused nonce for an address.

+ 1 - 1
contracts/utils/structs/BitMaps.sol

@@ -17,7 +17,7 @@ pragma solidity ^0.8.20;
  */
 library BitMaps {
     struct BitMap {
-        mapping(uint256 => uint256) _data;
+        mapping(uint256 bucket => uint256) _data;
     }
 
     /**

+ 1 - 1
contracts/utils/structs/DoubleEndedQueue.sol

@@ -45,7 +45,7 @@ library DoubleEndedQueue {
     struct Bytes32Deque {
         uint128 _begin;
         uint128 _end;
-        mapping(uint128 => bytes32) _data;
+        mapping(uint128 index => bytes32) _data;
     }
 
     /**

+ 1 - 1
contracts/utils/structs/EnumerableMap.sol

@@ -65,7 +65,7 @@ library EnumerableMap {
     struct Bytes32ToBytes32Map {
         // Storage of keys
         EnumerableSet.Bytes32Set _keys;
-        mapping(bytes32 => bytes32) _values;
+        mapping(bytes32 key => bytes32) _values;
     }
 
     /**

+ 1 - 1
contracts/utils/structs/EnumerableSet.sol

@@ -53,7 +53,7 @@ library EnumerableSet {
         bytes32[] _values;
         // Position of the value in the `values` array, plus 1 because index 0
         // means a value is not in the set.
-        mapping(bytes32 => uint256) _indexes;
+        mapping(bytes32 value => uint256) _indexes;
     }
 
     /**

+ 1 - 1
scripts/generate/templates/EnumerableMap.js

@@ -74,7 +74,7 @@ error EnumerableMapNonexistentKey(bytes32 key);
 struct Bytes32ToBytes32Map {
     // Storage of keys
     EnumerableSet.Bytes32Set _keys;
-    mapping(bytes32 => bytes32) _values;
+    mapping(bytes32 key => bytes32) _values;
 }
 
 /**

+ 1 - 1
scripts/generate/templates/EnumerableSet.js

@@ -63,7 +63,7 @@ struct Set {
     bytes32[] _values;
     // Position of the value in the \`values\` array, plus 1 because index 0
     // means a value is not in the set.
-    mapping(bytes32 => uint256) _indexes;
+    mapping(bytes32 value => uint256) _indexes;
 }
 
 /**