Hats Protocol Docs
hatsprotocol.xyzGithub
  • ๐Ÿ‘‹Welcome to Hats Protocol
  • ๐ŸงขGetting Started with Hats
  • โญQuick Start
  • Using The Hats App
    • ๐Ÿค Essentials For Hat Wearers
    • ๐ŸŽฉCreating My First Hat
    • ๐Ÿง™Admins: Creating, Issuing, and Revising Hats
    • ๐Ÿ‘ฅWhat Hats Do I Need?
    • ๐ŸŒณDrafting, Exporting, and Deploying Tree Changes
    • ๐Ÿ—๏ธSetting a Hat's Basic Properties
    • ๐ŸฅณAdding Wearers
    • ๐Ÿ”Connecting Hats w/ Permissions & Authorities
      • Types of Hat-Powered Authorities
      • Connecting Hats to Token Gates
        • Hats Protocol Contract Addresses
        • Finding a Hat's Token ID
      • Documenting Hat Powers & Responsibilities
    • ๐ŸŒŸRevocation & Eligibility: Requirements for Wearers
    • โšกDeactivating & Reactivating Hats
    • โœ…Making Hats Claimable
    • ๐Ÿ”—Linking Trees Together
    • โ›“๏ธHats Protocol Supported Chains
    • โ“Glossary & FAQ
  • Hats Integrations
    • ๐Ÿ”Permissions & Authorities
      • Coordinape
      • Council Voting Vault
      • Charmverse
      • Discord
        • Collab.Land --> Discord
        • Guild.xyz --> Discord
      • Farcaster Casting Rights
      • Fileverse
      • Google Workspace
      • Hats Account
      • Role-Based Compensation
      • Safe Multisig Signing Authority
      • Telegram
        • Collab.Land --> Telegram
        • Guild.xyz --> Telegram
      • Snapshot: Voting, Weight & Proposal Creation
      • Wonderverse
    • ๐ŸŒŸEligibility & Accountability Criteria
      • Agreement Eligibility
      • Allow-List Eligibility
      • CoLinks Eligibility
      • ERC20 Eligibility
      • ERC721 Eligibility
      • ERC1155 Eligibility
      • Hat-Wearing Eligibility
      • Hats Election Eligibility
      • JokeRace Eligibility
      • Pass-Through (Hat-Based) Eligibility
      • Staking Eligibility
      • Subscription or Membership Fee (Unlock Protocol)
      • Gitcoin Passport Eligibility
    • โšกActivation & Deactivation Criteria
      • Seasonal/ Time-Expiry Toggle
      • Pass-Through (Hat-Based) Toggle
    • ๐Ÿ‘ทHatter Modules
      • Multi Claims Hatter
      • DAOhaus Moloch v3 Membership & Share Allocation
  • For Developers
    • ๐Ÿ‘ทHats Protocol, for Developers
      • Hat Properties
      • Wearing a Hat
      • Hat Admins & Hatter Contracts
      • Hats Trees
      • Hat IDs
      • Linking Hats Trees
      • Eligibility Modules
      • Toggle Modules
      • Hat Mutability and Editing
      • Creating Hats
      • Minting Hats
      • Transfering Hats
      • Renouncing Hats
      • Batch Actions
      • Hat Image URIs
      • ERC1155 Compatibility
      • โ›“๏ธSupported Chains
    • ๐Ÿค–v1 Protocol Spec
      • Hats.sol
      • HatsEvents.sol
      • HatsErrors.sol
      • HatsIdUtilities.sol
      • Interfaces
        • IHats.sol
        • IHatsIdUtilities.sol
        • IHatsEligibility.sol
        • IHatsToggle.sol
    • ๐Ÿ–ฅ๏ธv1 SDK
      • Core
        • Getting Started
        • Onchain Reads
        • Onchain Writes
        • Multicall
        • Claiming Hats
        • Utilities
      • Subgraph
        • Getting Started
        • Fetching Hats
        • Fetching Wearers
        • Fetching Trees
        • Misc
        • Types
      • Hat Details
        • Getting Started
        • Usage
    • ๐Ÿ”ญv1 Subgraphs
    • ๐ŸงฉHats Modules
      • ๐Ÿ”ŒModules SDK
        • Getting Started
        • Get Available Modules
        • Create New Instance/s
        • Composing Modules
        • Interact With Instances
        • Utilities
        • Types
      • โš’๏ธBuilding Hats Modules
        • Inside a Hats Module
          • Immutable Arguments
          • Module Setup
          • Versioning
        • Creating New Modules
        • How Module Instances Are Deployed
        • Modules Registry
        • About Module Chains
    • ๐Ÿ”Hats Signer Gate v2
    • ๐Ÿ‘’Hats Signer Gate SDK
      • Getting Started
      • Creating New Instances
      • Hats Signer Gate
      • Multi Hats Signer Gate
      • HSG & MHSG Handlers
    • ๐Ÿ’ผHats Account SDK
      • 1 of N Hats Account
        • Getting Started
        • Creating New Instances
        • Executing From An Instance
        • Constants
        • Types
    • ๐ŸŒHats Security Audits
  • Legal
    • Terms
      • Terms of Service
      • Acceptable Use
      • Privacy Policy
      • Cookie Policy
      • Attribution
