Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config property to Hat struct #65

Merged
merged 2 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions src/Hats.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,22 @@ contract Hats is ERC1155, HatsIdUtilities {

struct Hat {
// 1st storage slot
address eligibility; // can revoke Hat based on ruling; 20 bytes (+20)
uint32 maxSupply; // the max number of identical hats that can exist; 24 bytes (+4)
bool active; // can be altered by toggle, via setHatStatus(); 25 bytes (+1)
uint8 lastHatId; // indexes how many different hats an admin is holding; 26 bytes (+1)
// 2nd storage slot
address toggle; // controls when Hat is active; 20 bytes (+20)
// 3rd+ storage slot
address eligibility; // ─┐ can revoke Hat based on ruling | 20
uint32 maxSupply; // the max number of identical hats that can exist | 4
uint8 lastHatId; // ─┘ indexes how many different hats an admin is holding | 1
// 2nd slot
address toggle; // ─┐ controls when Hat is active | 20
uint96 config; // ─┘ active status & other settings (see schema below) | 12
// 3rd+ slot (optional)
string details;
// optional
string imageURI;
}

/* Hat.config schema (by bit)
* 0th bit | `active` status; can be altered by toggle, via setHatStatus()
* 1 - 95 | unassigned
*/

/*//////////////////////////////////////////////////////////////
HATS STORAGE
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -443,7 +447,7 @@ contract Hats is ERC1155, HatsIdUtilities {

hat.toggle = _toggle;
hat.imageURI = _imageURI;
hat.active = true;
hat.config = uint96(1 << 95);

_hats[_id] = hat;

Expand All @@ -465,8 +469,8 @@ contract Hats is ERC1155, HatsIdUtilities {
// optimize later
Hat storage hat = _hats[_hatId];

if (_newStatus != hat.active) {
hat.active = _newStatus;
if (_newStatus != _getHatStatus(hat)) {
_setHatStatus(hat, _newStatus);
emit HatStatusChanged(_hatId, _newStatus);
updated = true;
}
Expand Down Expand Up @@ -626,7 +630,7 @@ contract Hats is ERC1155, HatsIdUtilities {
if (success && returndata.length > 0) {
active = abi.decode(returndata, (bool));
} else {
active = _hat.active;
active = _getHatStatus(_hat);
}
}

Expand All @@ -639,6 +643,19 @@ contract Hats is ERC1155, HatsIdUtilities {
return _isActive(hat, _hatId);
}

function _getHatStatus(Hat memory _hat) internal view returns (bool) {
return (_hat.config >> 95 != 0);
}

function _setHatStatus(Hat storage _hat, bool _status) internal {
if (_status) {
_hat.config |= uint96(1 << 95);
} else {
_hat.config &= ~uint96(1 << 95);
}

}

/// @notice Checks whether a wearer of a Hat is in good standing
/// @dev Public function for use when passing a Hat object is not possible or preferable
/// @param _wearer The address of the Hat wearer
Expand Down