State Facets Initialization
Setting States and Selectors
When internal state facets are initialized with the respective uint256
values, you can begin initializing the StateFacetStorage
.
To simplify development, you can use an enum to define the states, with each state corresponding to its index position and
represented as a uint256
value.
Note
State 0
is the default (nonexistent campaign) state, State 1
always follows a createFractions()
call, and all subsequent states are configurable based on platform requirements.
After defining the states in the enum, you have to create a variable (_initStateArg
) that would contain encoded
data which will be passed to the function when initializing the StateFacetStorage
.
Besides states, it includes before and after hooks - facet functions that get executed when the changeState()
function is called.
For instance, when changeState(campaignId, from = X, to = Y)
is called:
- First, all
afterHooks
associated with stateX
(the departing state) are executed, if any. - Then, all
beforeHooks
associated with stateY
(the arriving state) are executed, if any.
You must use selectors when filling out the before and after hook arrays. Below you can find a code example that highlights
the creation of encoded data for the initialization of the StateFacetStorage
. Consider this example solely for demonstration
purposes, as it should be properly refined for your specific use case:
This 2D array represents a state transition matrix, where each row corresponds to a specific state, and the entries in
that row indicate the states it can transition to. In this matrix X
, the array at index 1
is
[STATES.PURCHASE
, STATES.REJECTED
], indicating that from state 1
(FRACTIONS_CREATED
), transitions to state
2
(PURCHASE
) and state 6
(REJECTED
) are allowed.
X[1][1]
implies a valid transition from state 1
to state 6
. Indices 4
, 5
, and 6
(corresponding to states
DONE
, NON_FUNDED
, and REJECTED
, respectively) contain empty arrays. This indicates that once a campaign reaches
any of these terminal states, no further transitions are allowed—it remains in that state permanently.
The state and selector arrays can include multiple state options and function selectors, which are executed in sequence.
Each index in the state array aligns with the corresponding before and after hook arrays at the same position. For example:
Note
To learn how to obtain function selectors for each required facet and initialize them, refer to the RWA Fraction Market guide.
The changeState()
function in StateFacetStorage
reveals an important detail: the after hooks are
executed before the before hooks, which may seem counterintuitive at first glance:
The reason this happens is because when a campaign transitions from state x
to state y
, it first goes through
the after hooks, marking the end of the current state x
, and then, upon entering state y
, it calls the
before hooks of the new state: