From b3b9f30f0546bfe8b6b5930195097b1dc426bbc4 Mon Sep 17 00:00:00 2001 From: Shredder <110225819+EmperorOrokuSaki@users.noreply.github.com> Date: Fri, 24 Feb 2023 16:51:21 +0330 Subject: [PATCH] chore: remove counters library from the contract code. (#137) * chore: remove the counters lib from the contract and create private vars instead. * merge: develop. fix conflicts. * style: remove unnecessary white space --- contracts/contracts/FleekAccessControl.sol | 25 +++++++++++----------- contracts/contracts/FleekERC721.sol | 13 ++++++----- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/contracts/contracts/FleekAccessControl.sol b/contracts/contracts/FleekAccessControl.sol index 8b5cf4f..3638586 100644 --- a/contracts/contracts/FleekAccessControl.sol +++ b/contracts/contracts/FleekAccessControl.sol @@ -2,7 +2,6 @@ pragma solidity ^0.8.7; -import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; error MustHaveCollectionRole(uint8 role); @@ -11,7 +10,6 @@ error MustHaveAtLeastOneOwner(); error RoleAlreadySet(); contract FleekAccessControl is Initializable { - using Counters for Counters.Counter; /** * @dev All available collection roles. @@ -57,7 +55,7 @@ contract FleekAccessControl is Initializable { * @dev _collectionRolesCounter[role] is the number of addresses that have the role. * This is prevent Owner role to go to 0. */ - mapping(CollectionRoles => Counters.Counter) private _collectionRolesCounter; + mapping(CollectionRoles => uint256) private _collectionRolesCounter; /** * @dev _collectionRoles[role][address] is the mapping of addresses that have the role. @@ -69,7 +67,7 @@ contract FleekAccessControl is Initializable { * The version is incremented every time the token roles are cleared. * Should be incremented every token transfer. */ - mapping(uint256 => Counters.Counter) private _tokenRolesVersion; + mapping(uint256 => uint256) private _tokenRolesVersion; /** * @dev _tokenRoles[tokenId][version][role][address] is the mapping of addresses that have the role. @@ -108,7 +106,7 @@ contract FleekAccessControl is Initializable { * @dev Returns `True` if a certain address has the token role. */ function hasTokenRole(uint256 tokenId, TokenRoles role, address account) public view returns (bool) { - uint256 currentVersion = _tokenRolesVersion[tokenId].current(); + uint256 currentVersion = _tokenRolesVersion[tokenId]; return _tokenRoles[tokenId][currentVersion][role][account]; } @@ -119,7 +117,7 @@ contract FleekAccessControl is Initializable { if (hasCollectionRole(role, account)) revert RoleAlreadySet(); _collectionRoles[role][account] = true; - _collectionRolesCounter[role].increment(); + _collectionRolesCounter[role] += 1; emit CollectionRoleChanged(role, account, true, msg.sender); } @@ -129,11 +127,11 @@ contract FleekAccessControl is Initializable { */ function _revokeCollectionRole(CollectionRoles role, address account) internal { if (!hasCollectionRole(role, account)) revert RoleAlreadySet(); - if (role == CollectionRoles.Owner && _collectionRolesCounter[role].current() == 1) + if (role == CollectionRoles.Owner && _collectionRolesCounter[role] == 1) revert MustHaveAtLeastOneOwner(); _collectionRoles[role][account] = false; - _collectionRolesCounter[role].decrement(); + _collectionRolesCounter[role] -= 1; emit CollectionRoleChanged(role, account, false, msg.sender); } @@ -144,7 +142,7 @@ contract FleekAccessControl is Initializable { function _grantTokenRole(uint256 tokenId, TokenRoles role, address account) internal { if (hasTokenRole(tokenId, role, account)) revert RoleAlreadySet(); - uint256 currentVersion = _tokenRolesVersion[tokenId].current(); + uint256 currentVersion = _tokenRolesVersion[tokenId]; _tokenRoles[tokenId][currentVersion][role][account] = true; emit TokenRoleChanged(tokenId, role, account, true, msg.sender); @@ -156,18 +154,19 @@ contract FleekAccessControl is Initializable { function _revokeTokenRole(uint256 tokenId, TokenRoles role, address account) internal { if (!hasTokenRole(tokenId, role, account)) revert RoleAlreadySet(); - uint256 currentVersion = _tokenRolesVersion[tokenId].current(); + uint256 currentVersion = _tokenRolesVersion[tokenId]; _tokenRoles[tokenId][currentVersion][role][account] = false; emit TokenRoleChanged(tokenId, role, account, false, msg.sender); } /** - * @dev Clears all token roles for a certain tokenId and grants the owner role to a new address. - * Should only be used for transferring tokens. + * @dev This empty reserved space is put in place to allow future versions to add new + * variables without shifting down storage in the inheritance chain. + * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps */ function _clearTokenRoles(uint256 tokenId) internal { - _tokenRolesVersion[tokenId].increment(); + _tokenRolesVersion[tokenId] += 1; emit TokenRolesCleared(tokenId, msg.sender); } diff --git a/contracts/contracts/FleekERC721.sol b/contracts/contracts/FleekERC721.sol index 1561a47..aacef29 100644 --- a/contracts/contracts/FleekERC721.sol +++ b/contracts/contracts/FleekERC721.sol @@ -3,7 +3,6 @@ pragma solidity ^0.8.7; import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol"; -import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts/utils/Base64.sol"; import "@openzeppelin/contracts/utils/Strings.sol"; import "./FleekAccessControl.sol"; @@ -15,7 +14,6 @@ error ThereIsNoTokenMinted(); contract FleekERC721 is Initializable, ERC721Upgradeable, FleekAccessControl, FleekPausable { using Strings for uint256; - using Counters for Counters.Counter; using FleekStrings for FleekERC721.App; using FleekStrings for FleekERC721.AccessPoint; using FleekStrings for string; @@ -116,7 +114,7 @@ contract FleekERC721 is Initializable, ERC721Upgradeable, FleekAccessControl, Fl AccessPointCreationStatus status; } - Counters.Counter private _appIds; + uint256 private _appIds; mapping(uint256 => App) private _apps; mapping(string => AccessPoint) private _accessPoints; @@ -126,6 +124,7 @@ contract FleekERC721 is Initializable, ERC721Upgradeable, FleekAccessControl, Fl function initialize(string memory _name, string memory _symbol) public initializer { __ERC721_init(_name, _symbol); __FleekAccessControl_init(); + _appIds = 0; __FleekPausable_init(); } @@ -160,9 +159,10 @@ contract FleekERC721 is Initializable, ERC721Upgradeable, FleekAccessControl, Fl uint24 color, bool accessPointAutoApproval ) public payable requireCollectionRole(CollectionRoles.Owner) returns (uint256) { - uint256 tokenId = _appIds.current(); + uint256 tokenId = _appIds; _mint(to, tokenId); - _appIds.increment(); + + _appIds += 1; App storage app = _apps[tokenId]; app.name = name; @@ -176,7 +176,6 @@ contract FleekERC721 is Initializable, ERC721Upgradeable, FleekAccessControl, Fl // The mint interaction is considered to be the first build of the site. Updates from now on all increment the currentBuild by one and update the mapping. app.currentBuild = 0; app.builds[0] = Build(commitHash, gitRepository); - emit NewMint( tokenId, name, @@ -239,7 +238,7 @@ contract FleekERC721 is Initializable, ERC721Upgradeable, FleekAccessControl, Fl * @dev Returns the last minted tokenId. */ function getLastTokenId() public view virtual returns (uint256) { - uint256 current = _appIds.current(); + uint256 current = _appIds; if (current == 0) revert ThereIsNoTokenMinted(); return current - 1; }