Hats.sol
Hats
Inherits: IHats, ERC1155, Multicallable, HatsIdUtilities
Author: Haberdasher Labs
Hats are DAO-native, revocable, and programmable roles that are represented as non-transferable ERC-1155-similar tokens for composability
This is a multi-tenant contract that can manage all hats for a given chain. While it fully implements the ERC1155 interface, it does not fully comply with the ERC1155 standard.
State Variables
name
The name of the contract, typically including the version
lastTopHatId
The first 4 bytes of the id of the last tophat created.
baseImageURI
The fallback image URI for hat tokens with no imageURI
specified in their branch
_hats
Internal mapping of hats to hat ids. See HatsIdUtilities.sol for more info on how hat ids work
badStandings
Mapping of wearers in bad standing for certain hats
Used by external contracts to trigger penalties for wearers in bad standing hatId => wearer => !standing
Functions
constructor
All arguments are immutable; they can only be set once during construction
Parameters
mintTopHat
Creates and mints a Hat that is its own admin, i.e. a "topHat"
A topHat has no eligibility and no toggle
Parameters
Returns
createHat
Creates a new hat. The msg.sender must wear the _admin
hat.
Initializes a new Hat struct, but does not mint any tokens.
Parameters
Returns
batchCreateHats
Creates new hats in batch. The msg.sender must be an admin of each hat.
This is a convenience function that loops through the arrays and calls createHat
.
Parameters
Returns
getNextId
Gets the id of the next child hat of the hat _admin
Does not incrememnt lastHatId
Parameters
Returns
mintHat
Mints an ERC1155-similar token of the Hat to an eligible recipient, who then "wears" the hat
The msg.sender must wear an admin Hat of _hatId
, and the recipient must be eligible to wear _hatId
Parameters
Returns
batchMintHats
Mints new hats in batch. The msg.sender must be an admin of each hat.
This is a convenience function that loops through the arrays and calls mintHat
.
Parameters
Returns
setHatStatus
Toggles a Hat's status from active to deactive, or vice versa
The msg.sender must be set as the hat's toggle
Parameters
Returns
checkHatStatus
Checks a hat's toggle module and processes the returned status
May change the hat's status in storage
Parameters
Returns
_pullHatStatus
setHatWearerStatus
Report from a hat's eligibility on the status of one of its wearers and, if false
, revoke their hat
Burns the wearer's hat, if revoked
Parameters
Returns
checkHatWearerStatus
Check a hat's eligibility for a report on the status of one of the hat's wearers and, if false
, revoke their hat
Burns the wearer's hat, if revoked
Parameters
Returns
renounceHat
Stop wearing a hat, aka "renounce" it
Burns the msg.sender's hat
Parameters
_createHat
Internal call for creating a new hat
Initializes a new Hat in storage, but does not mint any tokens
Parameters
_processHatStatus
Internal function to process hat status
Updates a hat's status if different from current
Parameters
Returns
_processHatWearerStatus
Internal call to process wearer status from the eligibility module
Burns the wearer's Hat token if _eligible is false, and updates badStandings state if necessary
Parameters
Returns
_setHatStatus
Internal function to set a hat's status in storage
Flips the 0th bit of _hat.config via bitwise operation
Parameters
_staticBalanceOf
Internal function to retrieve an account's internal "static" balance directly from internal storage,
This function bypasses the dynamic _isActive
and _isEligible
checks
Parameters
Returns
_checkAdmin
Checks whether msg.sender is an admin of a hat, and reverts if not
_checkAdminOrWearer
checks whether the msg.sender is either an admin or wearer or a hat, and reverts the appropriate error if not
transferHat
Transfers a hat from one wearer to another eligible wearer
The hat must be mutable, and the transfer must be initiated by an admin
Parameters
makeHatImmutable
Set a mutable hat to immutable
Sets the second bit of hat.config to 0
Parameters
changeHatDetails
Change a hat's details
Hat must be mutable, except for tophats.
Parameters
changeHatEligibility
Change a hat's details
Hat must be mutable
Parameters
changeHatToggle
Change a hat's details
Hat must be mutable
Parameters
changeHatImageURI
Change a hat's details
Hat must be mutable, except for tophats
Parameters
changeHatMaxSupply
Change a hat's details
Hat must be mutable; new max supply cannot be less than current supply
Parameters
requestLinkTopHatToTree
Submits a request to link a Hat Tree under a parent tree. Requests can be submitted by either... a) the wearer of a topHat, previous to any linkage, or b) the admin(s) of an already-linked topHat (aka tree root), where such a request is to move the tree root to another admin within the same parent tree
A topHat can have at most 1 request at a time. Submitting a new request will replace the existing request.
Parameters
approveLinkTopHatToTree
Approve a request to link a Tree under a parent tree, with options to add eligibility or toggle modules and change its metadata
Requests can only be approved by wearer or an admin of the _newAdminHat
, and there can only be one link per tree root at a given time.
Parameters
unlinkTopHatFromTree
Unlink a Tree from the parent tree
*This can only be called by an admin of the tree root. Fails if the topHat to unlink has no non-zero wearer, which can occur if...
It's wearer is in badStanding
It has been revoked from its wearer (and possibly burned)ห
It is not active (ie toggled off)*
Parameters
relinkTopHatWithinTree
Move a tree root to a different position within the same parent tree, without a request. Valid destinations include within the same local tree as the origin, or to the local tree of the tippyTopHat. TippyTopHat wearers can bypass this restriction to relink to anywhere in its full tree.
Caller must be both an admin tree root and admin or wearer of _newAdminHat
.
Parameters
_linkTopHatToTree
Internal function to link a Tree under a parent Tree, with protection against circular linkages and relinking to a separate Tree, with options to add eligibility or toggle modules and change its metadata
Linking _topHatDomain
replaces any existing links
Parameters
viewHat
View the properties of a given Hat
Parameters
Returns
isWearerOfHat
Checks whether a given address wears a given Hat
Convenience function that wraps balanceOf
Parameters
Returns
isAdminOfHat
Checks whether a given address serves as the admin of a given Hat
Recursively checks if _user
wears the admin Hat of the Hat in question. This is recursive since there may be a string of Hats as admins of Hats.
Parameters
Returns
_isActive
Checks the active status of a hat
For internal use instead of isActive
when passing Hat as param is preferable
Parameters
Returns
isActive
Checks the active status of a hat
Parameters
Returns
_getHatStatus
Internal function to retrieve a hat's status from storage
reads the 0th bit of the hat's config
Parameters
Returns
_isMutable
Internal function to retrieve a hat's mutability setting
reads the 1st bit of the hat's config
Parameters
Returns
isInGoodStanding
Checks whether a wearer of a Hat is in good standing
Parameters
Returns
_isEligible
Internal call to check whether an address is eligible for a given Hat
Tries an external call to the Hat's eligibility module, defaulting to existing badStandings state if the call fails (ie if the eligibility module address does not conform to the IHatsEligibility interface)
Parameters
Returns
isEligible
Checks whether an address is eligible for a given Hat
Public function for use when passing a Hat object is not possible or preferable
Parameters
Returns
hatSupply
Gets the current supply of a Hat
Only tracks explicit burns and mints, not dynamic revocations
Parameters
Returns
getHatEligibilityModule
Gets the eligibility module for a hat
Parameters
Returns
getHatToggleModule
Gets the toggle module for a hat
Parameters
Returns
getHatMaxSupply
Gets the max supply for a hat
Parameters
Returns
getImageURIForHat
Gets the imageURI for a given hat
If this hat does not have an imageURI set, recursively get the imageURI from its admin
Parameters
Returns
_constructURI
Constructs the URI for a Hat, using data from the Hat struct
Parameters
Returns
balanceOf
Gets the Hat token balance of a user for a given Hat
Balance is dynamic based on the hat's status and wearer's eligibility, so off-chain balance data indexed from events may not be in sync
Parameters
Returns
_mintHat
Internal call to mint a Hat token to a wearer
Unsafe if called when _wearer
has a non-zero balance of _hatId
Parameters
_burnHat
Internal call to burn a wearer's Hat token
Unsafe if called when _wearer
doesn't have a zero balance of _hatId
Parameters
setApprovalForAll
Approvals are not necessary for Hats since transfers are not handled by the wearer
Admins should use transferHat()
to transfer
safeTransferFrom
Safe transfers are not necessary for Hats since transfers are not handled by the wearer
Admins should use transferHat()
to transfer
safeBatchTransferFrom
Safe transfers are not necessary for Hats since transfers are not handled by the wearer
supportsInterface
ERC165 interface detection
While Hats Protocol conforms to the ERC1155 interface, it does not fully conform to the ERC1155 specification since it does not implement the ERC1155Receiver functionality. For this reason, this function overrides the ERC1155 implementation to return false for ERC1155.
Parameters
Returns
balanceOfBatch
Batch retrieval for wearer balances
Given the higher gas overhead of Hats balanceOf checks, large batches may be high cost or run into gas limits
Parameters
uri
View the uri for a Hat
Parameters
Returns
Structs
Hat
This contract's version is labeled v1. Previous versions labeled similarly as v1 and v1.0 are deprecated, and should be treated as beta deployments.
A Hat object containing the hat's properties
The members are packed to minimize storage costs
Last updated