@@ -9,13 +9,7 @@ import { LitContracts } from '@lit-protocol/contracts-sdk';
99import { LitNodeClient } from '@lit-protocol/lit-node-client' ;
1010import { logger } from '@lit-protocol/logger' ;
1111
12- import {
13- Action ,
14- LitActionAction ,
15- LogContextAction ,
16- MintPkpAction ,
17- TransactionAction ,
18- } from './actions' ;
12+ import { Action , ACTION_REPOSITORY } from './actions' ;
1913import { MachineContext } from './context/machine-context' ;
2014import {
2115 ContractEventData ,
@@ -27,6 +21,7 @@ import {
2721import { State , StateParams } from './states' ;
2822import { CheckFn , Transition } from './transitions' ;
2923import {
24+ ActionConstructor ,
3025 ActionDefinition ,
3126 BaseStateMachineParams ,
3227 ContextOrLiteral ,
@@ -60,6 +55,7 @@ export class StateMachine {
6055
6156 public id : string ;
6257 public status : MachineStatus = 'stopped' ;
58+ private readonly actionsRepository : Record < string , ActionConstructor > ;
6359 private states = new Map < string , State > ( ) ;
6460 private transitions = new Map < string , Map < string , Transition > > ( ) ;
6561 private currentState ?: State ;
@@ -74,6 +70,10 @@ export class StateMachine {
7470 ...params . context ,
7571 } ) ;
7672
73+ this . actionsRepository = {
74+ ...ACTION_REPOSITORY ,
75+ ...params . actionRepository ,
76+ } ;
7777 this . litNodeClient = params . litNodeClient ;
7878 this . litContracts = params . litContracts ;
7979 this . privateKey = params . privateKey ;
@@ -125,6 +125,10 @@ export class StateMachine {
125125 litContracts : litContractsInstance ,
126126 privateKey,
127127 onError,
128+ actionRepository : {
129+ ...ACTION_REPOSITORY ,
130+ ...machineConfig . actionRepository ,
131+ } ,
128132 } ) ;
129133
130134 const stateTransitions = [ ] as TransitionDefinition [ ] ;
@@ -571,66 +575,18 @@ export class StateMachine {
571575 ) : voidAsyncFunction {
572576 const actions = [ ] as Action [ ] ;
573577
574- actionDefinitions . forEach ( ( action ) => {
575- switch ( action . key ) {
576- case 'context' :
577- if ( typeof action . log ?. path === 'string' ) {
578- actions . push (
579- new LogContextAction ( {
580- debug : this . debug ,
581- stateMachine : this ,
582- path : action . log . path ,
583- } )
584- ) ;
585- }
586- break ;
587- case 'litAction' :
588- actions . push (
589- new LitActionAction ( {
590- debug : this . debug ,
591- stateMachine : this ,
592- ...action ,
593- } )
594- ) ;
595- break ;
596- case 'transaction' :
597- actions . push (
598- new TransactionAction ( {
599- debug : this . debug ,
600- stateMachine : this ,
601- ...action ,
602- } )
603- ) ;
604- break ;
605- case 'usePkp' :
606- if ( 'pkp' in action ) {
607- this . context . set (
608- 'activePkp' ,
609- this . resolveContextPathOrLiteral ( action . pkp )
610- ) ;
611- } else if ( 'mint' in action ) {
612- const mintPkpAction = new MintPkpAction ( {
613- debug : this . debug ,
614- stateMachine : this ,
615- } ) ;
616- actions . push ( mintPkpAction ) ;
617- }
618- if ( this . debug ) {
619- const activePkp = this . context . get ( 'activePkp' ) ;
620- logger . info ( `Machine configured to use pkp ${ activePkp } ` ) ;
621- }
622- break ;
623- default :
624- throw new AutomationError (
625- {
626- info : {
627- action,
628- } ,
629- } ,
630- `Unknown action. Check error info.`
631- ) ;
578+ for ( const action of actionDefinitions ) {
579+ const ActionCtor = this . actionsRepository [ action . key ] ;
580+ if ( ! ActionCtor ) {
581+ throw new AutomationError (
582+ { info : { action } } ,
583+ `Action key "${ action . key } " not found in action repository`
584+ ) ;
632585 }
633- } ) ;
586+ actions . push (
587+ new ActionCtor ( { debug : this . debug , stateMachine : this , ...action } )
588+ ) ;
589+ }
634590
635591 return async ( ) => {
636592 await Promise . all ( actions . map ( ( action ) => action . run ( ) ) ) . catch ( ( err ) => {
0 commit comments