2023-08-22 11:52:30 -07:00
|
|
|
import dotenv from 'dotenv';
|
2023-08-11 06:15:55 -07:00
|
|
|
dotenv.config();
|
|
|
|
|
2023-08-22 11:52:30 -07:00
|
|
|
import { getDB } from './databases/utils';
|
|
|
|
import { getSNS } from './services/SNS/utils';
|
2023-08-11 06:15:55 -07:00
|
|
|
import { makeFinalizedWatcher } from './watchers/utils';
|
2023-08-18 05:47:11 -07:00
|
|
|
import { InfrastructureController } from './infrastructure/infrastructure.controller';
|
2023-09-21 07:21:05 -07:00
|
|
|
|
2023-08-23 08:18:33 -07:00
|
|
|
import { env, supportedChains } from './config';
|
2023-08-22 11:52:30 -07:00
|
|
|
import { DBOptionTypes } from './databases/types';
|
|
|
|
import { SNSOptionTypes } from './services/SNS/types';
|
2023-09-21 07:21:05 -07:00
|
|
|
import WebServer from './services/WebServer';
|
|
|
|
import { WatcherOptionTypes } from './watchers/types';
|
|
|
|
import { logInfo } from './utils/logger';
|
|
|
|
import { ChainName } from '@certusone/wormhole-sdk';
|
|
|
|
import { NETWORK } from './consts';
|
|
|
|
|
2023-10-06 06:01:16 -07:00
|
|
|
const version = '1.2.0';
|
2023-08-22 11:52:30 -07:00
|
|
|
class EventWatcher {
|
2023-09-21 07:21:05 -07:00
|
|
|
private watchers: WatcherOptionTypes[] = [];
|
2023-08-22 11:52:30 -07:00
|
|
|
|
|
|
|
constructor(private db: DBOptionTypes, private sns: SNSOptionTypes) {
|
2023-09-21 07:21:05 -07:00
|
|
|
logInfo({ labels: ['EventWatcher'], message: `Initializing...` });
|
2023-08-22 11:52:30 -07:00
|
|
|
}
|
2023-08-11 10:28:54 -07:00
|
|
|
|
2023-09-21 07:21:05 -07:00
|
|
|
async run() {
|
|
|
|
await this.db.start();
|
2023-08-18 05:47:11 -07:00
|
|
|
|
2023-09-21 07:21:05 -07:00
|
|
|
const chains = env.CHAINS ? env.CHAINS.split(',') : supportedChains;
|
2023-08-11 06:15:55 -07:00
|
|
|
|
2023-09-21 07:21:05 -07:00
|
|
|
for (const chain of chains) {
|
|
|
|
try {
|
|
|
|
const watcher = makeFinalizedWatcher(chain as ChainName);
|
|
|
|
this.watchers.push(watcher);
|
|
|
|
watcher.setDB(this.db);
|
|
|
|
watcher.setServices(this.sns);
|
|
|
|
watcher.watch();
|
2023-10-03 13:03:52 -07:00
|
|
|
} catch (e: unknown) {
|
|
|
|
let message;
|
|
|
|
if (e instanceof Error) {
|
|
|
|
message = e.message;
|
|
|
|
} else {
|
|
|
|
message = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
logInfo({ labels: ['EventWatcher'], message: `${message}` });
|
2023-09-21 07:21:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 10:28:54 -07:00
|
|
|
|
2023-09-21 07:21:05 -07:00
|
|
|
async stop() {
|
|
|
|
for (const watcher of this.watchers) {
|
|
|
|
watcher.stop();
|
|
|
|
logInfo({ labels: [watcher.chain], message: 'Watcher Stopped' });
|
|
|
|
}
|
2023-08-22 11:52:30 -07:00
|
|
|
}
|
2023-09-21 07:21:05 -07:00
|
|
|
}
|
2023-08-11 10:28:54 -07:00
|
|
|
|
2023-09-21 07:21:05 -07:00
|
|
|
const start = async () => {
|
|
|
|
logInfo({
|
|
|
|
labels: ['App'],
|
|
|
|
message: `Initializing... - v${version} - NETWORK: {{ ${NETWORK.toUpperCase()} }}`,
|
|
|
|
});
|
|
|
|
// Dependencies / Instances
|
|
|
|
const db: DBOptionTypes = getDB();
|
|
|
|
const sns: SNSOptionTypes = getSNS();
|
|
|
|
|
|
|
|
// Init and run the web server
|
|
|
|
const infrastructureController = new InfrastructureController(db);
|
|
|
|
const webServer = new WebServer(infrastructureController);
|
|
|
|
await webServer.start();
|
2023-08-11 10:28:54 -07:00
|
|
|
|
2023-09-21 07:21:05 -07:00
|
|
|
// Init and run the event watcher
|
|
|
|
const eventWatcher = new EventWatcher(db, sns);
|
|
|
|
await eventWatcher.run();
|
|
|
|
|
|
|
|
// Handle shutdown
|
|
|
|
const handleShutdown = async () => {
|
|
|
|
logInfo({ labels: ['App'], message: 'Shutting down...' });
|
|
|
|
try {
|
|
|
|
await Promise.allSettled([eventWatcher.stop(), webServer.stop(), db.stop()]);
|
|
|
|
|
|
|
|
logInfo({ labels: ['App'], message: 'Exited with code 0' });
|
|
|
|
process.exit();
|
|
|
|
} catch (error: unknown) {
|
|
|
|
logInfo({ labels: ['App'], message: 'Exited with code 1' });
|
|
|
|
process.exit(1);
|
2023-08-11 10:28:54 -07:00
|
|
|
}
|
2023-09-21 07:21:05 -07:00
|
|
|
};
|
2023-08-23 08:18:33 -07:00
|
|
|
|
2023-09-21 07:21:05 -07:00
|
|
|
process.on('SIGINT', handleShutdown);
|
|
|
|
process.on('SIGTERM', handleShutdown);
|
|
|
|
};
|
2023-08-11 06:15:55 -07:00
|
|
|
|
2023-09-21 07:21:05 -07:00
|
|
|
start();
|