Skip to content
Lattice

InvariantCheckerLib

Git Source

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

NameTypeDescription
$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

NameTypeDescription
keybytes32The invariant key.
targetaddressThe contract address to call (must not be address(0)).
selectorbytes4The 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

NameTypeDescription
keybytes32The 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

NameTypeDescription
keybytes32The invariant key to query.

Returns

NameTypeDescription
targetaddressThe contract address to call.
selectorbytes4The 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

NameTypeDescription
keybytes32The invariant key to check.

checkInvariants

Checks multiple invariants in order, reverting on the first failure.

function checkInvariants(bytes32[] calldata keys) internal view;

Parameters

NameTypeDescription
keysbytes32[]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

NameTypeDescription
keybytes32
targetaddress
selectorbytes4

_unregisterInvariant

Internal — removes an invariant entry.

function _unregisterInvariant(bytes32 key) internal;

Parameters

NameTypeDescription
keybytes32

_checkInvariant

Internal — performs the staticcall and checks the bool return.

function _checkInvariant(bytes32 key) internal view;

Parameters

NameTypeDescription
keybytes32