caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

5.7 KiB

TimedRoles

Timed multiroles authorization mixin.

Note:

This implementation is agnostic to the Ownable that the contract inherits from. It performs a self-staticcall to the owner() function to determine the owner. This is useful for situations where the contract inherits from OpenZeppelin's Ownable, such as in LayerZero's OApp contracts.

This implementation performs a self-staticcall to MAX_TIMED_ROLE() to determine the maximum timed role that can be set/unset. If the inheriting contract does not have MAX_TIMED_ROLE(), then any timed role can be set/unset.

This implementation allows for any uint256 role, it does NOT take in a bitmask of roles. This is to accommodate teams that are allergic to bitwise flags.

By default, the owner() is the only account that is authorized to set timed roles. This behavior can be changed via overrides.

This implementation is compatible with any Ownable. This implementation is NOT compatible with OwnableRoles.

As timed roles can turn active or inactive anytime, enumeration is omitted here. Querying the number of active timed roles will cost O(n) instead of O(1).

Names are deliberately prefixed with "Timed", so that this contract can be used in conjunction with EnumerableRoles without collisions.

Events

TimedRoleSet(address,uint256,uint40,uint40)

event TimedRoleSet(
    address indexed holder,
    uint256 indexed timedRole,
    uint40 start,
    uint40 expires
)

The active time range of the timed role has been set.

Custom Errors

TimedRoleHolderIsZeroAddress()

error TimedRoleHolderIsZeroAddress()

Cannot set the timed role of the zero address.

InvalidTimedRole()

error InvalidTimedRole()

The timed role has exceeded the maximum timed role.

TimedRolesUnauthorized()

error TimedRolesUnauthorized()

Unauthorized to perform the action.

InvalidTimedRoleRange()

error InvalidTimedRoleRange()

The expires cannot be less than the start.

Public Update Functions

setTimedRole(address,uint256,uint40,uint40)

function setTimedRole(
    address holder,
    uint256 timedRole,
    uint40 start,
    uint40 expires
) public payable virtual

Sets the active time range of timedRole of holder to [start, expires].
The timedRole is active when start <= block.timestamp && block.timestamp <= expires.

Public Read Functions

timedRoleActive(address,uint256)

function timedRoleActive(address holder, uint256 timedRole)
    public
    view
    virtual
    returns (bool isActive, uint40 start, uint40 expires)

Returns whether the timedRole is active for holder and the active time range.

Internal Functions

_setTimedRole(address,uint256,uint40,uint40)

function _setTimedRole(
    address holder,
    uint256 timedRole,
    uint40 start,
    uint40 expires
) internal virtual

Set the timed role for holder directly without authorization guard.

_validateTimedRole(uint256)

function _validateTimedRole(uint256 timedRole) internal view virtual

Requires the timedRole is not greater than MAX_TIMED_ROLE().
If MAX_TIMED_ROLE() is not implemented, this is an no-op.

_authorizeSetTimedRole(address,uint256,uint40,uint40)

function _authorizeSetTimedRole(
    address holder,
    uint256 timedRole,
    uint40 start,
    uint40 expires
) internal virtual

Checks that the caller is authorized to set the timed role.

_hasAnyTimedRoles(address,bytes)

function _hasAnyTimedRoles(address holder, bytes memory encodedTimeRoles)
    internal
    view
    virtual
    returns (bool result)

Returns if holder has any roles in encodedTimeRoles.
encodedTimeRoles is abi.encode(SAMPLE_TIMED_ROLE_0, SAMPLE_TIMED_ROLE_1, ...).

_checkTimedRole(uint256)

function _checkTimedRole(uint256 timedRole) internal view virtual

Reverts if msg.sender does not have timedRole.

_checkTimedRoles(bytes)

function _checkTimedRoles(bytes memory encodedTimedRoles)
    internal
    view
    virtual

Reverts if msg.sender does not have any timed role in encodedTimedRoles.

_checkOwnerOrTimedRole(uint256)

function _checkOwnerOrTimedRole(uint256 timedRole) internal view virtual

Reverts if msg.sender is not the contract owner and does not have timedRole.

_checkOwnerOrTimedRoles(bytes)

function _checkOwnerOrTimedRoles(bytes memory encodedTimedRoles)
    internal
    view
    virtual

Reverts if msg.sender is not the contract owner and
does not have any timed role in encodedTimedRoles.

Modifiers

onlyTimedRole(uint256)

modifier onlyTimedRole(uint256 timedRole) virtual

Marks a function as only callable by an account with timedRole.

onlyTimedRoles(bytes)

modifier onlyTimedRoles(bytes memory encodedTimedRoles) virtual

Marks a function as only callable by an account with any role in encodedTimedRoles.
encodedTimedRoles is abi.encode(SAMPLE_TIMED_ROLE_0, SAMPLE_TIMED_ROLE_1, ...).

onlyOwnerOrTimedRole(uint256)

modifier onlyOwnerOrTimedRole(uint256 timedRole) virtual

Marks a function as only callable by the owner or by an account with timedRole.

onlyOwnerOrTimedRoles(bytes)

modifier onlyOwnerOrTimedRoles(bytes memory encodedTimedRoles) virtual

Marks a function as only callable by the owner or
by an account with any role in encodedTimedRoles.
Checks for ownership first, then checks for roles.
encodedTimedRoles is abi.encode(SAMPLE_TIMED_ROLE_0, SAMPLE_TIMED_ROLE_1, ...).