Skip to content

Commit

Permalink
WIP: initial action matchers setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubcolony committed Nov 28, 2024
1 parent 2e6f710 commit 385b2d2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/actions/actionHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ActionHandler } from './types';

export const handleMintTokensAction: ActionHandler = async (events) => {
console.log('Mint Tokens action ', events);
};
1 change: 1 addition & 0 deletions src/actions/actionMatchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const actionMatchers = [];
17 changes: 17 additions & 0 deletions src/actions/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ContractEvent } from '~graphql';

interface BaseActionMatcher {
handler: (events: ContractEvent[]) => Promise<void>;
}

interface SimpleActionMatcher extends BaseActionMatcher {
eventSignatures: string[];
}

interface FunctionActionMatcher extends BaseActionMatcher {
matcherFn: (events: ContractEvent[]) => boolean;
}

export type ActionMatcher = SimpleActionMatcher | FunctionActionMatcher;

export type ActionHandler = (events: ContractEvent[]) => Promise<void>;
13 changes: 13 additions & 0 deletions src/blockProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
setLastBlockNumber,
} from '~utils';
import { BLOCK_PAGING_SIZE } from '~constants';
import { ContractEventsSignatures } from '~types';
import { handleMintTokensAction } from '~actions/actionHandlers';

let isProcessing = false;
const blockLogs = new Map<number, Log[]>();
Expand Down Expand Up @@ -176,6 +178,8 @@ export const processNextBlock = async (): Promise<void> => {
}
}

const blockEvents = [];

for (const log of logs) {
// Find listeners that match the log
const listeners = getMatchingListeners(log.topics, log.address);
Expand All @@ -201,11 +205,20 @@ export const processNextBlock = async (): Promise<void> => {
continue;
}

blockEvents.push(event);

// Call the handler in a blocking way to ensure events get processed sequentially
await listener.handler(event, listener);
}
}

const actionMatchers = [
{
eventSignatures: [ContractEventsSignatures.TokensMinted],
handler: handleMintTokensAction,
},
];

verbose('processed block', currentBlockNumber);

lastBlockNumber = currentBlockNumber;
Expand Down

0 comments on commit 385b2d2

Please sign in to comment.