|
| 1 | +--- |
| 2 | +title: "Almost Micro-Rollup" |
| 3 | +description: "Packaging state machines inside the rollup and other things" |
| 4 | +--- |
| 5 | + |
| 6 | +### State Machines |
| 7 | + |
| 8 | +State machines are simple units that can be used to model the state of a system. They are composed of states, events, and transitions. A state machine can be in one state at a time, |
| 9 | +and can transition to another state when an event is triggered. The state machine can also have actions that are executed when a transition occurs. |
| 10 | + |
| 11 | +Stackr-JS provides a simple state machine implementation that takes in the Rollup state and STF |
| 12 | + |
| 13 | +<Warning>All of this should go outside the `state.ts` file</Warning> |
| 14 | + |
| 15 | +**example** |
| 16 | + |
| 17 | +```ts |
| 18 | +import { StateMachine } from "@stackr/stackr-js/execution"; |
| 19 | +import * as genesisState from "../genesis-state.json"; |
| 20 | + |
| 21 | +const counterFsm = new StateMachine({ |
| 22 | + state: new CounterRollup(genesisState.state), |
| 23 | + stf: counterSTF, |
| 24 | +}); |
| 25 | +``` |
| 26 | + |
| 27 | +### Genesis State |
| 28 | + |
| 29 | +The genesis state is the initial state of the state machine. |
| 30 | +It is the state that is used to initialize the state machine. It is also the state that is used to initialize the state of the rollup. |
| 31 | + |
| 32 | +You need to set the genesis state inside `genesis-state.json` file. |
| 33 | + |
| 34 | +The format is |
| 35 | + |
| 36 | +`{ "state" : WHATEVER_YOU_WANT_AS_JSON }` |
| 37 | + |
| 38 | +example |
| 39 | + |
| 40 | +<CodeGroup> |
| 41 | +```json counter |
| 42 | +{ "state": 0 } |
| 43 | +``` |
| 44 | + |
| 45 | +```json amm |
| 46 | +{ |
| 47 | + "state": [ |
| 48 | + { |
| 49 | + "owner": "Alice", |
| 50 | + "quantityA": 10, |
| 51 | + "quantityB": 0 |
| 52 | + }, |
| 53 | + { |
| 54 | + "owner": "Bob", |
| 55 | + "quantityA": 20, |
| 56 | + "quantityB": 10 |
| 57 | + }, |
| 58 | + { |
| 59 | + "owner": "Charlie", |
| 60 | + "quantityA": 200, |
| 61 | + "quantityB": 0 |
| 62 | + } |
| 63 | + ] |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +```json chess |
| 68 | +{ "state": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" } |
| 69 | +``` |
| 70 | + |
| 71 | +</CodeGroup> |
| 72 | + |
| 73 | +### Input type schema |
| 74 | + |
| 75 | +The user-input type schema is the schema that is used to validate the user input. It is used to validate the user input before it is applied to the state machine. |
| 76 | + |
| 77 | +<Info> |
| 78 | + This is what the user/ another entity send to the **app** or **rollup**. This |
| 79 | + is NOT what is applied to the State transition function. |
| 80 | +</Info> |
| 81 | + |
| 82 | +You can define to get a custom input from the user using this schema -> optionally process it or add more items -> create an object for **ActionInput** for the state machine |
| 83 | + |
| 84 | +To define a user input type schema, there are some special things to take care of since Stackr creates **EIP-712** typed data field for the same |
| 85 | + |
| 86 | +Example, for a chess game you can define something like |
| 87 | + |
| 88 | +```ts |
| 89 | +const actionSchemaType = { |
| 90 | + move: "String", |
| 91 | +}; |
| 92 | +``` |
| 93 | + |
| 94 | +Or for a counter you can define something like |
| 95 | + |
| 96 | +```ts |
| 97 | +const actionSchemaType = { |
| 98 | + type: "String", |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +or for a AMM you can define something like |
| 103 | + |
| 104 | +```ts |
| 105 | +const actionSchemaType = { |
| 106 | + type: "String", |
| 107 | + owner: "String", |
| 108 | + quantityA: "Number", |
| 109 | + quantityB: "Number", |
| 110 | +}; |
| 111 | +``` |
| 112 | + |
| 113 | +#### Notice the types |
| 114 | + |
| 115 | +<Warning> |
| 116 | + These are not TypeScript interfaces, these are actual Objects hence the way |
| 117 | + the type is passed on is inside quotes. |
| 118 | +</Warning> |
| 119 | + |
| 120 | +#### Supported user input types |
| 121 | + |
| 122 | +These are same as solidity types |
| 123 | + |
| 124 | +```ts internal stackr-js |
| 125 | +export type PrimitiveType = "String" | "Uint" | "Bool" | "Address" | "Bytes"; |
| 126 | +export type SchemaType = |
| 127 | + | PrimitiveType |
| 128 | + | { [key: string]: SchemaType } |
| 129 | + | SchemaType[]; |
| 130 | +``` |
| 131 | + |
| 132 | +So you can define both simple and complex input schema |
| 133 | + |
| 134 | +<CodeGroup> |
| 135 | +```ts simple |
| 136 | +const actionSchemaType = { |
| 137 | + type: "String", |
| 138 | + owner: "String", |
| 139 | + quantityA: "Number", |
| 140 | + quantityB: "Number", |
| 141 | +}; |
| 142 | +``` |
| 143 | + |
| 144 | +```ts nested |
| 145 | +const actionSchemaType = { |
| 146 | + type: "String", |
| 147 | + owner: "String", |
| 148 | + quantityA: "Number", |
| 149 | + quantityB: "Number", |
| 150 | + nested: { |
| 151 | + type: "String", |
| 152 | + owner: "String", |
| 153 | + quantityA: "Number", |
| 154 | + quantityB: "Number", |
| 155 | + }, |
| 156 | + array: [ |
| 157 | + { |
| 158 | + type: "String", |
| 159 | + owner: "String", |
| 160 | + quantityA: "Number", |
| 161 | + quantityB: "Number", |
| 162 | + }, |
| 163 | + ], |
| 164 | +}; |
| 165 | +``` |
| 166 | + |
| 167 | +</CodeGroup> |
| 168 | + |
| 169 | +<Info> |
| 170 | + Stackr Converts these to EIP-712 typed data fields and expects user signatures |
| 171 | + on it |
| 172 | +</Info> |
| 173 | + |
| 174 | +You can now generate an `ActionSchema` from this |
| 175 | + |
| 176 | +```ts |
| 177 | +import { ActionSchema } from "@stackr/stackr-js"; |
| 178 | + |
| 179 | +const userInputSchema = new ActionSchema("update-move", actionSchemaType); |
| 180 | +``` |
| 181 | + |
| 182 | +This `userInputSchema` can be used to create instances of user Actions |
0 commit comments