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>
7.3 KiB
Timelock
Simple timelock.
Note:
- This implementation only supports ERC7821 style execution.
- This implementation uses EnumerableRoles for better auditability.
- This implementation uses custom errors with arguments for easier debugging.
executionDatacan be encoded in three different ways:abi.encode(calls).abi.encode(calls, abi.encode(predecessor)).abi.encode(calls, abi.encode(predecessor, salt)).
- Where
callsis of type(address,uint256,bytes)[], andpredecessoris the id of the proposal that is required to be already executed. - If
predecessorisbytes32(0), it will be ignored (treated as if not required). - The optional
saltallows for multiple proposals representing the same payload. - The proposal id is given by:
keccak256(abi.encode(mode, keccak256(executionData))).
We recommended including the salt, even though it is optional for convenience.
Supported modes:
bytes32(0x01000000000000000000...): does not support optionalopData.bytes32(0x01000000000078210001...): supports optionalopData. WhereopDataisabi.encode(predecessor)orabi.encode(predecessor, salt), and...is the remaining 22 bytes which can be anything. For ease of mind, just use:0x0100000000007821000100000000000000000000000000000000000000000000.
Inherits:
Constants
ADMIN_ROLE
uint256 public constant ADMIN_ROLE = 0
Role that can add / remove roles without wait time.
This role cannot directly propose, execute, or cancel.
This role is NOT exempt from the execution wait time.
PROPOSER_ROLE
uint256 public constant PROPOSER_ROLE = 1
Role that can propose operations.
EXECUTOR_ROLE
uint256 public constant EXECUTOR_ROLE = 2
Role that can execute operations.
CANCELLER_ROLE
uint256 public constant CANCELLER_ROLE = 3
Role that can cancel proposed operations.
MAX_ROLE
uint256 public constant MAX_ROLE = 3
The maximum role.
OPEN_ROLE_HOLDER
address public constant OPEN_ROLE_HOLDER =
0x0303030303030303030303030303030303030303
Assign this holder to a role to allow anyone to call
the function guarded by onlyRoleOrOpenRole.
Enums
OperationState
enum OperationState {
Unset, // 0.
Waiting, // 1.
Ready, // 2.
Done // 3.
}
Represents the state of an operation.
Custom Errors
TimelockInsufficientDelay(uint256,uint256)
error TimelockInsufficientDelay(uint256 delay, uint256 minDelay)
The proposed operation has insufficient delay.
TimelockInvalidOperation(bytes32,uint256)
error TimelockInvalidOperation(bytes32 id, uint256 expectedStates)
The operation cannot be performed.
The expectedStates is a bitmap with the bits enabled for
each enum position, starting from the least significant bit.
TimelockUnexecutedPredecessor(bytes32)
error TimelockUnexecutedPredecessor(bytes32 predecessor)
The operation has a predecessor that has not been executed.
TimelockUnauthorized()
error TimelockUnauthorized()
Unauthorized to call the function.
TimelockDelayOverflow()
error TimelockDelayOverflow()
The delay cannot be greater than 2 ** 254 - 1.
TimelockAlreadyInitialized()
error TimelockAlreadyInitialized()
The timelock has already been initialized.
Events
Proposed(bytes32,bytes32,bytes,uint256)
event Proposed(
bytes32 indexed id,
bytes32 mode,
bytes executionData,
uint256 readyTimestamp
)
The proposal id has been created.
Executed(bytes32,bytes32,bytes)
event Executed(bytes32 indexed id, bytes32 mode, bytes executionData)
The proposal id has been executed.
Cancelled(bytes32)
event Cancelled(bytes32 indexed id)
The proposal id has been cancelled.
MinDelaySet(uint256)
event MinDelaySet(uint256 newMinDelay)
The minimum delay has been set to newMinDelay.
Initializer
initialize(uint256,address,address[],address[],address[])
function initialize(
uint256 initialMinDelay,
address initialAdmin,
address[] calldata proposers,
address[] calldata executors,
address[] calldata cancellers
) public virtual
Initializes the timelock contract.
_initializeTimelockAuthorizationCheck()
function _initializeTimelockAuthorizationCheck() internal virtual
The Timelock is best used via a minimal proxy.
But in case it is not, we want to guard initialize from frontrun griefing.
Authorizing both msg.sender and tx.origin caters to the use case where
the Timelock is being deployed via a factory (e.g. Nicks, CreateX).
Always call initialize as soon as possible after deployment.
In the rare case where msg.sender or tx.origin are untrusted
and abused to frontrun, initialize will revert on reinitialization,
so you will know that the deployment is compromised and must be discarded.
Public Update Functions
propose(bytes32,bytes,uint256)
function propose(bytes32 mode, bytes calldata executionData, uint256 delay)
public
virtual
onlyRole(PROPOSER_ROLE)
returns (bytes32 id)
Proposes an execute payload (mode, executionData) with delay.
Emits a Proposed event.
cancel(bytes32)
function cancel(bytes32 id) public virtual onlyRole(CANCELLER_ROLE)
Cancels the operation with id.
Emits a Cancelled event.
setMinDelay(uint256)
function setMinDelay(uint256 newMinDelay) public virtual
Allows the timelock itself to set the minimum delay.
Emits a MinDelaySet event.
Public View Functions
minDelay()
function minDelay() public view virtual returns (uint256 result)
Returns the minimum delay.
readyTimestamp(bytes32)
function readyTimestamp(bytes32 id)
public
view
virtual
returns (uint256 result)
Returns the ready timestamp for id.
operationState(bytes32)
function operationState(bytes32 id)
public
view
virtual
returns (OperationState result)
Returns the current operation state of id.
Internal Helpers
_bulkSetRole(address[],uint256,bool)
function _bulkSetRole(
address[] calldata addresses,
uint256 role,
bool active
) internal virtual
Helper to set roles in bulk.
Overrides
_execute(bytes32,bytes,Call[],bytes)
function _execute(
bytes32 mode,
bytes calldata executionData,
Call[] calldata calls,
bytes calldata opData
) internal virtual override(ERC7821)
For ERC7821.
To ensure that the function can only be called by the proper role holder.
To ensure that the operation is ready to be executed.
Updates the operation state and emits a Executed event after the calls.
_authorizeSetRole(address,uint256,bool)
function _authorizeSetRole(address, uint256, bool)
internal
virtual
override(EnumerableRoles)
This guards the public setRole function,
such that it can only be called by the timelock itself, or an admin.