Fraction ProtocolgenericFacetsstateFacet

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 from 0.
  • 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 (see CreateFractionsSkeleton.sol).
  • The remaining states (however many they are) are not predefined and can be configured according to the platform's needs._

AlreadyInitialized

error AlreadyInitialized()

Thrown when attempting to re-initialize.

InvalidInputLength

error InvalidInputLength()

Thrown when input lengths are wrong.

FromStateNotCurrent

error FromStateNotCurrent(uint256 fromState, uint256 currentState)

Thrown when changing state and input fromState is not the current state.

StateExceedsSupportedLimit

error StateExceedsSupportedLimit(uint256 toState, uint256 supportedLimit)

Thrown when changing state and input toState is exceeding the supported state limit.

StateTransitionNotSupported

error StateTransitionNotSupported(uint256 fromState, uint256 toState)

Thrown when changing state and the transition from fromState to toState is not supported.

STORAGE_SLOT

bytes32 STORAGE_SLOT

Unique identifier for the storage slot where the Layout struct is stored. Derived from the ERC7201 formula. STORAGE_SLOT: 0x0da254b06dbae1b11b421a1ec8b60c0a4a6868be607c6d907d78edc499952400

Layout

struct Layout {
  bool isInitialized;
  uint256 totalStatesSupported;
  mapping(uint256 => uint256) stateOfId;
  mapping(uint256 => struct StateFacetStorage.StateHooks) stateHooksInfo;
}

StateHooks

struct StateHooks {
  bytes4[] beforeSelectors;
  bytes4[] afterSelectors;
  mapping(uint256 => bool) isStateTransitionSupported;
}

layout

function layout() internal pure returns (struct StateFacetStorage.Layout l)

Retrieves a reference to the Layout struct stored at a specified storage slot

initStateFacet

function initStateFacet(struct StateFacetStorage.Layout l, bytes initStateData) internal

Initializes the StateFacetStorage by setting the schema for state transitions and their respective hooks.

Parameters

NameTypeDescription
lstruct StateFacetStorage.LayoutA reference to the Layout struct in storage.
initStateDatabytes

changeState

function changeState(struct StateFacetStorage.Layout l, uint256 campaignId, uint256 fromState, uint256 toState) internal

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

NameTypeDescription
lstruct StateFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted campaign.
fromStateuint256The state to depart from (should be current state).
toStateuint256The destination state (the new state to settle).

execute

function execute(bytes4[] selectorsToExecute, uint256 campaignId) internal

Executes the hooks that are supposed to be called before or after state. (Details on StateFacet.sol)

Parameters

NameTypeDescription
selectorsToExecutebytes4[]The selectors of functions to be executed.
campaignIduint256The unique identifier of the targeted campaign.

getStateOfId

function getStateOfId(struct StateFacetStorage.Layout l, uint256 campaignId) internal view returns (uint256)

Retrieves the current state of the specified campaign.

Parameters

NameTypeDescription
lstruct StateFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted campaign.

Return Values

NameTypeDescription
[0]uint256The current state of campaignId.

On this page