diff --git a/.changeset/tricky-vans-cry.md b/.changeset/tricky-vans-cry.md new file mode 100644 index 0000000..c586cd4 --- /dev/null +++ b/.changeset/tricky-vans-cry.md @@ -0,0 +1,5 @@ +--- +'@rosen-bridge/watcher': patch +--- + +Fix watcher network switch in scanner init diff --git a/src/jobs/initScanner.ts b/src/jobs/initScanner.ts index 1bc2980..558de00 100644 --- a/src/jobs/initScanner.ts +++ b/src/jobs/initScanner.ts @@ -1,17 +1,18 @@ -import { - CardanoBlockFrostScanner, - CardanoKoiosScanner, - CardanoOgmiosScanner, - GeneralScanner, -} from '@rosen-bridge/scanner'; -import { BitcoinEsploraScanner } from '@rosen-bridge/bitcoin-esplora-scanner'; -import { BitcoinRpcScanner } from '@rosen-bridge/bitcoin-rpc-scanner'; +import { CardanoOgmiosScanner, GeneralScanner } from '@rosen-bridge/scanner'; import * as Constants from '../config/constants'; import { getConfig } from '../config/config'; import { scanner } from '../utils/scanner'; import WinstonLogger from '@rosen-bridge/winston-logger'; import { EvmRpcScanner } from '@rosen-bridge/evm-rpc-scanner'; +const allConfig = getConfig(); +const { + general: config, + cardano: cardanoConfig, + bitcoin: bitcoinConfig, + ethereum: ethereumConfig, +} = allConfig; + const logger = WinstonLogger.getInstance().getLogger(import.meta.url); const scanningJob = async (interval: number, scanner: GeneralScanner) => { @@ -26,49 +27,40 @@ const scanningJob = async (interval: number, scanner: GeneralScanner) => { }; export const scannerInit = () => { - const allConfig = getConfig(); - const config = allConfig.general; scanningJob(config.ergoInterval, scanner.ergoScanner).then(() => null); - if (config.networkWatcher === Constants.CARDANO_CHAIN_NAME) { - const cardanoConfig = allConfig.cardano; - if (cardanoConfig.ogmios) - (scanner.observationScanner as CardanoOgmiosScanner).start(); - else if (cardanoConfig.koios) { - scanningJob( - cardanoConfig.koios.interval, - scanner.observationScanner as CardanoKoiosScanner - ).then(() => null); - } else if (cardanoConfig.blockfrost) { + switch (config.networkWatcher) { + case Constants.CARDANO_CHAIN_NAME: + if (cardanoConfig.ogmios) { + (scanner.observationScanner as CardanoOgmiosScanner).start(); + break; + } scanningJob( - cardanoConfig.blockfrost.interval, - scanner.observationScanner as CardanoBlockFrostScanner + cardanoConfig.koios + ? cardanoConfig.koios.interval + : cardanoConfig.blockfrost!.interval, + scanner.observationScanner as GeneralScanner ).then(() => null); - } else throw new Error(`Cardano scanner is not configured properly`); - } else if (config.networkWatcher === Constants.BITCOIN_CHAIN_NAME) { - const bitcoinConfig = allConfig.bitcoin; - if (bitcoinConfig.esplora) { + break; + case Constants.BITCOIN_CHAIN_NAME: scanningJob( - bitcoinConfig.esplora.interval, - scanner.observationScanner as BitcoinEsploraScanner + bitcoinConfig.rpc + ? bitcoinConfig.rpc.interval + : bitcoinConfig.esplora!.interval, + scanner.observationScanner as GeneralScanner ).then(() => null); - } else if (bitcoinConfig.rpc) { + break; + case Constants.ETHEREUM_CHAIN_NAME: scanningJob( - bitcoinConfig.rpc.interval, - scanner.observationScanner as BitcoinRpcScanner - ).then(() => null); - } else throw new Error(`Bitcoin scanner is not configured properly`); - } else if (config.networkWatcher === Constants.ETHEREUM_CHAIN_NAME) { - const ethereumConfig = allConfig.ethereum; - if (ethereumConfig.rpc) { - scanningJob( - ethereumConfig.rpc.interval, + ethereumConfig.rpc!.interval, scanner.observationScanner as EvmRpcScanner ).then(() => null); - } else throw new Error(`Ethereum scanner is not configured properly`); - } else { - throw new Error( - `The observing network [${config.networkWatcher}] is not supported` - ); + break; + case Constants.ERGO_CHAIN_NAME: + break; + default: + throw new Error( + `The observing network [${config.networkWatcher}] is not supported` + ); } // TODO: Add commitment cleanup job