Powered by GitBook
On this page
  • HatsIdUtilities
  • State Variables
  • Functions
  1. For Developers
  2. v1 Protocol Spec

HatsIdUtilities.sol

PreviousHatsErrors.solNextInterfaces

Last updated 1 year ago

HatsIdUtilities

Inherits: IHatsIdUtilities

Author: Haberdasher Labs

Functions for working with Hat Ids from Hats Protocol. Factored out of Hats.sol for easier use by other contracts.

State Variables

linkedTreeRequests

Mapping of tophats requesting to link to admin hats in other trees

Linkage only occurs if request is approved by the new admin

mapping(uint32 => uint256) public linkedTreeRequests;

linkedTreeAdmins

Mapping of approved & linked tophats to admin hats in other trees, used for grafting one hats tree onto another

Trees can only be linked to another tree via their tophat

mapping(uint32 => uint256) public linkedTreeAdmins;

TOPHAT_ADDRESS_SPACE

Hat Ids serve as addresses. A given Hat's Id represents its location in its hat tree: its level, its admin, its admin's admin (etc, all the way up to the tophat). The top level consists of 4 bytes and references all tophats. Each level below consists of 16 bits, and contains up to 65,536 child hats. A uint256 contains 4 bytes of space for tophat addresses, giving room for ((256 - 32) / 16) = 14 levels of delegation, with the admin at each level having space for 65,536 different child hats. A hat tree consists of a single tophat and has a max depth of 14 levels.

Number of bits of address space for tophat ids, ie the tophat domain

uint256 internal constant TOPHAT_ADDRESS_SPACE = 32;

LOWER_LEVEL_ADDRESS_SPACE

Number of bits of address space for each level below the tophat

uint256 internal constant LOWER_LEVEL_ADDRESS_SPACE = 16;

MAX_LEVELS

Maximum number of levels below the tophat, ie max tree depth (256 - TOPHAT_ADDRESS_SPACE) / LOWER_LEVEL_ADDRESS_SPACE;

uint256 internal constant MAX_LEVELS = 14;

Functions

buildHatId

Constructs a valid hat id for a new hat underneath a given admin

Reverts if the admin has already reached MAX_LEVELS

function buildHatId(uint256 _admin, uint16 _newHat) public pure returns (uint256 id);

Parameters

Name
Type
Description

_admin

uint256

the id of the admin for the new hat

_newHat

uint16

the uint16 id of the new hat

Returns

Name
Type
Description

id

uint256

The constructed hat id

getHatLevel

Identifies the level a given hat in its hat tree

function getHatLevel(uint256 _hatId) public view returns (uint32 level);

Parameters

Name
Type
Description

_hatId

