Skip to content

Commit cc83252

Browse files
committed
feat: make event listener use an action repository so action definitions are extensible on machine construction and referenceable in machine definition
1 parent 27d1e9e commit cc83252

File tree

4 files changed

+45
-66
lines changed

4 files changed

+45
-66
lines changed

packages/event-listener/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './lib/actions';
12
export * from './lib/listeners';
23
export * from './lib/states';
34
export * from './lib/state-machine';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
import { LitActionAction } from './lit-action';
2+
import { LogContextAction } from './log-context';
3+
import { MintPkpAction } from './mint-pkp';
4+
import { TransactionAction } from './transaction';
5+
import { ActionConstructor } from '../types';
6+
17
export * from './action';
28
export * from './lit-action';
39
export * from './log-context';
410
export * from './mint-pkp';
511
export * from './transaction';
12+
13+
export const ACTION_REPOSITORY: Record<string, ActionConstructor> = {
14+
context: LogContextAction,
15+
litAction: LitActionAction,
16+
transaction: TransactionAction,
17+
usePkp: MintPkpAction,
18+
};

packages/event-listener/src/lib/state-machine.ts

Lines changed: 22 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ import { LitContracts } from '@lit-protocol/contracts-sdk';
99
import { LitNodeClient } from '@lit-protocol/lit-node-client';
1010
import { 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';
1913
import { MachineContext } from './context/machine-context';
2014
import {
2115
ContractEventData,
@@ -27,6 +21,7 @@ import {
2721
import { State, StateParams } from './states';
2822
import { CheckFn, Transition } from './transitions';
2923
import {
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) => {

packages/event-listener/src/lib/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ethers } from 'ethers';
33
import { LitContracts } from '@lit-protocol/contracts-sdk';
44
import { LitNodeClient } from '@lit-protocol/lit-node-client';
55

6+
import { Action, ActionParams } from './actions/action';
67
import { BaseTransitionParams } from './transitions';
78

89
export type Address = `0x${string}`;
@@ -35,6 +36,12 @@ export interface UpdatesContext {
3536
}
3637

3738
// Action Types
39+
export type ActionConstructor = new (params: any) => Action;
40+
41+
export interface RawActionDefinition extends ActionParams {
42+
key: string;
43+
}
44+
3845
export interface LitActionActionDefinition {
3946
key: 'litAction';
4047
code?: ContextOrLiteral<string>;
@@ -96,6 +103,7 @@ export interface UseCapacityNFTActionDefinition {
96103
}
97104

98105
export type ActionDefinition =
106+
| RawActionDefinition
99107
| ContextActionDefinition
100108
| LitActionActionDefinition
101109
| MintCapacityNFTActionDefinition
@@ -176,6 +184,7 @@ export interface TransitionParams
176184

177185
// Machine Types
178186
export interface BaseStateMachineParams {
187+
actionRepository?: Record<string, ActionConstructor>;
179188
context?: Record<string, unknown>;
180189
debug?: boolean;
181190
litContracts: LitContracts;

0 commit comments

Comments
 (0)