Skip to content

Commit

Permalink
refactor: move event listener to blocks/eventManager
Browse files Browse the repository at this point in the history
  • Loading branch information
bassgeta committed Dec 2, 2024
1 parent 098a978 commit 687545c
Show file tree
Hide file tree
Showing 19 changed files with 306 additions and 70 deletions.
1 change: 1 addition & 0 deletions apps/main-chain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@joincolony/graphql": "workspace:*",
"@joincolony/clients": "workspace:*",
"@joincolony/blocks": "workspace:*",
"@joincolony/utils": "workspace:*"
}
}
Expand Down
7 changes: 5 additions & 2 deletions apps/main-chain/src/blockProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Log } from '@ethersproject/abstract-provider';
import { blocksMap, getLatestSeenBlockNumber } from '~blockListener';
import { getMatchingListeners } from '~eventListeners';
import eventManager from '~eventManager';
import { getInterfaceByListener } from '~interfaces';
import rpcProvider from '~provider';

Expand Down Expand Up @@ -182,7 +182,10 @@ export const processNextBlock = async (): Promise<void> => {

for (const log of logs) {
// Find listeners that match the log
const listeners = getMatchingListeners(log.topics, log.address);
const listeners = eventManager.getMatchingListeners(
log.topics,
log.address,
);
if (!listeners.length) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/main-chain/src/eventListeners/colony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { ContractEventsSignatures, EventHandler } from '~types';
import { notNull } from '~utils';
import {
addEventListener,
addNetworkEventListener,
addTokenEventListener,
} from '~eventListeners';
Expand Down Expand Up @@ -51,13 +50,14 @@ import { handleProxyColonyRequested } from '~handlers/proxyColonies';
import setTokenAuthority from '~handlers/tokens/setTokenAuthority';
import { addProxyColoniesEventListener } from './proxyColonies';
import { output } from '@joincolony/utils';
import eventManager from '~eventManager';

const addColonyEventListener = (
eventSignature: ContractEventsSignatures,
address: string,
handler: EventHandler,
): void => {
addEventListener({
eventManager.addEventListener({
type: EventListenerType.Colony,
address,
eventSignature,
Expand Down
45 changes: 0 additions & 45 deletions apps/main-chain/src/eventListeners/eventListeners.ts

This file was deleted.

10 changes: 4 additions & 6 deletions apps/main-chain/src/eventListeners/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { Extension, getExtensionHash } from '@colony/colony-js';
import { ContractEventsSignatures, EventHandler } from '~types';
import {
EventListenerType,
addEventListener,
getEventListeners,
setEventListeners,
setupListenerForOneTxPaymentExtensions,
setupListenersForStagedExpenditureExtensions,
} from '~eventListeners';
import eventManager from '~eventManager';
import {
ExtensionFragment,
ListExtensionsDocument,
Expand Down Expand Up @@ -44,7 +42,7 @@ export const addExtensionEventListener = (
colonyAddress: string,
handler: EventHandler,
): void => {
addEventListener({
eventManager.addEventListener({
type: EventListenerType.Extension,
eventSignature,
address: extensionAddress,
Expand All @@ -58,8 +56,8 @@ export const addExtensionEventListener = (
export const removeExtensionEventListeners = (
extensionAddress: string,
): void => {
const existingListeners = getEventListeners();
setEventListeners(
const existingListeners = eventManager.getEventListeners();
eventManager.setEventListeners(
existingListeners.filter((listener) => {
if (listener.type !== EventListenerType.Extension) {
return true;
Expand Down
1 change: 0 additions & 1 deletion apps/main-chain/src/eventListeners/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './eventListeners';
export * from './types';
export * from './colony';
export * from './network';
Expand Down
5 changes: 3 additions & 2 deletions apps/main-chain/src/eventListeners/network.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { EventListenerType } from '@joincolony/blocks';
import { utils } from 'ethers';

import { EventListenerType, addEventListener } from '~eventListeners';
import eventManager from '~eventManager';
import { ContractEventsSignatures, EventHandler } from '~types';

export const addNetworkEventListener = (
eventSignature: ContractEventsSignatures,
handler: EventHandler,
): void =>
addEventListener({
eventManager.addEventListener({
type: EventListenerType.Network,
eventSignature,
topics: [utils.id(eventSignature)],
Expand Down
5 changes: 3 additions & 2 deletions apps/main-chain/src/eventListeners/proxyColonies.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { utils } from 'ethers';

import { EventListenerType, addEventListener } from '~eventListeners';
import { ContractEventsSignatures, EventHandler } from '~types';
import eventManager from '~eventManager';
import { EventListenerType } from '@joincolony/blocks';

export const addProxyColoniesEventListener = (
eventSignature: ContractEventsSignatures,
handler: EventHandler,
): void =>
addEventListener({
eventManager.addEventListener({
type: EventListenerType.ProxyColonies,
eventSignature,
topics: [utils.id(eventSignature)],
Expand Down
12 changes: 6 additions & 6 deletions apps/main-chain/src/eventListeners/token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContractEventsSignatures, EventHandler } from '~types';
import { addEventListener, getEventListeners } from '~eventListeners';
import { EventListenerType, EventListener } from './types';
import eventManager from '~eventManager';
import { EventListenerType, EventListener } from '@joincolony/blocks';
import { utils } from 'ethers';
import isEqual from 'lodash/isEqual';

Expand Down Expand Up @@ -32,12 +32,12 @@ export const addTokenEventListener = (
* As a general rule, this will only *NOT* apply to the token Transfer event
* as that's the only one treated differently.
*/
const listenerExists = getEventListeners().some((existingListener) =>
isEqual(existingListener, tokenListener),
);
const listenerExists = eventManager
.getEventListeners()
.some((existingListener) => isEqual(existingListener, tokenListener));
if (listenerExists) {
return;
}

return addEventListener(tokenListener);
return eventManager.addEventListener(tokenListener);
};
5 changes: 5 additions & 0 deletions apps/main-chain/src/eventManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EventManager } from '@joincolony/blocks';

const eventManager = new EventManager();

export default eventManager;
1 change: 1 addition & 0 deletions apps/main-chain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { utils } from 'ethers';

import { startBlockListener } from '~blockListener';
import '~amplifyClient';
import '~eventManager';
import { startStatsServer } from '~stats';
import {
setupListenersForColonies,
Expand Down
4 changes: 2 additions & 2 deletions apps/main-chain/src/stats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';

import { getLastBlockNumber, getStats, initStats } from '~utils';
import { getListenersStats } from '~eventListeners';
import eventManager from '~eventManager';
import rpcProvider from '~provider';
import { output } from '@joincolony/utils';

Expand Down Expand Up @@ -33,7 +33,7 @@ app.get('/stats', async (_, res) => {
* Use to check currently active listeners
*/
app.get('/listeners', async (_, res) => {
res.type('json').send(getListenersStats());
res.type('json').send(eventManager.getListenersStats());
});

export const startStatsServer = async (): Promise<void> => {
Expand Down
8 changes: 8 additions & 0 deletions packages/blocks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@joincolony/blocks",
"main": "src/index.ts",
"version": "1.0.0",
"dependencies": {
"@joincolony/utils": "workspace:*"
}
}
48 changes: 48 additions & 0 deletions packages/blocks/src/eventManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { verbose } from '@joincolony/utils';
import { EventListener } from './types';

export class EventManager {
private listeners: EventListener[] = [];

public getEventListeners(): EventListener[] {
return this.listeners;
}

public setEventListeners(newListeners: EventListener[]): void {
this.listeners = newListeners;
}

public addEventListener(listener: EventListener): void {
verbose(
`Added listener for event ${listener.eventSignature}`,
listener.address ? `filtering address ${listener.address}` : '',
);
this.listeners.push(listener);
}

public getMatchingListeners(
logTopics: string[],
logAddress: string,
): EventListener[] {
return this.listeners.filter((listener) => {
if (listener.address && logAddress !== listener.address) {
return false;
}

if (listener.topics.length > logTopics.length) {
return false;
}

return listener.topics.every((topic, index) => {
return (
topic === null ||
topic.toLowerCase() === logTopics[index].toLowerCase()
);
});
});
}

public getListenersStats(): string {
return JSON.stringify(this.listeners);
}
}
2 changes: 2 additions & 0 deletions packages/blocks/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './eventManager';
export * from './types';
Loading

0 comments on commit 687545c

Please sign in to comment.