index.ts
1 import express from 'express'; 2 import logger from './core/logger'; 3 4 import appSettings from './config/appSettings'; 5 import initJobProcessingQueue from './queue/initJobProcessingQueue'; 6 import { arenaConfig } from './queue/queueMonitoring'; 7 import { connectToDb } from './db/database'; 8 import { registerEventHandlers } from './events/registrations'; 9 import poll from './events/poll'; 10 import { getHandlers } from './events/eventHandlerUtils'; 11 import getProvider from './core/getProvider'; 12 import { 13 addressDriverContract, 14 dripsContract, 15 nftDriverContract, 16 repoDriverContract, 17 } from './core/contractClients'; 18 import { toAddress } from './utils/ethereumAddressUtils'; 19 import loadChainConfig from './config/loadChainConfig'; 20 import './events/types'; 21 import networkConstant from '../contracts/CURRENT_NETWORK/network-constant'; 22 import { healthEndpoint } from './health'; 23 24 process.on('uncaughtException', (error: Error) => { 25 logger.error('Uncaught Exception', { 26 message: error.message, 27 stack: error.stack, 28 }); 29 30 // Railway will restart the process if it exits with a non-zero exit code. 31 process.exit(1); 32 }); 33 34 (async () => { 35 await init(); 36 })(); 37 38 async function init() { 39 if (appSettings.network !== networkConstant) { 40 throw new Error( 41 `Built contracts types are for network ${networkConstant}, but the app is configured for ${appSettings.network} network. Please re-run 'npm run build:contracts' after changing the NETWORK env var.`, 42 ); 43 } 44 45 logger.info('Starting the application...'); 46 logger.info(`App Settings: ${JSON.stringify(appSettings, null, 2)}`); 47 48 await connectToDb(); 49 await initJobProcessingQueue(); 50 51 registerEventHandlers(); 52 53 const { block: startBlock } = loadChainConfig(); 54 55 await poll( 56 [ 57 { 58 contract: dripsContract, 59 address: toAddress(await dripsContract.getAddress()), 60 }, 61 { 62 contract: addressDriverContract, 63 address: toAddress(await addressDriverContract.getAddress()), 64 }, 65 { 66 contract: nftDriverContract, 67 address: toAddress(await nftDriverContract.getAddress()), 68 }, 69 { 70 contract: repoDriverContract, 71 address: toAddress(await repoDriverContract.getAddress()), 72 }, 73 ], 74 getHandlers(), 75 getProvider(), 76 startBlock, 77 ); 78 79 const app = express(); 80 81 if (appSettings.shouldStartMonitoringUI) { 82 app.use('/arena', arenaConfig); 83 app.use('/health', healthEndpoint); 84 } 85 86 app.listen(appSettings.queueUiPort, () => { 87 logger.info( 88 `Monitoring available on port ${appSettings.queueUiPort}. Routes: /health, /arena`, 89 ); 90 }); 91 }