InvariantCheckerLib
Title: InvariantChecker Library
Author: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80)
Library implementing a registry of named on-chain invariants for Diamond facets.
Admin registers (key → target + selector) pairs. Anyone can call checkInvariant
to staticcall the target and verify the bool return value. The check reverts on
false return or on a failed staticcall.
Functions
invariantCheckerStorage
Returns the storage struct for InvariantChecker at its ERC-7201 slot.
function invariantCheckerStorage() internal pure returns (InvariantCheckerStorage storage $);Returns
| Name | Type | Description |
|---|---|---|
| $ | InvariantCheckerStorage |
registerInterface
Registers support for the IInvariantChecker interface via ERC-165.
Writes true to the precomputed ERC-165 map slot.
function registerInterface() internal;__InvariantChecker_init
Initializes the InvariantChecker module.
Must be called inside an initializer-guarded function (see Initializable).
Registers the IInvariantChecker interface ID for ERC-165 discovery.
function __InvariantChecker_init() internal;registerInvariant
Registers or updates an invariant.
Requires DEFAULT_ADMIN_ROLE. Reverts if target == address(0).
Emits InvariantRegistered.
function registerInvariant(bytes32 key, address target, bytes4 selector) internal;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The invariant key. |
| target | address | The contract address to call (must not be address(0)). |
| selector | bytes4 | The function selector to invoke (must return a bool). |
unregisterInvariant
Removes a registered invariant.
Requires DEFAULT_ADMIN_ROLE. Reverts InvariantNotRegistered if key is not registered.
Emits InvariantUnregistered.
function unregisterInvariant(bytes32 key) internal;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The invariant key to remove. |
getInvariant
Returns the target address and function selector registered for key.
function getInvariant(bytes32 key) internal view returns (address target, bytes4 selector);Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The invariant key to query. |
Returns
| Name | Type | Description |
|---|---|---|
| target | address | The contract address to call. |
| selector | bytes4 | The function selector to invoke. |
checkInvariant
Performs the staticcall check for key and reverts on violation or call failure.
Reverts with InvariantNotRegistered if the key is unregistered.
Reverts with InvariantViolatedError if the call returns false.
Reverts with InvariantCheckFailed if the staticcall itself reverts.
function checkInvariant(bytes32 key) internal view;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The invariant key to check. |
checkInvariants
Checks multiple invariants in order, reverting on the first failure.
function checkInvariants(bytes32[] calldata keys) internal view;Parameters
| Name | Type | Description |
|---|---|---|
| keys | bytes32[] | The array of invariant keys to check sequentially. |
_registerInvariant
Internal — registers or updates an invariant entry.
function _registerInvariant(bytes32 key, address target, bytes4 selector) internal;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 | |
| target | address | |
| selector | bytes4 |
_unregisterInvariant
Internal — removes an invariant entry.
function _unregisterInvariant(bytes32 key) internal;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 |
_checkInvariant
Internal — performs the staticcall and checks the bool return.
function _checkInvariant(bytes32 key) internal view;Parameters
| Name | Type | Description |
|---|---|---|
| key | bytes32 |