uint256

the id of the hat in question

Returns

Name
Type
Description

level

uint32

(0 to type(uint32).max)

getLocalHatLevel

Identifies the level a given hat in its local hat tree

Similar to getHatLevel, but does not account for linked trees

function getLocalHatLevel(uint256 _hatId) public pure returns (uint32 level);

Parameters

Name
Type
Description

_hatId

uint256

the id of the hat in question

Returns

Name
Type
Description

level

uint32

The local level, from 0 to 14

isTopHat

Checks whether a hat is a topHat

function isTopHat(uint256 _hatId) public view returns (bool _isTopHat);

Parameters

Name
Type
Description

_hatId

uint256

The hat in question

Returns

Name
Type
Description

_isTopHat

bool

Whether the hat is a topHat

isLocalTopHat

Checks whether a hat is a topHat in its local hat tree

Similar to isTopHat, but does not account for linked trees

function isLocalTopHat(uint256 _hatId) public pure returns (bool _isLocalTopHat);

Parameters

Name
Type
Description

_hatId

uint256

The hat in question

Returns

Name
Type
Description

_isLocalTopHat

bool

Whether the hat is a topHat for its local tree

isValidHatId

function isValidHatId(uint256 _hatId) public pure returns (bool validHatId);

getAdminAtLevel

Gets the hat id of the admin at a given level of a given hat

This function traverses trees by following the linkedTreeAdmin pointer to a hat located in a different tree

function getAdminAtLevel(uint256 _hatId, uint32 _level) public view returns (uint256 admin);

Parameters

Name
Type
Description

_hatId

uint256

the id of the hat in question

_level

uint32

the admin level of interest

Returns

Name
Type
Description

admin

uint256

The hat id of the resulting admin

getAdminAtLocalLevel

Gets the hat id of the admin at a given level of a given hat local to the tree containing the hat.

function getAdminAtLocalLevel(uint256 _hatId, uint32 _level) public pure returns (uint256 admin);

Parameters

Name
Type
Description

_hatId

uint256

the id of the hat in question

_level

uint32

the admin level of interest

Returns

Name
Type
Description

admin

uint256

The hat id of the resulting admin

getTopHatDomain

Gets the tophat domain of a given hat

A domain is the identifier for a given hat tree, stored in the first 4 bytes of a hat's id

function getTopHatDomain(uint256 _hatId) public pure returns (uint32 domain);

Parameters

Name
Type
Description

_hatId

uint256

the id of the hat in question

Returns

Name
Type
Description

domain

uint32

The domain of the hat's tophat

getTippyTopHatDomain

Gets the domain of the highest parent tophat โ€” the "tippy tophat"

function getTippyTopHatDomain(uint32 _topHatDomain) public view returns (uint32 domain);

Parameters

Name
Type
Description

_topHatDomain

uint32

the 32 bit domain of a (likely linked) tophat

Returns

Name
Type
Description

domain

uint32

The tippy tophat domain

noCircularLinkage

Checks For any circular linkage of trees

function noCircularLinkage(uint32 _topHatDomain, uint256 _linkedAdmin) public view returns (bool notCircular);

Parameters

Name
Type
Description

_topHatDomain

uint32

the 32 bit domain of the tree to be linked

_linkedAdmin

uint256

the hatId of the potential tree admin

Returns

Name
Type
Description

notCircular

bool

circular link has not been found

sameTippyTopHatDomain

Checks that a tophat domain and its potential linked admin are from the same tree, ie have the same tippy tophat domain

function sameTippyTopHatDomain(uint32 _topHatDomain, uint256 _newAdminHat) public view returns (bool sameDomain);

Parameters

Name
Type
Description

_topHatDomain

uint32

The 32 bit domain of the tophat to be linked

_newAdminHat

uint256

The new admin for the linked tree

Returns

Name
Type
Description

sameDomain

bool

Whether the _topHatDomain and the domain of its potential linked _newAdminHat domains are the same

๐Ÿค–
Git Source