StateFacetStorage
This library manages the storage and logic for StateFacet
which acts as a programmable state machine,
enabling state transitions within campaigns and triggering specific functions (hooks) based on a defined schema.
_The campaign states configuration follows a schema where each state has an associated set of states representing
the allowed state transitions. Platform facet functions can be designated as either beforeHooks
or afterHooks
via
their respective function selectors. Each state can have multiple beforeHooks
and/or afterHooks
assigned.
A state's beforeHooks
are triggered sequentially when the state is reached from another one. A state's afterHooks
are triggered sequentially when there is a departure from this state to another.
IMPORTANT:
- States are represented internally as
uint256
starting from0
. - State
0
is the default state (i.e., campaign does not exist) for any campaign within the platform. - State
1
is always exclusively reached after the createFractions call (seeCreateFractionsSkeleton.sol
). - The remaining states (however many they are) are not predefined and can be configured according to the platform's needs._
AlreadyInitialized
Thrown when attempting to re-initialize.
InvalidInputLength
Thrown when input lengths are wrong.
FromStateNotCurrent
Thrown when changing state and input fromState
is not the current state.
StateExceedsSupportedLimit
Thrown when changing state and input toState
is exceeding the supported state limit.
StateTransitionNotSupported
Thrown when changing state and the transition from fromState
to toState
is not supported.
STORAGE_SLOT
Unique identifier for the storage slot where the Layout struct is stored. Derived from the ERC7201 formula. STORAGE_SLOT: 0x0da254b06dbae1b11b421a1ec8b60c0a4a6868be607c6d907d78edc499952400
Layout
StateHooks
layout
Retrieves a reference to the Layout struct stored at a specified storage slot
initStateFacet
Initializes the StateFacetStorage
by setting the schema for state transitions and their respective hooks.
Parameters
Name | Type | Description |
---|---|---|
l | struct StateFacetStorage.Layout | A reference to the Layout struct in storage. |
initStateData | bytes |
changeState
Changes the specified campaign's state from fromState
to toState
.
Can only be called internally by the diamond proxy itself (i.e., from other facets).
The campaign's current state must be fromState
, and toState
must be a valid transition from the current state;
otherwise, the function reverts.
Parameters
Name | Type | Description |
---|---|---|
l | struct StateFacetStorage.Layout | A reference to the Layout struct in storage. |
campaignId | uint256 | The unique identifier of the targeted campaign. |
fromState | uint256 | The state to depart from (should be current state). |
toState | uint256 | The destination state (the new state to settle). |
execute
Executes the hooks that are supposed to be called before or after state. (Details on StateFacet.sol)
Parameters
Name | Type | Description |
---|---|---|
selectorsToExecute | bytes4[] | The selectors of functions to be executed. |
campaignId | uint256 | The unique identifier of the targeted campaign. |
getStateOfId
Retrieves the current state of the specified campaign.
Parameters
Name | Type | Description |
---|---|---|
l | struct StateFacetStorage.Layout | A reference to the Layout struct in storage. |
campaignId | uint256 | The unique identifier of the targeted campaign. |
Return Values
Name | Type | Description |
---|---|---|
[0] | uint256 | The current state of campaignId . |